Split metadata validators in validators and parsers. Move document and upload wizard metadata validation logic to model. Add metadata tests.
This commit is contained in:
@@ -2,42 +2,33 @@ from __future__ import unicode_literals
|
||||
|
||||
from dateutil.parser import parse
|
||||
|
||||
from django.core.exceptions import ValidationError
|
||||
|
||||
class MetadataValidator(object):
|
||||
from .parsers import MetadataParser
|
||||
|
||||
|
||||
class MetadataValidator(MetadataParser):
|
||||
_registry = []
|
||||
|
||||
@classmethod
|
||||
def register(cls, parser):
|
||||
cls._registry.append(parser)
|
||||
|
||||
@classmethod
|
||||
def get_all(cls):
|
||||
return cls._registry
|
||||
|
||||
@classmethod
|
||||
def get_import_path(cls):
|
||||
return cls.__module__ + '.' + cls.__name__
|
||||
|
||||
@classmethod
|
||||
def get_import_paths(cls):
|
||||
return [validator.get_import_path() for validator in cls.get_all()]
|
||||
|
||||
def parse(self, input_data):
|
||||
raise NotImplementedError
|
||||
def validate(self, input_data):
|
||||
try:
|
||||
self.execute(input_data)
|
||||
except Exception as exception:
|
||||
raise ValidationError(exception)
|
||||
|
||||
|
||||
class DateAndTimeValidator(MetadataValidator):
|
||||
def validate(self, input_data):
|
||||
def execute(self, input_data):
|
||||
return parse(input_data).isoformat()
|
||||
|
||||
|
||||
class DateValidator(MetadataValidator):
|
||||
def validate(self, input_data):
|
||||
def execute(self, input_data):
|
||||
return parse(input_data).date().isoformat()
|
||||
|
||||
|
||||
class TimeValidator(MetadataValidator):
|
||||
def validate(self, input_data):
|
||||
def execute(self, input_data):
|
||||
return parse(input_data).time().isoformat()
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user