Skip to content

Commit

Permalink
add manage unicode interface and fetch it from database instead of se…
Browse files Browse the repository at this point in the history
…ttings
  • Loading branch information
GuillaumeBlanchet authored and j-bernard committed Jun 28, 2022
1 parent 4b58cfe commit 45e2d28
Show file tree
Hide file tree
Showing 27 changed files with 446 additions and 48 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,6 @@ src/lgr_web/static/

# Helm secret directory
helm/secrets

# SQLite dev db
src/lgr_web/dev.db
11 changes: 11 additions & 0 deletions manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
21 changes: 9 additions & 12 deletions src/lgr_advanced/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
6 changes: 1 addition & 5 deletions src/lgr_advanced/lgr_editor/forms/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)
Expand Down Expand Up @@ -75,4 +71,4 @@ def scripts(cls, unicode_database):

scripts[(vr_model, vr_pk)] = vr_scripts

return scripts
return scripts
17 changes: 13 additions & 4 deletions src/lgr_advanced/lgr_editor/forms/importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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):
Expand Down
11 changes: 7 additions & 4 deletions src/lgr_advanced/lgr_editor/forms/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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=(
('', ''),
Expand All @@ -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())
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<label class="col-sm-{{ label_width|default:2 }} control-label" for="id_{{ field.name }}">{{ field.label }}</label>
<div class="col col-sm-{{ control_width|default:10 }}">
{{ 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' %}
<span onClick="cleanInput($(this))" class="search-clear glyphicon glyphicon-remove-circle"></span>
{% endif %}
{% if field.errors %}
Expand Down
Original file line number Diff line number Diff line change
@@ -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})
Empty file.
12 changes: 12 additions & 0 deletions src/lgr_advanced/tests/test_label_forms_view.py
Original file line number Diff line number Diff line change
@@ -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)
20 changes: 10 additions & 10 deletions src/lgr_advanced/views.py
Original file line number Diff line number Diff line change
@@ -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


Expand Down Expand Up @@ -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:
Expand All @@ -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))
4 changes: 2 additions & 2 deletions src/lgr_idn_table_review/icann_tools/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down
18 changes: 15 additions & 3 deletions src/lgr_manage/forms.py
Original file line number Diff line number Diff line change
@@ -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


Expand Down Expand Up @@ -46,7 +46,6 @@ class Meta:


class MSRCreateForm(forms.ModelForm):

class Meta:
model = MSR
fields = '__all__'
Expand 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__'
33 changes: 32 additions & 1 deletion src/lgr_manage/locale/ar/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -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 <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <[email protected]>\n"
Expand Down Expand Up @@ -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 ""

Expand All @@ -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 ""
Expand Down Expand Up @@ -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 ""
Expand Down
Loading

0 comments on commit 45e2d28

Please sign in to comment.