Jelajahi Sumber

[ADD] user validations

Gogs 7 tahun lalu
induk
melakukan
824d90c785

+ 1 - 0
.env

@@ -1,4 +1,5 @@
 SECRET_KEY = '^*&s%i#9p7tq(#%f)--#ki2*qx8=iv5173eg35$qupv8%+fyv7'
+ALLOWED_HOSTS = *
 DEBUG = True
 JWT_ACCEPT_HEADER = 'HTTP_AUTHORIZATION'
 JWT_PREFIX_HEADER = 'JWT'

+ 33 - 9
api/resources/user_resource.py

@@ -3,7 +3,9 @@ from __future__ import unicode_literals
 from tastypie import fields
 from tastypie.resources import ModelResource, ALL, ALL_WITH_RELATIONS
 from tastypie.authorization import Authorization
+from tastypie.exceptions import ImmediateHttpResponse
 from core.models.user import User
+from api.validations.user_validation import UserValidation
 from api.resources.group_resource import GroupResource
 
 '''
@@ -15,15 +17,37 @@ class UserResource(ModelResource):
         queryset = User.objects.all()
         authorization = Authorization()
         always_return_data = True
-        # filtering = {
-        #     'slug': ALL,
-        #     'groups': ALL_WITH_RELATIONS,
-        #     'created': ['exact', 'range', 'gt', 'gte', 'lt', 'lte'],
-        # }
+        validation = UserValidation()
 
     '''
     '''
-    def obj_create(self, bundle, **kwargs):
-        print(bundle)
-        print(kwargs)
-        return super(UserResource, self).obj_create(bundle)
+    def save(self, bundle, skip_errors=False):
+        if bundle.via_uri:
+            return bundle
+
+        self.is_valid(bundle)
+
+        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
+        bundle.data['groups'] = bundle.data.get('groups', [])
+
+        if bundle.obj.pk:
+            self.authorized_update_detail(self.get_object_list(bundle.request), bundle)
+        else:
+            self.authorized_create_detail(self.get_object_list(bundle.request), bundle)
+
+        self.save_related(bundle)
+
+        obj_id = self.create_identifier(bundle.obj)
+
+        if obj_id not in bundle.objects_saved or bundle.obj._state.adding:
+            bundle.obj.save()
+            bundle.objects_saved.add(obj_id)
+
+        m2m_bundle = self.hydrate_m2m(bundle)
+        self.save_m2m(m2m_bundle)
+        
+        return bundle

+ 0 - 0
api/validations/__init__.py


+ 31 - 0
api/validations/user_validation.py

@@ -0,0 +1,31 @@
+# -*- 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 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'
+
+        return errors

+ 1 - 0
core/models/user.py

@@ -1,6 +1,7 @@
 # -*- coding: utf-8 -*-
 from __future__ import unicode_literals
 from django.contrib.auth.models import AbstractUser
+from django.contrib import auth
 from django.db import models
 import uuid
 

+ 4 - 3
odoo_control/settings.py

@@ -1,12 +1,10 @@
 # -*- coding: utf-8 -*-
 from __future__ import unicode_literals
-from decouple import config
+from decouple import config, Csv
 import os
 
 BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
 
-ALLOWED_HOSTS = ['*']
-
 INSTALLED_APPS = [
     'django.contrib.admin',
     'django.contrib.auth',
@@ -14,6 +12,7 @@ INSTALLED_APPS = [
     'django.contrib.sessions',
     'django.contrib.messages',
     'django.contrib.staticfiles',
+    'django_extensions',
     'tastypie',
     'core.apps.CoreConfig',
     'api.apps.ApiConfig',
@@ -90,6 +89,8 @@ TASTYPIE_DEFAULT_FORMATS = ['json', 'xml']
 
 SECRET_KEY = config('SECRET_KEY')
 
+ALLOWED_HOSTS = config('ALLOWED_HOSTS', cast=Csv())
+
 DEBUG = config('DEBUG', default=False, cast=bool)
 
 JWT_ACCEPT_HEADER = config('JWT_ACCEPT_HEADER')