def put(self, request, pk=None): profile = self.get_object(pk) serializer = ProfileSerializer(instance=profile, data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data) return Response(serializer.errors)
def signup_many(request): if request.method == 'POST': profiles = request.data.get('profiles', None) for profile in profiles: username = profile.get('username', None) password = profile.get('password', None) age = profile.get('age', None) occupation = profile.get('occupation', None) gender = profile.get('gender', None) create_profile(username=username, password=password, age=age, occupation=occupation, gender=gender) return Response(status=status.HTTP_201_CREATED) if request.method == 'GET': movieid = request.GET.get('movieid', None) userid = request.GET.get('userid', None) if movieid: profiles = Profile.objects.raw('select api_profile.id, api_profile.user_id,api_profile.gender,api_profile.age,api_profile.occupation,api_rating.rating from api_movie,api_rating,api_profile where api_movie.id = api_rating.movieid_id and api_rating.userid_id = api_profile.user_id and api_movie.id = '+movieid) serializer = ProfileSerializer(profiles, many=True) elif userid: movies = Movie.objects.raw('select api_movie.id, api_movie.title,api_movie.genres, api_rating.rating as rating from api_profile,api_rating,api_movie where api_profile.user_id = api_rating.userid_id and api_movie.id = api_rating.movieid_id and api_profile.user_id = '+userid+' group by api_movie.id') serializer = UserMovieSerializer(movies, many=True) else: profiles = Profile.objects.all() serializer = ProfileSerializer(profiles, many=True) return Response(data=serializer.data, status=status.HTTP_200_OK)
def put(self, request, username, format=None): profile = self.get_object(username) serializer = ProfileSerializer(profile, data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def post(self, request): serializer = ProfileSerializer try: user1 = Profile.objects.get(user=request.user) useremail = bleach.clean(request.data.get('email')) user2 = Profile.objects.get(EmailAddress=useremail) #Can only add a friend if they are not already part of your friends list and not a part of theirs. success = 'fail' if user1 not in user2.Friends.all(): user2.Friends.add(user1) success = 'success' if user2 not in user1.Friends.all(): user1.Friends.add(user2) success = 'success' user1.save() user2.save() serializer = ProfileSerializer(user2) print('success', success) return Response({'profile': serializer.data, 'success': success}) except Exception as e: print('exception: ', e) #error case serializer = ProfileSerializer(user1) return Response({'profile': serializer.data, 'success': 'fail'})
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 get(self, request, *args, **kwargs): """ profile情報を表示する endpoint: /api/profiles/ name: - """ """テスト項目 済 keyを使ってこのエンドポイントにアクセスするとProfileデータを受け取る 済 test_Tokenオブジェクトに存在しないkeyを使った場合rauthentication_failedの値が返る """ # print(self.request.META) # print(self.request.META['HTTP_AUTHORIZATION']) # HTTPリクエストヘッダーのトークン情報からユーザーを特定する # token = self.request.META['HTTP_AUTHORIZATION'].split(" ")[1] token = getTokenFromHeader(self) # print(token) # Userオブジェクトの取得 # user_obj = Token.objects.get(key=token).user user_obj = getUserByToken(token) if user_obj is None: return Response({"result": "fail"}) profile_obj = Profile.objects.get(user=user_obj) # print(profile_obj) print(Response(ProfileSerializer(profile_obj).data)) return Response(ProfileSerializer(profile_obj).data)
def save(self, profile, data=None): profile = ProfileSerializer(profile, data=data) if profile.is_valid(): profile.save() response, status = profile.data, STATUS_200 else: response, status = profile.errors, STATUS_400 return response, status
def register(request): serializer = UserSerializer(data=request.data) serializer_profile = ProfileSerializer(data=request.data.get("profile")) if serializer.is_valid() and serializer_profile.is_valid(): serializer.save() serializer_profile.save() return Response(serializer_profile.data) return Response(serializer.errors)
def post(self, request, format=None): image = req['display_image'] req['user'] = self.request.user.id #self.request.user. serializer = ProfileSerializer(data=req) if serializer.is_valid(): serializer.save(owner=self.request.user) return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_403_FORBIDDEN)
def post(self, request: Request) -> Response: 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 patch(self, request: Request, pk: int) -> Response: profile = get_object_or_404(Profile, pk=pk) serializer = ProfileSerializer(profile, data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def put(self, request, pk, format=None): #data= JSONParser().parse(request) profile = self.get_object(pk) req = request.data req['user'] = self.request.user.id # self.request.user. serializer = ProfileSerializer(profile, data=req) if serializer.is_valid(): serializer.save(owner=self.request.user) return Response(serializer.data, status=status.HTTP_200_OK) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def get(self, request, provider_id=None, identity_id=None): """Authentication Required, retrieve the users profile.""" #logger.info(request.user) user = request.user #logger.debug(user.get_profile()) profile = user.get_profile() serialized_data = ProfileSerializer(profile).data identity = user.select_identity() identity_id = identity.id provider_id = identity.provider.id serialized_data.update({}) response = Response(serialized_data) return response
def fetch_profile(request, pk): profile = Profile.objects.filter(uuid=pk) if not profile.exists(): return Response({'error': 'Profile doesn\'t exist.'}, status=status.HTTP_200_OK) if request.method == 'GET': serializer = ProfileSerializer(profile.first(), many=False, context={'request': request}) return Response({'profile': serializer.data}, status=status.HTTP_200_OK) elif request.method == 'PATCH': serializer = ProfileSerializer(profile.first(), data=request.data, partial=True, context={'request': request}) try: serializer.is_valid(raise_exception=True) except: return Response({'error': str(serializer.errors)}, status=status.HTTP_400_BAD_REQUEST) serializer.save() return Response({'profile': serializer.data}, status=status.HTTP_200_OK)
def get(self, request, provider_id=None, identity_id=None): """ """ # logger.info(request.user) user = request.user # logger.debug(user.get_profile()) profile = user.get_profile() serialized_data = ProfileSerializer(profile).data identity = user.select_identity() identity_id = identity.id provider_id = identity.provider.id serialized_data.update({}) response = Response(serialized_data) return response
def post(self, request): profile = Profile.objects.filter(user=request.user) profile.FirstName = request.data.get('firstname') profile.LastName = request.data.get('lastname') profile.EmailAddress = request.data.get('email') profile.Location = request.data.get('location') #reget their coordinates if they change profile.coordinates = getCoordinates(request.data.get('location')) profile.save() serializer = ProfileSerializer(profile) if not serializer.is_valid(): return Response({'serializer': serializer, 'user': profile}) serializer.save() return Response({'user': serializer.data})
def profile_view(request): user = Userss.objects.get(username=request.data['username']) request.data['user'] = user serializer = ProfileSerializer(data=request.data) if serializer.is_valid(): serializer.save() #print(user.profile) #print(serializer.data['picture']) j = {} j['user'] = serializer.data['user'] j['picture'] = user.profile.picture.url return Response(data=j, status=status.HTTP_201_CREATED) else: return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def post(self, request): user = get_user_object(request) serializer = ProfileSerializer(data=request.data) if serializer.is_valid(): try: profile = serializer.save(user=user) except IntegrityError: raise ValidationError( {'integrity error': 'Duplicate creation attempt'}) except Exception as e: raise ValidationError({'detail': e}) else: user.profiles.add(profile) return Response(serializer.data) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def put(self, request, provider_id=None, identity_id=None): """ Update a users profile If VALID save the profile else raise ValidationError """ user = request.user profile = user.get_profile() serializer = ProfileSerializer(profile, data=request.DATA) if serializer.is_valid(): serializer.save() response = Response(serializer.data) return response else: return Response(serializer.errors)
def put(self, request, provider_id=None, identity_id=None): """ Authentication Required, Update the users profile. Returns: 203 - Success, no body. 400 - Bad key/value on update, errors in body. """ user = request.user profile = user.get_profile() serializer = ProfileSerializer(profile, data=request.DATA) if serializer.is_valid(): serializer.save() response = Response(serializer.data) return response else: return Response(serializer.errors)
def put(self, request, provider_uuid=None, identity_uuid=None): """ Authentication Required, Update the users profile. Returns: 203 - Success, no body. 400 - Bad key/value on update, errors in body. """ user = request.user profile = user.userprofile serializer = ProfileSerializer(profile, data=request.data) if serializer.is_valid(): serializer.save() response = Response(serializer.data) return response else: return Response(serializer.errors)
def patch(self, request, provider_id=None, identity_id=None): """ Update a users profile If VALID save the profile else raise ValidationError """ user = request.user profile = user.get_profile() mutable_data = request.DATA.copy() if mutable_data.has_key('selected_identity'): user_data = { 'selected_identity': mutable_data.pop('selected_identity') } serializer = AtmoUserSerializer(user, data=user_data, partial=True) if serializer.is_valid(): serializer.save() serializer = ProfileSerializer(profile, data=mutable_data, partial=True) if serializer.is_valid(): serializer.save() response = Response(serializer.data) return response else: return Response(serializer.errors)
def login(request): if request.method == 'POST': request_data = request.data.get('params', None) if request_data: try: username = request_data.get('username', None) password = request_data.get('password', None) user = User.objects.get(username=username) except User.DoesNotExist: err = "아이디 또는 비밀번호가 틀렸습니다." return Response(data=err, status=status.HTTP_401_UNAUTHORIZED) if not check_password(password, user.password): err = "아이디 또는 비밀번호가 틀렸습니다." return Response(data=err, status=status.HTTP_401_UNAUTHORIZED) else: request.session['user'] = user.username profile = Profile.objects.get(user=user) serializer = ProfileSerializer(profile) return Response(data=serializer.data, status=status.HTTP_200_OK)
def profileSearch(request): if request.method == 'GET': username = request.GET.get('username') user = get_object_or_404(User, username=username) profile = get_object_or_404(Profile, id=user.id) serializer = ProfileSerializer(profile) return Response(data=serializer.data, status=status.HTTP_200_OK)
def getUsers(request): if request.method == 'GET': age = request.GET.get('age', None) gender = request.GET.get('gender', None) occupation = request.GET.get('occupation', None) page = request.GET.get('page', 1) profiles = Profile.objects.all() if age: profiles = profiles.filter(age=age) if gender: profiles = profiles.filter(gender=gender) if occupation: profiles = profiles.filter(occupation=occupation) if page: page = int(page) if page <= len(profiles) // 50 + 1: start = 50 * (page - 1) end = start + 50 if len(profiles[start:]) < 50: profiles = profiles[start:] profiles = profiles[start:end] serializer = ProfileSerializer(profiles, many=True) return JsonResponse({ 'result': serializer.data, 'status': status.HTTP_200_OK }) return JsonResponse({ 'status': status.HTTP_400_BAD_REQUEST, 'msg': 'Invalid Request Method' })
def post(self, request): try: username = bleach.clean(request.data.get('username')) password = bleach.clean(request.data.get('password')) firstname = bleach.clean(request.data.get('firstname')) lastname = bleach.clean(request.data.get('lastname')) email = bleach.clean(request.data.get('email')) location = bleach.clean(request.data.get('location')) #GetCoordinates returns the coordinates based on the users input location - provided by gooogle api coordinates = getCoordinates(location) user = User.objects.create_user(username=username, password=password) user.save() profile = Profile(user=user, FirstName=firstname, LastName=lastname, EmailAddress=email, Location=location, coordinates=coordinates) profile.save() serializer = ProfileSerializer(profile) return Response({ 'user': serializer.data, "token": AuthToken.objects.create(user) }) except: return HttpResponseBadRequest()
def profiles(request): if request.method == 'GET': user = request.GET.get('user', request.GET.get('user', None)) title = request.GET.get('title', None) profiles = Profile.objects.all() serializer = ProfileSerializer(profiles, many=True) return Response(data=serializer.data, status=status.HTTP_200_OK) if request.method == 'DELETE': Profile.objects.all() profile.delete() return Response(status=status.HTTP_200_OK) if request.method == 'POST': profiles = request.data.get('profiles', None) for profile in profiles: user = profile.get('user', None) gender = profile.get('gender', None) age = profile.get('age', None) occupation = profile.get('occupation', None) if not (user and gender and age): continue Profile(user=user, gender=gender, age=age, occupation=occupation).save() return Response(status=status.HTTP_200_OK)
def put(self, request, format=None): access_key_check(request) token = Token.objects.get( token=request.META.get('HTTP_AUTHORIZATION')[6:]) profile = Profile.objects.filter(token=token) if 'first_name' in request.data: new_first_name = request.data['first_name'] profile.update(first_name=new_first_name) if 'last_name' in request.data: new_last_name = request.data['last_name'] profile.update(last_name=new_last_name) if 'email' in request.data: new_email = request.data['email'] profile.update(email=new_email) if 'photo' in request.data: new_photo = request.data['photo'] if new_photo: profile.update(photo=upload_photo(new_photo, profile.last().phone)) profile = Profile.objects.get(token=token) serializer = ProfileSerializer(profile) return Response(serializer.data, status=status.HTTP_200_OK)
def fetch_payment(request, pk): payment = Payment.objects.filter(uuid=pk) if not payment.exists(): return Response({'error': 'Payment doesn\'t exist.'}, status=status.HTTP_200_OK) stripe.api_key = settings.STRIPE_KEY payment = payment.first() pi = stripe.PaymentIntent.retrieve(payment.stripe_id, ) serializer = ProfileSerializer(payment.entertainer, many=False, context={'request': request}) if pi.status == 'succeeded': return Response( { 'success': True, 'entertainer': serializer.data, 'value': pi.amount_received / 100 }, status=status.HTTP_200_OK) else: return Response( { 'error': 'Payment was never completed', 'entertainer': serializer.data, 'code': 1 }, status=status.HTTP_200_OK)
def patch(self, request, provider_id=None, identity_id=None): """ Authentication Required, Update the users profile. Returns: 203 - Success, no body. 400 - Bad key/value on update, errors in body. """ user = request.user profile = user.get_profile() mutable_data = request.DATA.copy() if mutable_data.has_key('selected_identity'): user_data = { 'selected_identity': mutable_data.pop('selected_identity') } serializer = AtmoUserSerializer(user, data=user_data, partial=True) if serializer.is_valid(): serializer.save() serializer = ProfileSerializer(profile, data=mutable_data, partial=True) if serializer.is_valid(): serializer.save() response = Response(serializer.data) return response else: return Response(serializer.errors)
def post(self, request): """ User Class: Create a new user in the database Returns success 200 OK - NO BODY on creation """ params = request.DATA user = request.user if user.username is not 'admin' or not user.is_superuser: return Response('Only admin and superusers can create accounts', status=status.HTTP_401_UNAUTHORIZED) username = params['username'] #STEP1 Create the account on the provider provider = Provider.objects.get(location='EUCALYPTUS') driver = AccountDriver(provider) user = driver.add_user(username) #STEP2 Retrieve the identity from the provider if user: user_keys = driver.get_key(username) driver.create_key(user_keys) #STEP3 Return the new users serialized profile serialized_data = ProfileSerializer(user.get_profile()).data response = Response(serialized_data) return response
def get(self, request, format=None): access_key_check(request) token = Token.objects.get( token=request.META.get('HTTP_AUTHORIZATION')[6:]) profile = Profile.objects.get(token=token) serializer = ProfileSerializer(profile) return Response(serializer.data, status=status.HTTP_200_OK)
def get_author(uuid, host): """ Gets the Author, creats a new one if necesary """ if not uuid or not host: return None # Get Author author = Author.objects.filter(uuid=uuid).first() if author is None: # New foreign author, create them profile_data = get_foreign_profile_data(uuid, get_node(host)) serializer = ProfileSerializer(data=profile_data, partial=True) # try: serializer.is_valid(raise_exception=True) author = serializer.save() # except: # logger.log("Couldn't parse new Author profile for %s from %s." % (uuid, host)) # author = None elif author.user is None: # Existing Foreign Author, update them profile_data = get_foreign_profile_data(uuid, get_node(host)) serializer = ProfileSerializer(author, data=profile_data, partial=True) # try: serializer.is_valid(raise_exception=True) author = serializer.save() # except: # logger.log("Couldn't parse update data of Author profile for %s from %s." % (uuid, host.host)) # TBH, we don't care if we can't handle their bad data return author