summaryrefslogtreecommitdiff
path: root/app/modules/common.py
blob: 9e96f416f555f953d3ac2623a620e7dfb7cf029e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# -*- mode: python; -*-

import inspect
from flask import Blueprint, request, render_template, redirect, flash, jsonify
from flask_login import login_required, current_user

from .. import db
from .. import models
from . import forms

from wtforms import SelectField


common = Blueprint("common", __name__)


@common.route("/modules/<module>/add/<table>", methods=["GET", "POST"])
@login_required
def add_item(module, table):
    """Add new item to table accessible via module."""
    # print("db table keys are", db.metadata.tables.keys())
    if table not in db.metadata.tables.keys():
        return render_template("errors/item-not-found.html", table=table)
    form = getattr(forms, f"Add{table}")()
    if form.validate_on_submit():
        model = getattr(models, table)
        table_fields = inspect.signature(model).parameters
        form_values = {key: request.form[key] for key in table_fields}
        print(f"Ready to insert {form_values}")
        record = model(**form_values)
        db.session.add(record)
        db.session.commit()
        item_pk = model.query.order_by(model.primary_key.desc()).first().primary_key
        flash(f"Successfully added item #{item_pk} to {table} table.", "info")
        return redirect(f"/modules/{module}")
    return render_template("modules/add-item.html", table=table, form=form)


@common.route("/modules/<module>/edit/<table>/<int:pk>", methods=["GET", "POST"])
@login_required
def edit_item(module, table, pk):
    """Edit existing item in table accessible via module."""
    if table not in db.metadata.tables.keys():
        return render_template("errors/item-not-found.html", table=table)
    model = getattr(models, table)
    item = model.query.filter_by(primary_key=pk).first()
    # Instantiate form with selected item's field values.
    form = getattr(forms, f"Add{table}")(**item.__dict__)
    if form.validate_on_submit():
        table_fields = inspect.signature(model).parameters
        form_values = {key: request.form[key] for key in table_fields}
        print(f"Ready to update {form_values}")
        model.query.filter_by(primary_key=pk).update(form_values)
        db.session.commit()
        flash(f"Successfully edited item #{pk} in {table} table.", "info")
        return redirect(f"/modules/{module}")
    return render_template("modules/edit-item.html", table=table, pk=pk, form=form)


@common.route("/modules/<module>/delete/<table>/<int:pk>", methods=["POST"])
@login_required
def delete_item(module, table, pk):
    """Delete item with Primary Key = pk from table in module."""
    model = getattr(models, table)
    record = model.query.filter_by(primary_key=pk).first()
    db.session.delete(record)
    db.session.commit()
    flash(f"Successfully removed item #{pk} from {table} table.", "info")
    return redirect(f"/modules/{module}")


@common.route("/modules/settings")
@login_required
def settings():
    return render_template("modules/settings.html", user=current_user)
Copyright 2019--2024 Marius PETER