def create(self, request, *args, **kwargs): user = request.user if not user.is_superuser: return Response( {'error': "Užívateľ nie je admin"}, status=status.HTTP_401_UNAUTHORIZED, ) serializer = UserCreateSerializer(data=request.data) data = {} if not serializer.is_valid(): return Response( [serializer.errors], status=status.HTTP_400_BAD_REQUEST, ) user = serializer.save() data["email"] = user.email data["name"] = user.name data["surname"] = user.surname data["phone"] = str(user.phone) token = Token.objects.get(user=user).key data["token"] = token return Response( data, status=status.HTTP_201_CREATED, )
def post(self, request, format=None): serializer = UserCreateSerializer(data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def create(self, request, *args, **kwargs): """ """ data = request.data data = dict(data.items()) serializer = UserCreateSerializer(data=data) host = request.META['HTTP_HOST'] if serializer.is_valid(): user = get_user_model().objects.create_user(**serializer.data) user.set_password(data.get('password')) user.is_active = False user.save() # here generating the activation key activation_key = str(uuid.uuid4()) user.activation_key = activation_key user.save() profile = Profile(user=user) profile.save() mail_user_activation_key(user, host=host) # getting application application = Application.objects.get(name="mmb") # creating access token access_token = AccessToken(user=user, expires=expires, token=generate_token(), application=application) access_token.save() refresh_token = RefreshToken.objects.create( user=user, token=generate_token(), access_token=access_token, application=application) response = { 'access_token': access_token.token, 'token_type': 'Bearer', 'expires_in': settings.OAUTH2_PROVIDER['ACCESS_TOKEN_EXPIRE_SECONDS'], 'refresh_token': refresh_token.token } return Response(response, status=status.HTTP_200_OK) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def registration(request): serializer = UserCreateSerializer(data=request.data) if not serializer.is_valid(): return Response(serializer.errors, status.HTTP_400_BAD_REQUEST) user = serializer.save() refresh = RefreshToken.for_user(user) res = { 'refresh': str(refresh), 'access': str(refresh.access_token), } return Response(res, status.HTTP_201_CREATED)
def post(self, request): """POST method for creating new user""" serializer = UserCreateSerializer(data=request.data) if not serializer.is_valid(): return Response(data=serializer.errors, status=status.HTTP_400_BAD_REQUEST) try: new_user = serializer.save() except: return Response(data={'error': sys.exc_info()[0]}, status=status.HTTP_400_BAD_REQUEST) serializer = UserSerializer(new_user) return Response(data=serializer.data, status=status.HTTP_201_CREATED)
def post(self, request): #function is used to create new user from online form user = UserCreateSerializer(data=request.data) if user.is_valid(): if "password" in request.data and len( request.data['password']) > 7: user = user.save() user.set_password(request.data['password']) # user.is_staff=1 # user.is_superuser=1 user.save() token = Token.objects.get_or_create(user=user) return Response({ "status": True, "msg": "login sucess" }, headers={"Authorization": token}) else: Response({ "status": False, "msg": "invalid password" }, status=status.HTTP_400_BAD_REQUEST) else: print(user.errors) for key in user.errors: pass return Response({ "status": False, "msg": "error " + key }, status=status.HTTP_400_BAD_REQUEST)
def register(request): request_data = request.POST if request.body: request_data = loads(request.body.decode('utf-8')) user_data = UserCreateSerializer(data=request_data) data = None if user_data.is_valid(): user = user_data.save() data = { 'username': user_data.data.get('username'), 'jwt': get_jwt_for_id(user.id) } else: return StandardResponse(errors=user_data.errors) return StandardResponse(data)
def get(self, request, pk, format=None): """ API endpoint that get specific user. --- Body example: ``` { "id": 1, "username": "******", "email": "*****@*****.**", "profile": { "id": 1, "hours": 5, "abilities": [ { "id": 1, "name": "Gamificacao" } ], "user": 1, "phone": 10, "photo": null, "coins": 0 } } ``` """ user = self.get_object(pk) serializer = UserCreateSerializer(user) return Response(serializer.data)
def test_user_create_serializer(self): """ Testing user serializer """ user = { "email": fake.email(), "first_name": fake.first_name(), "last_name": fake.last_name(), "phone": fake.phone_number(), "role_id": self.role.id, } serializer = UCR(data=user) valid = serializer.is_valid() self.assertTrue(valid)
def test_create_serializer_happy(self): data = dict( email="*****@*****.**", password="******", re_password="******", organization="Test", ) serializer = UserCreateSerializer(data=data) serializer.is_valid(raise_exception=True) self.assertEqual(User.objects.count(), 0) self.assertEqual(Organization.objects.count(), 0) user = serializer.save() self.assertEqual(User.objects.count(), 1) self.assertEqual(Organization.objects.count(), 1) self.assertEqual(user.organization.name, data["organization"])
def test_create_method_on_serializer(self): """ Testing create method in UserCreateSerializer """ user = { "email": fake.email(), "first_name": fake.first_name(), "last_name": fake.last_name(), "phone": fake.phone_number(), "role_id": self.role.id, } serializer = UCR(data=user) serializer.is_valid() serializer.save() exists = User.objects.filter(email=user["email"], phone=user["phone"]).exists() self.assertTrue(exists)
def create_user(request): user_ser = UserCreateSerializer(data=request.data) if not user_ser.is_valid(): raise GeneralException("Request is invalid.") if User.objects.filter(username=user_ser.data['username']).exists(): raise GeneralException("User with given username already exists.") validated_data = user_ser.data created_user = User.objects.create( username=validated_data['username'], email=validated_data['email'], first_name=validated_data['first_name'], last_name=validated_data['last_name'], ) created_user.set_password(validated_data['password']) created_user.save() serializer = UserSerializer(created_user, many=False) return Response(serializer.data)
def get(self, request, format=None): """ API endpoint that list all users. --- Body example: ``` [ { "id": 1, "username": "******", "email": "*****@*****.**", "profile": { "id": 1, "hours": 5, "abilities": [ { "id": 1, "name": "Gamificacao" } ], "user": 1, "phone": 10, "photo": null, "coins": 0 } }, { "id": 2, "username": "******", "email": "*****@*****.**", "profile": { "id": 2, "hours": 10, "abilities": [ { "id": 1, "name": "teste2" } ], "user": 2, "phone": 96362067, "photo": null, "coins": 0 } } ] ``` """ users = User.objects.all() serializer = UserCreateSerializer(users, many=True) return Response(serializer.data)
def post(self, request): serializer = UserCreateSerializer(data=request.data) serializer.is_valid(raise_exception=True) if serializer.is_valid(): serializer.save() else: return Response({'response': 'error', 'message': serializer.errors}) return Response({'response': 'success', 'message': 'Climber created successfully'})
class BlogSerializer(serializers.ModelSerializer): written_by = UserCreateSerializer( many=False, read_only=True ) ingredient = serializers.SlugRelatedField( many=False, read_only=True, slug_field='name' ) comments = CommentSerializer( many=True, read_only=True ) class Meta: model = Blog fields = ('id', 'title', 'ingredient', 'written_by', 'byline', 'body', 'photo_url', 'created_at', 'comments')
def post(request): serializer = UserCreateSerializer(data=request.data) serializer.is_valid(raise_exception=True) serializer.save() return Response(status=status.HTTP_201_CREATED)
def get(self, request, pk, format=None): user = self.get_object(pk) serializer = UserCreateSerializer(user) return Response(serializer.data)
def get(self, request, format=None): users = User.objects.all() serializer = UserCreateSerializer(users, many=True) return Response(serializer.data)
def post(self, request, *args, **kwargs): serializer = UserCreateSerializer(data=request.data) serializer.is_valid(raise_exception=True) serializer.save() return Response(serializer.data, status=HTTP_201_CREATED)
def user_create_serializer(db, user_create_serializer_data): return UserCreateSerializer(data=user_create_serializer_data)
def post(self, request, format=None): """ API endpoint that create the users. --- Body example: ``` { "username": "******", "email": "*****@*****.**", "password": "******", "profile": { "id": 1, "hours": 15, "abilities": [ { "id": 1, "name": "Gamificacao" } ], "user": 1, "phone": 990987610, "photo": null, "coins": 0 } } ``` Response example: ``` { "id": 1, "username": "******", "email": "*****@*****.**", "profile": { "id": 1, "hours": 15, "abilities": [ { "id": 1, "name": "Gamificacao" } ], "user": 1, "phone": 990987610, "photo": null, "coins": 0 } } ``` """ serializer = UserCreateSerializer(data=request.data) if serializer.is_valid(): serializer.save() user = User.objects.get(username=request.data['username']) abilities_data = '' try: profile_data = request.data['profile'] if profile_data.get('id'): del profile_data['id'] abilities_data = profile_data.pop('abilities') except KeyError: profile_data = {} profile_data['user'] = user profile = Profile.objects.create(**profile_data) for abilitiest in abilities_data: abilitiest = Abilities.objects.filter(id=abilitiest['id']) profile.abilities.add(*abilitiest) profile.save() profile_serializer = ProfileSerializer(profile) return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)