diff --git a/api/users/schemas.py b/api/users/schemas.py deleted file mode 100644 index 3e9cb336f86..00000000000 --- a/api/users/schemas.py +++ /dev/null @@ -1,8 +0,0 @@ -import os -import json - -here = os.path.split(os.path.abspath(__file__))[0] - -def from_json(fname): - with open(os.path.join(here, fname)) as f: - return json.load(f) diff --git a/api/users/schemas/__init__.py b/api/users/schemas/__init__.py new file mode 100644 index 00000000000..e69de29bb2d diff --git a/api/users/education-schema.json b/api/users/schemas/education-schema.json similarity index 100% rename from api/users/education-schema.json rename to api/users/schemas/education-schema.json diff --git a/api/users/employment-schema.json b/api/users/schemas/employment-schema.json similarity index 100% rename from api/users/employment-schema.json rename to api/users/schemas/employment-schema.json diff --git a/api/users/schemas/utils.py b/api/users/schemas/utils.py new file mode 100644 index 00000000000..79fcc466df0 --- /dev/null +++ b/api/users/schemas/utils.py @@ -0,0 +1,40 @@ +import os +import json +import datetime +import jsonschema + +from api.base.exceptions import InvalidModelValueError + +here = os.path.split(os.path.abspath(__file__))[0] + +def from_json(fname): + with open(os.path.join(here, fname)) as f: + return json.load(f) + + +def validate_user_json(value, json_schema): + try: + jsonschema.validate(value, from_json(json_schema)) + except jsonschema.ValidationError as e: + if len(e.path) > 1: + raise InvalidModelValueError("For '{}' the field value {}".format(e.path[-1], e.message)) + raise InvalidModelValueError(e.message) + except jsonschema.SchemaError as e: + raise InvalidModelValueError(e.message) + + validate_dates(value) + + +def validate_dates(info): + for history in info: + + if history.get('startYear'): + startDate = datetime.date(history['startYear'], history.get('startMonth', 1), 1) + + if not history['ongoing']: + if history.get('endYear'): + endDate = datetime.date(history['endYear'], history.get('endMonth', 1), 1) + + if history.get('startYear') and history.get('endYear'): + if (endDate - startDate).days <= 0: + raise InvalidModelValueError(detail='End date must be greater than or equal to the start date.') diff --git a/api/users/serializers.py b/api/users/serializers.py index ce93278e881..4bf968cbab6 100644 --- a/api/users/serializers.py +++ b/api/users/serializers.py @@ -1,6 +1,3 @@ -import datetime - -import jsonschema from guardian.models import GroupObjectPermission from django.utils import timezone @@ -17,7 +14,7 @@ from api.files.serializers import QuickFilesSerializer from osf.exceptions import ValidationValueError, ValidationError from osf.models import OSFUser, QuickFilesNode -from api.users.schemas import from_json +from api.users.schemas.utils import validate_user_json class QuickFilesRelationshipField(RelationshipField): @@ -132,38 +129,12 @@ def profile_image_url(self, user): size = self.context['request'].query_params.get('profile_image_size') return user.profile_image_url(size=size) - def _validate_user_json(self, value, json_schema): - try: - jsonschema.validate(value, from_json(json_schema)) - except jsonschema.ValidationError as e: - if len(e.path) > 1: - raise InvalidModelValueError("For '{}' the field value {}".format(e.path[-1], e.message)) - raise InvalidModelValueError(e.message) - except jsonschema.SchemaError as e: - raise InvalidModelValueError(e.message) - - self._validate_dates(value) - - def _validate_dates(self, info): - for history in info: - - if history.get('startYear'): - startDate = datetime.date(history['startYear'], history.get('startMonth', 1), 1) - - if not history['ongoing']: - if history.get('endYear'): - endDate = datetime.date(history['endYear'], history.get('endMonth', 1), 1) - - if history.get('startYear') and history.get('endYear'): - if (endDate - startDate).days <= 0: - raise InvalidModelValueError(detail='End date must be greater than or equal to the start date.') - def validate_employment(self, value): - self._validate_user_json(value, 'employment-schema.json') + validate_user_json(value, 'employment-schema.json') return value def validate_education(self, value): - self._validate_user_json(value, 'education-schema.json') + validate_user_json(value, 'education-schema.json') return value def update(self, instance, validated_data):