def validate_user_phone_number(self,phone_number, otp): if not otp and not phone_number : return HttpBadRequest("otp and phone_number required") try: otp_handler.validate_otp(otp, phone_number=phone_number) except Exception as ex: data = {'status': 0, 'message': "invalid OTP"} logger.info("[Exception OTP]:{0}". format(ex)) return HttpResponse(json.dumps(data), content_type="application/json")
def validate_otp(request): if request.method == 'GET': return render(request, 'portal/validate_otp.html') elif request.method == 'POST': try: otp = request.POST['otp'] username = request.POST['username'] logger.info('OTP {0} recieved for validation. username {1}'.format(otp, username)) user = User.objects.get(username=username) user_profile = models.UserProfile.objects.get(user=user) otp_handler.validate_otp(otp, user=user_profile) logger.info('OTP validated for name {0}'.format(username)) return render(request, 'portal/reset_pass.html', {'otp': otp}) except Exception as ex: logger.error('OTP validation failed for name {0} : {1}'.format(username, ex)) return HttpResponseRedirect('/aftersell/users/otp/generate?token=invalid')
def change_user_password(self, request, **kwargs): if request.method != 'POST': return HttpResponse(json.dumps({"message":"method not allowed"}), content_type="application/json",status=401) try: load = json.loads(request.body) except: return HttpResponse(content_type="application/json", status=404) type = kwargs['type'] otp_token = load.get('otp_token') password = load.get('password1') repassword = load.get('password2') invalid_password = check_password(repassword) if (invalid_password): return HttpBadRequest("password is not meant according to the rules") auth_key = load.get('auth_key') user_details = {} if not type: return HttpBadRequest("type not defined use email/phone") if password != repassword: return HttpBadRequest("password1 and password2 not matched") try: if type=='phone': try: if not (settings.ENV in settings.IGNORE_ENV and otp_token in settings.HARCODED_OTPS): consumer = afterbuy_model.OTPToken.objects.get(token=otp_token).user otp_handler.validate_otp(otp_token, user=consumer) except Exception: raise ImmediateHttpResponse( response=http.HttpBadRequest('Wrong OTP!')) user_details['id'] = consumer.user.id elif type=='email': try: user_obj = afterbuy_model.EmailToken.objects.get(activation_key=auth_key).user except Exception: raise ImmediateHttpResponse( response=http.HttpBadRequest('invalid authentication key!')) user_details['email'] = user_obj.user.email user = User.objects.filter(**user_details)[0] user.set_password(password) user.save() data = {'status': 1, 'message': "password updated successfully"} except Exception as ex: logger.error('Invalid details, mobile {0} and exception {1}'.format(request.POST.get('phone_number', ''),ex)) data = {'status': 0, 'message': "password not updated"} return HttpResponse(json.dumps(data), content_type="application/json")
def validate_otp_phone(self, request, **kwargs): ''' Validate otp sent to phone args : phone number and otp return : status 1 on successfull validation ''' if request.method != 'POST': return HttpResponse(json.dumps({"message":"method not allowed"}), content_type="application/json",status=401) try: load = json.loads(request.body) otp_token = load.get('otp_token') phone_number = load.get('phone_number') if not otp_token or not phone_number: return HttpBadRequest("OTP and phone number is mandatory") try: consumer_user = get_model('Consumer', settings.BRAND).objects.get(phone_number=phone_number, is_email_verified=True) otp_handler.validate_otp(otp_token, phone_number=phone_number) access_token = self.generate_access_token(request, consumer_user) return HttpResponse(json.dumps({'status': 1,'access_token':access_token, 'message':'OTP validated'}), content_type='application/json') except Exception as ex: user = get_model('Consumer', settings.BRAND).objects.get(phone_number=phone_number, user__is_active=True) otp_handler.validate_otp(otp_token, phone_number=phone_number) user.is_phone_verified = True user.save(using=settings.BRAND) logger.info("Exception checking exisiting user {0}".format(ex)) return HttpResponse(json.dumps({'status': 1, 'message':'OTP validated'}), content_type='application/json') except Exception as ex: logger.info("Exception while validating OTP {0}".format(ex)) return HttpBadRequest("OTP couldnot be validated")
def validate_otp_email(self, request, **kwargs): ''' Validate the otp sent to email and map user products Args : email , phone number , otp Returns : map the products and returns access token ''' if request.method != 'POST': return HttpResponse(json.dumps({'message':"Method not allowed"}), content_type='application/json') try: load = json.loads(request.body) phone_number = load.get('phone_number') otp_token = load.get('otp_token') email = load.get('email') if not otp_token or not phone_number or not email: return HttpBadRequest("OTP , phone number , email is mandatory") otp_handler.validate_otp(otp_token, email=email) consumer = get_model('Consumer', settings.BRAND).objects.select_related('user').get(user__email=email, phone_number=phone_number) user = consumer.user user.is_active = True user.save(using=settings.BRAND) consumer.is_email_verified = True consumer.save(using=settings.BRAND) user_products = get_model('UserProduct', settings.BRAND).objects.\ select_related('consumer').filter(~Q(consumer__phone_number=phone_number) & Q(consumer__user__email=email) & Q(consumer__user__is_active=True)) if len(user_products) >0: for product in user_products: user = product.consumer.user user.is_active = False user.save(using=settings.BRAND) product.save(using=settings.BRAND) products = [] for product in user_products: products.append(get_model('UserProduct', settings.BRAND)(consumer=consumer, nick_name=product.nick_name, product_type=product.product_type, purchase_date=product.purchase_date, brand_product_id=product.brand_product_id, image_url=product.image_url, color=product.color, is_deleted=product.is_deleted, description=product.description, is_accepted=product.is_accepted, service_reminder=product.service_reminder, details_completed=product.details_completed, manual_link=product.manual_link, warranty_year=product.warranty_year, insurance_year=product.insurance_year)) new_products = get_model('UserProduct', settings.BRAND).objects.bulk_create(products) new_products = get_model('UserProduct', settings.BRAND).objects.\ select_related('consumer').filter(Q(consumer__phone_number=phone_number) & Q(consumer__user__email=email)) product_dict = {} for user_product in user_products: product_mapping = filter(lambda product : product.brand_product_id == user_product.brand_product_id, new_products) product_dict[user_product.brand_product_id] = product_mapping[0] insurance_details = get_model('ProductInsuranceInfo', settings.BRAND).objects.filter(product__in=user_products) self.update_product_insurance_warranty(product_dict, insurance_details) warranty_details = get_model('ProductWarrantyInfo', settings.BRAND).objects.filter(product__in=user_products) self.update_product_insurance_warranty(product_dict, warranty_details) else: consumers = get_model('Consumer', settings.BRAND).objects.select_related('user').filter(Q(user__email=email)& ~Q(phone_number=phone_number), Q(user__is_active=True)) for consumer_obj in consumers: user = consumer_obj.user user.is_active = False user.save(using=settings.BRAND) consumer_obj.save(using=settings.BRAND) all_consumers = get_model('Consumer', settings.BRAND).objects.select_related('user')\ .filter(Q(phone_number=phone_number) & ~Q(user__email=email) & Q(user__is_active=True)) if len(all_consumers) >0: all_consumers[0].has_discrepancy = True user_obj = all_consumers[0].user user_obj.is_active = False user_obj.save(using=settings.BRAND) all_consumers[0].save(using=settings.BRAND) access_token = self.generate_access_token(request, consumer) return HttpResponse(json.dumps(access_token), content_type='application/json') except Exception as ex: logger.info("Exception while validating email otp - {0}".format(ex)) return HttpBadRequest("OTP could not be validated")