Skip to content

Commit

Permalink
Sort documentation page in canonical order
Browse files Browse the repository at this point in the history
  • Loading branch information
JeltevanBoheemen committed Jul 5, 2024
1 parent 0a2d421 commit 2b42309
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 7 deletions.
6 changes: 5 additions & 1 deletion backend/addcorpus/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -431,11 +431,12 @@ def clean(self):
e
])


class CorpusDocumentationPage(models.Model):
class PageType(models.TextChoices):
GENERAL = ('general', 'General information')
CITATION = ('citation', 'Citation')
LICENSE = ('license', 'Licence')
LICENSE = ('license', 'License')
TERMS_OF_SERVICE = ('terms_of_service', 'Terms of service')
WORDMODELS = ('wordmodels', 'Word models')

Expand All @@ -455,6 +456,9 @@ class PageType(models.TextChoices):
help_text='markdown contents of the documentation'
)

def __str__(self):
return f'{self.corpus_configuration.corpus.name} - {self.type}'

class Meta:
constraints = [
UniqueConstraint(
Expand Down
7 changes: 6 additions & 1 deletion backend/addcorpus/tests/test_corpus_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,14 @@ def test_no_corpora(db, settings, admin_client):
def test_corpus_documentation_view(admin_client, basic_mock_corpus, settings):
response = admin_client.get(f'/api/corpus/documentation/{basic_mock_corpus}/')
assert response.status_code == 200
pages = response.data

# check that the pages are sorted in canonical order
page_types = [page['type'] for page in pages]
assert page_types == ['General information', 'Citation', 'License']

# should contain citation guidelines
citation_page = next(page for page in response.data if page['type'] == 'Citation')
citation_page = next(page for page in pages if page['type'] == 'Citation')

# check that the page template is rendered with context
content = citation_page['content']
Expand Down
18 changes: 13 additions & 5 deletions backend/addcorpus/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,22 +42,30 @@ def send_corpus_file(corpus='', subdir='', filename=''):

return FileResponse(open(path, 'rb'))


class CorpusDocumentationPageViewset(viewsets.ModelViewSet):
permission_classes = [IsAuthenticatedOrReadOnly, CorpusAccessPermission]
serializer_class = CorpusDocumentationPageSerializer

def get_queryset(self):
corpus_name = corpus_name_from_request(self.request)
pages = CorpusDocumentationPage.objects.filter(corpus_configuration__corpus__name=corpus_name)

@staticmethod
def get_relevant_pages(pages, corpus_name):
# only include wordmodels documentation if models are present
if Corpus.objects.get(name=corpus_name).has_python_definition:
definition = load_corpus_definition(corpus_name)
if definition.word_models_present:
return pages

return pages.exclude(type=CorpusDocumentationPage.PageType.WORDMODELS)

def get_queryset(self):
corpus_name = corpus_name_from_request(self.request)
pages = CorpusDocumentationPage.objects.filter(
corpus_configuration__corpus__name=corpus_name)
relevant_pages = self.get_relevant_pages(pages, corpus_name)
canonical_order = [e.value for e in CorpusDocumentationPage.PageType]

return sorted(
relevant_pages, key=lambda p: canonical_order.index(p.type))


class CorpusImageView(APIView):
'''
Expand Down
1 change: 1 addition & 0 deletions backend/corpora_test/basic/license/license.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Do whatever you please.
2 changes: 2 additions & 0 deletions backend/corpora_test/basic/mock_csv_corpus.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ class MockCSVCorpus(CSVCorpusDefinition):
max_date = datetime.datetime(year=2022, month=12, day=31)
data_directory = os.path.join(here, 'source_data')
citation_page = 'citation.md'
license_page = 'license.md'
description_page = 'mock-csv-corpus.md'

languages = ['en']
category = 'book'
Expand Down

0 comments on commit 2b42309

Please sign in to comment.