removed slugs entirely, since their only purpose was purely cosmetic anyway.

This commit is contained in:
jonaswinkler 2020-12-09 00:04:37 +01:00
parent 5753c83618
commit 74a99cf330
9 changed files with 55 additions and 36 deletions

View File

@ -17,8 +17,6 @@ class CorrespondentAdmin(admin.ModelAdmin):
list_filter = ("matching_algorithm",)
list_editable = ("match", "matching_algorithm")
readonly_fields = ("slug",)
class TagAdmin(admin.ModelAdmin):
@ -31,8 +29,6 @@ class TagAdmin(admin.ModelAdmin):
list_filter = ("colour", "matching_algorithm")
list_editable = ("colour", "match", "matching_algorithm")
readonly_fields = ("slug", )
class DocumentTypeAdmin(admin.ModelAdmin):
@ -44,8 +40,6 @@ class DocumentTypeAdmin(admin.ModelAdmin):
list_filter = ("matching_algorithm",)
list_editable = ("match", "matching_algorithm")
readonly_fields = ("slug",)
class DocumentAdmin(admin.ModelAdmin):
@ -106,7 +100,7 @@ class DocumentAdmin(admin.ModelAdmin):
for tag in obj.tags.all():
r += self._html_tag(
"span",
tag.slug + ", "
tag.name + ", "
)
return r

View File

@ -259,7 +259,7 @@ class Consumer(LoggingMixin):
relevant_tags = set(file_info.tags)
if relevant_tags:
tag_names = ", ".join([t.slug for t in relevant_tags])
tag_names = ", ".join([t.name for t in relevant_tags])
self.log("debug", "Tagging with {}".format(tag_names))
document.tags.add(*relevant_tags)

View File

@ -29,10 +29,7 @@ def _tags_from_path(filepath):
path_parts = Path(filepath).relative_to(
settings.CONSUMPTION_DIR).parent.parts
for part in path_parts:
tag_ids.add(Tag.objects.get_or_create(
slug=slugify(part),
defaults={"name": part},
)[0].pk)
tag_ids.add(Tag.objects.get_or_create(name=part)[0].pk)
return tag_ids

View File

@ -0,0 +1,25 @@
# Generated by Django 3.1.4 on 2020-12-08 22:09
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('documents', '1005_checksums'),
]
operations = [
migrations.RemoveField(
model_name='correspondent',
name='slug',
),
migrations.RemoveField(
model_name='documenttype',
name='slug',
),
migrations.RemoveField(
model_name='tag',
name='slug',
),
]

View File

