# -*- coding: utf-8 -*- from __future__ import unicode_literals from tastypie.validation import Validation class UserValidation(Validation): ''' ''' def is_valid(self, bundle, request=None): if not bundle.data: return { '__all__': 'data not provided in bundle' } errors = {} if request.method == 'POST': if not 'first_name' in bundle.data: errors['first_name'] = 'this field is required' if not 'last_name' in bundle.data: errors['last_name'] = 'this field is required' if not 'username' in bundle.data: errors['username'] = 'this field is required' if not 'email' in bundle.data: errors['email'] = 'this field is required' if not 'password' in bundle.data: errors['password'] = 'this field is required' else: if 'username' in bundle.data: errors['username'] = 'cannot update this field' if 'password' in bundle.data: errors['password'] = 'cannot update password here, use endpoint for specific action' if 'last_login' in bundle.data: errors['last_login'] = 'cannot create or update this field' if 'date_joined' in bundle.data: errors['date_joined'] = 'cannot create or update this field' return errors