def get(self, request): user_per_page = constants.USER_PER_PAGE total_pages = 0 page_start = 0 page_end = user_per_page user_list = [] credit_user_list = AccountManagement.objects.filter( Q(credit_user=request.user), ~Q(debit_user=request.user)).values_list( 'debit_user', flat=True).distinct('debit_user') debit_user_list = AccountManagement.objects.filter( ~Q(credit_user=request.user), Q(debit_user=request.user)).values_list( 'credit_user', flat=True).distinct('credit_user') user_list = User.objects.filter( Q(id__in=credit_user_list) | Q(id__in=debit_user_list)).order_by('-updated_date_time') if 'page_no' in request.GET and request.GET['page_no'] != '0': page_end = int(request.GET['page_no']) * user_per_page page_start = page_end - user_per_page user_list_serializer = GetUserListSerializer( user_list[page_start:page_end], many=True, ) else: user_list_serializer = GetUserListSerializer( user_list[page_start:page_end], many=True, ) total_user = len(user_list) if total_user != 0: total_user_reminder = total_user % user_per_page total_pages = int(total_user / user_per_page) if total_user_reminder != 0: total_pages += 1 response_data = { "total_pages": total_pages, "total_user_count": total_user, "user_list": user_list_serializer.data, "items_per_page": user_per_page } if len(response_data["user_list"]) == 0: response = helper.getPositiveResponse("No user found", response_data) else: response = helper.getPositiveResponse("", response_data) return Response(response, status=response['statusCode'])
def get(self, request): user_per_page = constants.USER_PER_PAGE total_pages = 0 page_start = 0 page_end = user_per_page phone_list = [] user_list = [] phone_list.append(request.user.phone) user_list = User.objects.filter(~Q(phone__in=phone_list)).extra( select={ 'lower_name': 'lower(business_name)' }).order_by('lower_name') if 'page_no' in request.GET and request.GET['page_no'] != '0': page_end = int(request.GET['page_no']) * user_per_page page_start = page_end - user_per_page user_list_serializer = GetUserListSerializer( user_list[page_start:page_end], many=True, ) else: user_list_serializer = GetUserListSerializer( user_list[page_start:page_end], many=True, ) total_user = len(user_list) if total_user != 0: total_user_reminder = total_user % user_per_page total_pages = int(total_user / user_per_page) if total_user_reminder != 0: total_pages += 1 response_data = { "total_pages": total_pages, "total_user_count": total_user, "user_list": user_list_serializer.data, "items_per_page": user_per_page } if len(response_data["user_list"]) == 0: response = helper.getPositiveResponse("No user found", response_data) else: response = helper.getPositiveResponse("", response_data) return Response(response, status=response['statusCode'])
def post(self, request): forgot_password_serializer = ForgotpasswordSerializer( data=request.data) forgot_password_serializer.is_valid(raise_exception=True) validated_data = forgot_password_serializer.validated_data verification_token = helper.randomGeneratorCode() try: user = User.objects.get(phone=validated_data['mobile_number']) try: user_verification = UserVerification.objects.get(user=user) user_verification.verification_token = verification_token user_verification.save() except UserVerification.DoesNotExist: user_verification = UserVerification( verification_token=verification_token, user=user) user_verification.save() token = { 'set_password_token': user_verification.verification_token } response = helper.getPositiveResponse('', token) except User.DoesNotExist: response = helper.getNegativeResponse("User does not exist") return Response(response)
def sendNotification(title, body, data, token): print(token) """ @desc: Send notification @param: data (dictionary) 1) title (string) 2) body (string) 3) data (dict with any format) 4) token (string) @return: Dictionary: {'status' : SUCCESS|FAIL, 'msg' : (string), 'status_code' : (int), data : (dict)} """ try: notification = messaging.Notification(title=title, body=body) web_notification = messaging.WebpushNotification(icon=constants.NOTIFICATION_ICON) web_push_config = messaging.WebpushConfig(notification=web_notification) message = messaging.Message( notification=notification, data=data, token=token, webpush=web_push_config ) message_id = firebase_admin.messaging.send(message) print(message_id) return helper.getPositiveResponse("Message send successfully",message_id) except Exception as e: return helper.getNegativeResponse("Message send failed")
def get(self, request): bank_detail_list = BankDetails.objects.filter(user=request.user) bank_detail_serializer = GetBankdetailSerializer(bank_detail_list, many=True) response = helper.getPositiveResponse("Retrieved bank detail", bank_detail_serializer.data) return Response(response)
def get(self, request): user_profile_serializer = GetUserProfileSerializer(request.user) print(user_profile_serializer) response = helper.getPositiveResponse("Retrieved profile", user_profile_serializer.data) # response = get_response("Retrieved profile", BaseAPIView.PASS_RESPONSE_STATUS,BaseAPIView.SUCCESS_CODE,user_profile_serializer.data) return Response(response)
def post(self, request): user_signin_ser = UserSignInSerializer(data=request.data) user_signin_ser.is_valid(raise_exception=True) validated_data = user_signin_ser.validated_data try: user = User.objects.get(phone=validated_data['mobile_number']) except: response = helper.getNegativeResponse( 'Mobile number or password is incorrect') return Response(response, status=response['statusCode']) # raise ValidationError('Mobile number or password is incorrect', code="002") if not user.check_password(validated_data['password']): response = helper.getNegativeResponse( 'Mobile number or password is incorrect') return Response(response, status=response['statusCode']) # raise ValidationError('Mobile number or password is incorrect') jwt_payload_handler = api_settings.JWT_PAYLOAD_HANDLER jwt_encode_handler = api_settings.JWT_ENCODE_HANDLER payload = jwt_payload_handler(user) token = jwt_encode_handler(payload) response = helper.getPositiveResponse("Valid user.", token) # response = get_response('Valid user.',BaseAPIView.PASS_RESPONSE_STATUS,BaseAPIView.SUCCESS_CODE,token) return Response(response)
def post(self, request): manage_bank_serializer = ManageBankSerializer(data=request.data) manage_bank_serializer.is_valid(raise_exception=True) validated_data = manage_bank_serializer.validated_data validated_data['user'] = request.user manage_bank_serializer.save() response = helper.getPositiveResponse("bank detail save successfully") return Response(response)
def post(self, request): print('post..') manage_user_serializer = ManageAccountSerializer(data=request.data) manage_user_serializer.is_valid(raise_exception=True) validated_data = manage_user_serializer.validated_data validated_data['user'] = request.user manage_user_serializer.save() response = helper.getPositiveResponse("Entry created successfully") return Response(response)
def delete(self, request): if 'bank_id' not in request.data: raise ValidationError('bank id is require') try: bank_detail = BankDetails.objects.get(id=request.data['bank_id']) bank_detail.delete() # bank_detail.save() response = helper.getPositiveResponse( "bank id is delete successfully") return Response(response) except BankDetails.DoesNotExist: raise ValidationError('bank id is alredy deleted')
def post(self, request): user_signup_ser = UserSignUpSerializer(data=request.data) user_signup_ser.is_valid(raise_exception=True) user = user_signup_ser.save() jwt_payload_handler = api_settings.JWT_PAYLOAD_HANDLER jwt_encode_handler = api_settings.JWT_ENCODE_HANDLER payload = jwt_payload_handler(user) token = jwt_encode_handler(payload) # logging.info('User registered successfully') response = helper.getPositiveResponse("User registered.", token) # response = get_response('User registered.',BaseAPIView.PASS_RESPONSE_STATUS, BaseAPIView.SUCCESS_CODE, token) return Response(response)
def delete(self, request): try: user_remove_serializer = UserRemoveSerializer(data=request.data) print(user_remove_serializer) user_remove_serializer.is_valid(raise_exception=True) validated_data = user_remove_serializer.validated_data user = User.objects.get(id=validated_data['user_id']) user.delete() response = helper.getPositiveResponse("User Deleted") except User.DoesNotExist: response = helper.getNegativeResponse("User does not exist") return Response(response)
def post(self, request): try: token = request.META['HTTP_AUTHORIZATION'][4:] black_list_token = BlackList(token=token) black_list_token.save() if 'fcm_token' in request.data and request.data['fcm_token']: request.user.fcm_token.remove(request.data['fcm_token']) request.user.save() response = helper.getPositiveResponse("User logged out") except Exception as e: raise ValidationError(e) return Response(response)
def post(self, request): response = {} # try: user = request.user fcm_serializer = FCMTokenSerializer(User.objects.get(id=user.id), data=request.data) if fcm_serializer.is_valid() == False: response = self.getErrorResponse(fcm_serializer, status.HTTP_400_BAD_REQUEST) return Response(response, status=response['statusCode']) fcm_serializer.save() response = helper.getPositiveResponse("") return Response(response, status=response['statusCode'])
def post(self, request): set_password_serializer = SetPasswordSerializer(data=request.data) set_password_serializer.is_valid(raise_exception=True) validated_data = set_password_serializer.validated_data try: user_verification = UserVerification.objects.get( verification_token=validated_data['set_password_token']) except UserVerification.DoesNotExist: response = helper.getNegativeResponse("Invalid verification token") return Response(response, status=response['statusCode']) user_verification.user.set_password(validated_data['password']) user_verification.user.save() response = helper.getPositiveResponse('Set password successfully') return Response(response)
def get(self, request): print('get method') records_per_page = constants.USER_PER_PAGE paginated_response = { "total_pages": 0, "total_records_count": 0, "records": [], "items_per_page": records_per_page } if 'user_id' not in request.GET: raise ValidationError('user id is require') credit_user_list = AccountManagement.objects.filter( Q(credit_user=request.user, debit_user__id=request.GET['user_id']) | Q(debit_user=request.user, credit_user__id=request.GET['user_id'])) if 'currentMonth' in request.GET and 'currentYear' in request.GET: credit_user_list = credit_user_list.filter( current_time__year=request.GET['currentYear']).filter( current_time__month=request.GET['currentMonth']) context = { 'user': request.GET['user_id'], 'request_user': request.user, 'current_month': request.GET['currentMonth'], 'current_year': request.GET['currentYear'] } # debit_user_list = AccountManagement.objects.filter(debit_user=request.user) # print(debit_user_list) # if credit_user_list: print('in credit') paginated_response = self.get_paginated_records( request, credit_user_list) credit_debit_serializer = GetCreditDetailSerializer( paginated_response['records'], context=context, many=True) paginated_response['records'] = credit_debit_serializer.data paginated_response['items_per_page'] = records_per_page response = helper.getPositiveResponse('', paginated_response) return Response(response)
def post(self, request): if 'business_photo' not in request.data: response = helper.getNegativeResponse('business photo is require') return Response(response, status=response['statusCode']) # raise ParseError("Empty content") f = request.data['business_photo'] try: img = Image.open(f) img.verify() except: response = helper.getNegativeResponse('Unsupported image type') return Response(response, status=response['statusCode']) # raise ParseError("Unsupported image type") request.user.profile_photo = f request.user.save() data = {'business_photo': request.user.profile_photo.url} response = helper.getPositiveResponse("Profile updated successfully", data) return Response(response, status=200)
def put(self, request): # old_email = request.user.email user_profile_serializer = UserProfileSerializer(request.user, data=request.data) user_profile_serializer.is_valid(raise_exception=True) validated_data = user_profile_serializer.validated_data if user_profile_serializer.is_valid() == False: response = helper.getNegativeResponse(user_profile_serializer) # response = self.getErrorResponse(user_profile_serializer, status.HTTP_400_BAD_REQUEST) return Response(response, status=response['statusCode']) # validated_data['email'] = validated_data['email'].lower() user_profile_serializer.save() # if validated_data['email'] != old_email: # jwt_payload_handler = api_settings.JWT_PAYLOAD_HANDLER # jwt_encode_handler = api_settings.JWT_ENCODE_HANDLER # payload = jwt_payload_handler(request.user) # payload['user_id'] = str(payload['user_id']) # token = jwt_encode_handler(payload) # else: # token = request.META['HTTP_AUTHORIZATION'][4:] response = helper.getPositiveResponse("Profile updated!") return Response(response)
def put(self, request): response = {} user = request.user passward_serializer = UserPasswordUpdateSerializer(data=request.data) passward_serializer.is_valid(raise_exception=True) password_data = passward_serializer.validated_data if user.check_password(password_data['oldPassword']) == True: if password_data['password'] != password_data[ 'passwordConfirmation']: response = helper.getNegativeResponse( 'Password and confirmpassword did not match') return Response(response, status=response['statusCode']) # raise ValidationError('Password and confirmpassword did not match') else: user.set_password(password_data['password']) user.save() else: response = helper.getNegativeResponse('Old password is not valid.') return Response(response, status=response['statusCode']) # raise ValidationError('Old password is not valid.') response = helper.getPositiveResponse("Password updated!") return Response(response)