user_validation.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. # -*- coding: utf-8 -*-
  2. from __future__ import unicode_literals
  3. from tastypie.validation import Validation
  4. class UserValidation(Validation):
  5. '''
  6. '''
  7. def is_valid(self, bundle, request=None):
  8. if not bundle.data:
  9. return {
  10. '__all__': 'data not provided in bundle'
  11. }
  12. errors = {}
  13. if request.method == 'POST':
  14. if not 'first_name' in bundle.data:
  15. errors['first_name'] = 'this field is required'
  16. if not 'last_name' in bundle.data:
  17. errors['last_name'] = 'this field is required'
  18. if not 'username' in bundle.data:
  19. errors['username'] = 'this field is required'
  20. if not 'email' in bundle.data:
  21. errors['email'] = 'this field is required'
  22. if not 'password' in bundle.data:
  23. errors['password'] = 'this field is required'
  24. else:
  25. if 'username' in bundle.data:
  26. errors['username'] = 'cannot update this field'
  27. if 'password' in bundle.data:
  28. errors['password'] = 'cannot update password here, use endpoint for specific action'
  29. if 'last_login' in bundle.data:
  30. errors['last_login'] = 'cannot create or update this field'
  31. if 'date_joined' in bundle.data:
  32. errors['date_joined'] = 'cannot create or update this field'
  33. return errors