Skip to content

Commit

Permalink
move schemas and make utility functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
Johnetordoff committed Jul 31, 2018
1 parent 53ab147 commit 633aa46
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 40 deletions.
8 changes: 0 additions & 8 deletions api/users/schemas.py

This file was deleted.

Empty file added api/users/schemas/__init__.py
Empty file.
File renamed without changes.
File renamed without changes.
40 changes: 40 additions & 0 deletions api/users/schemas/utils.py
Original file line number Diff line number Diff line change
@@ -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.')
35 changes: 3 additions & 32 deletions api/users/serializers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import datetime

import jsonschema
from guardian.models import GroupObjectPermission

from django.utils import timezone
Expand All @@ -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):
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit 633aa46

Please sign in to comment.