def create_profile( strategy, backend, user=None, flow=None, current_partial=None, *args, **kwargs ): # pylint: disable=too-many-arguments,unused-argument """ Creates a new profile for the user Args: strategy (social_django.strategy.DjangoStrategy): the strategy used to authenticate backend (social_core.backends.base.BaseAuth): the backend being used to authenticate user (User): the current user flow (str): the type of flow (login or register) current_partial (Partial): the partial for the step in the pipeline Raises: RequireProfileException: if the profile data is missing or invalid """ if backend.name != EmailAuth.name or user.profile.is_complete: return {} data = strategy.request_data().copy() serializer = ProfileSerializer(instance=user.profile, data=data) if not serializer.is_valid(): raise RequireProfileException( backend, current_partial, errors=serializer.errors ) serializer.save() sync_hubspot_user(user) return {}
def put(self, request): profile = Profile.objects.get(user=self.request.user) serializer = ProfileSerializer(instance=profile, data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data) return Response(serializer.errors)
def put(self, request, format=None): my_profile = request.user.get_profile() # User should be able to edit first_name, last_name, email here # TODO: Make sure this is safe as we are making two calls to serialize the request user_serializer = UserSerializer( request.user, data=request.DATA, partial=True ) if not user_serializer.is_valid(): return errors.New400BadRequest(user_serializer.errors) user_serializer.save() serializer = ProfileSerializer( my_profile, data=request.DATA, partial=True ) print serializer.errors if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_200_OK) return errors.New400BadRequest(serializer.errors)
def profile(self, request ): user = request.user profile = Profile.objects.get(user=user) partial = request.method == 'PATCH' serializer = ProfileSerializer(profile, data=request.data , partial =partial) serializer.is_valid(raise_exception=True) serializer.save() return Response(serializer.data , status=200)
def create_profile(sender, instance, created, *args, **kwargs): if not created: return profile_serializer = ProfileSerializer(data={"user": instance.id}) if profile_serializer.is_valid(): profile_serializer.save() else: print(profile_serializer.errors)
def snippet_list(request): #List all code snippets, or create a new snippet. if request.method == 'GET': profile = Profile.objects.all() serializer = ProfileSerializer(profile, many=True) return JsonResponse(serializer.data, safe=False) elif request.method == 'POST': data = JSONParser().parse(request) serializer = ProfileSerializer(data=data) if serializer.is_valid(): serializer.save() return JsonResponse(serializer.data, status=201) return JsonResponse(serializer.errors, status=400)
def create(self, request): serializer = ProfileSerializer(data=request.data) if serializer.is_valid(): new_user = serializer.save() return Response(serializer.data, status=status.HTTP_201_CREATED) else: return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def profile_list(request): """ List all code snippets, or create a new snippet. """ if request.method == 'GET': profiles = Profile.objects.all() serializer = ProfileSerializer(profiles, many=True) return Response(serializer.data) elif request.method == 'POST': serializer = ProfileSerializer(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_user(request): try: data = JSONParser().parse(request) data['username'] = data['email'] if User.objects.filter(username=data['username']).exists(): return JsonResponse({'message': 'User already exists'}, status=200) serializer = SignupSerializer(data=data) profile_serializer = ProfileSerializer(data=data) if serializer.is_valid(): user = serializer.save() data['user_id'] = user.id if profile_serializer.is_valid(): profile = profile_serializer.save() guest_role = Roles.objects.get(role_name='guest') user_with_role = UserRoles.objects.create(user=user, role=guest_role) token = Token.objects.create(user=user) response = { 'token': token.key, 'role': guest_role.role_name, 'phone_number': profile.phone_number, 'gender': profile.gender } return JsonResponse(response, status=200) return JsonResponse(profile_serializer.errors, status=400) return JsonResponse(serializer.errors, status=400) except BaseException as error: print(error) response = {'error': "some error occurred while signing up"} return JsonResponse(response, status=400)
def post(self, request, format=None): """ API endpoint that create the profiles. --- Body example: ``` { "hours": 15, "abilities": [ { "id": 1, "name": "Gamificacao" } ], "user": 1, "phone": 990987610, "photo": null, "coins": 0 } ``` Response example: ``` { "id": 1, "hours": 15, "abilities": [ { "id": 1, "name": "Gamificacao" } ], "user": 1, "phone": 990987610, "photo": null, "coins": 0 } ``` """ serializer = ProfileSerializer(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 test_partial_update(self): user = UserFactory() profile = ProfileFactory(user=user) new_profile_data = ProfileFactory.build() new_user_data = UserFactory.build() data = { "description": new_profile_data.description, "last_name": new_user_data.last_name, } serializer = ProfileSerializer(instance=profile, data=data, partial=True) assert serializer.is_valid(), serializer.errors serializer.save() user.refresh_from_db() assert user.last_name == data["last_name"] profile.refresh_from_db() assert profile.description == data["description"]
def snippet_detail(request, pk): """ Retrieve, update or delete a code snippet. """ try: snippet = Profile.objects.get(pk=pk) except Profile.DoesNotExist: return HttpResponse(status=404) if request.method == 'GET': serializer = ProfileSerializer(snippet) return JsonResponse(serializer.data) elif request.method == 'PUT': data = JSONParser().parse(request) serializer = ProfileSerializer(snippet, data=data) if serializer.is_valid(): serializer.save() return JsonResponse(serializer.data) return JsonResponse(serializer.errors, status=400) elif request.method == 'DELETE': snippet.delete() return HttpResponse(status=204)
def test_update(self): user = UserFactory() profile = ProfileFactory(user=user) new_profile_data = ProfileFactory.build() new_user_data = UserFactory.build() data = { "birth_date": new_profile_data.birth_date, "description": new_profile_data.description, "first_name": new_user_data.first_name, "last_name": new_user_data.last_name, "username": new_user_data.username, } serializer = ProfileSerializer(instance=profile, data=data) assert serializer.is_valid(), serializer.errors profile = serializer.save() assert profile.description == data["description"] assert profile.birth_date == data["birth_date"] assert profile.user.last_name == data["last_name"] assert profile.user.first_name == data["first_name"] assert profile.user.username == data["username"]
def post(self, request, format=None): serializer = ProfileSerializer(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)