Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions pycroft/lib/finance/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@
process_transactions,
ImportedTransactions,
)
from .repayment.fields import (
IBANField,
)


def user_has_paid(user: User) -> bool:
Expand Down
18 changes: 18 additions & 0 deletions pycroft/lib/finance/repayment/fields.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from typing import Any, Mapping

from marshmallow import fields, ValidationError
from schwifty import IBAN


class IBANField(fields.Field):
"""Field that serializes to a IBAN and deserializes
to a string.
"""

def _deserialize(
self, value: Any, attr: str | None, data: Mapping[str, Any], **kwargs
) -> IBAN:
try:
return IBAN(value, validate_bban=True)
except ValueError as error:
raise ValidationError("Field must be a valid IBAN.") from error
16 changes: 16 additions & 0 deletions pycroft/model/repayment.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from sqlalchemy import String, ForeignKey
from sqlalchemy.orm import relationship, Mapped, mapped_column

from pycroft.model.base import IntegerIdModel


class RepaymentRequest(IntegerIdModel):
"""A request for transferring back excess membership contributions"""

id: Mapped[int] = mapped_column(primary_key=True)
user_id: Mapped[int] = mapped_column(
ForeignKey("user.id", ondelete="CASCADE", onupdate="CASCADE")
)
beneficiary: Mapped[str] = mapped_column(nullable=False)
iban: Mapped[str] = mapped_column(String, nullable=False)
amount: Mapped[Decimal] = mapped_column(Decimal, nullable=False)
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ dependencies = [
"pydantic ~= 2.4.0",
"python-dotenv ~= 0.21.0",
"reportlab ~= 3.6.13", # usersheet generation
"schwifty ~= 2024.5.4",
"sentry-sdk[Flask] ~= 1.29.2",
"simplejson ~= 3.11.1", # decimal serialization
"SQLAlchemy >= 2.0.1",
Expand Down
26 changes: 25 additions & 1 deletion web/api/v0/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from flask import jsonify, current_app, Response
from flask.typing import ResponseReturnValue
from flask_restful import Api, Resource as FlaskRestfulResource, abort
from schwifty import IBAN
from sqlalchemy.exc import IntegrityError
from sqlalchemy import select
from sqlalchemy.orm import joinedload, selectinload, undefer, with_polymorphic
Expand All @@ -16,7 +17,7 @@

from pycroft.helpers import utc
from pycroft.helpers.i18n import Message
from pycroft.lib.finance import estimate_balance, get_last_import_date
from pycroft.lib.finance import estimate_balance, get_last_import_date, IBANField
from pycroft.lib.host import change_mac, host_create, interface_create, host_edit
from pycroft.lib.net import SubnetFullException
from pycroft.lib.swdd import get_swdd_person_id, get_relevant_tenancies, \
Expand Down Expand Up @@ -796,3 +797,26 @@ def patch(self, token: str, password: str) -> ResponseReturnValue:


api.add_resource(ResetPasswordResource, '/user/reset-password')


class RequestRepaymentResource(Resource):
def get(self, user_id: int) -> Response:
current_app.logger.warning("RECEIVED GET FOR REQUEST_REPAYMENT.")
return jsonify(False)

@use_kwargs(
{
"beneficiary": fields.Str(required=True),
"iban": IBANField(required=True),
"amount": fields.Decimal(required=True),
},
location="form",
)
def post(
self, user_id: int, beneficiary: str, iban: str, amount: Decimal
) -> Response:
current_app.logger.warning({beneficiary, iban, amount})
return jsonify({"success": True})


api.add_resource(RequestRepaymentResource, "/user/<int:user_id>/request-repayment")
5 changes: 5 additions & 0 deletions web/blueprints/finance/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1621,3 +1621,8 @@ def payment_reminder_mail() -> ResponseReturnValue:
page_title="Zahlungserinnerungen per E-Mail versenden",
form_args=form_args,
form=form)

@bp.route("/repayment_requests", methods=("GET", "POST"))
@access.require("finance_change")
def handle_repayment_requests() -> ResponseReturnValue:
return render_template()