Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add hyphen and underscore as valid term id and improve error messages #247

Open
wants to merge 3 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 26 additions & 5 deletions evoks/tests/views/test_vocabulary_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ def test_overview_search(self):
# create term so that search finds something
self.c.post(
'/vocabularies/{0}/terms'.format(self.vocabulary.name),
{'create-term': '', 'term-subject': 'testterm', 'term-label': 'testlabel'}
{'create-term': '', 'term-subject': 'testterm_43-2_23', 'term-label': 'testlabel'}
)

response = self.c.get(
Expand Down Expand Up @@ -502,20 +502,41 @@ def test_terms_view_add_term(self):
def test_create_term(self):
response = self.c.post(
'/vocabularies/{0}/terms'.format(self.vocabulary.name),
{'create-term': '', 'term-subject': 'testterm', 'term-label': 'testlabel'},
{'create-term': '', 'term-subject': 'testterm_43-2_23', 'term-label': 'testlabel'},
follow=True
)
self.assertEqual(response.status_code, 200)
voc = Vocabulary.objects.get(name=self.vocabulary.name)
self.assertTrue(voc.term_set.filter(name='testterm').exists())
self.assertTrue(voc.term_set.filter(name='testterm_43-2_23').exists())

# create same term again
error_response = self.c.post(
'/vocabularies/{0}/terms'.format(self.vocabulary.name),
{'create-term': '', 'term-subject': 'testterm', 'term-label': 'testlabel'}
{'create-term': '', 'term-subject': 'testterm_43-2_23', 'term-label': 'testlabel'}
)
self.assertEqual(error_response.status_code, 409)

# create term with empty subject
error_response = self.c.post(
'/vocabularies/{0}/terms'.format(self.vocabulary.name),
{'create-term': '', 'term-subject': '', 'term-label': 'testlabelempty'}
)
self.assertEqual(error_response.status_code, 400)

# create term with space in subject
error_response = self.c.post(
'/vocabularies/{0}/terms'.format(self.vocabulary.name),
{'create-term': '', 'term-subject': 'test term', 'term-label': 'testlabelspace'}
)
self.assertEqual(error_response.status_code, 400)

# create term with disallowed character in subject
error_response = self.c.post(
'/vocabularies/{0}/terms'.format(self.vocabulary.name),
{'create-term': '', 'term-subject': 'testterm!', 'term-label': 'testlabelquestionmark'}
)
self.assertEqual(error_response.status_code, 400)

# base view tests

def test_base_view(self):
Expand Down Expand Up @@ -571,7 +592,7 @@ def test_base_search(self):
# create term so that search finds something
self.c.post(
'/vocabularies/{0}/terms'.format(self.vocabulary.name),
{'create-term': '', 'term-subject': 'testterm', 'term-label': 'testlabel'}
{'create-term': '', 'term-subject': 'testterm_43-2_23', 'term-label': 'testlabel'}
)

response = self.c.get(
Expand Down
2 changes: 1 addition & 1 deletion evoks/theme/templates/modals/create_vocabulary.html
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ <h3 class="text-lg leading-6 font-medium text-gray-900" id="modal-title">
<div class="mt-1">
<input type="text" name="name" id="text" maxlength="50" required='True'
pattern="[A-Za-z0-9_-]+"
title="Error, only letters and numbers without blanks are supported"
title="error, only alphanumberic characters, underscore (_) or hyphen (-) allowed (no blanks)"
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Fix typo in the title attribute.

There's a typo in "alphanumberic" which should be "alphanumeric".

-                                                    title="error, only alphanumberic characters, underscore (_) or hyphen (-) allowed (no blanks)"
+                                                    title="error, only alphanumeric characters, underscore (_) or hyphen (-) allowed (no blanks)"
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
title="error, only alphanumberic characters, underscore (_) or hyphen (-) allowed (no blanks)"
title="error, only alphanumeric characters, underscore (_) or hyphen (-) allowed (no blanks)"

class="shadow-sm focus:ring-gray-500 focus:border-gray-500 block w-full sm:text-sm border-gray-300 rounded-md"
placeholder="">
</div>
Expand Down
4 changes: 2 additions & 2 deletions evoks/vocabularies/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -605,8 +605,8 @@ def terms(request: HttpRequest, voc_name: str) -> HttpResponse:
term_subject = request.POST['term-subject']
term_label = request.POST['term-label']

if not (bool(re.match('^[a-zA-Z0-9]+$', term_subject))):
return HttpResponseBadRequest('invalid subject')
if not (bool(re.match('^[a-zA-Z0-9_-]+$', term_subject))):
return HttpResponseBadRequest('error, subject may only contain alphanumberic characters, underscores (_) or hyphens (-) (no blanks)')

term_name = term_subject.replace('/', '_')

Expand Down
Loading