def index(): subscriptions = Subscription.select() return jsonify([{ "id": s.id, "name": s.name, "amount_of_meals": s.amount_of_meals, "price": s.price, "description": s.description } for s in subscriptions])
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 subscriptions(id): subscriptions = Subscription.select().where(Subscription.for_user == id) subscription_list = [] for s in subscriptions: if (datetime.now().timestamp() - s.created_at.timestamp() <= (60 * 60 * 24 * 30) ): subscription_list.append( s.as_dict() ) subscription_active = ( len( subscription_list ) > 0 ) result = jsonify({ 'data' : { 'subscription_is_active' : subscription_active, 'subscriptions' : subscription_list } }) return result
def show_update(): subscription_plan = Subscription.select() return render_template('subscriptions/show_update.html', subscription_plan = subscription_plan)
def subscriptions(self, limit=50, offset=0): return (Subscription.select() .join(UserSubscription) .where(UserSubscription.user == self) .limit(limit) .offset(offset))
def subscription(): user_id = get_jwt_identity() current_user = User.get_by_id(user_id) subs_obj = Subscription.select().where( Subscription.user_id == current_user.id).order_by( Subscription.id.asc()) subs_arr = [] monthly_amount = 0 if subs_obj: for sub in subs_obj: str_amount = "" if sub.amount == 0: str_amount = None elif sub.subs_type == "yearly" and sub.amount is not None: str_amount = "RM" + str(sub.amount) + "/y" monthly_amount = monthly_amount + (sub.amount / 12) elif sub.subs_type == "monthly" and sub.amount is not None: str_amount = "RM" + str(sub.amount) + "/m" monthly_amount = monthly_amount + sub.amount elif sub.subs_type == "weekly" and sub.amount is not None: str_amount = "RM" + str(sub.amount) + "/w" monthly_amount = monthly_amount + (sub.amount * 4) elif sub.subs_type == "daily" and sub.amount is not None: str_amount = "RM" + str(sub.amount) + "/d" monthly_amount = monthly_amount + (sub.amount * 30) temp_date = sub.next_payment if (sub.next_payment - date.today()).days <= 0 and sub.paid == True: if sub.subs_type == "daily": new_next_payment = add_days(temp_date, int(sub.frequency)) query = Subscription.update( payment_date=temp_date, next_payment=new_next_payment, paid=False, due=False).where(Subscription.id == sub.id) query.execute() elif sub.subs_type == "weekly": new_next_payment = add_weeks(temp_date, int(sub.frequency)) query = Subscription.update( payment_date=temp_date, next_payment=new_next_payment, paid=False, due=False).where(Subscription.id == sub.id) query.execute() elif sub.subs_type == "monthly": new_next_payment = add_months(temp_date, int(sub.frequency)) query = Subscription.update( payment_date=temp_date, next_payment=new_next_payment, paid=False, due=False).where(Subscription.id == sub.id) query.execute() elif sub.subs_type == "yearly": new_next_payment = add_years(temp_date, int(sub.frequency)) query = Subscription.update( payment_date=temp_date, next_payment=new_next_payment, paid=False, due=False).where(Subscription.id == sub.id) query.execute() elif (sub.next_payment - date.today()).days <= 0 and sub.paid == False: query = Subscription.update(due=True).where( Subscription.id == sub.id) query.execute() 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', 'subscriptions': subs_arr, 'monthly_amount': monthly_amount, 'username': current_user.username } return jsonify(responseObj), 200 else: responseObj = { 'status': 'success but array is empty', 'subscriptions': subs_arr, 'monthly_amount': monthly_amount, 'username': current_user.username } return jsonify(responseObj), 200