Skip to content

Commit

Permalink
Merge pull request #625 from projectcaluma/fix-document-model
Browse files Browse the repository at this point in the history
fix!: make document title and description not localized
  • Loading branch information
czosel authored Aug 30, 2024
2 parents dc8562c + b4dc203 commit c02a0a8
Show file tree
Hide file tree
Showing 11 changed files with 147 additions and 166 deletions.
6 changes: 3 additions & 3 deletions alexandria/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,8 @@ def document_post_data(category):
b"%PDF-1.\ntrailer<</Root<</Pages<</Kids[<</MediaBox[0 0 3 3]>>]>>>>>>"
),
"data": io.BytesIO(
json.dumps(
{"title": {"en": "winstonsmith"}, "category": category.pk}
).encode("utf-8")
json.dumps({"title": "winstonsmith", "category": category.pk}).encode(
"utf-8"
)
),
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from django.core.management.base import BaseCommand
from pghstore import loads
from tqdm import tqdm

from alexandria.core.models import Document


class Command(BaseCommand):
help = "Migrate document title and description from localized to unlocalized."

def add_arguments(self, parser):
parser.add_argument("source_language")

def handle(self, *args, **options):
source_language = options["source_language"]
for document in tqdm(Document.objects.all()):
try:
document.title = loads(document.title)[source_language]
document.description = loads(document.description)[source_language]
document.save()
except ValueError:
pass
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Generated by Django 4.2.15 on 2024-08-29 07:51

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("alexandria_core", "0017_file_vector"),
]

operations = [
migrations.AlterField(
model_name="document",
name="description",
field=models.TextField(
blank=True, null=True, verbose_name="document description"
),
),
migrations.AlterField(
model_name="document",
name="title",
field=models.CharField(
blank=True, null=True, verbose_name="document title"
),
),
]
8 changes: 2 additions & 6 deletions alexandria/core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,12 +151,8 @@ class Mark(SlugModel):


class Document(UUIDModel):
title = LocalizedCharField(
_("document title"), blank=True, null=True, required=False
)
description = LocalizedTextField(
_("document description"), null=True, blank=True, required=False
)
title = models.CharField(_("document title"), blank=True, null=True)
description = models.TextField(_("document description"), null=True, blank=True)
category = models.ForeignKey(
Category,
on_delete=models.SET_NULL,
Expand Down
202 changes: 57 additions & 145 deletions alexandria/core/tests/__snapshots__/test_viewsets.ambr

Large diffs are not rendered by default.

14 changes: 14 additions & 0 deletions alexandria/core/tests/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,17 @@ def test_generate_empty_content_vector(db, settings, file_factory):

assert file_without_vector.content_vector == "'old':1A"
assert file_without_vector.language is None


def test_migrate_document_title_and_description(db, settings, document_factory):
doc = document_factory(title='"en"=>"pdf-test.pdf"', description='"en"=>""')
doc2 = document_factory(title="already_migrated", description="")

call_command("migrate_document_title_and_description", "en")

doc.refresh_from_db()
doc2.refresh_from_db()
assert doc.title == "pdf-test.pdf"
assert doc.description == ""
assert doc2.title == "already_migrated"
assert doc2.description == ""
8 changes: 3 additions & 5 deletions alexandria/core/tests/test_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,8 @@ def test_document_search_filter(
"""
tag1 = tag_factory(name="bar_tag") # matches
tag2 = tag_factory(name="foo_tag") # doesn't match
doc = document_factory(title={"en": "foo_title", "de": "bar_title"}) # matches
doc2 = document_factory(
title={"en": "bar_title", "de": "foo_title"}
) # doesn't match
doc = document_factory(title="foo_title") # matches
doc2 = document_factory(title="bar_title") # doesn't match
doc.tags.add(tag1)
doc2.tags.add(tag2)
file_factory(name="filename.pdf", document=doc)
Expand Down Expand Up @@ -386,4 +384,4 @@ def test_document_category_filters(
assert response.status_code == HTTP_200_OK
data = response.json()["data"]
assert len(data) == expected_count
assert sorted([doc["attributes"]["title"]["en"] for doc in data]) == snapshot
assert sorted([doc["attributes"]["title"] for doc in data]) == snapshot
6 changes: 3 additions & 3 deletions alexandria/core/tests/test_permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class CustomPermission:
@permission_for(Document)
def has_permission_for_document(self, request):
if request.method == "PATCH":
return request.data["title"]["en"] == "new"
return request.data["title"] == "new"
elif request.method in {"DELETE", "POST"}:
return request.user.username == "admin"

Expand All @@ -73,7 +73,7 @@ def has_object_permission_for_document(self, request, instance):
"data": {
"id": doc.pk,
"type": "documents",
"attributes": {"title": {"de": "", "en": "new", "fr": ""}},
"attributes": {"title": "new"},
"relationships": {
"category": {"data": {"id": doc.category.pk, "type": "categories"}}
},
Expand All @@ -96,7 +96,7 @@ def has_object_permission_for_document(self, request, instance):
assert result["data"]["attributes"]["created-at"] == TIMESTAMP
elif method == "patch":
doc.refresh_from_db()
assert doc.title["en"] == "new"
assert doc.title == "new"


@pytest.mark.freeze_time(TIMESTAMP)
Expand Down
4 changes: 1 addition & 3 deletions alexandria/core/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,7 @@ def convert(self, request, pk=None):
user=user,
group=group,
category=document.category,
document_title={
k: f"{ splitext(v)[0]}.pdf" for k, v in document.title.items()
},
document_title=document.title,
file_name=file_name,
file_content=ContentFile(response.content, file_name),
mime_type="application/pdf",
Expand Down
15 changes: 14 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ requests = "^2.31.0"
tika = "^2.6.0"
tqdm = "^4.66.5"
gunicorn = "^23.0.0"
# only needed in migrate_document_title_and_description.py, can be dropped in the future
pghstore = "^2.0.2"

[tool.poetry.group.dev.dependencies]
django-extensions = "3.2.3"
Expand Down

0 comments on commit c02a0a8

Please sign in to comment.