# -*- coding: utf-8 -*-
# Generated by Django 1.9.7 on 2016-07-06 10:05
from __future__ import unicode_literals

from django.conf import settings
import django.contrib.postgres.fields.hstore
import django.contrib.postgres.fields.jsonb
import django.core.validators
from django.db import migrations, models
import django.db.models.deletion
import django.utils.timezone
import munch.apps.campaigns.fields
import munch.apps.campaigns.models
import munch.core.mail.utils
import munch.core.utils.models
from django.contrib.postgres.operations import HStoreExtension


class Migration(migrations.Migration):

    initial = True

    dependencies = [
        ('core', '0002_permissions'),
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
    ]

    operations = [
        HStoreExtension(),
        migrations.CreateModel(
            name='Mail',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('recipient', models.EmailField(max_length=254, verbose_name='recipient')),
                ('creation_date', models.DateTimeField(default=django.utils.timezone.now, verbose_name='creation date')),
                ('first_status_date', models.DateTimeField(blank=True, null=True, verbose_name='first status date')),
                ('latest_status_date', models.DateTimeField(blank=True, null=True, verbose_name='latest status date')),
                ('delivery_duration', models.DurationField(null=True, verbose_name='delivery duration')),
                ('had_delay', models.BooleanField(default=False, verbose_name='had delay')),
                ('identifier', models.CharField(db_index=True, default=munch.apps.campaigns.models.get_base_mail_identifier, max_length=35, unique=True, verbose_name='identifier')),
                ('properties', django.contrib.postgres.fields.hstore.HStoreField(blank=True, null=True, verbose_name='properties')),
                ('source_type', models.CharField(blank=True, max_length=100, validators=[django.core.validators.RegexValidator(message='source should be composed of lowercase letters and dashes', regex='^[a-z-]*$')], verbose_name='source type')),
                ('source_ref', models.CharField(blank=True, max_length=200, verbose_name='source reference')),
                ('curstatus', munch.apps.campaigns.fields.FSMAutoField(choices=[('unknown', 'unknown'), ('ignored', 'ignored'), ('deleted', 'deleted'), ('queued', 'queued'), ('sending', 'sending'), ('delayed', 'delayed (retrying)'), ('delivered', 'delivered to remote MTA'), ('dropped', 'not delivered (soft-bounce)'), ('bounced', 'not delivered (hard-bounce)')], default='unknown', max_length=50, verbose_name='current status')),
            ],
            bases=(models.Model, munch.core.utils.models.OwnedModelMixin),
        ),
        migrations.CreateModel(
            name='MailStatus',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('status', models.CharField(choices=[('unknown', 'unknown'), ('ignored', 'ignored'), ('deleted', 'deleted'), ('queued', 'queued'), ('sending', 'sending'), ('delayed', 'delayed (retrying)'), ('delivered', 'delivered to remote MTA'), ('dropped', 'not delivered (soft-bounce)'), ('bounced', 'not delivered (hard-bounce)')], default='queued', max_length=15, verbose_name='status')),
                ('creation_date', models.DateTimeField(verbose_name='creation_date')),
                ('source_ip', models.GenericIPAddressField(blank=True, null=True, verbose_name='source ip')),
                ('source_hostname', models.CharField(blank=True, max_length=100, null=True, verbose_name='source hostname')),
                ('destination_domain', models.CharField(max_length=150, verbose_name='destination domain')),
                ('status_code', models.CharField(blank=True, help_text='X.XXX.XXX, as defined in RFC3463', max_length=25, validators=[django.core.validators.RegexValidator(message='status code should follow rfc3463', regex='([234](\\.\\d{1,3}){2})')], verbose_name='SMTP status code')),
                ('raw_msg', models.TextField(verbose_name='raw message')),
                ('mail', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='statuses', to='campaigns.Mail', verbose_name='mail')),
            ],
            options={
                'verbose_name': 'email status',
                'verbose_name_plural': 'email statuses',
                'default_permissions': ('view', 'add', 'change', 'delete', 'view_mine', 'change_mine', 'delete_mine', 'view_organizations', 'change_organizations', 'delete_organizations'),
                'abstract': False,
                'get_latest_by': 'creation_date',
            },
            bases=(models.Model, munch.core.utils.models.OwnedModelMixin),
        ),
        migrations.CreateModel(
            name='Message',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('identifier', models.CharField(db_index=True, default=munch.core.mail.utils.mk_base64_uuid, editable=False, max_length=35, unique=True, verbose_name='identifier')),
                ('name', models.CharField(max_length=60, verbose_name='name')),
                ('sender_email', models.EmailField(max_length=254, verbose_name='sender address')),
                ('sender_name', models.CharField(max_length=50, verbose_name='sender name')),
                ('subject', models.CharField(blank=True, max_length=130, verbose_name='subject')),
                ('html', models.TextField(blank=True)),
                ('status', munch.apps.campaigns.fields.FSMAutoField(choices=[('new', 'Created'), ('message_ok', 'Message approved'), ('message_issues', 'Message contains issues'), ('sending', 'Sending'), ('sent', 'Sent')], default='new', max_length=50, verbose_name='status')),
                ('creation_date', models.DateTimeField(auto_now_add=True, help_text='automatic', verbose_name='creation date')),
                ('send_date', models.DateTimeField(blank=True, null=True, verbose_name='send date')),
                ('completion_date', models.DateTimeField(blank=True, null=True, verbose_name='complete sent date')),
                ('properties', django.contrib.postgres.fields.hstore.HStoreField(blank=True, null=True, verbose_name='properties')),
                ('external_optout', models.BooleanField(default=False, help_text='If enabled, this message, instead of optout form, recipients will be invited to contact you.', verbose_name='external optout')),
                ('detach_images', models.BooleanField(default=False, help_text='Host linked images on our servers instead of original links.', verbose_name='host images')),
                ('track_open', models.BooleanField(default=False, help_text='Detect who opens message by adding hidden image.', verbose_name='track opens')),
                ('track_clicks', models.BooleanField(default=False, help_text='Detect which links were clicked by replacing original links by redirections', verbose_name='track clicks')),
                ('spam_score', models.FloatField(blank=True, null=True, verbose_name='spam score')),
                ('spam_details', django.contrib.postgres.fields.jsonb.JSONField(blank=True, null=True, verbose_name='Triggered anti-spam rules')),
                ('is_spam', models.BooleanField(default=False, verbose_name='Detected as spam')),
                ('spam_check_error', models.TextField(blank=True, null=True, verbose_name='Spam check error')),
                ('msg_issue', models.CharField(blank=True, max_length=300, null=True, verbose_name='message issue')),
                ('msg_links', django.contrib.postgres.fields.hstore.HStoreField(blank=True, null=True, verbose_name='message links')),
                ('author', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL, verbose_name='author')),
                ('category', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='messages', to='core.Category', verbose_name='category')),
            ],
            options={
                'verbose_name': 'message',
                'verbose_name_plural': 'messages',
                'default_permissions': ('view', 'add', 'change', 'delete', 'view_mine', 'change_mine', 'delete_mine', 'view_organizations', 'change_organizations', 'delete_organizations', 'previewsend_organizations', 'previewsend_mine', 'previewsend'),
                'abstract': False,
            },
            bases=(models.Model, munch.core.utils.models.OwnedModelMixin),
        ),
        migrations.CreateModel(
            name='MessageAttachment',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('file', models.FileField(upload_to=munch.apps.campaigns.models.get_attachment_storage_path, verbose_name='attached file')),
                ('original_name', models.CharField(max_length=100, verbose_name='original name')),
                ('b64size', models.PositiveIntegerField(default=0, verbose_name='base 64 size')),
                ('message', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='attachments', to='campaigns.Message', verbose_name='message')),
            ],
            options={
                'default_permissions': ('view', 'add', 'change', 'delete', 'view_mine', 'change_mine', 'delete_mine', 'view_organizations', 'change_organizations', 'delete_organizations'),
                'abstract': False,
            },
            bases=(models.Model, munch.core.utils.models.OwnedModelMixin),
        ),
        migrations.CreateModel(
            name='PreviewMail',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('recipient', models.EmailField(max_length=254, verbose_name='recipient')),
                ('creation_date', models.DateTimeField(default=django.utils.timezone.now, verbose_name='creation date')),
                ('first_status_date', models.DateTimeField(blank=True, null=True, verbose_name='first status date')),
                ('latest_status_date', models.DateTimeField(blank=True, null=True, verbose_name='latest status date')),
                ('delivery_duration', models.DurationField(null=True, verbose_name='delivery duration')),
                ('had_delay', models.BooleanField(default=False, verbose_name='had delay')),
                ('identifier', models.CharField(db_index=True, default=munch.apps.campaigns.models.get_base_mail_identifier, max_length=35, unique=True, verbose_name='identifier')),
                ('properties', django.contrib.postgres.fields.hstore.HStoreField(blank=True, null=True, verbose_name='properties')),
                ('source_type', models.CharField(blank=True, max_length=100, validators=[django.core.validators.RegexValidator(message='source should be composed of lowercase letters and dashes', regex='^[a-z-]*$')], verbose_name='source type')),
                ('source_ref', models.CharField(blank=True, max_length=200, verbose_name='source reference')),
                ('curstatus', munch.apps.campaigns.fields.FSMAutoField(choices=[('unknown', 'unknown'), ('ignored', 'ignored'), ('deleted', 'deleted'), ('queued', 'queued'), ('sending', 'sending'), ('delayed', 'delayed (retrying)'), ('delivered', 'delivered to remote MTA'), ('dropped', 'not delivered (soft-bounce)'), ('bounced', 'not delivered (hard-bounce)')], default='unknown', max_length=50, verbose_name='current status')),
                ('message', munch.apps.campaigns.models.TolerantForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='previewmails', to='campaigns.Message', verbose_name='message')),
            ],
            options={
                'default_permissions': ('view', 'add', 'change', 'delete', 'view_mine', 'change_mine', 'delete_mine', 'view_organizations', 'change_organizations', 'delete_organizations'),
                'abstract': False,
            },
            bases=(models.Model, munch.core.utils.models.OwnedModelMixin),
        ),
        migrations.AddField(
            model_name='mail',
            name='message',
            field=munch.apps.campaigns.models.TolerantForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='mails', to='campaigns.Message', verbose_name='message'),
        ),
        migrations.AlterUniqueTogether(
            name='mail',
            unique_together=set([('recipient', 'message')]),
        ),
    ]
