Skip to content

Commit 6d9903a

Browse files
committed
Inform users of word duplication in document form
1 parent 6598cae commit 6d9903a

6 files changed

Lines changed: 119 additions & 3 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ UNRELEASED
33

44
* [ [#353](https://github.com/digitalfabrik/lunes-cms/issues/353) ] Filter feedback by creator of related objects
55
* [ [#468](https://github.com/digitalfabrik/lunes-cms/issues/468) ] Excel list out of existing vocabulary in cms
6+
* [ [#470](https://github.com/digitalfabrik/lunes-cms/issues/470) ] Inform users of word duplication in document form
67

78

89
2024.5.0

lunes_cms/api/utils.py

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@
44

55
from django.core.exceptions import PermissionDenied
66
from django.db.models import Count, Q
7+
from django.http import JsonResponse
8+
from django.utils.translation import ugettext_lazy as _
9+
710
from rest_framework import routers
811

9-
from ..cms.models import Discipline, GroupAPIKey
12+
from ..cms.models import Discipline, GroupAPIKey, Document
1013
from ..cms.utils import get_child_count
1114

1215

@@ -151,3 +154,31 @@ def check_group_object_permissions(request, group_id):
151154
api_key_object = GroupAPIKey.get_from_token(key)
152155
if api_key_object.group_id != int(group_id):
153156
raise PermissionDenied()
157+
158+
159+
def get_documents_by_string(request, word):
160+
"""
161+
Function to find existing words that match the input in the "word" field of document
162+
163+
:param request: current request
164+
:type request: HttpRequest
165+
166+
:param group_id: input in the "word"field of document
167+
:type group_id: str
168+
169+
:return: Whether any duplicate was found, and some details of the word if found
170+
:rtype: JsonResponse
171+
172+
"""
173+
174+
if duplicate := Document.objects.filter(word=word).first():
175+
result = {
176+
"message": _("The word is already registered in the system."),
177+
"word": duplicate.word + " (" + duplicate.word_type + ")",
178+
"definition": duplicate.definition
179+
if duplicate.definition
180+
else _("No definition is provided"),
181+
}
182+
183+
return JsonResponse(result)
184+
return JsonResponse({"message": _("This word is not yet registered in the system")})

lunes_cms/api/v1/urls.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from django.urls import include, path
55
from drf_spectacular.views import SpectacularAPIView, SpectacularSwaggerView
66

7-
from ..utils import OptionalSlashRouter
7+
from ..utils import OptionalSlashRouter, get_documents_by_string
88
from . import views
99

1010
#: The namespace for this URL config (see :attr:`django.urls.ResolverMatch.app_name`)
@@ -59,4 +59,9 @@
5959
),
6060
name="swagger-ui",
6161
),
62+
path(
63+
"documents_by_string/<word>",
64+
get_documents_by_string,
65+
name="documents_by_string",
66+
),
6267
]

lunes_cms/cms/fixtures/test_data.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6891,6 +6891,7 @@
68916891
"word_type": "Nomen",
68926892
"word": "Ei",
68936893
"singular_article": 3,
6894+
"definition": "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua",
68946895
"audio": "audio/ei.mp3",
68956896
"creation_date": "2021-08-09T13:52:38.131Z",
68966897
"created_by": null,

lunes_cms/locale/de/LC_MESSAGES/django.po

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ msgid ""
77
msgstr ""
88
"Project-Id-Version: PACKAGE VERSION\n"
99
"Report-Msgid-Bugs-To: \n"
10-
"POT-Creation-Date: 2024-06-02 09:04+0000\n"
10+
"POT-Creation-Date: 2024-06-13 20:53+0000\n"
1111
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
1212
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
1313
"Language-Team: LANGUAGE <LL@li.org>\n"
@@ -21,6 +21,18 @@ msgstr ""
2121
msgid "API"
2222
msgstr "API"
2323

24+
#: api/utils.py:163
25+
msgid "The word is already registered in the system."
26+
msgstr "Das Word ist schon im System eingetragen."
27+
28+
#: api/utils.py:165
29+
msgid "No definition is provided"
30+
msgstr "Keine Definition ist eingegeben."
31+
32+
#: api/utils.py:170
33+
msgid "This word is not yet registered in the system"
34+
msgstr "Das Word ist noch nicht im System eingetragen."
35+
2436
#: api/v1/serializers/feedback_serializer.py:22
2537
msgid ""
2638
"The content type must be either 'discipline', 'training set' or 'document'."

lunes_cms/static/js/toggle_plural_field.js

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ if (!$) {
22
$ = django.jQuery;
33
}
44

5+
//Toggle the plural field depending on the chosen word type
56
$(document).ready(() => {
67
$("#id_word_type").change((event) =>
78
$("#id_plural")
@@ -11,6 +12,7 @@ $(document).ready(() => {
1112
$("#id_word_type").trigger("change");
1213
});
1314

15+
//Toggle the drop down for grammatical gender depending on the chosen word type
1416
$(document).ready(() => {
1517
$("#id_word_type").change((event) =>
1618
$("#id_grammatical_gender")
@@ -19,3 +21,67 @@ $(document).ready(() => {
1921
);
2022
$("#id_word_type").trigger("change");
2123
});
24+
25+
26+
27+
function show_duplicates(data, parent) {
28+
var existingMessage = document.getElementById("result");
29+
30+
if (existingMessage) {
31+
existingMessage.remove();
32+
}
33+
34+
35+
result = document.createElement("div");
36+
result.setAttribute("id", "result");
37+
38+
39+
if (data["word"]) {
40+
// If there is a duplicated word, use orange background
41+
result.style.backgroundColor = "orange";
42+
result.style.padding = "25px"
43+
44+
// Show alert message
45+
messageBox = document.createElement("div");
46+
message = document.createTextNode(data["message"]);
47+
messageBox.append(message);
48+
result.append(messageBox);
49+
// Show the duplicated word with its word type
50+
wordBox = document.createElement("div");
51+
word = document.createTextNode(data["word"]);
52+
wordBox.style.fontWeight = "bold";
53+
wordBox.append(word);
54+
result.append(wordBox);
55+
// Show its definition too for more detail
56+
definition = document.createTextNode(data["definition"]);
57+
result.append(definition);
58+
} else {
59+
// If there is no duplicate, use green background
60+
result.style.backgroundColor = "green";
61+
result.style.padding = "25px"
62+
// Show message that no duplicate was found
63+
messageBox = document.createElement("div");
64+
message = document.createTextNode(data["message"]);
65+
messageBox.append(message);
66+
result.append(messageBox);
67+
}
68+
69+
70+
parent.prepend(result);
71+
}
72+
73+
74+
$(document).ready(() => {
75+
$("#id_word").change((event) =>
76+
$.ajax({
77+
type: 'GET',
78+
url: '/api/documents_by_string/' + $(event.target).val(),
79+
dataType: "json",
80+
success: function(data) {
81+
show_duplicates(data, $(event.target).closest(".card-body"));
82+
}
83+
})
84+
);
85+
86+
$("#id_word_type").trigger("change");
87+
});

0 commit comments

Comments
 (0)