# Copyright 2025 Laurent Defert
#
#  This file is part of Sosse.
#
# Sosse is free software: you can redistribute it and/or modify it under the terms of the GNU Affero
# General Public License as published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# Sosse is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
# the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License along with Sosse.
# If not, see <https://www.gnu.org/licenses/>.

# Generated by Django 3.2.19 on 2025-08-19 13:52

import datetime

import django.contrib.postgres.fields
from django.db import migrations, models


class Migration(migrations.Migration):
    dependencies = [
        ("se", "0017_sosse_1_14_0"),
    ]

    operations = [
        migrations.CreateModel(
            name="CrawlPolicyBackup",
            fields=[
                ("id", models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID")),
                (
                    "url_regex",
                    models.TextField(
                        help_text="URL regular expressions for this policy. (one by line, lines starting with # are ignored)"
                    ),
                ),
                ("url_regex_pg", models.TextField()),
                ("enabled", models.BooleanField(default=True)),
                (
                    "recursion",
                    models.CharField(
                        choices=[
                            ("always", "Crawl all pages"),
                            ("depth", "Depending on depth"),
                            ("never", "Never crawl"),
                        ],
                        default="always",
                        max_length=6,
                    ),
                ),
                ("mimetype_regex", models.TextField(default=".*")),
                (
                    "recursion_depth",
                    models.PositiveIntegerField(
                        default=0,
                        help_text="Level of external links (links that don't match the regex) to recurse into",
                    ),
                ),
                (
                    "keep_params",
                    models.BooleanField(
                        default=True,
                        help_text='When disabled, URL parameters (parameters after "?") are removed from URLs, this can be useful if some parameters are random, change sorting or filtering, ...',
                        verbose_name="Index URL parameters",
                    ),
                ),
                ("hide_documents", models.BooleanField(default=False, help_text="Hide documents from search results")),
                (
                    "default_browse_mode",
                    models.CharField(
                        choices=[
                            ("detect", "Detect"),
                            ("selenium", "Chromium"),
                            ("firefox", "Firefox"),
                            ("requests", "Python Requests"),
                        ],
                        default="detect",
                        help_text="Python Request is faster, but can't execute Javascript and may break pages",
                        max_length=8,
                    ),
                ),
                (
                    "snapshot_html",
                    models.BooleanField(
                        default=True,
                        help_text="Archive binary files, HTML content and download related assets",
                        verbose_name="Archive content 🔖",
                    ),
                ),
                (
                    "snapshot_exclude_url_re",
                    models.TextField(
                        blank=True,
                        default="",
                        help_text="Regex of URL to skip related assets downloading",
                        verbose_name="Assets exclude URL regex",
                    ),
                ),
                (
                    "snapshot_exclude_mime_re",
                    models.TextField(
                        blank=True,
                        default="",
                        help_text="Regex of mimetypes to skip related assets saving",
                        verbose_name="Assets exclude mime regex",
                    ),
                ),
                (
                    "snapshot_exclude_element_re",
                    models.TextField(
                        blank=True,
                        default="",
                        help_text="Regex of HTML elements to skip related assets downloading",
                        verbose_name="Assets exclude HTML regex",
                    ),
                ),
                (
                    "thumbnail_mode",
                    models.CharField(
                        choices=[
                            ("preview", "Page preview from metadata"),
                            ("prevscreen", "Preview from meta, screenshot as fallback"),
                            ("screenshot", "Take a screenshot"),
                            ("none", "No thumbnail"),
                        ],
                        default="preview",
                        help_text="Save thumbnails to display in search results",
                        max_length=10,
                    ),
                ),
                (
                    "take_screenshots",
                    models.BooleanField(
                        default=False, help_text="Store pages as screenshots", verbose_name="Take screenshots 📷"
                    ),
                ),
                (
                    "screenshot_format",
                    models.CharField(choices=[("png", "png"), ("jpg", "jpg")], default="jpg", max_length=3),
                ),
                (
                    "remove_nav_elements",
                    models.CharField(
                        choices=[
                            ("idx", "From index"),
                            ("scr", "From index and screenshots"),
                            ("yes", "From index, screens and HTML snaps"),
                            ("no", "No"),
                        ],
                        default="idx",
                        help_text="Remove navigation related elements",
                        max_length=4,
                    ),
                ),
                (
                    "script",
                    models.TextField(
                        blank=True,
                        default="",
                        help_text="Javascript code to execute after the page is loaded. If an object is returned, its content will be used to overwrite the document's fields",
                    ),
                ),
                (
                    "store_extern_links",
                    models.BooleanField(default=False, help_text="Store links to non-indexed pages"),
                ),
                (
                    "recrawl_freq",
                    models.CharField(
                        choices=[("none", "Once"), ("constant", "Constant time"), ("adaptive", "Adaptive")],
                        default="adaptive",
                        help_text="Adaptive frequency will increase delay between two crawls when the page stays unchanged",
                        max_length=8,
                        verbose_name="Crawl frequency",
                    ),
                ),
                (
                    "recrawl_dt_min",
                    models.DurationField(
                        blank=True,
                        default=datetime.timedelta(days=1),
                        help_text="Min. time before recrawling a page",
                        null=True,
                    ),
                ),
                (
                    "recrawl_dt_max",
                    models.DurationField(
                        blank=True,
                        default=datetime.timedelta(days=365),
                        help_text="Max. time before recrawling a page",
                        null=True,
                    ),
                ),
                (
                    "hash_mode",
                    models.CharField(
                        choices=[("raw", "Raw content"), ("no_numbers", "Normalize numbers")],
                        default="no_numbers",
                        help_text="Content to check for modifications",
                        max_length=10,
                        verbose_name="Change detection",
                    ),
                ),
                (
                    "recrawl_condition",
                    models.CharField(
                        choices=[
                            ("change", "On change only"),
                            ("always", "Always"),
                            ("manual", "On change or manual trigger"),
                        ],
                        default="manual",
                        help_text="Specifies the conditions under which a page is reprocessed",
                        max_length=10,
                        verbose_name="Condition",
                    ),
                ),
                (
                    "auth_login_url_re",
                    models.TextField(
                        blank=True,
                        help_text="A redirection to an URL matching the regex will trigger authentication",
                        null=True,
                        verbose_name="Login URL regex",
                    ),
                ),
                (
                    "auth_form_selector",
                    models.TextField(
                        blank=True,
                        help_text="CSS selector pointing to the authentication &lt;form&gt; element",
                        null=True,
                        verbose_name="Form selector",
                    ),
                ),
                (
                    "tags",
                    django.contrib.postgres.fields.ArrayField(
                        base_field=models.CharField(max_length=128), blank=True, default=list, size=None
                    ),
                ),
                (
                    "webhooks",
                    django.contrib.postgres.fields.ArrayField(
                        base_field=models.CharField(max_length=512), blank=True, default=list, size=None
                    ),
                ),
            ],
        ),
        migrations.CreateModel(
            name="AuthFieldBackup",
            fields=[
                ("id", models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID")),
                ("key", models.CharField(max_length=256, verbose_name="<input> name attribute")),
                ("value", models.CharField(max_length=256)),
                (
                    "crawl_policy",
                    models.ForeignKey(on_delete=models.deletion.CASCADE, to="se.crawlpolicybackup"),
                ),
            ],
        ),
    ]
