Skip to content

Commit

Permalink
transaction add/edit/delete
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeWent committed Jul 11, 2024
1 parent 84b7687 commit bcc3a5f
Show file tree
Hide file tree
Showing 11 changed files with 109 additions and 18 deletions.
9 changes: 9 additions & 0 deletions api/app/routes/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,15 @@ def update_transaction(
return transaction_service.update(transaction_id, transaction_update)


@transaction_router.delete("/{transaction_id}")
def delete_transaction(
transaction_id: int,
transaction_service: TransactionService = Depends(),
actor_entity: Entity = Depends(get_entity_from_token),
) -> int:
return transaction_service.delete(transaction_id)


@transaction_router.post("/{transaction_id}/tags", response_model=TagSchema)
def add_tag_to_transaction(
transaction_id: int,
Expand Down
1 change: 1 addition & 0 deletions api/app/schemas/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class TransactionCreateSchema(BaseUpdateSchema):
from_entity_id: int
amount: Decimal
currency: str
confirmed: bool | None = False

@field_validator("amount")
def amount_must_be_positive(cls, v):
Expand Down
29 changes: 21 additions & 8 deletions ui/app/controllers/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@
entity_bp = Blueprint("entity", __name__)


class EntityForm(FlaskForm):
name = StringField("Name", validators=[DataRequired()])
comment = StringField("Comment")
telegram_id = StringField("Telegram ID")
active = BooleanField("Active", default=True)
submit = SubmitField("Submit")


@entity_bp.route("/")
@token_required
def list():
Expand All @@ -19,6 +27,19 @@ def list():
)


@entity_bp.route("/add", methods=["GET", "POST"])
@token_required
def add():
form = EntityForm()
if form.validate_on_submit():
api = get_refinance_api_client()
data = form.data
data.pop("csrf_token")
api.http("POST", "entities", data=data)
return redirect(url_for("entity.list"))
return render_template("entity/add.jinja2", form=form)


@entity_bp.route("/<int:id>")
@token_required
def detail(id):
Expand All @@ -38,14 +59,6 @@ def detail(id):
)


class EntityForm(FlaskForm):
name = StringField("Name", validators=[DataRequired()])
comment = StringField("Comment")
telegram_id = StringField("Telegram ID")
active = BooleanField("Active", default=True)
submit = SubmitField("Save")


@entity_bp.route("/<int:id>/edit", methods=["GET", "POST"])
@token_required
def edit(id):
Expand Down
42 changes: 32 additions & 10 deletions ui/app/controllers/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,17 @@


class TransactionForm(FlaskForm):
amount = FloatField("Amount", validators=[DataRequired()])
from_entity_id = IntegerField("From Entity ID", validators=[DataRequired()])
to_entity_id = IntegerField("To Entity ID", validators=[DataRequired()])
amount = FloatField("Amount", validators=[DataRequired()])
currency = StringField("Currency", validators=[DataRequired()])
confirmed = BooleanField("Confirmed", default=False)
comment = StringField("Comment")
submit = SubmitField("Save")
confirmed = BooleanField("Confirmed", default=False)
submit = SubmitField("Submit")


class DeleteConfirmForm(FlaskForm):
delete = SubmitField("Delete")


@transaction_bp.route("/")
Expand Down Expand Up @@ -53,19 +57,37 @@ def add():
form = TransactionForm()
if form.validate_on_submit():
api = get_refinance_api_client()
api.http("POST", "transactions", data=form.data)
data = form.data
data.pop("csrf_token")
api.http("POST", "transactions", data=data)
return redirect(url_for("transaction.list"))
return render_template("transaction/add.jinja2", form=form)


@transaction_bp.route("/edit/<int:id>", methods=["GET", "POST"])
@transaction_bp.route("/<int:id>/edit", methods=["GET", "POST"])
@token_required
def edit(id):
api = get_refinance_api_client()
transaction_data = api.http("GET", f"transactions/{id}").json()
form = TransactionForm(obj=transaction_data)
transaction = Transaction(**api.http("GET", f"transactions/{id}").json())
form = TransactionForm(**transaction.__dict__)
if form.validate_on_submit():
form.populate_obj(transaction_data)
api.http("PATCH", f"transactions/{id}", data=transaction_data)
form.populate_obj(transaction)
api.http("PATCH", f"transactions/{id}", data=form.data)
return redirect(url_for("transaction.detail", id=id))
return render_template("transaction/edit.jinja2", form=form)
return render_template(
"transaction/edit.jinja2", form=form, transaction=transaction
)


@transaction_bp.route("/<int:id>/delete", methods=["GET", "POST"])
@token_required
def delete(id):
api = get_refinance_api_client()
transaction = Transaction(**api.http("GET", f"transactions/{id}").json())
form = DeleteConfirmForm()
if form.validate_on_submit():
api.http("DELETE", f"transactions/{id}")
return redirect(url_for("transaction.list"))
return render_template(
"transaction/delete.jinja2", form=form, transaction=transaction
)
10 changes: 10 additions & 0 deletions ui/app/templates/entity/add.jinja2
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{% extends 'base.jinja2' %}
{% from 'form.jinja2' import render_form %}

{% block title %}New entity{% endblock %}

{% block content %}
<form method="post">
{{ render_form(form) }}
</form>
{% endblock %}
1 change: 1 addition & 0 deletions ui/app/templates/entity/list.jinja2
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{% extends "base.jinja2" %}
{% block title %}Entities{% endblock %}
{% block content %}
<p><a href="{{ url_for('entity.add') }}">Add</a></p>
<ul>
{% for entity in entities %}
<li><a href="{{ url_for('entity.detail', id=entity.id) }}">{{ entity.name }}</a></li>
Expand Down
10 changes: 10 additions & 0 deletions ui/app/templates/transaction/add.jinja2
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{% extends 'base.jinja2' %}
{% from 'form.jinja2' import render_form %}

{% block title %}New transaction{% endblock %}

{% block content %}
<form method="post">
{{ render_form(form) }}
</form>
{% endblock %}
10 changes: 10 additions & 0 deletions ui/app/templates/transaction/delete.jinja2
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{% extends 'base.jinja2' %}
{% from 'form.jinja2' import render_form %}

{% block title %}Delete transaction {{ transaction.id }}?{% endblock %}

{% block content %}
<form method="post">
{{ render_form(form) }}
</form>
{% endblock %}
4 changes: 4 additions & 0 deletions ui/app/templates/transaction/detail.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,8 @@
<td>{{ transaction.confirmed }}</a></td>
</tr>
</table>
<p><a href="{{ url_for('transaction.edit', id=transaction.id) }}">Edit</a></p>
{% if not transaction.confirmed %}
<p><a href="{{ url_for('transaction.delete', id=transaction.id) }}">Delete</a></p>
{% endif %}
{% endblock %}
10 changes: 10 additions & 0 deletions ui/app/templates/transaction/edit.jinja2
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{% extends 'base.jinja2' %}
{% from 'form.jinja2' import render_form %}

{% block title %}Edit transaction {{ transaction.id }}{% endblock %}

{% block content %}
<form method="post">
{{ render_form(form) }}
</form>
{% endblock %}
1 change: 1 addition & 0 deletions ui/app/templates/transaction/list.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
{% block title %}Transactions{% endblock %}

{% block content %}
<p><a href="{{ url_for('transaction.add') }}">Add</a></p>
{{ transaction_list(transactions) }}
{% endblock %}

0 comments on commit bcc3a5f

Please sign in to comment.