def add_promo_code_helper(session, code, description, expiration_time, rules): pc = PromoCode(code=code, description=description, expiration_time=expiration_time, rules=rules) session.add(pc) return pc.code
def create_order(): """ Creates Order row. Updates order with Razorpay Order ID. Inserts rows in Item tables with the Order ID :return: {'id': order.id, 'rp_order_id': razorpay_order_id} """ req = request.get_json() test_ids = req['tests'] if req['tests'] is not None else [] promo = req['promo_code'] # will be null if not present total_amount = 0 promo_id = None razorpay_order_id = None # set to null by default tests = Test.query \ .filter(Test.id.in_(test_ids)) \ .options(load_only('price')) \ .all() for test in tests: total_amount += test.price total_amount = max(0, total_amount) if promo: try: promo_amt, promo_id = PromoCode.consume_promo_code( promo, current_user.id) total_amount -= promo_amt except ValueError as e: return Error(str(e), 400)() order = Order(status=OrderStatusTypes.created, amount=total_amount, promo_code_id=promo_id, user_id=current_user.id) db.session.add(order) db.session.flush() if total_amount > 0: razorpay_order_id = Razorpay.create_order(order.id, total_amount) else: order.status = OrderStatusTypes.paid # mark free order as paid order.rp_order_id = razorpay_order_id # This adds rows to OrderTest which is important for User to get # access when Payment goes through for test in tests: test_id = test.id order_test = OrderTest(order_id=order.id, test_id=test_id) db.session.add(order_test) db.session.commit() return {'id': order.id, 'rp_order_id': razorpay_order_id}
def add_codes_helper(sess, chunk, n): codes = [] for i in range(chunk, min(chunk + chunk_size, num_codes)): code = "_".join(datagen.words(nb=3)) + "_" + str(time.time()) codes.append(PromoCode(code=code, description=datagen.paragraph(), expiration_time=datetime.datetime.now( ) + datetime.timedelta(days=random.randint(0, 30)), rules={"type": "percent_discount", "value": "10%"})) sess.bulk_save_objects(codes)
def validate_promo_code(code): """ Validates the promo code. For documentation on the conditions of how a user may consume a promo, visit Promocode.consume_promo_code :param code: :return: """ try: promo_value, _ = PromoCode.consume_promo_code(code, current_user.id, should_flush=False) return {"promo_value": promo_value} except ValueError: return Error("Invalid Promo Code", 400)()
def create_promo_code(): """ Allows admin to create Promo Code for User's discount :param promo_code: :param promo_value: :param promo_max_usage: :param promo_valid: :param promo_multiple_use: :return: """ req = request.get_json() try: promo_code = PromoCode(**req) db.session.add(promo_code) db.session.commit() return "" except exc.IntegrityError: return Error('Promo Code already exists', 400)()
def promocode(): if current_user.is_activated == False: return redirect(url_for('home')) if request.method == 'POST': name = request.form.get('name') type = request.form.get('type') amount = request.form.get('amount') code_check = PromoCode.query.filter_by(name=name).first() if code_check: flash("This code exists!", "danger") return redirect(url_for('promocode')) promocode = PromoCode(name=name, type=type, value=amount) db.session.add(promocode) db.session.commit() return redirect(url_for('home')) return render_template('promocode.html')