def post(self, request): try: user_profile = UserProfile.objects.get(user=self.request.user) except UserProfile.DoesNotExist: raise NotFound('User not found.') user = user_profile.user if 'first_name' in request.data: user.first_name = request.data['first_name'] if 'last_name' in request.data: user.last_name = request.data['last_name'] user_profile.name = user.first_name + ' ' + user.last_name user_profile.url_slug = (user.first_name + user.last_name).lower() + str(user.id) user.save() if 'image' in request.data: user_profile.image = get_image_from_data_url( request.data['image'])[0] if 'thumbnail_image' in request.data: user_profile.thumbnail_image = get_image_from_data_url( request.data['thumbnail_image'])[0] if 'background_image' in request.data: user_profile.background_image = get_image_from_data_url( request.data['background_image'], True, 1280)[0] if 'location' in request.data: geo_location = get_location(request.data['location']) user_profile.location = geo_location if 'biography' in request.data: user_profile.biography = request.data['biography'] if 'website' in request.data: user_profile.website = request.data['website'] if 'availability' in request.data: try: availability = Availability.objects.get( id=int(request.data['availability'])) except Availability.DoesNotExist: raise NotFound('Availability not found.') user_profile.availability = availability if 'skills' in request.data: for skill in user_profile.skills.all(): if not skill.id in request.data['skills']: user_profile.skills.remove(skill) for skill_id in request.data['skills']: try: skill = Skill.objects.get(id=int(skill_id)) user_profile.skills.add(skill) except Skill.DoesNotExist: logger.error("Passed skill id {} does not exists") user_profile.save() serializer = UserProfileSerializer(user_profile) return Response(serializer.data, status=status.HTTP_200_OK)
def get(self, request): try: user_profile = UserProfile.objects.get(user=self.request.user) except UserProfile.DoesNotExist: raise NotFound('User not found.') serializer = UserProfileSerializer(user_profile) return Response(serializer.data, status=status.HTTP_200_OK)
def post(self, request): try: user_profile = UserProfile.objects.get(user=self.request.user) except UserProfile.DoesNotExist: raise NotFound('User not found.') user = user_profile.user if 'first_name' in request.data: user.first_name = request.data['first_name'] if 'last_name' in request.data: user.last_name = request.data['last_name'] user_profile.name = user.first_name + ' ' + user.last_name user_profile.url_slug = (user.first_name + user.last_name).lower() + str(user.id) user.save() logger.error("starting to save image") if 'image' in request.data: user_profile.image = get_image_from_data_url( request.data['image'])[0] logger.error("done with image") if 'background_image' in request.data: user_profile.background_image = get_image_from_data_url( request.data['background_image'], True, 1280)[0] logger.error("done with background image") if 'country' in request.data: user_profile.country = request.data['country'] if 'state' in request.data: user_profile.state = request.data['state'] if 'city' in request.data: user_profile.city = request.data['city'] if 'biography' in request.data: user_profile.biography = request.data['biography'] if 'availability' in request.data: try: availability = Availability.objects.get( id=int(request.data['availability'])) except Availability.DoesNotExist: raise NotFound('Availability not found.') user_profile.availability = availability if 'skills' in request.data: for skill in user_profile.skills.all(): if not skill.id in request.data['skills']: logger.error("this skill needs to be deleted: " + skill.name) user_profile.skills.remove(skill) for skill_id in request.data['skills']: try: skill = Skill.objects.get(id=int(skill_id)) user_profile.skills.add(skill) except Skill.DoesNotExist: logger.error("Passed skill id {} does not exists") user_profile.save() serializer = UserProfileSerializer(user_profile) return Response(serializer.data, status=status.HTTP_200_OK)
def get(self, request, url_slug, format=None): try: profile = UserProfile.objects.get(url_slug=str(url_slug)) except UserProfile.DoesNotExist: return Response({'message': 'Profile not found.'}, status=status.HTTP_404_NOT_FOUND) if self.request.user.is_authenticated: serializer = UserProfileSerializer(profile) return Response(serializer.data, status=status.HTTP_200_OK) else: serializer = UserProfileMinimalSerializer(profile) return Response(serializer.data, status=status.HTTP_200_OK)