Exemple #1
0
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
Exemple #2
0
def status(id):
    user_id = get_jwt_identity()
    sub = Subscription.get_by_id(id)
    query = Subscription.update(paid=not sub.paid).where(Subscription.id == id)

    if query.execute():
        subs_obj = Subscription.select().where(
            Subscription.user_id == user_id).order_by(Subscription.id.asc())
        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', 'subscriptions': subs_arr}

        return jsonify(responseObj), 200

    else:
        responseObj = {
            'status': 'failed',
            'message': 'Failed to change your paid status'
        }

        return jsonify(responseObj), 400
Exemple #3
0
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