Merge branch 'master' into feature/merge_master
This commit is contained in:
@@ -79,10 +79,14 @@ class MetadataForm(forms.Form):
|
||||
attrs={'readonly': 'readonly'}
|
||||
)
|
||||
|
||||
def clean_value(self):
|
||||
return self.metadata_type.validate_value(
|
||||
document_type=self.document_type, value=self.cleaned_data['value']
|
||||
)
|
||||
def clean(self):
|
||||
if self.cleaned_data.get('update') and hasattr(self, 'metadata_type'):
|
||||
self.cleaned_data['value'] = self.metadata_type.validate_value(
|
||||
document_type=self.document_type,
|
||||
value=self.cleaned_data.get('value')
|
||||
)
|
||||
|
||||
return self.cleaned_data
|
||||
|
||||
|
||||
MetadataFormSet = formset_factory(MetadataForm, extra=0)
|
||||
|
||||
@@ -126,7 +126,7 @@ class MetadataType(models.Model):
|
||||
|
||||
if self.lookup:
|
||||
lookup_options = self.get_lookup_values()
|
||||
if value not in lookup_options:
|
||||
if value and value not in lookup_options:
|
||||
raise ValidationError(
|
||||
_('Value is not one of the provided options.')
|
||||
)
|
||||
|
||||
@@ -63,8 +63,9 @@ class MetadataTestCase(TestCase):
|
||||
self.document.metadata_value_of.test, TEST_DEFAULT_VALUE
|
||||
)
|
||||
|
||||
def test_lookup(self):
|
||||
def test_lookup_with_incorrect_value(self):
|
||||
self.metadata_type.lookup = TEST_LOOKUP_TEMPLATE
|
||||
self.metadata_type.save()
|
||||
|
||||
document_metadata = DocumentMetadata(
|
||||
document=self.document, metadata_type=self.metadata_type,
|
||||
@@ -76,8 +77,15 @@ class MetadataTestCase(TestCase):
|
||||
document_metadata.full_clean()
|
||||
document_metadata.save()
|
||||
|
||||
# Should not return error
|
||||
document_metadata.value = TEST_CORRECT_LOOKUP_VALUE
|
||||
def test_lookup_with_correct_value(self):
|
||||
self.metadata_type.lookup = TEST_LOOKUP_TEMPLATE
|
||||
self.metadata_type.save()
|
||||
|
||||
document_metadata = DocumentMetadata(
|
||||
document=self.document, metadata_type=self.metadata_type,
|
||||
value=TEST_CORRECT_LOOKUP_VALUE
|
||||
)
|
||||
|
||||
document_metadata.full_clean()
|
||||
document_metadata.save()
|
||||
|
||||
@@ -85,6 +93,22 @@ class MetadataTestCase(TestCase):
|
||||
self.document.metadata_value_of.test, TEST_CORRECT_LOOKUP_VALUE
|
||||
)
|
||||
|
||||
def test_empty_optional_lookup(self):
|
||||
"""
|
||||
Checks for GitLab issue #250
|
||||
Empty optional lookup metadata trigger validation error
|
||||
"""
|
||||
|
||||
self.metadata_type.lookup = TEST_LOOKUP_TEMPLATE
|
||||
self.metadata_type.save()
|
||||
|
||||
document_metadata = DocumentMetadata(
|
||||
document=self.document, metadata_type=self.metadata_type
|
||||
)
|
||||
|
||||
document_metadata.full_clean()
|
||||
document_metadata.save()
|
||||
|
||||
def test_validation(self):
|
||||
self.metadata_type.validation = TEST_DATE_VALIDATOR
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@ from __future__ import absolute_import, unicode_literals
|
||||
|
||||
from django.conf import settings
|
||||
from django.contrib import messages
|
||||
from django.core.exceptions import PermissionDenied
|
||||
from django.core.exceptions import PermissionDenied, ValidationError
|
||||
from django.core.urlresolvers import reverse, reverse_lazy
|
||||
from django.http import HttpResponseRedirect
|
||||
from django.shortcuts import get_object_or_404, render_to_response
|
||||
@@ -125,20 +125,24 @@ def metadata_edit(request, document_id=None, document_id_list=None):
|
||||
except Exception as exception:
|
||||
errors.append(exception)
|
||||
|
||||
if errors:
|
||||
for error in errors:
|
||||
if settings.DEBUG:
|
||||
raise
|
||||
for error in errors:
|
||||
if settings.DEBUG:
|
||||
raise
|
||||
else:
|
||||
if isinstance(error, ValidationError):
|
||||
exception_message = ', '.join(error.messages)
|
||||
else:
|
||||
messages.error(
|
||||
request, _(
|
||||
'Error editing metadata for document: '
|
||||
'%(document)s; %(exception)s.'
|
||||
) % {
|
||||
'document': document,
|
||||
'exception': ', '.join(exception.messages)
|
||||
}
|
||||
)
|
||||
exception_message = unicode(error)
|
||||
|
||||
messages.error(
|
||||
request, _(
|
||||
'Error editing metadata for document: '
|
||||
'%(document)s; %(exception)s.'
|
||||
) % {
|
||||
'document': document,
|
||||
'exception': exception_message
|
||||
}
|
||||
)
|
||||
else:
|
||||
messages.success(
|
||||
request,
|
||||
|
||||
Reference in New Issue
Block a user