# Generated by Django 5.2.11 on 2026-03-03 16:27

import django.db.models.deletion
import django.utils.timezone
from django.conf import settings
from django.db import migrations
from django.db import models


class Migration(migrations.Migration):
    initial = True

    dependencies = [
        ("documents", "0001_initial"),
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
    ]

    replaces = [
        ("paperless_mail", "0001_initial"),
        ("paperless_mail", "0001_initial_squashed_0009_mailrule_assign_tags"),
        ("paperless_mail", "0002_auto_20201117_1334"),
        ("paperless_mail", "0003_auto_20201118_1940"),
        ("paperless_mail", "0004_mailrule_order"),
        ("paperless_mail", "0005_help_texts"),
        ("paperless_mail", "0006_auto_20210101_2340"),
        ("paperless_mail", "0007_auto_20210106_0138"),
        ("paperless_mail", "0008_auto_20210516_0940"),
        ("paperless_mail", "0009_alter_mailrule_action_alter_mailrule_folder"),
        ("paperless_mail", "0009_mailrule_assign_tags"),
        ("paperless_mail", "0010_auto_20220311_1602"),
        ("paperless_mail", "0011_remove_mailrule_assign_tag"),
        (
            "paperless_mail",
            "0011_remove_mailrule_assign_tag_squashed_0024_alter_mailrule_name_and_more",
        ),
        ("paperless_mail", "0012_alter_mailrule_assign_tags"),
        ("paperless_mail", "0013_merge_20220412_1051"),
        ("paperless_mail", "0014_alter_mailrule_action"),
        ("paperless_mail", "0015_alter_mailrule_action"),
        ("paperless_mail", "0016_mailrule_consumption_scope"),
        ("paperless_mail", "0017_mailaccount_owner_mailrule_owner"),
        ("paperless_mail", "0018_processedmail"),
        ("paperless_mail", "0019_mailrule_filter_to"),
        ("paperless_mail", "0020_mailaccount_is_token"),
        ("paperless_mail", "0021_alter_mailaccount_password"),
        ("paperless_mail", "0022_mailrule_assign_owner_from_rule_and_more"),
        ("paperless_mail", "0023_remove_mailrule_filter_attachment_filename_and_more"),
        ("paperless_mail", "0024_alter_mailrule_name_and_more"),
        (
            "paperless_mail",
            "0025_alter_mailaccount_owner_alter_mailrule_owner_and_more",
        ),
        ("paperless_mail", "0026_mailrule_enabled"),
        (
            "paperless_mail",
            "0027_mailaccount_expiration_mailaccount_account_type_and_more",
        ),
        ("paperless_mail", "0028_alter_mailaccount_password_and_more"),
        ("paperless_mail", "0029_mailrule_pdf_layout"),
    ]

    operations = [
        migrations.CreateModel(
            name="MailAccount",
            fields=[
                (
                    "id",
                    models.AutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                (
                    "name",
                    models.CharField(max_length=256, unique=True, verbose_name="name"),
                ),
                (
                    "imap_server",
                    models.CharField(max_length=256, verbose_name="IMAP server"),
                ),
                (
                    "imap_port",
                    models.IntegerField(
                        blank=True,
                        help_text="This is usually 143 for unencrypted and STARTTLS connections, and 993 for SSL connections.",
                        null=True,
                        verbose_name="IMAP port",
                    ),
                ),
                (
                    "imap_security",
                    models.PositiveIntegerField(
                        choices=[
                            (1, "No encryption"),
                            (2, "Use SSL"),
                            (3, "Use STARTTLS"),
                        ],
                        default=2,
                        verbose_name="IMAP security",
                    ),
                ),
                ("username", models.CharField(max_length=256, verbose_name="username")),
                ("password", models.TextField(verbose_name="password")),
                (
                    "is_token",
                    models.BooleanField(
                        default=False,
                        verbose_name="Is token authentication",
                    ),
                ),
                (
                    "character_set",
                    models.CharField(
                        default="UTF-8",
                        help_text="The character set to use when communicating with the mail server, such as 'UTF-8' or 'US-ASCII'.",
                        max_length=256,
                        verbose_name="character set",
                    ),
                ),
                (
                    "account_type",
                    models.PositiveIntegerField(
                        choices=[(1, "IMAP"), (2, "Gmail OAuth"), (3, "Outlook OAuth")],
                        default=1,
                        verbose_name="account type",
                    ),
                ),
                (
                    "refresh_token",
                    models.TextField(
                        blank=True,
                        help_text="The refresh token to use for token authentication e.g. with oauth2.",
                        null=True,
                        verbose_name="refresh token",
                    ),
                ),
                (
                    "expiration",
                    models.DateTimeField(
                        blank=True,
                        help_text="The expiration date of the refresh token. ",
                        null=True,
                        verbose_name="expiration",
                    ),
                ),
                (
                    "owner",
                    models.ForeignKey(
                        blank=True,
                        default=None,
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        to=settings.AUTH_USER_MODEL,
                        verbose_name="owner",
                    ),
                ),
            ],
            options={
                "verbose_name": "mail account",
                "verbose_name_plural": "mail accounts",
            },
        ),
        migrations.CreateModel(
            name="MailRule",
            fields=[
                (
                    "id",
                    models.AutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                ("name", models.CharField(max_length=256, verbose_name="name")),
                ("order", models.IntegerField(default=0, verbose_name="order")),
                ("enabled", models.BooleanField(default=True, verbose_name="enabled")),
                (
                    "folder",
                    models.CharField(
                        default="INBOX",
                        help_text="Subfolders must be separated by a delimiter, often a dot ('.') or slash ('/'), but it varies by mail server.",
                        max_length=256,
                        verbose_name="folder",
                    ),
                ),
                (
                    "filter_from",
                    models.CharField(
                        blank=True,
                        max_length=256,
                        null=True,
                        verbose_name="filter from",
                    ),
                ),
                (
                    "filter_to",
                    models.CharField(
                        blank=True,
                        max_length=256,
                        null=True,
                        verbose_name="filter to",
                    ),
                ),
                (
                    "filter_subject",
                    models.CharField(
                        blank=True,
                        max_length=256,
                        null=True,
                        verbose_name="filter subject",
                    ),
                ),
                (
                    "filter_body",
                    models.CharField(
                        blank=True,
                        max_length=256,
                        null=True,
                        verbose_name="filter body",
                    ),
                ),
                (
                    "filter_attachment_filename_include",
                    models.CharField(
                        blank=True,
                        help_text="Only consume documents which entirely match this filename if specified. Wildcards such as *.pdf or *invoice* are allowed. Case insensitive.",
                        max_length=256,
                        null=True,
                        verbose_name="filter attachment filename inclusive",
                    ),
                ),
                (
                    "filter_attachment_filename_exclude",
                    models.CharField(
                        blank=True,
                        help_text="Do not consume documents which entirely match this filename if specified. Wildcards such as *.pdf or *invoice* are allowed. Case insensitive.",
                        max_length=256,
                        null=True,
                        verbose_name="filter attachment filename exclusive",
                    ),
                ),
                (
                    "maximum_age",
                    models.PositiveIntegerField(
                        default=30,
                        help_text="Specified in days.",
                        verbose_name="maximum age",
                    ),
                ),
                (
                    "attachment_type",
                    models.PositiveIntegerField(
                        choices=[
                            (1, "Only process attachments."),
                            (2, "Process all files, including 'inline' attachments."),
                        ],
                        default=1,
                        help_text="Inline attachments include embedded images, so it's best to combine this option with a filename filter.",
                        verbose_name="attachment type",
                    ),
                ),
                (
                    "consumption_scope",
                    models.PositiveIntegerField(
                        choices=[
                            (1, "Only process attachments."),
                            (
                                2,
                                "Process full Mail (with embedded attachments in file) as .eml",
                            ),
                            (
                                3,
                                "Process full Mail (with embedded attachments in file) as .eml + process attachments as separate documents",
                            ),
                        ],
                        default=1,
                        verbose_name="consumption scope",
                    ),
                ),
                (
                    "pdf_layout",
                    models.PositiveIntegerField(
                        choices=[
                            (0, "System default"),
                            (1, "Text, then HTML"),
                            (2, "HTML, then text"),
                            (3, "HTML only"),
                            (4, "Text only"),
                        ],
                        default=0,
                        verbose_name="pdf layout",
                    ),
                ),
                (
                    "action",
                    models.PositiveIntegerField(
                        choices=[
                            (1, "Delete"),
                            (2, "Move to specified folder"),
                            (3, "Mark as read, don't process read mails"),
                            (4, "Flag the mail, don't process flagged mails"),
                            (
                                5,
                                "Tag the mail with specified tag, don't process tagged mails",
                            ),
                        ],
                        default=3,
                        verbose_name="action",
                    ),
                ),
                (
                    "action_parameter",
                    models.CharField(
                        blank=True,
                        help_text="Additional parameter for the action selected above, i.e., the target folder of the move to folder action. Subfolders must be separated by dots.",
                        max_length=256,
                        null=True,
                        verbose_name="action parameter",
                    ),
                ),
                (
                    "assign_title_from",
                    models.PositiveIntegerField(
                        choices=[
                            (1, "Use subject as title"),
                            (2, "Use attachment filename as title"),
                            (3, "Do not assign title from rule"),
                        ],
                        default=1,
                        verbose_name="assign title from",
                    ),
                ),
                (
                    "assign_correspondent_from",
                    models.PositiveIntegerField(
                        choices=[
                            (1, "Do not assign a correspondent"),
                            (2, "Use mail address"),
                            (3, "Use name (or mail address if not available)"),
                            (4, "Use correspondent selected below"),
                        ],
                        default=1,
                        verbose_name="assign correspondent from",
                    ),
                ),
                (
                    "assign_owner_from_rule",
                    models.BooleanField(
                        default=True,
                        verbose_name="Assign the rule owner to documents",
                    ),
                ),
                (
                    "account",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="rules",
                        to="paperless_mail.mailaccount",
                        verbose_name="account",
                    ),
                ),
                (
                    "assign_correspondent",
                    models.ForeignKey(
                        blank=True,
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        to="documents.correspondent",
                        verbose_name="assign this correspondent",
                    ),
                ),
                (
                    "assign_document_type",
                    models.ForeignKey(
                        blank=True,
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        to="documents.documenttype",
                        verbose_name="assign this document type",
                    ),
                ),
                (
                    "assign_tags",
                    models.ManyToManyField(
                        blank=True,
                        to="documents.tag",
                        verbose_name="assign this tag",
                    ),
                ),
                (
                    "owner",
                    models.ForeignKey(
                        blank=True,
                        default=None,
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        to=settings.AUTH_USER_MODEL,
                        verbose_name="owner",
                    ),
                ),
            ],
            options={
                "verbose_name": "mail rule",
                "verbose_name_plural": "mail rules",
            },
        ),
        migrations.CreateModel(
            name="ProcessedMail",
            fields=[
                (
                    "id",
                    models.AutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                (
                    "folder",
                    models.CharField(
                        editable=False,
                        max_length=256,
                        verbose_name="folder",
                    ),
                ),
                (
                    "uid",
                    models.CharField(
                        editable=False,
                        max_length=256,
                        verbose_name="uid",
                    ),
                ),
                (
                    "subject",
                    models.CharField(
                        editable=False,
                        max_length=256,
                        verbose_name="subject",
                    ),
                ),
                (
                    "received",
                    models.DateTimeField(editable=False, verbose_name="received"),
                ),
                (
                    "processed",
                    models.DateTimeField(
                        default=django.utils.timezone.now,
                        editable=False,
                        verbose_name="processed",
                    ),
                ),
                (
                    "status",
                    models.CharField(
                        editable=False,
                        max_length=256,
                        verbose_name="status",
                    ),
                ),
                (
                    "error",
                    models.TextField(
                        blank=True,
                        editable=False,
                        null=True,
                        verbose_name="error",
                    ),
                ),
                (
                    "owner",
                    models.ForeignKey(
                        blank=True,
                        default=None,
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        to=settings.AUTH_USER_MODEL,
                        verbose_name="owner",
                    ),
                ),
                (
                    "rule",
                    models.ForeignKey(
                        editable=False,
                        on_delete=django.db.models.deletion.CASCADE,
                        to="paperless_mail.mailrule",
                    ),
                ),
            ],
            options={
                "abstract": False,
            },
        ),
        migrations.AddConstraint(
            model_name="mailrule",
            constraint=models.UniqueConstraint(
                fields=("name", "owner"),
                name="paperless_mail_mailrule_unique_name_owner",
            ),
        ),
        migrations.AddConstraint(
            model_name="mailrule",
            constraint=models.UniqueConstraint(
                condition=models.Q(("owner__isnull", True)),
                fields=("name",),
                name="paperless_mail_mailrule_name_unique",
            ),
        ),
    ]
