def patch(self, request, user_id=None): try: data = request.data.copy() profile_id = data['profile_id'] user = UserProfile.objects.get(id=profile_id) except: raise Http404 phone_linked = data.get('phone_number') queryset = OTPModel.objects.filter(phone_linked=phone_linked) if not queryset.exists(): email_linked = User.objects.get(profile=user).email OTP_create_send(email_linked, phone_linked) serializer = UserProfileSerializer(user, data=data, partial=True, context={'request': request}) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_202_ACCEPTED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) return Response({'detail': 'Phone number already in use.'}, status=status.HTTP_403_FORBIDDEN)
def post(self, request, user_id): data = request.data.copy() data['user'] = user_id serializer = UserProfileSerializer(data=data, context={'request': request}) if serializer.is_valid(): serializer.save() if data.get('is_employed') == 'true': print('here') WorkExperience.objects.create( user=self.get_user(serializer.data['id']), organization_name=f"{data['organization_name']}", position=data['position'], start_date=data['start_date'], end_date=data['end_date']) SocialProfile.objects.create( user=self.get_user(serializer.data['id']), tagline= f"{data['position']} at {data['organization_name']}", current_industry=self.get_industry(serializer.data['id'])) return Response(serializer.data, status=status.HTTP_201_CREATED) Education.objects.create( user=self.get_user(serializer.data['id']), organization_name=f"{data['organization_name']}", start_date=data['start_date'], end_date=data['end_date']) SocialProfile.objects.create( user=self.get_user(serializer.data['id']), tagline=f"{data['position']} at {data['organization_name']}", current_academia=self.get_academia(serializer.data['id'])) return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_201_CREATED)
def put(self, request, profile_id=None): user = UserProfile.objects.get(id=profile_id) data = request.data.copy() try: del data['current_industry'] except: pass try: del data['current_academia'] except: pass try: del data['phone_number'] except: pass try: del data['bio'] except: pass profile_serializer = UserProfileSerializer( user, data=data, partial=True, context={'request': request}) if profile_serializer.is_valid(): profile_serializer.save() banner_serializer = SocialProfileSerializer( user.social_profile, data=data, partial=True, context={'request': request}) if banner_serializer.is_valid(): banner_serializer.save() data = {**profile_serializer.data, **banner_serializer.data} return Response(data, status=status.HTTP_202_ACCEPTED) return Response(banner_serializer.errors, status=status.HTTP_400_BAD_REQUEST) return Response(profile_serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def Profile(request, username, format=None): """ Retrieve, update the profile i.e profile picture and access token. """ try: user = User.objects.get(username=username) rkmender = Rkmender.objects.get(user_id = user.id) except User.DoesNotExist: return Response(status=status.HTTP_404_NOT_FOUND) if request.method == 'GET': serializer = UserProfileSerializer(rkmender) return Response(serializer.data) elif request.method == 'PUT': serializer = UserProfileSerializer(rkmender, data=request.DATA, partial = True) if serializer.is_valid(): serializer.save() return Response(serializer.data) else: return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)