Adaptation du formulaire

This commit is contained in:
Paul Marillonnet 2017-02-24 14:56:35 +01:00
parent e5101a8513
commit 012c8d58fe
1 changed files with 32 additions and 32 deletions

View File

@ -16,9 +16,9 @@
from django import forms
from django.core.exceptions import ValidationError
from django.utils.translation import ugettext_lazy as _
#from django.utils.translation import ugettext_lazy as _
from .models import Query, get_code, identifier_re
from .models import Query#, get_code, identifier_re
class QueryForm(forms.ModelForm):
@ -33,36 +33,36 @@ class QueryForm(forms.ModelForm):
}
fields = '__all__'
def clean_lines_of_expressions(self, lines, named=False):
if not lines:
return lines
errors = []
for i, line in enumerate(lines.splitlines()):
if named:
line = line.split(':', 1)
if len(line) != 2:
errors.append(ValidationError(
_('Syntax error line %d: each line must be prefixed '
'with an identifier followed by a colon.') % (i + 1)))
continue
name, line = line
if not identifier_re.match(name):
errors.append(
ValidationError(_('Syntax error line %d: invalid identifier, '
'it must starts with a letter and only '
'contains letters, digits and _.') % (i + 1)))
continue
try:
get_code(line)
except SyntaxError as e:
errors.append(ValidationError(
_('Syntax error line %(line)d at character %(character)d') % {
'line': i + 1,
'character': e.offset
}))
if errors:
raise ValidationError(errors)
return lines
#def clean_lines_of_expressions(self, lines, named=False):
# if not lines:
# return lines
# errors = []
# for i, line in enumerate(lines.splitlines()):
# if named:
# line = line.split(':', 1)
# if len(line) != 2:
# errors.append(ValidationError(
# 'Syntax error line %d: each line must be prefixed '
# 'with an identifier followed by a colon.' % (i + 1)))
# continue
# name, line = line
# if not identifier_re.match(name):
# errors.append(
# ValidationError('Syntax error line %d: invalid identifier, '
# 'it must starts with a letter and only '
# 'contains letters, digits and _.' % (i + 1)))
# continue
# try:
# get_code(line)
# except SyntaxError as e:
# errors.append(ValidationError(
# 'Syntax error line %(line)d at character %(character)d' % {
# 'line': i + 1,
# 'character': e.offset
# }))
# if errors:
# raise ValidationError(errors)
# return lines
def clean_filters(self):
return self.clean_lines_of_expressions(self.data.get('filters'))