Skip to content

Commit

Permalink
Modify dropdown validate against to include rz-lgr, RefLgr and members.
Browse files Browse the repository at this point in the history
Add a component to the utils to fetch the latest RefLgr list.
  • Loading branch information
codzy-ds authored and j-bernard committed Jun 28, 2022
1 parent e820e60 commit 4843418
Show file tree
Hide file tree
Showing 15 changed files with 107 additions and 49 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ pip-delete-this-directory.txt

# IDE
.idea
*.iml

# Documentation
doc/_build/
Expand All @@ -46,4 +47,4 @@ src/lgr_web/static/
helm/secrets

# SQLite dev db
src/lgr_web/dev.db
src/lgr_web/dev.db
2 changes: 1 addition & 1 deletion src/lgr_advanced/lgr_editor/forms/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class ValidatingRepertoire:

@classmethod
def list(cls) -> List[LgrBaseModel]:
return sorted(list(chain(RzLgr.objects.all(), MSR.objects.all())), key=lambda l: l.name)
return sorted(list(chain(RzLgr.objects.filter(active=True), MSR.objects.filter(active=True))), key=lambda l: l.name)

@classmethod
def choices(cls):
Expand Down
2 changes: 1 addition & 1 deletion src/lgr_advanced/lgr_tools/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ def __init__(self, *args, **kwargs):
for lgr_obj in LgrModel.objects.filter(owner=user,
set_info__isnull=True))
self.fields['rz_lgr'].choices = [('', ''), ] + list((lgr_obj.pk, lgr_obj.name)
for lgr_obj in RzLgr.objects.all())
for lgr_obj in RzLgr.objects.filter(active=True))


class LGRComputeVariantsSelector(forms.Form):
Expand Down
22 changes: 18 additions & 4 deletions src/lgr_basic/forms.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# -*- coding: utf-8 -*-
from dal import autocomplete
from django import forms
from django.core.exceptions import ValidationError
from django.utils.translation import ugettext_lazy as _
Expand All @@ -8,17 +9,27 @@
from lgr_advanced.lgr_exceptions import lgr_exception_to_text
from lgr_models.exceptions import LGRUnsupportedUnicodeVersionException
from lgr_models.models.unicode import UnicodeVersion
from lgr_utils.components import RefLgrAutocompleteField
from lgr_utils.unidb import get_db_by_version
from lgr_models.models.lgr import RzLgr
from lgr_models.models.lgr import LgrBaseModel
from lgr_utils.views import RefLgrAutocomplete


class ValidateLabelSimpleForm(forms.Form):
rz_lgr = forms.ModelChoiceField(label='', queryset=RzLgr.objects.all(), empty_label=None)
# The order of fields is important, it is tied to the order of the "clean_data" execution. Since clean_label uses
# lgr, clean_lgr needs to execute before.
lgr = RefLgrAutocompleteField(label='',
required=True,
choices=RefLgrAutocomplete.get_list(),
widget=autocomplete.ListSelect2(
url='ref-lgr-autocomplete'))

labels = forms.CharField(label='', required=False,
widget=forms.TextInput(attrs={'name': '',
'class': 'form-label form-control',
'onkeyup': 'buttonValidateEnabled()',
'placeholder': _('Label')}))

labels_file = forms.FileField(label='', help_text=FILE_FIELD_ENCODING_HELP,
required=False)
collisions = forms.BooleanField(label='', widget=forms.CheckboxInput(attrs={'id': 'check-for-collision'}),
Expand All @@ -37,10 +48,13 @@ def clean(self):

return cleaned_data

def clean_lgr(self):
return LgrBaseModel.from_tuple(self.cleaned_data['lgr'])

def clean_labels(self):
rz_lgr_object: RzLgr = self.cleaned_data['rz_lgr']
lgr_object = self.cleaned_data['lgr']
try:
udata = get_db_by_version(rz_lgr_object.to_lgr().metadata.unicode_version)
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)
Expand Down
12 changes: 7 additions & 5 deletions src/lgr_basic/templates/lgr_basic/basic_mode.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
<div class="col-lg-8 col-lg-offset-2 validate-label-simple-form">
<h1>{% trans "Label Validation Tool" %}</h1>
<hr class="header-sep"/>
{{ form.media }}
<form class="form-horizontal validate-label" method="POST" enctype="multipart/form-data">
{% csrf_token %}

