Skip to content

Commit

Permalink
Example layout generation view
Browse files Browse the repository at this point in the history
  • Loading branch information
fbanados committed Oct 21, 2024
1 parent 213d2a7 commit 56ab12c
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/morphodict/frontend/templatetags/morphodict_extras.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from django.utils.html import escape, format_html
from django.utils.safestring import mark_safe

from morphodict.paradigm.crkeng_corpus_frequency import (
from morphodict.search.crkeng_corpus_frequency import (
observed_wordforms,
)
from morphodict.relabelling import read_labels
Expand Down
3 changes: 3 additions & 0 deletions src/morphodict/paradigm/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Paradigm generation

The `.tsv` files in the `layouts/` folder are the ones used by default. If the specific dictionary folder `sssttt/resources/layouts/` folder exists, that one takes precedence.
44 changes: 44 additions & 0 deletions src/morphodict/paradigm/test_layout.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import logging
import re
from http import HTTPStatus
from typing import Dict, Optional, cast

import pytest
from django.http import (
HttpResponseBadRequest,
HttpResponseNotAllowed,
HttpResponseNotFound,
)
from django.test import Client
from django.urls import reverse
from pytest_django.asserts import assertInHTML

from crkeng.app.preferences import DisplayMode
from morphodict.lexicon.models import Wordform

@pytest.mark.parametrize(
("lemma_text", "layout", "paradigm_size", "entries"),
[
("wâpamêw", "VTA", "full", ["kiwâpamikonaw", "ê-kî-wâpamiskik", "ê-wâpamitân", "niwâpamâw"]),
("kotiskâwêw", "VAI", "full", ["nikotiskâwân", "ê-wî-kotiskâwêt", "kotiskâwê", "nikotiskâwân"]),
("acimosis", "NA", "full", ["acimosis", "acimosisak", "kicacimosisimiwâwa"])
],
)
def test_paradigm_from_full_page_and_api(client: Client, lemma_text: str, paradigm_size: str, layout: str, entries: list[str]):
"""
The paradigm returned from the full details page and the API endpoint should
contain the exact same information.
"""
# Get standalone page:
response = client.get(
reverse("morphodict-paradigm-layout"),
{
"paradigm-size": paradigm_size,
"lemma":lemma_text,
"layout": layout
},
)
assert response.status_code == HTTPStatus.OK
standalone_html = response.content.decode("UTF-8")
for entry in entries:
assert entry in standalone_html
5 changes: 5 additions & 0 deletions src/morphodict/paradigm/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,9 @@
views.paradigm_internal,
name="morphodict-paradigm-detail",
),
path(
"_paradigm_layout/",
views.paradigm_for_lemma,
name="morphodict-paradigm-layout"
)
]
38 changes: 38 additions & 0 deletions src/morphodict/paradigm/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,43 @@ def paradigm_internal(request):
},
)

@require_GET
def paradigm_for_lemma(request):
"""
Render word-detail.html for a lemma. `index` view function renders a whole page that contains word-detail.html too.
This function, however, is used by javascript to dynamically replace the paradigm with the ones of different sizes.
Unlike `paradigm_internal`, we take a lemma and a paradigm layout to produce the paradigm.
`lemma`, `layout`, and `paradigm-size` are the expected query params in the request
4xx errors will have a single string as error message
:raise 400 Bad Request: when the query params are not as expected or inappropriate
:raise 404 Not Found: when the lemma-id isn't found in the database
:raise 405 Method Not Allowed: when method other than GET is used
"""
lemma = request.GET.get("lemma")
layout = request.GET.get("layout")
paradigm_size = request.GET.get("paradigm-size")

manager = default_paradigm_manager()

try:
if not (paradigm := manager.paradigm_for(layout, lemma, paradigm_size)):
return HttpResponseBadRequest("paradigm does not exist")
except ParadigmDoesNotExistError:
return HttpResponseBadRequest("paradigm does not exist")

return render(
request,
"morphodict/components/paradigm.html",
{
"lemma": lemma,
"paradigm_size": paradigm_size,
"paradigm": paradigm,
"show_morphemes": request.COOKIES.get("show_morphemes"),
},
)

def paradigm_for(wordform: Wordform, paradigm_size: str) -> Optional[Paradigm]:
"""
Expand Down Expand Up @@ -93,6 +130,7 @@ def paradigm_for(wordform: Wordform, paradigm_size: str) -> Optional[Paradigm]:

return None


def paradigm_context_for_lemma(lemma: Wordform, request) -> dict[str, Any]:
paradigm = lemma.paradigm
if paradigm is not None:
Expand Down

0 comments on commit 56ab12c

Please sign in to comment.