def show_coupons(coupon_code_=None): #try: connect = get_connection() cur = connect.cursor() qry = 'select * from coupons where 1=1' qry_c = None # if coupon_code_ is not None: qry += ' and coupon_code = %s' qry_c = [coupon_code_] # elif coupon_id_ is not None: # qry += ' and coupon_id_ = %s' # qry_c = [coupon_id_] # else: # # return "One parameter must be involved!" # raise HTTPException(status_code=400, detail="One parameter must be involved!") cur.execute(qry, qry_c) res = [] # out = [] lst = [] if cur.rowcount != 0: res = cur.fetchall() if res: # count = 0 # for i in res: # counter = 0 for j in res[0]: lst.append(j) # counter += 1 # out.append(lst) # count += 1 return lst
def track_of_coupon(coupon_code_): # try: connect = get_connection() cur = connect.cursor() cur.execute('select id from invoices where coupon_code = %s', [coupon_code_]) res = [] if cur.rowcount != 0: res = cur.fetchall() if res: return [i[0] for i in res] return res
def get_user(user_id: int): # try: connect = get_connection() cur = connect.cursor() cur.execute('select * from users where id = %s', [user_id]) res = [] if cur.rowcount != 0: res = cur.fetchall() if res: data = User(username=res[0][3], name=res[0][1], lname=res[0][2], created_at=res[0][4]) return data
def check_user(data: UserSchema): # try: connect = get_connection() cur = connect.cursor() d = json.loads(data.json()) cur.execute('select * from users where username = %s', [d['username']]) res = [] if cur.rowcount != 0: res = cur.fetchall() if res: for i in res: counter = 0 lst = [] for j in i: lst.append(j) counter += 1 return lst
def get_user_permission_by_user_id(user_id: int): # try: connect = get_connection() cur = connect.cursor() cur.execute( '''with a as ( select permission_id from user_permissions up where user_id = %s ) select role from permissions p where exists(select * from a where a.permission_id = p.id)''', [user_id]) res = [] if cur.rowcount != 0: res = cur.fetchall() if res: return [i[0] for i in res] # except(Exception, psycopg2.Error) as error: # raise HTTPException(status_code=500, detail="Internal server error!") return res
def get_user_of_coupon(coupon_code_, user_id): #try: connect = get_connection() cur = connect.cursor() qry = '''select id,user_id, rule->'rule'->>'count_of_use' as count_of_use ,rule->'rule'->>'expire_date' as expire_date,results->'results'->>'discount_type' as discount_type,results->'results'->>'discount_ceil' as discount_ceil from coupons where coupon_code = %s and user_id = %s''' cur.execute(qry, (coupon_code_, user_id)) res = [] lst = [] if cur.rowcount != 0: res = cur.fetchall() if res: for i in res: counter = 0 lst = [] for j in i: lst.append(j) counter += 1 return lst
def create_coupon(coupon: Coupon, response: Response, user_id=Depends(JWTBearer())): #user_id=Depends(JWTBearer()) ps_connection = get_connection() cur = ps_connection.cursor() try: coupon_ = get_coupon(coupon.coupon_code) if coupon_ is not None: response.status_code = status.HTTP_409_CONFLICT return "Coupon_code already exists!" # raise HTTPException(status_code=409, detail="Coupon_code already exists!") results = ''' { "results": { "discount_type": ''' + str(coupon.discount_type) + ''', "discount_percent": ''' + str(coupon.discount_percent) + ''', "discount_ceil": ''' + str(coupon.discount_ceil) + ''', "discount_amount": ''' + str(coupon.discount_amount) + ''' } } ''' rule = ''' { "rule": { "user_id": ''' + str(coupon.user_id) + ''', "product_id": ''' + str(coupon.product_id) + ''', "expire_date": "''' + str(coupon.expire_date) + '''", "count_of_use": ''' + str(coupon.count_of_use) + ''' } } ''' create_date = datetime.datetime.now( pytz.timezone('Asia/Tehran')).strftime("%Y-%m-%d %H:%M:%S") # date.today() segment = """ INSERT INTO coupons (coupon_code , created_at ,results , rule ,description , user_id ) values (%s,%s,%s,%s,%s,%s) RETURNING id""" record_to_insert = (coupon.coupon_code, str(create_date), results, rule, coupon.description, int(coupon.user_id)) cur.execute(segment, record_to_insert) ps_connection.commit() count = cur.rowcount coupon_id = cur.fetchone()[0] print(count, "Record inserted successfully into coupons table") print( datetime.datetime.now( pytz.timezone('Asia/Tehran')).strftime("%Y-%m-%d %H:%M:%S")) # data = ''' # { # "id": ''' + str(coupon_id) + ''', # "coupon_code": ''' + str(coupon.coupon_code) + ''', # "created_at": "''' + str(create_date) + '''", # "results": [''' + str(results) + '''], # "rule": [''' + str(rule) + '''], # "description": "''' + str(coupon.description) + '''", # "user_id": ''' + str(coupon.user_id) + ''' # } # ''' data = CouponOut(id=coupon_id, user_id=coupon.user_id, coupon_code=coupon.coupon_code, created_at=create_date, rule=rule, results=results, description=coupon.description) # status_ = set_routes_to_cache(key="coupon-code:"+str(coupon.coupon_code), value=data.json()) # if not status_: # response.status_code = status.HTTP_500_INTERNAL_SERVER_ERROR # return "Failed to set redis key" # print(get_routes_from_cache(key="coupon-code:"+str(coupon.coupon_code))) response.status_code = status.HTTP_201_CREATED except (Exception, psycopg2.Error) as error: raise HTTPException(status_code=500, detail="Internal server error!") # response.status_code = status.HTTP_500_INTERNAL_SERVER_ERROR # if ps_connection: # print("Failed to insert record into coupons table", error) return data
def create_invoice(invoice, quantity): sleep(quantity) d = json.loads(invoice) print(d) print(d['coupon_code']) celery_log.info(f"Order Complete!1") coupon_ = get_user_of_coupon(d['coupon_code'], d['user_id']) celery_log.info(f"Order Complete!2") try: if coupon_: # print(date.today()) # print(coupon_[3]) # d = coupon_[3] # datetime_obj = datetime.datetime.strptime(d[:-13], '%y-%m-%d %H:%M:%S') # print(datetime_obj) # if date.today() > date(coupon_[3]): # raise HTTPException(status_code=403, detail="Coupon is expired!") print(coupon_) celery_log.info(f"Order Complete!3") out = track_of_coupon(d['coupon_code']) celery_log.info(f"Order Complete!4") print(out) if len(out) >= int(coupon_[2]): # raise HTTPException(status_code=403, detail="Coupon has already been used!") return "Coupon has already been used!" ps_connection = get_connection() cur = ps_connection.cursor() try: celery_log.info(f"Order Complete!5") create_date = datetime.datetime.now(pytz.timezone('Asia/Tehran')).strftime("%Y-%m-%d %H:%M:%S") # date.today() segment = """ INSERT INTO invoices (user_id ,coupon_code , amount, coupon_discount, create_at, paid_at) values (%s,%s,%s,%s,%s,%s) RETURNING id""" record_to_insert = ( int(d['user_id']), d['coupon_code'], int(d['amount']), d['coupon_discount'], str(create_date), str(create_date)) cur.execute(segment, record_to_insert) ps_connection.commit() count = cur.rowcount invoice_id = cur.fetchone()[0] print(count, "Record inserted successfully into invoices table") data = InvoiceOut( id=invoice_id, user_id=d['user_id'], coupon_code=d['coupon_code'], amount=d['amount'], coupon_discount=d['coupon_discount'], create_at=create_date, paid_at=create_date ) print(data.json()) return data except (Exception, psycopg2.Error) as error: print(error) raise HTTPException(status_code=500, detail="Internal server error!...") else: # raise HTTPException(status_code=403, detail="Coupon not exists or is not authorized!") return "Coupon not exists or is not authorized!" except (Exception, psycopg2.Error) as error: print(error) return error