Expand All @@ -46,7 +47,7 @@ <h1>{% trans "Label Validation Tool" %}</h1>
{% trans 'Validate label(s) against' %}
</label>
<div class="col-sm-8 col-lg-8">
{% include "lgr_editor/_form_field.html" with field=form.rz_lgr %}
{% include "lgr_editor/_form_field.html" with field=form.lgr %}
<span class="text-secondary">
{% trans 'View LGR as' %}
<a href="" id="xmlLink" target="_blank" rel="noopener noreferrer">XML</a> |
Expand Down Expand Up @@ -209,12 +210,12 @@ <h1>{% trans "Label Validation Tool" %}</h1>
<script>
var isFormUploadActivated = false;

$("#id_rz_lgr").change(() => {
$("#id_lgr").change(() => {
changeRzValue();
});

function changeRzValue() {
const val = $('#id_rz_lgr').val();
const val = $('#id_lgr').val();

let renderLink = `{% url 'lgr_render' model=model lgr_pk=0 %}`;
renderLink = renderLink.replace('0', val);
Expand Down Expand Up @@ -327,8 +328,8 @@ <h1>{% trans "Label Validation Tool" %}</h1>
data.forEach((el) => {
if (el.name === 'labels' && !isFormUploadActivated) {
labelString += el.value + ';';
} else if (el.name === 'rz_lgr') {
sendData.push({name: 'rz_lgr', value: el.value})
} else if (el.name === 'lgr') {
sendData.push({name: 'lgr', value: el.value})
}
});

Expand Down Expand Up @@ -364,6 +365,7 @@ <h1>{% trans "Label Validation Tool" %}</h1>
$('#timeout-error').css('display', 'none');
$('#general-error').css('display', 'none');
$('#process-error').css('display', 'none');
console.log(sendData)

$.ajax({
type: 'POST',
Expand Down
19 changes: 14 additions & 5 deletions src/lgr_basic/tests/test_validate_labels_against_dropdown.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,34 @@
from lgr_basic.forms import ValidateLabelSimpleForm
from lgr_models.models.lgr import RzLgr
from lgr_models.tests.lgr_webclient_test_base import LgrWebClientTestBase


class TestValidateLabelsAgainstDropDown(LgrWebClientTestBase):
test_label = 'مكتب' # arab character valid since the beginning (rz lgr version 1)
dropdown_label = 'RZ-LGR 4'
dropdown_lgr_keys = LgrWebClientTestBase.default_root_zones + ['Ref. LGR']

def test_validate_labels_against_full(self):
self.login_admin()

# given
response = self.client.get('/b/')
dropdown = response.context['widget']
self.assertEquals('rz_lgr', dropdown['name'])
dropdown_dict = {v[1][0]['label']: v[1][0]['value'] for v in dropdown['optgroups']}
self.assertListEqual(list(dropdown_dict.keys()), self.default_root_zones)
form = response.context['form']
values = form.fields['rz_lgr'].choices
dropdown_dict = {v[0][0]:v[1][0][0] for v in values}

# when
response = self.client.post('/b/',
{'rz_lgr': dropdown_dict[self.dropdown_label], 'collisions': False,
'labels': self.test_label})

# then
self.assertEquals('rz_lgr', dropdown['name'])
self.assertListEqual(list(dropdown_dict.keys()), self.dropdown_lgr_keys)
self.assertNotContains(response, 'INVALID', status_code=200)

def test_validate_labels_against(self):
dropdown = ValidateLabelSimpleForm()
values = [v[1] for v in dropdown.fields['rz_lgr'].choices]
self.assertListEqual(values, self.default_root_zones)
values = [v[0][0] for v in dropdown.fields['rz_lgr'].choices]
self.assertListEqual(values, self.dropdown_lgr_keys)
2 changes: 1 addition & 1 deletion src/lgr_basic/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
from lgr_basic.views import BasicModeView

urlpatterns = [
path('', BasicModeView.as_view(), name='lgr_basic_mode')
path('', BasicModeView.as_view(), name='lgr_basic_mode'),
]
21 changes: 10 additions & 11 deletions src/lgr_basic/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
from django.conf import settings
from django.contrib import messages
from django.contrib.auth.mixins import LoginRequiredMixin
from django.shortcuts import redirect
from django.urls import reverse
from django.utils.safestring import mark_safe
from django.utils.translation import ugettext as _
Expand All @@ -19,7 +18,7 @@
from lgr_advanced.lgr_exceptions import lgr_exception_to_text
from lgr_advanced.lgr_tools.tasks import annotate_task, basic_collision_task
from lgr_advanced.lgr_validator.views import NeedAsyncProcess, evaluate_label_from_view
from lgr_models.models.lgr import RzLgr
from lgr_models.models.lgr import RzLgr, LgrBaseModel
from lgr_tasks.models import LgrTaskModel
from .forms import ValidateLabelSimpleForm

Expand All @@ -38,8 +37,8 @@ def form_valid(self, form):

labels_cp = form.cleaned_data['labels']
labels_file = form.cleaned_data.get('labels_file')
rz_lgr: RzLgr = form.cleaned_data['rz_lgr']
ctx['lgr_object'] = rz_lgr # needed to download results as csv
lgr: LgrBaseModel = form.cleaned_data['lgr']
ctx['lgr_object'] = lgr # needed to download results as csv
collisions = form.cleaned_data['collisions']

tlds = None
Expand All @@ -50,18 +49,18 @@ def form_valid(self, form):
if labels_file:
labels_json = LabelInfo.from_form(labels_file.name, labels_file.read()).to_dict()
ctx['validation_task'] = True
task_name = _('Annotate labels on LGR %s') % rz_lgr.pk
task_name = _('Annotate labels on LGR %s') % lgr.pk
if collisions:
task_name = _('Compute collisions and annotate labels on LGR %s') % rz_lgr.pk
task_name = _('Compute collisions and annotate labels on LGR %s') % lgr.pk
task = LgrTaskModel.objects.create(app=self.request.resolver_match.app_name,
name=task_name,
user=self.request.user)
if collisions:
basic_collision_task.apply_async((self.request.user.pk, rz_lgr.pk, labels_json, tld_json,
basic_collision_task.apply_async((self.request.user.pk, lgr.pk, labels_json, tld_json,
True, RzLgr._meta.label), task_id=task.pk)
ctx['collision_task'] = True
else:
annotate_task.apply_async((self.request.user.pk, rz_lgr.pk, labels_json, RzLgr._meta.label),
annotate_task.apply_async((self.request.user.pk, lgr.pk, labels_json, RzLgr._meta.label),
task_id=task.pk)

else:
Expand All @@ -74,16 +73,16 @@ def form_valid(self, form):
check_collisions = [l[0] for l in read_labels(StringIO(data))]
else:
task = LgrTaskModel.objects.create(app=self.request.resolver_match.app_name,
name=_('Compute collisions on LGR %s') % rz_lgr.pk,
name=_('Compute collisions on LGR %s') % lgr.pk,
user=self.request.user)
basic_collision_task.apply_async((self.request.user.pk, rz_lgr.pk, labels_json, tld_json,
basic_collision_task.apply_async((self.request.user.pk, lgr.pk, labels_json, tld_json,
False, RzLgr._meta.label), task_id=task.pk)
ctx['collision_task'] = True

for label_cplist in labels_cp:
try:
result = evaluate_label_from_view(self.request.user,
rz_lgr,
lgr,
label_cplist,
check_collisions=check_collisions)
if check_collisions:
Expand Down
2 changes: 1 addition & 1 deletion src/lgr_idn_table_review/idn_tool/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def clean_idn_tables(self):
class IdnTableReviewSelectReferenceForm(forms.Form):

def __init__(self, *args, **kwargs):
from lgr_idn_table_review.idn_tool.views import RefLgrAutocomplete
from lgr_utils.views import RefLgrAutocomplete

idn_tables = kwargs.pop('idn_tables', [])
super().__init__(*args, **kwargs)
Expand Down
4 changes: 1 addition & 3 deletions src/lgr_idn_table_review/idn_tool/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
IdnTableReviewSelectReferenceView,
IdnTableReviewListReports,
IdnTableReviewListReport,
IdnTableReviewDisplayIdnTable, RefLgrAutocomplete,
IdnTableReviewDisplayIdnTable,
IdnTableReviewDeleteReport)
from lgr_web.converters import FileNameConverter

Expand All @@ -20,6 +20,4 @@
path('report/<filename:report_id>', IdnTableReviewListReport.as_view(), name='lgr_review_report'),
path('report/<filename:report_id>/rm', IdnTableReviewDeleteReport.as_view(), name='lgr_review_delete_report'),
path('view/<int:lgr_pk>', IdnTableReviewDisplayIdnTable.as_view(), name='lgr_review_display_idn_table'),
# autocompletion
path('ref-lgr-autocomplete/', RefLgrAutocomplete.as_view(), name='ref-lgr-autocomplete'),
]
16 changes: 0 additions & 16 deletions src/lgr_idn_table_review/idn_tool/views.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# -*- coding: utf-8 -*-
import logging

from dal_select2.views import Select2GroupListView
from django.contrib.auth.mixins import LoginRequiredMixin
from django.core.exceptions import SuspiciousOperation
from django.http import HttpResponse
Expand All @@ -16,7 +15,6 @@
from lgr_idn_table_review.idn_tool.api import LGRIdnReviewApi
from lgr_idn_table_review.idn_tool.forms import LGRIdnTableReviewForm, IdnTableReviewSelectReferenceForm
from lgr_idn_table_review.idn_tool.tasks import idn_table_review_task
from lgr_models.models.lgr import RzLgr, RefLgr, RzLgrMember
from lgr_tasks.models import LgrTaskModel

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -144,17 +142,3 @@ class IdnTableReviewDeleteReport(IdnTableReviewViewMixin, View):
def post(self, request, *args, **kwargs):
self.api.delete_report(self.kwargs.get('report_id'))
return redirect(request.GET.get('next', '/'))


class RefLgrAutocomplete(LoginRequiredMixin, Select2GroupListView):

@staticmethod
def get_list():
lgr_choices = []
for rz in RzLgr.objects.all():
rz_member_choices = ((str(rz.to_tuple()), rz.name),) + tuple(
(str(rz_member.to_tuple()), rz_member.name) for rz_member in RzLgrMember.objects.filter(rz_lgr=rz))
lgr_choices += [((rz.name, rz.name), rz_member_choices)]
lgr_choices += [(('Ref. LGR', 'Ref. LGR'), tuple(
((str(ref_lgr.to_tuple()), ref_lgr.name) for ref_lgr in RefLgr.objects.all())))]
return lgr_choices
20 changes: 20 additions & 0 deletions src/lgr_utils/components.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from django.core.exceptions import ValidationError

from lgr_utils.views import RefLgrAutocomplete
from django import forms


class RefLgrAutocompleteField(forms.ChoiceField):
def validate(self, value):
found = False
for lgr_types in RefLgrAutocomplete.get_list():
for lgr in lgr_types[1]:
if value == lgr[0]:
found = True
break;
if not found:
raise ValidationError(
self.error_messages['invalid_choice'],
code='invalid_choice',
params={'value': value},
)
11 changes: 11 additions & 0 deletions src/lgr_utils/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.urls import path

from lgr_utils.views import RefLgrAutocomplete

urlpatterns = [
# autocompletion
path('ref-lgr-autocomplete/', RefLgrAutocomplete.as_view(), name='ref-lgr-autocomplete'),
]
18 changes: 18 additions & 0 deletions src/lgr_utils/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from dal_select2.views import Select2GroupListView
from django.contrib.auth.mixins import LoginRequiredMixin

from lgr_models.models.lgr import RzLgr, RzLgrMember, RefLgr


class RefLgrAutocomplete(LoginRequiredMixin, Select2GroupListView):

@staticmethod
def get_list():
lgr_choices = []
for rz in RzLgr.objects.filter(active=True):
rz_member_choices = ((str(rz.to_tuple()), rz.name),) + tuple(
(str(rz_member.to_tuple()), rz_member.name) for rz_member in RzLgrMember.objects.filter(rz_lgr=rz))
lgr_choices += [((rz.name, rz.name), rz_member_choices)]
lgr_choices += [(('Ref. LGR', 'Ref. LGR'), tuple(
((str(ref_lgr.to_tuple()), ref_lgr.name) for ref_lgr in RefLgr.objects.all())))]
return lgr_choices
2 changes: 2 additions & 0 deletions src/lgr_web/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import lgr_renderer.urls
import lgr_session.urls
import lgr_tasks.urls
import lgr_utils.urls
from .views import LGRModesView, LGRAboutView, LanguageAutocomplete

urlpatterns = [
Expand All @@ -29,6 +30,7 @@
path('storage/', include(lgr_session.urls.urlpatterns)),
path('render/', include(lgr_renderer.urls.urlpatterns)),
path('tasks/', include(lgr_tasks.urls.urlpatterns)),
path('u/', include(lgr_utils.urls.urlpatterns)),

# autocompletion
path('language-autocomplete/', LanguageAutocomplete.as_view(), name='language-autocomplete'),
Expand Down

0 comments on commit 4843418

Please sign in to comment.