def get_coupon_obj(user_id: int, coupon_number: str): session = Session() qr_coupon = session.query(QRCodeCoupon).filter_by( user_id=user_id, coupon_number=coupon_number).first() session.close() return qr_coupon
def is_coupon_by_number_exist(user_id: int, coupon_number: str): session = Session() instance = session.query(QRCodeCoupon).filter_by( user_id=user_id, coupon_number=coupon_number).first() session.close() return True if instance else False
def get_unused_coupon(user_id: int): session = Session() qr_coupon = session.query(QRCodeCoupon).filter_by( user_id=user_id, used=False).order_by(asc(QRCodeCoupon.expiry_date), asc(QRCodeCoupon.coupon_number)).first() session.close() return qr_coupon
def get_summary_by_expire_date(user_id: int): session = Session() summary = session.query(func.count(QRCodeCoupon.user_id), QRCodeCoupon.expiry_date, QRCodeCoupon.used).filter_by( user_id=user_id, used=False).group_by(QRCodeCoupon.user_id, QRCodeCoupon.expiry_date, QRCodeCoupon.used).all() session.close() return summary
def mark_coupon(user_id: int, coupon_number: str, mark: bool): session = Session() qr_coupon = session.query(QRCodeCoupon).filter_by( user_id=user_id, coupon_number=coupon_number).first() qr_coupon.used = mark if mark: qr_coupon.date_used = datetime.now() else: qr_coupon.date_used = None session.add(qr_coupon) session.commit() session.close()
def save_coupons(original_image: Image.Image, user_id: int, qr_codes: List): session = Session() existed = [] for num, qr in enumerate(qr_codes): # cut single coupon croped_img = get_cropped_image(original_image, qr) # parse data from image expiry_date = parse_coupon_date(croped_img) coupon_number = parse_coupon_number(qr.data) exist = is_coupon_by_number_exist(user_id, coupon_number) if not exist: # add file info to DB file_md = QRCodeCoupon(user_id, qr.data, coupon_number, expiry_date) session.add(file_md) else: existed.append(coupon_number) session.commit() session.close() return existed