Skip to content

Commit

Permalink
#72 refactoring [unicode] use a single unicode version in lgr-django
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillaumeBlanchet authored and j-bernard committed Jun 28, 2022
1 parent d7a92be commit c0b3c81
Show file tree
Hide file tree
Showing 28 changed files with 44 additions and 292 deletions.
2 changes: 1 addition & 1 deletion Installation_Guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ A template for production configuration is provided:
Please configure the `ALLOWED_HOSTS` array with the hostname serving the LGR
application. For example, if your application is available at
`lgr-editor.example.com`, then the configuration should be `ALLOWED_HOSTS = ['lgr-editor.example.com']`.
Also configure `SUPPORTED_UNICODE_VERSIONS` as well as `UNICODE_DATABASES` according to your ICU installation.
Also configure `SUPPORTED_UNICODE_VERSION` as well as `UNICODE_DATABASES` according to your ICU installation.

The default database, used to store sessions, users and admin LGRs, is `sqlite`.
It is possible to configure the use of another server using the `DATABASES` dictionary.
Expand Down
10 changes: 2 additions & 8 deletions src/lgr_advanced/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,20 @@
from __future__ import unicode_literals

from django import forms
from django.conf import settings
from django.utils.translation import ugettext_lazy as _

from lgr.tools.utils import parse_label_input
from lgr_advanced.lgr_exceptions import lgr_exception_to_text
from lgr_models.models.unicode import UnicodeVersion
from lgr_utils import unidb


class LabelFormsForm(forms.Form):
label = forms.CharField(label=_("Label"))
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: UnicodeVersion = self.cleaned_data['unicode_version']
udata = unidb.manager.get_db_by_version(unicode_version.version)
udata = unidb.manager.get_db_by_version(settings.SUPPORTED_UNICODE_VERSION)
try:
value = parse_label_input(label, idna_decoder=udata.idna_decode_label)
except ValueError as e:
Expand Down
4 changes: 2 additions & 2 deletions src/lgr_advanced/lgr_editor/forms/importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
from __future__ import unicode_literals

from django import forms
from django.conf import settings
from django.core import validators
from django.forms.utils import ErrorList
from django.utils.translation import ugettext_lazy as _

from lgr_models.models.unicode import UnicodeVersion
from .fields import FILE_FIELD_ENCODING_HELP, ValidatingRepertoire


Expand Down Expand Up @@ -34,7 +34,7 @@ def __init__(self, data=None, files=None, auto_id='id_%s', prefix=None, initial=
use_required_attribute, renderer)
self.fields['unicode_version'] = forms.CharField(
widget=forms.HiddenInput(),
initial=(UnicodeVersion.default(), UnicodeVersion.default()))
initial=(settings.SUPPORTED_UNICODE_VERSION, settings.SUPPORTED_UNICODE_VERSION))


class ImportLGRForm(NewLGRForm):
Expand Down
4 changes: 1 addition & 3 deletions src/lgr_advanced/lgr_editor/forms/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@

from dal import autocomplete
from django import forms
from django.conf import settings
from django.forms.formsets import formset_factory
from django.utils.translation import ugettext_lazy as _

from lgr_models.models.unicode import UnicodeVersion
from lgr_web.utils import IANA_LANG_REGISTRY
from .fields import ValidatingRepertoire
from .utils import BaseDisableableFormSet
Expand Down Expand Up @@ -51,7 +51,6 @@ class MetadataForm(forms.Form):

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,7 +61,6 @@ 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.get_activated())

