def find_user_for_order(username): try: user = User.query.filter_by(username=username).first() user_id = user.id return user_id except (InvalidRequestError, OperationalError, CompileError): raise InvalidUsageError('mysql connection or syntax is improper', 500)
def otp_gen(): try: otp = "" otp = str(r.randint(1000, 9999)) return otp except (InvalidRequestError, OperationalError): raise InvalidUsageError('sql connection or syntax is improper', 500)
def calculate_total_price(cart, user_id): try: total_price_product = 0 books_in_cart = [] for each_book in cart: product_id = each_book.id product_title = each_book.title image = each_book.image price = int(each_book.price) row = db.session.query(relationship_table_cart).filter_by( user_id=user_id, product_id=product_id).first() total_price_each_product = row.quantity * price books_in_cart.append( { "Id": product_id, "Title": product_title, "Image": image, "Price": price, "Quantity": row.quantity, "Amount for each book": total_price_each_product } ) total_price_product = total_price_product + total_price_each_product return total_price_product, books_in_cart except (InvalidRequestError, OperationalError, CompileError): raise InvalidUsageError('mysql connection or syntax is improper', 500)
def find_user_wishlist(username): try: user = User.query.filter_by(username=username).first() wishlist = user.wishlist wishlist = calling_book_details(wishlist) return wishlist except (InvalidRequestError, OperationalError, CompileError): raise InvalidUsageError('mysql connection or syntax is improper', 500)
def check_admin_otp(entered_otp, phone): try: otp = redis_db.get(phone).decode('utf-8') if otp == entered_otp: return True return False except (InvalidRequestError, OperationalError): raise InvalidUsageError('sql connection or syntax is improper', 500)
def check_for_admin_in_db(user_name): try: user = Admin.query.filter_by(username=user_name).first() if user: phone = user.phone return phone return make_response(jsonify({'response': 'Not an admin'})) except (InvalidRequestError, OperationalError): raise InvalidUsageError('mysql connection or syntax is improper', 500)
def add_or_delete_books_in_wishlist(user, book, action, book_quantity=0): try: if action == "add": book.products.append(user) if action == "delete": user.wishlist.remove(book) db.session.commit() return True except (InvalidRequestError, OperationalError, CompileError): raise InvalidUsageError('mysql connection or syntax is improper', 500)
def sort_books(sort_parameter): try: column_names = ProductData.__table__.columns.keys() if sort_parameter in column_names: sorted_books = ProductData.query.order_by( asc(sort_parameter)).all() return calling_book_details(sorted_books) except (InvalidRequestError, OperationalError, CompileError): raise InvalidUsageError('mysql connection or syntax is improper', 500)
def find_user_cart(username): try: user = User.query.filter_by(username=username).first() cart = user.cart user_id = user.id total_price_product, books_in_cart = calculate_total_price( cart, user_id) return [total_price_product, books_in_cart] except (InvalidRequestError, OperationalError, CompileError): raise InvalidUsageError('mysql connection or syntax is improper', 500)
def check_for_user_in_db(user_name, password): try: user = User.query.filter_by(username=user_name).first() if user: if user.is_verified == 1: if check_password_hash(user.password, password): return True return False except (InvalidRequestError, OperationalError): raise InvalidUsageError('mysql connection or syntax is improper', 500)
def add_order_details(order_id, address, user_id): try: order = Order(order_id=order_id, address=address, user=user_id) db.session.add(order) db.session.commit() return True except IntegrityError: return make_response( jsonify({'response': "user name or email already exists"}), 400) except (InvalidRequestError, OperationalError): raise InvalidUsageError('sql connection or syntax is improper', 500)
def insert_to_user_db(username, email, password, phone): try: hashed_password = generate_password_hash(password, method='sha256') new_user = User(username=username, email=email, password=hashed_password, phone=phone, is_verified=False) db.session.add(new_user) db.session.commit() except IntegrityError: return make_response(jsonify({'response': "user name or email already exists"}), 400) except (InvalidRequestError, OperationalError): raise InvalidUsageError('sql connection or syntax is improper', 500)
def search_books(search_parameter): try: search_result = ProductData.query.filter_by( title=search_parameter).first() if search_result == None: search_result = ProductData.query.filter_by( author=search_parameter) search_result = calling_book_details(search_result) return search_result except (InvalidRequestError, OperationalError, CompileError): raise InvalidUsageError('mysql connection or syntax is improper', 500)
def check_otp(entered_otp, phone): try: otp = redis_db.get(phone).decode('utf-8') user = User.query.filter_by(phone=phone).first() if otp == entered_otp: user.is_verified = 1 db.session.commit() else: return make_response(jsonify({'response': "Invalid otp or expired otp"}), 400) except (InvalidRequestError, OperationalError): raise InvalidUsageError('sql connection or syntax is improper', 500)
def add_or_delete_books(id, username, action, arg, book_quantity): try: user = User.query.filter_by(username=username).first() book = ProductData.query.filter_by(id=id).first() switcher = { 0: add_or_delete_books_in_wishlist, 1: add_or_delete_books_in_cart } return switcher.get(arg, "invalid")(user, book, action, book_quantity) except (InvalidRequestError, OperationalError, CompileError): raise InvalidUsageError('mysql connection or syntax is improper', 500)
def send_otp(phone, otp): try: account_sid = os.getenv('account_sid') auth_token = os.getenv('auth_token') client = Client(account_sid, auth_token) message = client.messages.create( to=phone, from_=os.getenv('from_phone_number'), body=otp) except Exception: return make_response(jsonify({'response': "invalid phone number"})) except (InvalidRequestError, OperationalError): raise InvalidUsageError('phone number is improper', 500)
def get_user_and_product_details(username): try: user = User.query.filter_by(username=username).first() email = user.email phone = user.phone order = Order.query.filter_by(user=user.id).first() order_id = order.order_id shipping_address = order.address cart_details = find_user_cart(username) return [ email, username, order_id, shipping_address, cart_details[1], cart_details[0], phone ] except (InvalidRequestError, OperationalError, CompileError): raise InvalidUsageError('mysql connection or syntax is improper', 500)
def display_wishlist_or_cart(username, arg): try: switcher = { 0: find_user_wishlist, 1: find_user_cart } result = switcher.get(arg, "invalid")(username) if result: return result else: result = "empty" return result except (InvalidRequestError, OperationalError, CompileError): raise InvalidUsageError('mysql connection or syntax is improper', 500)
def add_or_delete_books_in_cart(user, book, action, book_quantity): try: if action == "add": if book.quantity < book_quantity: return make_response(jsonify({'response': "only" + str(book.quantity) + "books available"})) book.products_to_order.append(user) db.session.commit() current_user_id = user.id current_product_id = book.id db.session.execute('UPDATE relationship_table_cart SET quantity = :quantity WHERE user_id = :user_id and product_id = :product_id', { 'quantity': book_quantity, 'user_id': current_user_id, 'product_id': current_product_id}) if action == "delete": user.cart.remove(book).all db.session.commit() return True except (InvalidRequestError, OperationalError, CompileError): raise InvalidUsageError('mysql connection or syntax is improper', 500)
def post(self): form = LoginForm(request.form) user_name = form.username.data password = form.password.data try: if not form.validate(): return make_response(login_response[400], 400) present_in_db, confirmed = DataBase.check_user_in_db( user_name, password) if present_in_db: if not confirmed: return make_response(login_response[411], 411) access_token = create_access_token(identity=user_name) app.logger.info('{} was logged in'.format(user_name)) redis.set(name=user_name, value=access_token) redis.expire(user_name, redis_time) return make_response( jsonify({ "respone:token": access_token, "status": 200 }), 200) return make_response(login_response[401], 401) except ConnectionError: raise InvalidUsageError(redis_error[500], 500)
def store_otp(phone, otp): try: redis_db.set(phone, otp.encode('utf-8')) redis_db.expire(phone, 1800) except Exception: raise InvalidUsageError('encoding error,try again', 500)
def store_access_token(user, token): try: redis_db.set(user, token) redis_db.expire(user, 1000) except Exception: raise InvalidUsageError('encoding error,try again', 500)