Skip to content

Commit

Permalink
Merge pull request #115 from SADiLaR/rename-documentfile2document
Browse files Browse the repository at this point in the history
Rename documentfile to document
  • Loading branch information
friedelwolff authored Aug 19, 2024
2 parents b0b2f02 + f483aac commit 56a503e
Show file tree
Hide file tree
Showing 18 changed files with 101 additions and 75 deletions.
27 changes: 13 additions & 14 deletions app/app/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
from django.utils.http import urlencode
from django.utils.translation import gettext as _

from general.filters import DocumentFileFilter
from general.models import DocumentFile, Institution, Language, Project, Subject
from general.filters import DocumentFilter
from general.models import Document, Institution, Language, Project, Subject


def health(request):
Expand Down Expand Up @@ -154,7 +154,7 @@ def institution_detail(request, institution_id):

institution = get_object_or_404(Institution, id=institution_id)
projects = Project.objects.filter(institution=institution).order_by("name")
documents = DocumentFile.objects.filter(institution=institution).order_by("title")
documents = Document.objects.filter(institution=institution).order_by("title")

context = {
"current_page": "institution_detail",
Expand All @@ -171,7 +171,7 @@ def documents(request):
template = "app/documents.html"

documents = (
DocumentFile.objects.select_related("institution")
Document.objects.select_related("institution")
.prefetch_related("subjects", "languages")
.order_by("title")
)
Expand Down Expand Up @@ -227,7 +227,7 @@ def document_detail(request, document_id):
template = "app/document_detail.html"

document = get_object_or_404(
DocumentFile.objects.select_related("institution").prefetch_related(
Document.objects.select_related("institution").prefetch_related(
Prefetch("subjects", queryset=Subject.objects.order_by("name")),
Prefetch("languages", queryset=Language.objects.order_by("name")),
),
Expand All @@ -248,8 +248,8 @@ def languages(request):
Language.objects.all()
.prefetch_related(
Prefetch(
"documentfile_set",
queryset=DocumentFile.objects.only("id", "title", "languages").order_by("title"),
"document_set",
queryset=Document.objects.only("id", "title", "languages").order_by("title"),
),
Prefetch(
"project_set",
Expand All @@ -261,7 +261,7 @@ def languages(request):

language_data = []
for language in languages:
documents = language.documentfile_set.all()
documents = language.document_set.all()
projects = language.project_set.all()
language_data.append(
{
Expand All @@ -285,8 +285,8 @@ def subjects(request):
Subject.objects.all()
.prefetch_related(
Prefetch(
"documentfile_set",
queryset=DocumentFile.objects.only("id", "title", "subjects").order_by("title"),
"document_set",
queryset=Document.objects.only("id", "title", "subjects").order_by("title"),
),
Prefetch(
"project_set",
Expand All @@ -299,7 +299,7 @@ def subjects(request):
subject_data = []

for subject in subjects:
documents = subject.documentfile_set.all()
documents = subject.document_set.all()
projects = subject.project_set.all()
if documents or projects:
subject_data.append(
Expand All @@ -311,7 +311,6 @@ def subjects(request):
)

paginator = Paginator(subject_data, 10)

page_number = request.GET.get("page")
page_obj = paginator.get_page(page_number)

Expand All @@ -329,7 +328,7 @@ def institutions(request):

# https://docs.djangoproject.com/en/stable/topics/db/aggregation/#combining-multiple-aggregations
subquery = (
DocumentFile.objects.filter(institution=OuterRef("id"))
Document.objects.filter(institution=OuterRef("id"))
.order_by()
.annotate(count=Func(F("id"), function="Count"))
)
Expand Down Expand Up @@ -371,7 +370,7 @@ def institutions(request):


def search(request):
f = DocumentFileFilter(request.GET, queryset=DocumentFile.objects.all())
f = DocumentFilter(request.GET, queryset=Document.objects.all())

template = "app/search.html"

Expand Down
12 changes: 6 additions & 6 deletions app/general/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@

from general.service.extract_text import GetTextError, pdf_to_text

from .models import DocumentFile, Institution, Language, Project, Subject
from .models import Document, Institution, Language, Project, Subject


class DocumentFileForm(ModelForm):
class DocumentForm(ModelForm):
class Meta:
model = DocumentFile
model = Document
fields = "__all__" # noqa: DJ007

def __init__(self, *args, **kwargs):
Expand Down Expand Up @@ -57,12 +57,12 @@ def clean(self):
return cleaned_data


class DocumentFileAdmin(SimpleHistoryAdmin):
class DocumentAdmin(SimpleHistoryAdmin):
ordering = ["title"]
list_display = ["title", "license", "document_type", "available"]
search_fields = ["title"]
list_filter = ["institution", "license", "document_type"]
form = DocumentFileForm
form = DocumentForm
history_list_display = ["title", "license", "document_type", "available"]


Expand Down Expand Up @@ -102,4 +102,4 @@ class InstitutionAdmin(SimpleHistoryAdmin):
admin.site.register(Institution, InstitutionAdmin)
admin.site.register(Language, LanguageAdmin)
admin.site.register(Subject, SubjectAdmin)
admin.site.register(DocumentFile, DocumentFileAdmin)
admin.site.register(Document, DocumentAdmin)
6 changes: 3 additions & 3 deletions app/general/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
from django.utils.translation import gettext_lazy as _
from django_filters import ModelMultipleChoiceFilter, MultipleChoiceFilter

from general.models import DocumentFile, Institution, Language, Project, Subject
from general.models import Document, Institution, Language, Project, Subject


class DocumentFileFilter(django_filters.FilterSet):
class DocumentFilter(django_filters.FilterSet):
search = django_filters.CharFilter(method="ignore", label=_("Search"))

institution = ModelMultipleChoiceFilter(
Expand All @@ -35,7 +35,7 @@ class DocumentFileFilter(django_filters.FilterSet):
)

class Meta:
model = DocumentFile
model = Document
fields = [
"institution",
"subjects",
Expand Down
4 changes: 2 additions & 2 deletions app/general/management/commands/dev_update_vector_search.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from django.core.management.base import BaseCommand

from general.models import DocumentFile
from general.models import Document


class Command(BaseCommand):
help = "Updating the Vector Search index on document_file."

def handle(self, *args, **options):
for document_file in DocumentFile.objects.all():
for document_file in Document.objects.all():
document_file.save() # This line updates the vector search for the document file
print(f"Updated {document_file.title}.")
4 changes: 2 additions & 2 deletions app/general/management/commands/import_documents.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from django.core.files.base import ContentFile
from django.core.management.base import BaseCommand

from general.models import DocumentFile
from general.models import Document
from general.service.extract_text import GetTextError, pdf_to_text


Expand Down Expand Up @@ -42,7 +42,7 @@ def save_data(self, file_path, file_name):
content_file = ContentFile(f.read(), name=file_name)

try:
instance = DocumentFile(
instance = Document(
title=file_name,
document_data=pdf_to_text(file_path),
uploaded_file=content_file,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Generated by Django 5.0.8 on 2024-08-17 14:47

from django.conf import settings
from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('general', '0012_alter_documentfile_search_vector'),
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]

operations = [
migrations.RenameModel(
old_name='DocumentFile',
new_name='Document',
),
migrations.RenameModel(
old_name='HistoricalDocumentFile',
new_name='HistoricalDocument',
),
migrations.RenameIndex(
model_name='document',
new_name='general_doc_search__12340c_gin',
old_name='general_doc_search__752b22_gin',
),
]
2 changes: 1 addition & 1 deletion app/general/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def __str__(self):
return self.name


class DocumentFile(models.Model):
class Document(models.Model):
file_validators = [FileExtensionValidator(["pdf"])]

# names and abbreviations based on
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
from django.core.files.uploadedfile import SimpleUploadedFile
from django.test import TestCase

from general.models import DocumentFile, Institution, Language, Subject
from general.models import Document, Institution, Language, Subject


class DocumentFileTest(TestCase):
class DocumentTest(TestCase):
def setUp(self):
self.subject = Subject.objects.create(name="Test Subject")
self.language = Language.objects.create(name="Test Language", iso_code="TL")
Expand All @@ -22,7 +22,7 @@ def setUp(self):
self.document_type = "Glossary"
self.institution = self.institution

self.document = DocumentFile.objects.create(
self.document = Document.objects.create(
title=self.title,
url=self.url,
uploaded_file=self.uploaded_file,
Expand All @@ -35,8 +35,8 @@ def setUp(self):
self.document.languages.add(self.language)

def test_document_creation(self):
self.assertEqual(DocumentFile.objects.count(), 1)
self.assertEqual(DocumentFile.objects.get().title, self.title)
self.assertEqual(Document.objects.count(), 1)
self.assertEqual(Document.objects.get().title, self.title)

def test_document_str_representation(self): # Test __str__ method
self.assertEqual(str(self.document), self.title)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@

from django.core.files.uploadedfile import SimpleUploadedFile

from general.admin import DocumentFileForm
from general.admin import DocumentForm
from general.models import Institution


class TestDocumentFileForm(unittest.TestCase):
class TestDocumentForm(unittest.TestCase):
def __init__(self, methodName: str = "runTest"):
super().__init__(methodName)
self.form = None
Expand All @@ -33,7 +33,7 @@ def test_clean_without_url_and_file(self):
"description": "Test description",
}

form = DocumentFileForm(tests_form)
form = DocumentForm(tests_form)
self.assertFalse(form.is_valid())
self.assertEqual(form.errors["url"], ["Either URL or uploaded file must be provided."])
self.assertEqual(
Expand All @@ -53,7 +53,7 @@ def test_clean_without_file(self):
"description": "",
}

form = DocumentFileForm(tests_form)
form = DocumentForm(tests_form)
self.assertTrue(form.is_valid())

#
Expand All @@ -70,7 +70,7 @@ def test_clean_without_url(self):
"description": "Test description",
}

form = DocumentFileForm(tests_form, files={"uploaded_file": self.file_mock})
form = DocumentForm(tests_form, files={"uploaded_file": self.file_mock})
self.assertTrue(form.is_valid())

def test_clean_with_large_file(self):
Expand All @@ -87,7 +87,7 @@ def test_clean_with_large_file(self):
"description": "Test description",
}

form = DocumentFileForm(tests_form, files={"uploaded_file": self.file_mock})
form = DocumentForm(tests_form, files={"uploaded_file": self.file_mock})
self.assertFalse(form.is_valid())
self.assertIn("uploaded_file", form.errors)
self.assertEqual(form.errors["uploaded_file"], ["File size must not exceed 10MB."])
Expand Down
4 changes: 2 additions & 2 deletions app/general/tests/test_document_detail.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from django.test import TestCase
from django.urls import reverse

from general.models import DocumentFile, Institution, Language, Project, Subject
from general.models import Document, Institution, Language, Project, Subject


class DocumentDetailViewTest(TestCase):
Expand All @@ -14,7 +14,7 @@ def setUp(self):
self.language1 = Language.objects.create(name="Afrikaans", iso_code="af")
self.language2 = Language.objects.create(name="English", iso_code="en")

self.document = DocumentFile.objects.create(
self.document = Document.objects.create(
title="Afrikaans_HL_P1_Feb-March_2011",
description="This is a description of the document.",
url="https://externaldocumentrepository.com/document1",
Expand Down
6 changes: 3 additions & 3 deletions app/general/tests/test_documents.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from django.test import TestCase
from django.urls import reverse

from general.models import DocumentFile, Institution, Language, Subject
from general.models import Document, Institution, Language, Subject


class DocumentViewTests(TestCase):
Expand All @@ -16,14 +16,14 @@ def setUp(self):
name="Institution", logo="institution_logo.png"
)

self.document5 = DocumentFile.objects.create(
self.document5 = Document.objects.create(
title="Document 5",
institution=self.institution,
)
self.document5.subjects.add(self.subject1)
self.document5.languages.add(self.language1)

self.document6 = DocumentFile.objects.create(
self.document6 = Document.objects.create(
title="Document 6",
institution=self.institution,
)
Expand Down
Loading

0 comments on commit 56a503e

Please sign in to comment.