From 45e2d282caf29052b39d6d3d306c5104a76091ba Mon Sep 17 00:00:00 2001 From: Guillaume Blanchet Date: Fri, 27 Aug 2021 09:27:27 -0400 Subject: [PATCH] add manage unicode interface and fetch it from database instead of settings --- .gitignore | 3 + manage.py | 11 ++++ src/lgr_advanced/forms.py | 21 +++---- src/lgr_advanced/lgr_editor/forms/fields.py | 6 +- src/lgr_advanced/lgr_editor/forms/importer.py | 17 ++++-- src/lgr_advanced/lgr_editor/forms/metadata.py | 11 ++-- .../templates/lgr_editor/_form_field.html | 2 +- .../tests/test_meta_data_unicode_version.py | 15 +++++ src/lgr_advanced/tests/__init__.py | 0 .../tests/test_label_forms_view.py | 12 ++++ src/lgr_advanced/views.py | 20 +++---- src/lgr_idn_table_review/icann_tools/tasks.py | 4 +- src/lgr_manage/forms.py | 18 +++++- .../locale/ar/LC_MESSAGES/django.po | 33 ++++++++++- .../locale/fr/LC_MESSAGES/django.po | 26 ++++++++- .../_idn_table_review_admin_base.html | 7 ++- .../lgr_manage/unicode_versions.html | 57 +++++++++++++++++++ src/lgr_manage/tests/__init__.py | 0 src/lgr_manage/tests/test_unicode_versions.py | 48 ++++++++++++++++ src/lgr_manage/urls.py | 7 +++ src/lgr_manage/views/unicode_versions.py | 31 ++++++++++ .../migrations/0006_unicodeversion.py | 22 +++++++ .../0007_populate_unicode_versions.py | 37 ++++++++++++ src/lgr_models/models/lgr.py | 28 +++++++++ src/lgr_models/tests/test_unicode_version.py | 52 +++++++++++++++++ src/lgr_web/settings/default.py | 2 + src/lgr_web/views.py | 4 +- 27 files changed, 446 insertions(+), 48 deletions(-) create mode 100644 src/lgr_advanced/lgr_editor/tests/test_meta_data_unicode_version.py create mode 100644 src/lgr_advanced/tests/__init__.py create mode 100644 src/lgr_advanced/tests/test_label_forms_view.py create mode 100644 src/lgr_manage/templates/lgr_manage/unicode_versions.html create mode 100644 src/lgr_manage/tests/__init__.py create mode 100644 src/lgr_manage/tests/test_unicode_versions.py create mode 100644 src/lgr_manage/views/unicode_versions.py create mode 100644 src/lgr_models/migrations/0006_unicodeversion.py create mode 100644 src/lgr_models/migrations/0007_populate_unicode_versions.py create mode 100644 src/lgr_models/tests/test_unicode_version.py diff --git a/.gitignore b/.gitignore index 7cee8778..fbfd2e1b 100644 --- a/.gitignore +++ b/.gitignore @@ -44,3 +44,6 @@ src/lgr_web/static/ # Helm secret directory helm/secrets + +# SQLite dev db +src/lgr_web/dev.db \ No newline at end of file diff --git a/manage.py b/manage.py index bd2ac585..89426ca1 100755 --- a/manage.py +++ b/manage.py @@ -2,9 +2,20 @@ import os import sys + +def import_pretty_traceback_dev_dep(): + try: + import pretty_traceback + pretty_traceback.install() + except ImportError: + pass # no need to fail because of missing dev dependency + + if __name__ == "__main__": os.environ.setdefault("DJANGO_SETTINGS_MODULE", "lgr_web.settings") + import_pretty_traceback_dev_dep() + # add "src" dir to sys.path src_path = os.path.abspath(os.path.join(os.path.dirname(__file__), 'src')) sys.path.insert(0, src_path) diff --git a/src/lgr_advanced/forms.py b/src/lgr_advanced/forms.py index efe1e595..32a4d2e0 100644 --- a/src/lgr_advanced/forms.py +++ b/src/lgr_advanced/forms.py @@ -5,26 +5,23 @@ from django.utils.translation import ugettext_lazy as _ from lgr.tools.utils import parse_label_input -from lgr_utils import unidb from lgr_advanced.lgr_exceptions import lgr_exception_to_text +from lgr_models.models.lgr import UnicodeVersion +from lgr_utils import unidb class LabelFormsForm(forms.Form): label = forms.CharField(label=_("Label")) - unicode_version = forms.ChoiceField(label=_("Unicode version"), - required=True, - help_text=_('The unicode version used')) - - def __init__(self, *args, **kwargs): - unicode_versions = kwargs.pop('unicode_versions', None) - super(LabelFormsForm, self).__init__(*args, **kwargs) - if unicode_versions is not None: - self.fields['unicode_version'].choices = unicode_versions + unicode_version = forms.ModelChoiceField(label=_("Unicode version"), + required=True, + help_text=_('The unicode version used'), + queryset=UnicodeVersion.get_activated(), + empty_label=None) def clean(self): label = self.cleaned_data['label'] - unicode_version = self.cleaned_data['unicode_version'] - udata = unidb.manager.get_db_by_version(unicode_version) + unicode_version: UnicodeVersion = self.cleaned_data['unicode_version'] + udata = unidb.manager.get_db_by_version(unicode_version.version) try: value = parse_label_input(label, idna_decoder=udata.idna_decode_label) except ValueError as e: diff --git a/src/lgr_advanced/lgr_editor/forms/fields.py b/src/lgr_advanced/lgr_editor/forms/fields.py index 315290cf..58a66ef1 100644 --- a/src/lgr_advanced/lgr_editor/forms/fields.py +++ b/src/lgr_advanced/lgr_editor/forms/fields.py @@ -6,16 +6,12 @@ from itertools import chain from typing import List -from django.conf import settings from django.core.cache import cache from django.utils.encoding import force_bytes from django.utils.translation import ugettext_lazy as _ from lgr_models.models.lgr import LgrBaseModel, RzLgr, MSR -UNICODE_VERSIONS = tuple((v, v) for v in settings.SUPPORTED_UNICODE_VERSIONS) -DEFAULT_UNICODE_VERSION = settings.SUPPORTED_UNICODE_VERSIONS[0] - FILE_FIELD_ENCODING_HELP = _('File must be encoded in UTF-8 and using 0x0A line ending.') logger = logging.getLogger(__name__) @@ -75,4 +71,4 @@ def scripts(cls, unicode_database): scripts[(vr_model, vr_pk)] = vr_scripts - return scripts \ No newline at end of file + return scripts diff --git a/src/lgr_advanced/lgr_editor/forms/importer.py b/src/lgr_advanced/lgr_editor/forms/importer.py index 2281b9e1..d347da46 100644 --- a/src/lgr_advanced/lgr_editor/forms/importer.py +++ b/src/lgr_advanced/lgr_editor/forms/importer.py @@ -3,10 +3,11 @@ from django import forms from django.core import validators +from django.forms.utils import ErrorList from django.utils.translation import ugettext_lazy as _ -from .fields import (DEFAULT_UNICODE_VERSION, - FILE_FIELD_ENCODING_HELP, ValidatingRepertoire) +from lgr_models.models.lgr import UnicodeVersion +from .fields import FILE_FIELD_ENCODING_HELP, ValidatingRepertoire class NewLGRForm(forms.Form): @@ -24,8 +25,16 @@ def __init__(self, *args, **kwargs): class CreateLGRForm(NewLGRForm): name = forms.CharField(label=_("Name")) - unicode_version = forms.CharField(widget=forms.HiddenInput(), - initial=DEFAULT_UNICODE_VERSION) + unicode_version = forms.CharField(widget=forms.HiddenInput()) + + def __init__(self, data=None, files=None, auto_id='id_%s', prefix=None, initial=None, error_class=ErrorList, + label_suffix=None, empty_permitted=False, field_order=None, use_required_attribute=None, + renderer=None): + super().__init__(data, files, auto_id, prefix, initial, error_class, label_suffix, empty_permitted, field_order, + use_required_attribute, renderer) + self.fields['unicode_version'] = forms.CharField( + widget=forms.HiddenInput(), + initial=(UnicodeVersion.default(), UnicodeVersion.default())) class ImportLGRForm(NewLGRForm): diff --git a/src/lgr_advanced/lgr_editor/forms/metadata.py b/src/lgr_advanced/lgr_editor/forms/metadata.py index 243ba239..3435ac68 100644 --- a/src/lgr_advanced/lgr_editor/forms/metadata.py +++ b/src/lgr_advanced/lgr_editor/forms/metadata.py @@ -8,8 +8,9 @@ from django.forms.formsets import formset_factory from django.utils.translation import ugettext_lazy as _ +from lgr_models.models.lgr import UnicodeVersion from lgr_web.utils import IANA_LANG_REGISTRY -from .fields import UNICODE_VERSIONS, DEFAULT_UNICODE_VERSION, ValidatingRepertoire +from .fields import ValidatingRepertoire from .utils import BaseDisableableFormSet @@ -47,11 +48,10 @@ class MetadataForm(forms.Form): widget=TextInputPlaceHolder('domain')) validity_start = forms.DateField(label=_("Validity start"), widget=DateInputPlaceHolder(), required=False) validity_end = forms.DateField(label=_("Validity end"), widget=DateInputPlaceHolder(), required=False) - unicode_version = forms.ChoiceField(label=_("Unicode version"), choices=UNICODE_VERSIONS, - initial=DEFAULT_UNICODE_VERSION, required=False) + description = forms.CharField(label=_("Description"), widget=forms.Textarea, required=False) description_type = forms.ChoiceField(label=_("Description type"), choices=DESCRIPTION_CONTENT_TYPES, required=False) - + unicode_version = forms.ChoiceField(label=_("Unicode version"), required=False) validating_repertoire = forms.ChoiceField(label=_("Validating repertoire"), choices=( ('', ''), @@ -62,6 +62,9 @@ def __init__(self, *args, **kwargs): additional_repertoires = kwargs.pop('additional_repertoires', []) disabled = kwargs.pop('disabled') super(MetadataForm, self).__init__(*args, **kwargs) + self.fields['unicode_version'].choices = tuple((v.version, v.version) for v in UnicodeVersion.all()) + self.fields['unicode_version'].initial = (UnicodeVersion.default(), UnicodeVersion.default()) + self.fields['validating_repertoire'].choices = self.fields['validating_repertoire'].choices + [ (_('Built-in'), ValidatingRepertoire.choices()) ] diff --git a/src/lgr_advanced/lgr_editor/templates/lgr_editor/_form_field.html b/src/lgr_advanced/lgr_editor/templates/lgr_editor/_form_field.html index 4b5bc89e..c5ba4c30 100644 --- a/src/lgr_advanced/lgr_editor/templates/lgr_editor/_form_field.html +++ b/src/lgr_advanced/lgr_editor/templates/lgr_editor/_form_field.html @@ -3,7 +3,7 @@
{{ field|add_class:"form-control" }} - {% if field.field.widget.input_type == 'text' or field.field.widget.input_type == 'email' %} + {% if not field.field.disabled and field.field.widget.input_type == 'text' or field.field.widget.input_type == 'email' %} {% endif %} {% if field.errors %} diff --git a/src/lgr_advanced/lgr_editor/tests/test_meta_data_unicode_version.py b/src/lgr_advanced/lgr_editor/tests/test_meta_data_unicode_version.py new file mode 100644 index 00000000..90d404a4 --- /dev/null +++ b/src/lgr_advanced/lgr_editor/tests/test_meta_data_unicode_version.py @@ -0,0 +1,15 @@ +from lgr_advanced.lgr_editor.forms import MetadataForm +from lgr_models.tests.test_unicode_version import TestUnicodeVersion + + +class TestMetaDataUnicodeVersion(TestUnicodeVersion): + + def test_unicode_versions(self): + """ + Verify the list displayed is unfiltered and contains all supported versions + """ + self.a_unicode_version.activated = True + self.a_unicode_version.save() + metadata_form = MetadataForm(additional_repertoires=[], disabled=False) + self.assertSetEqual(set(metadata_form.fields['unicode_version'].choices), + {(v, v) for v in self.versions_supported}) diff --git a/src/lgr_advanced/tests/__init__.py b/src/lgr_advanced/tests/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/src/lgr_advanced/tests/test_label_forms_view.py b/src/lgr_advanced/tests/test_label_forms_view.py new file mode 100644 index 00000000..9ff7f07f --- /dev/null +++ b/src/lgr_advanced/tests/test_label_forms_view.py @@ -0,0 +1,12 @@ +from lgr_models.tests.test_unicode_version import TestUnicodeVersion + + +class TestLabelFormsView(TestUnicodeVersion): + + def test_unicode_versions(self): + self.login() + + self.a_unicode_version.activated = True + self.a_unicode_version.save() + response = self.client.get('/a/label_forms/') + self.assertEqual(response.context['option']['label'], self.a_unicode_version.version) diff --git a/src/lgr_advanced/views.py b/src/lgr_advanced/views.py index 161f33a3..816fc940 100644 --- a/src/lgr_advanced/views.py +++ b/src/lgr_advanced/views.py @@ -1,17 +1,17 @@ # -*- coding: utf-8 -*- -from django.conf import settings from django.contrib import messages from django.contrib.auth.mixins import LoginRequiredMixin from django.views.generic import TemplateView, FormView from lgr.utils import cp_to_ulabel, format_cp -from lgr_models.models.lgr import RzLgr -from lgr_utils import unidb from lgr_advanced.api import LGRToolStorage from lgr_advanced.forms import LabelFormsForm from lgr_advanced.lgr_exceptions import lgr_exception_to_text from lgr_advanced.models import LgrModel from lgr_advanced.utils import list_built_in_lgr +from lgr_models.models.lgr import RzLgr +from lgr_models.models.lgr import UnicodeVersion +from lgr_utils import unidb from lgr_web.views import Interfaces, INTERFACE_SESSION_MODE_KEY @@ -43,16 +43,16 @@ class LabelFormsView(LoginRequiredMixin, FormView): form_class = LabelFormsForm template_name = 'lgr_advanced/label_forms.html' + def get_initial(self): + initial = super().get_initial() + initial['unicode_version'] = UnicodeVersion.default() + return initial + def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.label = '' self.udata = None - def get_form_kwargs(self): - kwargs = super().get_form_kwargs() - kwargs['unicode_versions'] = ((v, v) for v in settings.SUPPORTED_UNICODE_VERSIONS) - return kwargs - def get_context_data(self, **kwargs): ctx = super().get_context_data(**kwargs) if self.label: @@ -67,7 +67,7 @@ def get_context_data(self, **kwargs): def form_valid(self, form): self.label = form.cleaned_data['label'] - unicode_version = form.cleaned_data['unicode_version'] - self.udata = unidb.manager.get_db_by_version(unicode_version) + unicode_version: UnicodeVersion = form.cleaned_data['unicode_version'] + self.udata = unidb.manager.get_db_by_version(unicode_version.version) return self.render_to_response(self.get_context_data(form=form)) diff --git a/src/lgr_idn_table_review/icann_tools/tasks.py b/src/lgr_idn_table_review/icann_tools/tasks.py index bbcbd2a1..ccbf2a4b 100644 --- a/src/lgr_idn_table_review/icann_tools/tasks.py +++ b/src/lgr_idn_table_review/icann_tools/tasks.py @@ -16,12 +16,12 @@ from django.template.loader import render_to_string from lgr.tools.idn_review.review import review_lgr -from lgr_advanced.lgr_editor.forms import DEFAULT_UNICODE_VERSION from lgr_auth.models import LgrUser from lgr_idn_table_review.icann_tools.api import (get_icann_idn_repository_tables, get_reference_lgr, NoRefLgrFound) from lgr_idn_table_review.icann_tools.models import IdnReviewIcannReport, IANAIdnTable +from lgr_models.models.lgr import UnicodeVersion from lgr_session.api import LGRStorage from lgr_utils import unidb @@ -86,7 +86,7 @@ def idn_table_review_task(absolute_url, email_address): :param email_address: The e-mail address where the results will be sent """ report_id = datetime.now().strftime('%Y-%m-%d-%H%M%S.%f') - udata = unidb.manager.get_db_by_version(DEFAULT_UNICODE_VERSION) + udata = unidb.manager.get_db_by_version(UnicodeVersion.default()) user = LgrUser.objects.get(email=email_address) lgr_storage = LGRStorage(user) diff --git a/src/lgr_manage/forms.py b/src/lgr_manage/forms.py index 85d19e09..00ecf8b1 100644 --- a/src/lgr_manage/forms.py +++ b/src/lgr_manage/forms.py @@ -1,11 +1,11 @@ # -*- coding: utf-8 -*- from dal import autocomplete from django import forms -from django.forms import FileField +from django.forms import FileField, CharField from django.utils.translation import ugettext_lazy as _ from lgr_advanced.lgr_editor.forms import FILE_FIELD_ENCODING_HELP -from lgr_models.models.lgr import RzLgr, RzLgrMember, RefLgr, MSR +from lgr_models.models.lgr import RzLgr, RzLgrMember, RefLgr, MSR, UnicodeVersion from lgr_web.utils import IANA_LANG_REGISTRY @@ -46,7 +46,6 @@ class Meta: class MSRCreateForm(forms.ModelForm): - class Meta: model = MSR fields = '__all__' @@ -55,3 +54,16 @@ class Meta: 'name': _('Name'), } + +class UnicodeVersionUpdateForm(forms.ModelForm): + version = CharField(disabled=True) + + class Meta: + model = UnicodeVersion + fields = '__all__' + + +class UnicodeVersionCreateForm(forms.ModelForm): + class Meta: + model = UnicodeVersion + fields = '__all__' diff --git a/src/lgr_manage/locale/ar/LC_MESSAGES/django.po b/src/lgr_manage/locale/ar/LC_MESSAGES/django.po index a719b99a..672f5b33 100644 --- a/src/lgr_manage/locale/ar/LC_MESSAGES/django.po +++ b/src/lgr_manage/locale/ar/LC_MESSAGES/django.po @@ -8,7 +8,7 @@ msgid "" msgstr "" "Project-Id-Version: PACKAGE VERSION\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2021-08-11 13:27+0000\n" +"POT-Creation-Date: 2021-08-30 15:00+0000\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n" @@ -44,10 +44,13 @@ msgstr "" #: src/lgr_manage/templates/lgr_idn_table_review_admin/_idn_table_review_admin_base.html:4 #: src/lgr_manage/templates/lgr_idn_table_review_admin/_idn_table_review_admin_base.html:12 +#: src/lgr_manage/templates/lgr_idn_table_review_admin/unicode_version_detail.html:4 +#: src/lgr_manage/templates/lgr_idn_table_review_admin/unicode_version_detail.html:12 msgid "IDN review tool admin" msgstr "" #: src/lgr_manage/templates/lgr_idn_table_review_admin/_idn_table_review_admin_base.html:7 +#: src/lgr_manage/templates/lgr_idn_table_review_admin/unicode_version_detail.html:7 msgid "Switch mode" msgstr "" @@ -63,6 +66,10 @@ msgstr "" msgid "User management" msgstr "" +#: src/lgr_manage/templates/lgr_idn_table_review_admin/_idn_table_review_admin_base.html:27 +msgid "Unicode Versions" +msgstr "" + #: src/lgr_manage/templates/lgr_idn_table_review_admin/ref_lgr.html:7 msgid "Existing Reference LGRs" msgstr "" @@ -113,6 +120,30 @@ msgstr "" msgid "New Root Zone LGR" msgstr "" +#: src/lgr_manage/templates/lgr_idn_table_review_admin/unicode_version_detail.html:16 +msgid "Edit Unicode Version" +msgstr "" + +#: src/lgr_manage/templates/lgr_idn_table_review_admin/unicode_version_detail.html:21 +msgid "Save" +msgstr "" + +#: src/lgr_manage/templates/lgr_idn_table_review_admin/unicode_versions.html:7 +msgid "Unicode Versions List" +msgstr "" + +#: src/lgr_manage/templates/lgr_idn_table_review_admin/unicode_versions.html:12 +msgid "Version" +msgstr "" + +#: src/lgr_manage/templates/lgr_idn_table_review_admin/unicode_versions.html:13 +msgid "Activated" +msgstr "" + +#: src/lgr_manage/templates/lgr_idn_table_review_admin/unicode_versions.html:40 +msgid "No unicode version defined." +msgstr "" + #: src/lgr_manage/templates/lgr_idn_table_review_admin/user_management.html:7 msgid "Admin user" msgstr "" diff --git a/src/lgr_manage/locale/fr/LC_MESSAGES/django.po b/src/lgr_manage/locale/fr/LC_MESSAGES/django.po index a6fbeeea..4d46ea8b 100644 --- a/src/lgr_manage/locale/fr/LC_MESSAGES/django.po +++ b/src/lgr_manage/locale/fr/LC_MESSAGES/django.po @@ -7,7 +7,7 @@ msgid "" msgstr "" "Project-Id-Version: \n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2021-08-11 13:27+0000\n" +"POT-Creation-Date: 2021-08-30 15:00+0000\n" "PO-Revision-Date: 2021-02-26 16:05-0500\n" "Last-Translator: \n" "Language-Team: \n" @@ -43,10 +43,13 @@ msgstr "Fichier pour la LGR de référence" #: src/lgr_manage/templates/lgr_idn_table_review_admin/_idn_table_review_admin_base.html:4 #: src/lgr_manage/templates/lgr_idn_table_review_admin/_idn_table_review_admin_base.html:12 +#: src/lgr_manage/templates/lgr_idn_table_review_admin/unicode_version_detail.html:4 +#: src/lgr_manage/templates/lgr_idn_table_review_admin/unicode_version_detail.html:12 msgid "IDN review tool admin" msgstr "Admin de l'outil de vérification IDN" #: src/lgr_manage/templates/lgr_idn_table_review_admin/_idn_table_review_admin_base.html:7 +#: src/lgr_manage/templates/lgr_idn_table_review_admin/unicode_version_detail.html:7 msgid "Switch mode" msgstr "Changer de mode" @@ -62,6 +65,10 @@ msgstr "LGRs de référence" msgid "User management" msgstr "Gestion des utilisateurs" +#: src/lgr_manage/templates/lgr_idn_table_review_admin/_idn_table_review_admin_base.html:27 +msgid "Unicode Versions" +msgstr "Versions d'Unicode" + #: src/lgr_manage/templates/lgr_idn_table_review_admin/ref_lgr.html:7 msgid "Existing Reference LGRs" msgstr "LGRs de référence existantes" @@ -112,6 +119,22 @@ msgstr "Aucun LGR pour la Root Zone." msgid "New Root Zone LGR" msgstr "Nouvelle LGR pour la Root Zone" +#: src/lgr_manage/templates/lgr_idn_table_review_admin/unicode_versions.html:7 +msgid "Unicode Versions List" +msgstr "Liste des versions d'Unicode" + +#: src/lgr_manage/templates/lgr_idn_table_review_admin/unicode_versions.html:12 +msgid "Version" +msgstr "" + +#: src/lgr_manage/templates/lgr_idn_table_review_admin/unicode_versions.html:13 +msgid "Activated" +msgstr "Activée" + +#: src/lgr_manage/templates/lgr_idn_table_review_admin/unicode_versions.html:40 +msgid "No unicode version defined." +msgstr "Pas de version d'unicode définie" + #: src/lgr_manage/templates/lgr_idn_table_review_admin/user_management.html:7 msgid "Admin user" msgstr "Utilisateur admin" @@ -175,7 +198,6 @@ msgstr "Échec à la création de l'utilisateur" #~ "Le(s) fichier(s) doivent être encodés en UTF-8 et utiliser le retour à la " #~ "ligne UNIX." -#, python-format #~ msgid "" #~ "\n" #~ " Display %(remain)s more files\n" diff --git a/src/lgr_manage/templates/lgr_manage/_idn_table_review_admin_base.html b/src/lgr_manage/templates/lgr_manage/_idn_table_review_admin_base.html index 32722c62..8b312bf1 100644 --- a/src/lgr_manage/templates/lgr_manage/_idn_table_review_admin_base.html +++ b/src/lgr_manage/templates/lgr_manage/_idn_table_review_admin_base.html @@ -26,11 +26,14 @@ +
- {% block content-pane %} - {% endblock content-pane %} + {% block content-pane %} + {% endblock content-pane %}
{% endblock content %} diff --git a/src/lgr_manage/templates/lgr_manage/unicode_versions.html b/src/lgr_manage/templates/lgr_manage/unicode_versions.html new file mode 100644 index 00000000..883037ff --- /dev/null +++ b/src/lgr_manage/templates/lgr_manage/unicode_versions.html @@ -0,0 +1,57 @@ +{% extends "lgr_manage/_idn_table_review_admin_base.html" %} +{% load i18n %} + +{% block unicode-versions-active %}active{% endblock %} + +{% block content-pane %} +

