Moving the settings to main paperless application

This commit is contained in:
Trenton H 2023-12-14 09:57:50 -08:00
parent c7876dc0f1
commit d8b254e55e
7 changed files with 175 additions and 68 deletions

View File

@ -1,4 +1,4 @@
# Generated by Django 4.2.7 on 2023-12-11 19:59 # Generated by Django 4.2.7 on 2023-12-14 17:12
import django.core.validators import django.core.validators
from django.db import migrations from django.db import migrations
@ -6,8 +6,12 @@ from django.db import models
def _create_singleton(apps, schema_editor): def _create_singleton(apps, schema_editor):
settings_model = apps.get_model("paperless_tesseract", "OcrSettings") """
settings_model.objects.create() Creates the first and only instance of the settings models
"""
for model_name in ["CommonSettings", "OcrSettings", "TextSettings", "TikaSettings"]:
settings_model = apps.get_model("paperless", model_name)
settings_model.objects.create()
class Migration(migrations.Migration): class Migration(migrations.Migration):
@ -16,6 +20,39 @@ class Migration(migrations.Migration):
dependencies = [] dependencies = []
operations = [ operations = [
migrations.CreateModel(
name="CommonSettings",
fields=[
(
"id",
models.AutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
(
"output_type",
models.CharField(
blank=True,
choices=[
("pdf", "pdf"),
("pdfa", "pdfa"),
("pdfa-1", "pdfa-1"),
("pdfa-2", "pdfa-2"),
("pdfa-3", "pdfa-3"),
],
max_length=8,
null=True,
verbose_name="Sets the output PDF type",
),
),
],
options={
"abstract": False,
},
),
migrations.CreateModel( migrations.CreateModel(
name="OcrSettings", name="OcrSettings",
fields=[ fields=[
@ -45,22 +82,6 @@ class Migration(migrations.Migration):
verbose_name="Do OCR using these languages", verbose_name="Do OCR using these languages",
), ),
), ),
(
"output_type",
models.CharField(
blank=True,
choices=[
("pdf", "pdf"),
("pdfa", "pdfa"),
("pdfa-1", "pdfa-1"),
("pdfa-2", "pdfa-2"),
("pdfa-3", "pdfa-3"),
],
max_length=8,
null=True,
verbose_name="Sets the output PDF type",
),
),
( (
"mode", "mode",
models.CharField( models.CharField(
@ -135,7 +156,7 @@ class Migration(migrations.Migration):
models.FloatField( models.FloatField(
null=True, null=True,
validators=[ validators=[
django.core.validators.MinValueValidator(1_000_000.0), django.core.validators.MinValueValidator(1000000.0),
], ],
verbose_name="Sets the maximum image size for decompression", verbose_name="Sets the maximum image size for decompression",
), ),
@ -168,8 +189,60 @@ class Migration(migrations.Migration):
"verbose_name": "ocr settings", "verbose_name": "ocr settings",
}, },
), ),
migrations.RunPython( migrations.CreateModel(
code=_create_singleton, name="TextSettings",
reverse_code=migrations.RunPython.noop, fields=[
(
"id",
models.AutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
(
"thumbnail_font_name",
models.CharField(
blank=True,
max_length=64,
null=True,
verbose_name="Sets the output PDF type",
),
),
],
options={
"abstract": False,
},
), ),
migrations.CreateModel(
name="TikaSettings",
fields=[
(
"id",
models.AutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
(
"tika_url",
models.URLField(blank=True, null=True, verbose_name="Tika URL"),
),
(
"gotenberg_url",
models.URLField(
blank=True,
null=True,
verbose_name="Gotenberg URL",
),
),
],
options={
"abstract": False,
},
),
migrations.RunPython(_create_singleton, migrations.RunPython.noop),
] ]

View File

@ -1,4 +1,3 @@
from django.core.exceptions import ValidationError
from django.core.validators import MinValueValidator from django.core.validators import MinValueValidator
from django.db import models from django.db import models
from django.utils.translation import gettext_lazy as _ from django.utils.translation import gettext_lazy as _
@ -6,7 +5,23 @@ from django.utils.translation import gettext_lazy as _
DEFAULT_SINGLETON_INSTANCE_ID = 1 DEFAULT_SINGLETON_INSTANCE_ID = 1
class OcrSettings(models.Model): class AbstractSingletonModel(models.Model):
class Meta:
abstract = True
def save(self, *args, **kwargs):
"""
Always save as the first and only model
"""
self.pk = DEFAULT_SINGLETON_INSTANCE_ID
super().save(*args, **kwargs)
class CommonSettings(AbstractSingletonModel):
"""
Settings which are common across more than 1 parser
"""
class OutputTypeChoices(models.TextChoices): class OutputTypeChoices(models.TextChoices):
PDF = ("pdf", _("pdf")) PDF = ("pdf", _("pdf"))
PDF_A = ("pdfa", _("pdfa")) PDF_A = ("pdfa", _("pdfa"))
@ -14,6 +29,20 @@ class OcrSettings(models.Model):
PDF_A2 = ("pdfa-2", _("pdfa-2")) PDF_A2 = ("pdfa-2", _("pdfa-2"))
PDF_A3 = ("pdfa-3", _("pdfa-3")) PDF_A3 = ("pdfa-3", _("pdfa-3"))
output_type = models.CharField(
verbose_name=_("Sets the output PDF type"),
null=True,
blank=True,
max_length=8,
choices=OutputTypeChoices.choices,
)
class OcrSettings(AbstractSingletonModel):
"""
Settings for the Tesseract based OCR parser
"""
class ModeChoices(models.TextChoices): class ModeChoices(models.TextChoices):
SKIP = ("skip", _("skip")) SKIP = ("skip", _("skip"))
SKIP_NO_ARCHIVE = ("skip_noarchive", _("skip_noarchive")) SKIP_NO_ARCHIVE = ("skip_noarchive", _("skip_noarchive"))
@ -50,14 +79,6 @@ class OcrSettings(models.Model):
max_length=32, max_length=32,
) )
output_type = models.CharField(
verbose_name=_("Sets the output PDF type"),
null=True,
blank=True,
max_length=8,
choices=OutputTypeChoices.choices,
)
mode = models.CharField( mode = models.CharField(
verbose_name=_("Sets the OCR mode"), verbose_name=_("Sets the OCR mode"),
null=True, null=True,
@ -124,17 +145,34 @@ class OcrSettings(models.Model):
verbose_name = _("ocr settings") verbose_name = _("ocr settings")
def __str__(self) -> str: def __str__(self) -> str:
return "" return "OcrSettings"
def save(self, *args, **kwargs):
if not self.pk and OcrSettings.objects.exists():
# if you'll not check for self.pk
# then error will also be raised in the update of exists model
raise ValidationError(
"There is can be only one OcrSettings instance",
)
return super().save(*args, **kwargs)
@classmethod class TextSettings(AbstractSingletonModel):
def object(cls): """
return cls._default_manager.all().first() # Since only one item Settings for the text parser
"""
thumbnail_font_name = models.CharField(
verbose_name=_("Sets the output PDF type"),
null=True,
blank=True,
max_length=64,
)
class TikaSettings(AbstractSingletonModel):
"""
Settings for the Tika parser
"""
tika_url = models.URLField(
verbose_name=_("Tika URL"),
null=True,
blank=True,
)
gotenberg_url = models.URLField(
verbose_name=_("Gotenberg URL"),
null=True,
blank=True,
)

View File

@ -3,6 +3,8 @@ from django.contrib.auth.models import Permission
from django.contrib.auth.models import User from django.contrib.auth.models import User
from rest_framework import serializers from rest_framework import serializers
from paperless.models import OcrSettings
class ObfuscatedUserPasswordField(serializers.Field): class ObfuscatedUserPasswordField(serializers.Field):
""" """
@ -113,3 +115,9 @@ class ProfileSerializer(serializers.ModelSerializer):
"last_name", "last_name",
"auth_token", "auth_token",
) )
class OcrSettingsSerializer(serializers.ModelSerializer):
class Meta:
model = OcrSettings
fields = ["all"]

View File

@ -18,7 +18,9 @@ from rest_framework.viewsets import ModelViewSet
from documents.permissions import PaperlessObjectPermissions from documents.permissions import PaperlessObjectPermissions
from paperless.filters import GroupFilterSet from paperless.filters import GroupFilterSet
from paperless.filters import UserFilterSet from paperless.filters import UserFilterSet
from paperless.models import OcrSettings
from paperless.serialisers import GroupSerializer from paperless.serialisers import GroupSerializer
from paperless.serialisers import OcrSettingsSerializer
from paperless.serialisers import ProfileSerializer from paperless.serialisers import ProfileSerializer
from paperless.serialisers import UserSerializer from paperless.serialisers import UserSerializer
@ -160,3 +162,12 @@ class GenerateAuthTokenView(GenericAPIView):
return Response( return Response(
token.key, token.key,
) )
class OcrSettingsViewSet(ModelViewSet):
model = OcrSettings
queryset = OcrSettings.objects
serializer_class = OcrSettingsSerializer
permission_classes = (IsAuthenticated,)

View File

@ -1,9 +0,0 @@
from rest_framework import serializers
from paperless_tesseract.models import OcrSettings
class OcrSettingsSerializer(serializers.ModelSerializer):
class Meta:
model = OcrSettings
fields = ["all"]

View File

@ -1,14 +0,0 @@
from rest_framework.permissions import IsAuthenticated
from rest_framework.viewsets import ModelViewSet
from paperless_tesseract.models import OcrSettings
from paperless_tesseract.serialisers import OcrSettingsSerializer
class OcrSettingsViewSet(ModelViewSet):
model = OcrSettings
queryset = OcrSettings.objects
serializer_class = OcrSettingsSerializer
permission_classes = (IsAuthenticated,)