Browse Source

[ADD] put or patch user

Gogs 7 years ago
parent
commit
8f2975d0f4
2 changed files with 31 additions and 13 deletions
  1. 8 3
      api/resources/user_resource.py
  2. 23 10
      api/validations/user_validation.py

+ 8 - 3
api/resources/user_resource.py

@@ -1,7 +1,7 @@
 # -*- coding: utf-8 -*-
 from __future__ import unicode_literals
 from tastypie import fields
-from tastypie.resources import ModelResource, ALL, ALL_WITH_RELATIONS
+from tastypie.resources import ModelResource
 from tastypie.authorization import Authorization
 from tastypie.exceptions import ImmediateHttpResponse
 from core.models.user import User
@@ -30,8 +30,13 @@ class UserResource(ModelResource):
         if bundle.errors and not skip_errors:
             raise ImmediateHttpResponse(response=self.error_response(bundle.request, bundle.errors))
 
-        user = User.objects.create_user(bundle.data['username'], bundle.data['email'], bundle.data['password'])
-        bundle.obj = user
+        if bundle.obj._state.adding:
+            user = User.objects.create_user(bundle.data['username'], bundle.data['email'], bundle.data['password'])
+            user.first_name = bundle.data.get('first_name', '')
+            user.last_name = bundle.data.get('last_name', '')
+
+            bundle.obj = user
+        
         bundle.data['groups'] = bundle.data.get('groups', [])
 
         if bundle.obj.pk:

+ 23 - 10
api/validations/user_validation.py

@@ -13,19 +13,32 @@ class UserValidation(Validation):
 
         errors = {}
 
-        if not 'first_name' in bundle.data:
-            errors['first_name'] = 'this field is required'
+        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 '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 'username' in bundle.data:
+                errors['username'] = 'this field is required'
 
-        if not 'email' in bundle.data:
-            errors['email'] = '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'
+            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