{% trans 'Unicode Versions List' %}

+ + + + + + + + + + {% for u in object_list %} + + + + + + {% empty %} + + + + {% endfor %} +
{% trans 'Version' %}{% trans 'Activated' %}
{{ u.version }} +
+ {% csrf_token %} + {% if u.activated %} + + + {% else %} + + + {% endif %} +
+
{% trans 'No unicode version defined.' %}
+ +

{% trans 'New Unicode Version' %}

+

{% trans 'Note: this new version must be available in the website internal configurations to be displayed above' %}

+
+ {% csrf_token %} + {% include "lgr_editor/_form_field.html" with field=form.version control_width=8 %} + {% include "lgr_editor/_form_field_checkbox.html" with field=form.activated control_width=8 %} + +
+{% endblock content-pane %} + +{% block html_body_more %} + {{ block.super }} +{% endblock html_body_more %} \ No newline at end of file diff --git a/src/lgr_manage/tests/__init__.py b/src/lgr_manage/tests/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/src/lgr_manage/tests/test_unicode_versions.py b/src/lgr_manage/tests/test_unicode_versions.py new file mode 100644 index 00000000..a7ba8070 --- /dev/null +++ b/src/lgr_manage/tests/test_unicode_versions.py @@ -0,0 +1,48 @@ +from lgr_models.models import UnicodeVersion +from lgr_models.tests.test_unicode_version import TestUnicodeVersion + + +class TestUnicodeVersions(TestUnicodeVersion): + + def test_manage_tabs(self): + self.login() + + response = self.client.get('/m/') + self.assertContains(response, 'Root Zone LGRs', status_code=200) + self.assertContains(response, 'Reference LGRs') + self.assertContains(response, 'User management') + self.assertContains(response, 'Unicode Versions') + + def test_unicode_tab(self): + self.login() + + response = self.client.get('/m/unicode-versions') + self.assertContains(response, f'{self.a_version_supported}', status_code=200) + # check that all unicode versions are unactivated by default, and that the hidden input is the reverse: True: + self.assertContains(response, """""") + + def test_unicode_edit(self): + self.login() + + self.assertEqual(self.a_unicode_version.activated, False) + self.client.post(f'/m/unicode-versions/{self.a_unicode_version.pk}/update', {'activated': True}) + new_unicode_version = UnicodeVersion.objects.get(version=self.a_version_supported) + self.assertEqual(new_unicode_version.activated, True) + self.assertListEqual([new_unicode_version], list(UnicodeVersion.get_activated())) + + def test_unicode_create(self): + self.login() + + initial_versions = list(UnicodeVersion.objects.all().values_list('version', flat=True)) + + new_version = '9.9.9-test' + self.assertIn('10.0.0', initial_versions) + self.assertNotIn(new_version, initial_versions) + + response = self.client.post('/m/unicode-versions/create', {'version': new_version}, follow=True) + self.assertEqual(response.status_code, 200) + new_versions = list(UnicodeVersion.objects.all().values_list('version', flat=True)) + self.assertIn(new_version, new_versions) + + new_version = UnicodeVersion.objects.get(version=new_version) + self.assertEqual(new_version.activated, False) diff --git a/src/lgr_manage/urls.py b/src/lgr_manage/urls.py index 501d9d3e..65731249 100644 --- a/src/lgr_manage/urls.py +++ b/src/lgr_manage/urls.py @@ -4,6 +4,8 @@ from lgr_manage.views.msr import MSRView, MSRDeleteView, DisplayMSRView from lgr_manage.views.reference_lgr import RefLgrView, RefLgrDeleteView, DisplayRefLgrView from lgr_manage.views.rz_lgr import RzLgrView, RzLgrDeleteView, DisplayRzLgrView, DisplayRzLgrMemberView +from lgr_manage.views.unicode_versions import LgrUnicodeVersionUpdateView, LgrUnicodeVersionsListView, \ + LgrUnicodeVersionCreateView from lgr_manage.views.users import LgrUserView, LgrUserDeleteView, LgrUserChangeStatusView, LgrUserAdminUpdateView urlpatterns = [ @@ -23,4 +25,9 @@ path('users//update', LgrUserAdminUpdateView.as_view(), name='lgr_admin_update_user'), path('users//delete', LgrUserDeleteView.as_view(), name='lgr_admin_delete_user'), path('users//status', LgrUserChangeStatusView.as_view(), name='lgr_admin_change_user_status'), + path('unicode-versions', LgrUnicodeVersionsListView.as_view(), name='lgr_idn_admin_unicode_versions'), + path('unicode-versions//update', LgrUnicodeVersionUpdateView.as_view(), + name='lgr_idn_admin_unicode_version_update'), + path('unicode-versions/create', LgrUnicodeVersionCreateView.as_view(), + name='create_unicode_version') ] diff --git a/src/lgr_manage/views/unicode_versions.py b/src/lgr_manage/views/unicode_versions.py new file mode 100644 index 00000000..ecf868e2 --- /dev/null +++ b/src/lgr_manage/views/unicode_versions.py @@ -0,0 +1,31 @@ +from django.urls import reverse_lazy +from django.views.generic import ListView, UpdateView, CreateView + +from lgr_manage.forms import UnicodeVersionUpdateForm, UnicodeVersionCreateForm +from lgr_manage.views.common import BaseAdminView +from lgr_models.models.lgr import UnicodeVersion + + +class LgrUnicodeVersionsListView(BaseAdminView, ListView): + template_name = 'lgr_manage/unicode_versions.html' + queryset = UnicodeVersion.all() + model = UnicodeVersion + + def get_context_data(self, **kwargs): + context = super().get_context_data(**kwargs) + context['form'] = UnicodeVersionCreateForm() + return context + + +class LgrUnicodeVersionUpdateView(BaseAdminView, UpdateView): + template_name = 'lgr_manage/unicode_versions.html' + form_class = UnicodeVersionUpdateForm + model = UnicodeVersion + success_url = reverse_lazy('lgr_idn_admin_unicode_versions') + + +class LgrUnicodeVersionCreateView(BaseAdminView, CreateView): + template_name = 'lgr_manage/unicode_versions.html' + form_class = UnicodeVersionCreateForm + model = UnicodeVersion + success_url = reverse_lazy('lgr_idn_admin_unicode_versions') diff --git a/src/lgr_models/migrations/0006_unicodeversion.py b/src/lgr_models/migrations/0006_unicodeversion.py new file mode 100644 index 00000000..47cd6ba6 --- /dev/null +++ b/src/lgr_models/migrations/0006_unicodeversion.py @@ -0,0 +1,22 @@ +# Generated by Django 3.1.7 on 2021-08-25 18:29 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('lgr_models', '0005_populate_msrs'), + ] + + operations = [ + migrations.CreateModel( + name='UnicodeVersion', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('version', models.CharField(max_length=255, unique=True)), + ('activated', models.BooleanField()), + ], + options={'ordering': ['version']} + ), + ] diff --git a/src/lgr_models/migrations/0007_populate_unicode_versions.py b/src/lgr_models/migrations/0007_populate_unicode_versions.py new file mode 100644 index 00000000..678175f4 --- /dev/null +++ b/src/lgr_models/migrations/0007_populate_unicode_versions.py @@ -0,0 +1,37 @@ +# Generated by Django 3.1.7 on 2021-08-23 16:43 +from django.conf import settings +from django.db import migrations + +from lgr_models.models.lgr import UnicodeVersion + + +def get_supported_unicode_versions(): + return settings.SUPPORTED_UNICODE_VERSIONS + + +def initial_data(apps, schema_editor): + old_unicode_version: UnicodeVersion = apps.get_model("lgr_models", "UnicodeVersion") + known_unicode_versions = [ + '6.0.0', + '6.1.0', + '6.2.0', + '6.3.0', + '7.0.0', + '8.0.0', + '9.0.0', + '10.0.0' + ] + + for supported_version in known_unicode_versions: + old_unicode_version.objects.using(schema_editor.connection.alias).create(version=supported_version, + activated=False) + + +class Migration(migrations.Migration): + dependencies = [ + ('lgr_models', '0006_unicodeversion'), + ] + + operations = [ + migrations.RunPython(initial_data) + ] diff --git a/src/lgr_models/models/lgr.py b/src/lgr_models/models/lgr.py index 52e7ddb2..eb47949b 100644 --- a/src/lgr_models/models/lgr.py +++ b/src/lgr_models/models/lgr.py @@ -7,6 +7,7 @@ from django.core.cache import cache from django.core.files import File from django.db import models +from django.db.models import QuerySet from django.urls import reverse from django.utils.encoding import force_bytes @@ -276,3 +277,30 @@ class MSR(LgrBaseModel): def display_url(self): # FIXME view should be in models app return reverse('lgr_admin_display_msr', kwargs={'lgr_pk': self.pk}) + + +class UnicodeVersion(models.Model): + version = models.CharField(max_length=255, unique=True) + activated = models.BooleanField() + + class Meta: + ordering = ['version'] + + def __str__(self): + return self.version + + @classmethod + def default(cls) -> 'UnicodeVersion': + if hasattr(settings, 'DEFAULT_UNICODE_VERSION'): + return cls.all().get(version=settings.DEFAULT_UNICODE_VERSION) + return cls.all().first() + + @classmethod + def all(cls) -> QuerySet['UnicodeVersion']: + supported_versions = set(settings.SUPPORTED_UNICODE_VERSIONS).intersection( + settings.UNICODE_DATABASES.keys()) + return UnicodeVersion.objects.filter(version__in=supported_versions) + + @classmethod + def get_activated(cls) -> QuerySet['UnicodeVersion']: + return cls.all().filter(activated=True) diff --git a/src/lgr_models/tests/test_unicode_version.py b/src/lgr_models/tests/test_unicode_version.py new file mode 100644 index 00000000..c2fa4793 --- /dev/null +++ b/src/lgr_models/tests/test_unicode_version.py @@ -0,0 +1,52 @@ +import unittest + +from django.test import override_settings + +from lgr_models.models import UnicodeVersion +from lgr_models.tests.lgr_webclient_test_base import LgrWebClientTestBase +from lgr_web import settings + + +class TestUnicodeVersion(LgrWebClientTestBase): + + def setUp(self): + self.versions_supported = set(settings.SUPPORTED_UNICODE_VERSIONS).intersection( + settings.UNICODE_DATABASES.keys()) + # this test require that at least one version of unicode is supported: + self.a_version_supported = set(self.versions_supported).pop() + self.a_unicode_version = UnicodeVersion.objects.get(version=self.a_version_supported) + + @unittest.skip('These tests doesnt work, since overriding settings in classmethod seems to fail in Django') + @override_settings(DEFAULT_UNICODE_VERSION='10.0.0', + SUPPORTED_UNICODE_VERSIONS=('6.3.0', '10.0.0'), + UNICODE_DATABASES={'6.3.0': {}, '10.0.0': {}}) + def test_default_version(self): + self.assertEqual(settings.DEFAULT_UNICODE_VERSION, '10.0.0') + default_version = UnicodeVersion.default() + self.assertEqual(default_version.version, '10.0.0') + + @unittest.skip('These tests doesnt work, since overriding settings in classmethod seems to fail in Django') + @override_settings(DEFAULT_UNICODE_VERSION='10.0.0', + SUPPORTED_UNICODE_VERSIONS=('6.3.0', '10.0.0'), + UNICODE_DATABASES={'6.3.0': {}, '10.0.0': {}}) + def test_no_default_version(self): + """ + Return the first element of the list when no default unicode version is specified + """ + del settings.DEFAULT_UNICODE_VERSION + default_version = UnicodeVersion.default() + self.assertEqual(default_version.version, '6.3.0') + + def test_unicode_version(self): + qs = UnicodeVersion.objects.all() + unicode_versions = set(qs.values_list('version', flat=True)) + self.assertSetEqual(unicode_versions, { + '6.3.0', + '6.0.0', + '6.1.0', + '6.2.0', + '7.0.0', + '8.0.0', + '9.0.0', + '10.0.0' + }) diff --git a/src/lgr_web/settings/default.py b/src/lgr_web/settings/default.py index b32b332c..9ead410d 100644 --- a/src/lgr_web/settings/default.py +++ b/src/lgr_web/settings/default.py @@ -314,6 +314,8 @@ }, } +# default unicode version in dropdowns across the application +DEFAULT_UNICODE_VERSION = '6.3.0' # Validator will display variants inline if there are no more than the specified number of variants # Otherwise, only a .csv download link is offered. diff --git a/src/lgr_web/views.py b/src/lgr_web/views.py index b0e07aba..4eb8c5bc 100644 --- a/src/lgr_web/views.py +++ b/src/lgr_web/views.py @@ -2,10 +2,10 @@ from enum import Enum, auto from dal import autocomplete -from django.conf import settings from django.shortcuts import redirect from django.views.generic import TemplateView +from lgr_models.models.lgr import UnicodeVersion from lgr_web.utils import IANA_LANG_REGISTRY INTERFACE_SESSION_MODE_KEY = 'mode' @@ -54,7 +54,7 @@ class LGRAboutView(TemplateView): def get_context_data(self, **kwargs): ctx = super().get_context_data(**kwargs) - ctx['output'] = {"versions": settings.SUPPORTED_UNICODE_VERSIONS} + ctx['output'] = {"versions": (v.version for v in UnicodeVersion.all())} return ctx