self.fields['validating_repertoire'].choices = self.fields['validating_repertoire'].choices + [
(_('Built-in'), ValidatingRepertoire.choices())
Expand Down

This file was deleted.

4 changes: 2 additions & 2 deletions src/lgr_advanced/lgr_editor/views/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"""
import logging

from django.conf import settings
from django.contrib import messages
from django.urls import reverse
from django.utils.translation import ugettext_lazy as _
Expand Down Expand Up @@ -32,6 +33,7 @@ def get_initial(self):
initial = super().get_initial()

metadata = self.lgr.metadata
metadata.set_unicode_version(settings.SUPPORTED_UNICODE_VERSION)
language = metadata.languages[0] if len(metadata.languages) > 0 else ""
scope = metadata.scopes[0] if len(metadata.scopes) > 0 else Scope('')

Expand Down Expand Up @@ -111,8 +113,6 @@ def form_valid(self, form):
metadata.scopes[0] = scope
else:
metadata.scopes.append(scope)
if cd['unicode_version']:
metadata.set_unicode_version(cd['unicode_version'])
if cd['validity_start']:
metadata.set_validity_start(cd['validity_start'].isoformat())
if cd['validity_end']:
Expand Down
Empty file removed src/lgr_advanced/tests/__init__.py
Empty file.
12 changes: 0 additions & 12 deletions src/lgr_advanced/tests/test_label_forms_view.py

This file was deleted.

6 changes: 2 additions & 4 deletions src/lgr_advanced/views.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# -*- 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
Expand All @@ -10,7 +11,6 @@
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.unicode import UnicodeVersion
from lgr_utils import unidb


Expand Down Expand Up @@ -50,7 +50,6 @@ class LabelFormsView(LoginRequiredMixin, FormView):

def get_initial(self):
initial = super().get_initial()
initial['unicode_version'] = UnicodeVersion.default()
return initial

def __init__(self, *args, **kwargs):
Expand All @@ -72,7 +71,6 @@ def get_context_data(self, **kwargs):

def form_valid(self, form):
self.label = form.cleaned_data['label']
unicode_version: UnicodeVersion = form.cleaned_data['unicode_version']
self.udata = unidb.manager.get_db_by_version(unicode_version.version)
self.udata = unidb.manager.get_db_by_version(settings.SUPPORTED_UNICODE_VERSION)

return self.render_to_response(self.get_context_data(form=form))
12 changes: 3 additions & 9 deletions src/lgr_basic/forms.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
# -*- coding: utf-8 -*-
from dal import autocomplete
from django import forms
from django.conf import settings
from django.core.exceptions import ValidationError
from django.utils.translation import ugettext_lazy as _

from lgr.tools.utils import parse_label_input

from lgr_advanced.lgr_editor.forms.fields import FILE_FIELD_ENCODING_HELP
from lgr_advanced.lgr_exceptions import lgr_exception_to_text
from lgr_models.exceptions import LGRUnsupportedUnicodeVersionException
from lgr_models.models.lgr import LgrBaseModel
from lgr_models.models.unicode import UnicodeVersion
from lgr_utils.unidb import get_db_by_version


Expand Down Expand Up @@ -64,12 +63,7 @@ def clean_lgr(self):

def clean_labels(self):
lgr_object = self.cleaned_data['lgr']
try:
udata = get_db_by_version(lgr_object.to_lgr().metadata.unicode_version)
except LGRUnsupportedUnicodeVersionException:
# fallback to default version
udata = get_db_by_version(UnicodeVersion.default().version)

udata = get_db_by_version(settings.SUPPORTED_UNICODE_VERSION)
value = self.cleaned_data['labels']
labels = list()
for label in set(value.split(';')):
Expand Down
3 changes: 1 addition & 2 deletions src/lgr_idn_table_review/icann_tools/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
get_reference_lgr,
NoRefLgrFound, LGRIcannStorage)
from lgr_idn_table_review.icann_tools.models import IANAIdnTable
from lgr_models.models.unicode import UnicodeVersion
from lgr_tasks.models import LgrTaskModel
from lgr_utils import unidb

Expand Down Expand Up @@ -100,7 +99,7 @@ def idn_table_review_task(user_pk, absolute_url):


def process_idn_tables(user, absolute_url, report_id):
udata = unidb.manager.get_db_by_version(UnicodeVersion.default().version)
udata = unidb.manager.get_db_by_version(settings.SUPPORTED_UNICODE_VERSION)
lgr_storage = LGRIcannStorage(user)
count = 0
processed = []
Expand Down
16 changes: 0 additions & 16 deletions src/lgr_manage/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

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.unicode import UnicodeVersion
from lgr_models.models.settings import LGRSettings
from lgr_web.utils import IANA_LANG_REGISTRY

Expand Down Expand Up @@ -64,21 +63,6 @@ class MSRIsActiveForm(forms.Form):
class RzLgrIsActiveForm(forms.Form):
active = forms.ModelChoiceField(label='', queryset=RzLgr.objects.all(), empty_label=None)


class UnicodeVersionUpdateForm(forms.ModelForm):
version = CharField(disabled=True)

class Meta:
model = UnicodeVersion
fields = '__all__'


class UnicodeVersionCreateForm(forms.ModelForm):
class Meta:
model = UnicodeVersion
fields = '__all__'


class LgrSettingsForm(forms.ModelForm):
class Meta:
model = LGRSettings
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@
<li role="presentation" class="{% block user-management-active %}{% endblock %}">
<a href="{% url 'lgr_admin_user_management' %}">{% trans "User management" %}</a>
</li>
{# <li role="presentation" class="{% block unicode-versions-active %}{% endblock %}">#}
{# <a href="{% url 'lgr_admin_unicode_versions' %}">{% trans "Unicode Versions" %}</a>#}
{# </li>#}
<li role="presentation" class="{% block settings-active %}{% endblock %}">
<a href="{% url 'lgr_admin_settings' %}">{% trans "Tool Setting" %}</a>
</li>
Expand Down
Empty file removed src/lgr_manage/tests/__init__.py
Empty file.
4 changes: 2 additions & 2 deletions src/lgr_manage/tests/test_settings.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from django.urls import reverse

from lgr_models.tests.test_unicode_version import TestUnicodeVersion
from lgr_models.tests.lgr_webclient_test_base import LgrWebClientTestBase
from lgr_web.config import lgr_settings


class TestUnicodeVersions(TestUnicodeVersion):
class TestSettings(LgrWebClientTestBase):

def test_settings_user_unauthorized(self):
self.login_user()
Expand Down
44 changes: 0 additions & 44 deletions src/lgr_manage/tests/test_unicode_versions.py
Original file line number Diff line number Diff line change
@@ -1,44 +0,0 @@
import unittest

from lgr_models.models.unicode import UnicodeVersion
from lgr_models.tests.test_unicode_version import TestUnicodeVersion


class TestUnicodeVersions(TestUnicodeVersion):

@unittest.skip('Disabled for now')
def test_unicode_tab(self):
self.login_admin()

response = self.client.get('/m/unicode-versions')
self.assertContains(response, f'<td>{self.a_version_supported}</td>', status_code=200)
# check that all unicode versions are unactivated by default, and that the hidden input is the reverse: True:
self.assertContains(response, '<input type="hidden" name="activated" value="True">')

@unittest.skip('Disabled for now')
def test_unicode_edit(self):
self.login_admin()

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()))

@unittest.skip('Disabled for now')
def test_unicode_create(self):
self.login_admin()

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)
4 changes: 0 additions & 4 deletions src/lgr_manage/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,6 @@
path('users/<int:user_pk>/update', LgrUserAdminUpdateView.as_view(), name='lgr_admin_update_user'),
path('users/<int:user_pk>/delete', LgrUserDeleteView.as_view(), name='lgr_admin_delete_user'),
path('users/<int:user_pk>/status', LgrUserChangeStatusView.as_view(), name='lgr_admin_change_user_status'),
# path('unicode-versions', LgrUnicodeVersionsListView.as_view(), name='lgr_admin_unicode_versions'),
# path('unicode-versions/<int:pk>/update', LgrUnicodeVersionUpdateView.as_view(),
# name='lgr_admin_unicode_version_update'),
# path('unicode-versions/create', LgrUnicodeVersionCreateView.as_view(), name='lgr_admin_create_unicode_version'),
path('settings', LgrSettingsView.as_view(), name='lgr_admin_settings'),
path('settings/indexes', LgrCalculateIndexes.as_view(), name='lgr_admin_calculate_indexes')
]
36 changes: 0 additions & 36 deletions src/lgr_manage/views/unicode_versions.py
Original file line number Diff line number Diff line change
@@ -1,36 +0,0 @@
from django.db.models import Value, IntegerField
from django.db.models.functions import Replace, Cast
from django.urls import reverse_lazy
from django.views.generic import UpdateView, CreateView

from lgr_manage.forms import UnicodeVersionUpdateForm, UnicodeVersionCreateForm
from lgr_manage.views.common import BaseAdminMixin, BaseListAdminView
from lgr_models.models.unicode import UnicodeVersion


class LgrUnicodeVersionsListView(BaseListAdminView):
template_name = 'lgr_manage/unicode_versions.html'
# 10.0.0, 14.0.0, 9.0.0 -> 1400, 1000, 900 (to properly sort versions)
queryset = UnicodeVersion.all().order_by(
Cast(Replace('version', Value('.'), Value('')), output_field=IntegerField())
).reverse()
model = UnicodeVersion

def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['form'] = UnicodeVersionCreateForm()
return context


class LgrUnicodeVersionUpdateView(BaseAdminMixin, UpdateView):
template_name = 'lgr_manage/unicode_versions.html'
form_class = UnicodeVersionUpdateForm
model = UnicodeVersion
success_url = reverse_lazy('lgr_admin_unicode_versions')


class LgrUnicodeVersionCreateView(BaseAdminMixin, CreateView):
template_name = 'lgr_manage/unicode_versions.html'
form_class = UnicodeVersionCreateForm
model = UnicodeVersion
success_url = reverse_lazy('lgr_admin_unicode_versions')
13 changes: 0 additions & 13 deletions src/lgr_models/migrations/0007_populate_unicode_versions.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,7 @@
# 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.unicode 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',
Expand All @@ -26,10 +17,6 @@ def initial_data(apps, schema_editor):
'14.0.0'
]

for supported_version in known_unicode_versions:
old_unicode_version.objects.using(schema_editor.connection.alias).create(version=supported_version)


class Migration(migrations.Migration):
dependencies = [
('lgr_models', '0006_unicodeversion'),
Expand Down
Loading

0 comments on commit c0b3c81

Please sign in to comment.