Пример #1
0
class SettleDebt(Resource):
    @jwt_required
    @marshal_with(DebtModel.get_marshaller())
    def put(self, id):
        user = UserModel.find_by_username(get_jwt_identity())
        debt = DebtModel.find_by_id(id)

        if not debt:
            abort(404, message=DEBT_DOESNT_EXIST)

        debt = DebtModel.find_by_id(id)

        if not (debt.recepient.id == user.id or debt.debtee.id == user.id):
            abort(401, message=UNAUTHORIZED)

        debt.is_settled = True
        debt.persist()

        payoff = PayoffModel.find_by_id(debt.payoff_id)
        remaining_debts = DebtModel.find_unsettled_by_payoff(payoff.id)
        if not remaining_debts:
            payoff.is_settled = True
            payoff.persist()

        return debt, 200
Пример #2
0
class UserDebts(Resource):
    @jwt_required
    @marshal_with(DebtModel.get_marshaller())
    def get(self):
        user = UserModel.find_by_username(get_jwt_identity())

        debts = DebtModel.find_unsettled_by_user(user.id)

        return debts, 200
Пример #3
0
class CommunityDebts(Resource):
    @jwt_required
    @marshal_with(DebtModel.get_marshaller())
    def get(self, id):
        community = CommunityModel.find_by_id(id)
        user = UserModel.find_by_username(get_jwt_identity())

        if not community:
            abort(404, message=COMMUNIY_DOESNT_EXIST)

        if user.id not in [u.id for u in community.users]:
            abort(401, message=UNAUTHORIZED)

        debts = DebtModel.find_unsettled_by_community(id)

        return debts, 200