@ -36,7 +36,6 @@ class MatchingModel(models.Model):
)
name = models.CharField(max_length=128, unique=True)
slug = models.SlugField(blank=True, editable=False)
match = models.CharField(max_length=256, blank=True)
matching_algorithm = models.PositiveIntegerField(
@ -69,7 +68,6 @@ class MatchingModel(models.Model):
def save(self, *args, **kwargs):
self.match = self.match.lower()
self.slug = slugify(self.name)
models.Model.save(self, *args, **kwargs)
@ -384,9 +382,7 @@ class FileInfo:
def _get_correspondent(cls, name):
if not name:
return None
return Correspondent.objects.get_or_create(name=name, defaults={
"slug": slugify(name)
})[0]
return Correspondent.objects.get_or_create(name=name)[0]
@classmethod
def _get_title(cls, title):
@ -396,10 +392,7 @@ class FileInfo:
def _get_tags(cls, tags):
r = []
for t in tags.split(","):
r.append(Tag.objects.get_or_create(
slug=slugify(t),
defaults={"name": t}
)[0])
r.append(Tag.objects.get_or_create(name=t)[0])
return tuple(r)
@classmethod

View File

@ -1,4 +1,5 @@
import magic
from django.utils.text import slugify
from pathvalidate import validate_filename, ValidationError
from rest_framework import serializers
from rest_framework.fields import SerializerMethodField
@ -7,12 +8,16 @@ from .models import Correspondent, Tag, Document, Log, DocumentType
from .parsers import is_mime_type_supported
class CorrespondentSerializer(serializers.HyperlinkedModelSerializer):
class CorrespondentSerializer(serializers.ModelSerializer):
document_count = serializers.IntegerField(read_only=True)
last_correspondence = serializers.DateTimeField(read_only=True)
def get_slug(self, obj):
return slugify(obj.name)
slug = SerializerMethodField()
class Meta:
model = Correspondent
fields = (
@ -27,10 +32,14 @@ class CorrespondentSerializer(serializers.HyperlinkedModelSerializer):
)
class DocumentTypeSerializer(serializers.HyperlinkedModelSerializer):
class DocumentTypeSerializer(serializers.ModelSerializer):
document_count = serializers.IntegerField(read_only=True)
def get_slug(self, obj):
return slugify(obj.name)
slug = SerializerMethodField()
class Meta:
model = DocumentType
fields = (
@ -44,10 +53,14 @@ class DocumentTypeSerializer(serializers.HyperlinkedModelSerializer):
)
class TagSerializer(serializers.HyperlinkedModelSerializer):
class TagSerializer(serializers.ModelSerializer):
document_count = serializers.IntegerField(read_only=True)
def get_slug(self, obj):
return slugify(obj.name)
slug = SerializerMethodField()
class Meta:
model = Tag
fields = (

View File

@ -136,7 +136,7 @@ def set_tags(sender,
message = 'Tagging "{}" with "{}"'
logger(
message.format(document, ", ".join([t.slug for t in relevant_tags])),
message.format(document, ", ".join([t.name for t in relevant_tags])),
logging_group
)
@ -165,7 +165,7 @@ def run_post_consume_script(sender, document, **kwargs):
reverse("document-download", kwargs={"pk": document.pk}),
reverse("document-thumb", kwargs={"pk": document.pk}),
str(document.correspondent),
str(",".join(document.tags.all().values_list("slug", flat=True)))
str(",".join(document.tags.all().values_list("name", flat=True)))
)).wait()

View File

@ -27,7 +27,7 @@ class TestAttributes(TestCase):
self.assertEqual(file_info.title, title, filename)
self.assertEqual(tuple([t.slug for t in file_info.tags]), tags, filename)
self.assertEqual(tuple([t.name for t in file_info.tags]), tags, filename)
def test_guess_attributes_from_name0(self):
self._test_guess_attributes_from_name(
@ -188,7 +188,7 @@ class TestFieldPermutations(TestCase):
self.assertEqual(info.tags, (), filename)
else:
self.assertEqual(
[t.slug for t in info.tags], tags.split(','),
[t.name for t in info.tags], tags.split(','),
filename
)
@ -342,8 +342,8 @@ class TestFieldPermutations(TestCase):
info = FileInfo.from_filename(filename)
self.assertEqual(info.title, "0001")
self.assertEqual(len(info.tags), 2)
self.assertEqual(info.tags[0].slug, "tag1")
self.assertEqual(info.tags[1].slug, "tag2")
self.assertEqual(info.tags[0].name, "tag1")
self.assertEqual(info.tags[1].name, "tag2")
self.assertIsNone(info.created)
# Complex transformation with date in replacement string
@ -356,8 +356,8 @@ class TestFieldPermutations(TestCase):
info = FileInfo.from_filename(filename)
self.assertEqual(info.title, "0001")
self.assertEqual(len(info.tags), 2)
self.assertEqual(info.tags[0].slug, "tag1")
self.assertEqual(info.tags[1].slug, "tag2")
self.assertEqual(info.tags[0].name, "tag1")
self.assertEqual(info.tags[1].name, "tag2")
self.assertEqual(info.created.year, 2019)
self.assertEqual(info.created.month, 9)
self.assertEqual(info.created.day, 8)

View File

@ -103,10 +103,7 @@ class MailAccountHandler(LoggingMixin):
def _correspondent_from_name(self, name):
try:
return Correspondent.objects.get_or_create(
name=name, defaults={
"slug": slugify(name)
})[0]
return Correspondent.objects.get_or_create(name=name)[0]
except DatabaseError as e:
self.log(
"error",