def create(plan_id): subscription_plan = Subscription.get_by_id(plan_id) user = User.get_by_id(current_user.id) user.subscription = subscription_plan if user.save(): flash('Plan Selected') return redirect(url_for('transactions.new_checkout', subscription_id = subscription_plan.id)) else: flash("An error occured") return render_template('subscriptions/show.html', subscription_plan = subscription_plan)
def edit(id): user_id = get_jwt_identity() sub = Subscription.get_by_id(id) name = request.json.get('name') amount = request.json.get('amount') description = request.json.get('description') frequency = request.json.get('frequency') frequency = int(frequency) subs_type = request.json.get('subs_type') payment_date = request.json.get('payment_date') payment_date = convert_date(payment_date) if subs_type == "daily": next_payment = add_days(payment_date, frequency) elif subs_type == "weekly": next_payment = add_weeks(payment_date, frequency) elif subs_type == "monthly": next_payment = add_months(payment_date, frequency) elif subs_type == "yearly": next_payment = add_years(payment_date, frequency) query = Subscription.update( name=name, amount=amount, description=description, frequency=frequency, subs_type=subs_type, payment_date=payment_date, next_payment=next_payment).where(Subscription.id == sub.id) if query.execute(): responseObj = { 'status': 'success', 'message': 'Successfully edited subscription' } return jsonify(responseObj), 200 else: responseObj = { 'status': 'failed', 'message': 'Failed to edit your subscription' } return jsonify(responseObj), 400
def subs_delete(id): user_id = get_jwt_identity() sub = Subscription.get_by_id(id) if sub.delete_instance(): subs_obj = Subscription.select().where(Subscription.user_id == user_id) subs_arr = [] if subs_obj: for sub in subs_obj: str_amount = "" if sub.subs_type == "yearly" and sub.amount is not None: str_amount = "RM" + str(sub.amount) + "/y" elif sub.subs_type == "monthly" and sub.amount is not None: str_amount = "RM" + str(sub.amount) + "/m" elif sub.subs_type == "weekly" and sub.amount is not None: str_amount = "RM" + str(sub.amount) + "/w" elif sub.subs_type == "daily" and sub.amount is not None: str_amount = "RM" + str(sub.amount) + "/d" subs_list = { 'id': sub.id, 'name': sub.name, 'amount': sub.amount, 'str_amount': str_amount, 'subs_type': sub.subs_type, 'last_payment': sub.payment_date.strftime('%A %d %b %Y'), 'next_payment': sub.next_payment.strftime('%A %d %b %Y'), 'description': sub.description, 'paid': sub.paid, 'due': sub.due } subs_arr.append(subs_list) responseObj = { 'status': 'success', 'message': 'Successfully deleted subscription!', 'subscriptions': subs_arr } return jsonify(responseObj), 200 else: responseObj = { 'status': 'failed', 'message': 'Failed to delete your subscription' } return jsonify(responseObj), 400
def get_sub_data(id): user_id = get_jwt_identity() sub = Subscription.get_by_id(id) subs_list = { 'id': sub.id, 'name': sub.name, 'amount': sub.amount, 'description': sub.description, 'frequency': sub.frequency, 'subs_type': sub.subs_type, 'payment_date': sub.payment_date, } responseObj = {'status': 'success', 'subscriptions': subs_list} return jsonify(responseObj), 200
def update(plan_id): subscription_plan = Subscription.get_by_id(plan_id) user_id = get_jwt_identity() user = User.get_by_id(user_id) result = gateway.subscription.update(str(user_id), { "price": subscription_plan.price, "plan_id": subscription_plan.id, }) if result.is_success: user.subscription = subscription_plan if user.save(): return jsonify({"message": "Plan changed successfully!"}) else: return jsonify({"message": "Failed to change plan!"}) else: return jsonify({"message": "Failed to change plan!"})
def update(plan_id): subscription_plan = Subscription.get_by_id(plan_id) user = User.get_by_id(current_user.id) result = gateway.subscription.update(str(current_user.id), { "price" : subscription_plan.price, "plan_id" : subscription_plan.id, }) if result.is_success : user.subscription = subscription_plan if user.save(): flash("Plan changed successfully", "success") return redirect(url_for('home')) else : flash("Failed to change plan", "danger") return redirect(url_for('subscriptions.show')) else : flash("Failed to change plan", "danger") return redirect(url_for('subscriptions.show'))
def delete_subscription(subscription_id): subscription = Subscription.get_by_id(subscription_id) subscription.key.delete()