[Flask] Simple ERP for a farm.
1
# -*- mode: python; -*-
2
3
4
from flask import (
5
Blueprint,
6
render_template,
7
send_file,
8
request,
9
redirect,
10
flash,
11
url_for,
12
jsonify,
13
abort,
14
)
15
from flask_login import login_required, current_user
16
17
18
# from datetime import datetime
19
import inspect
20
21
from . import db
22
23
modules = ("modules",__name__)
24
25
26
from .models import *
27
from .forms import *
28
29
30
@modules.route("/modules")
31
defmodules_home():
32
return ("/index")
33
34
35
@modules.route("/add-<item>",methods=["GET","POST"])
36
@login_required
37
defadd_item(item):
38
"""Add a new item to a corresponding database table.
39
40
The item must match a database modelclassname(table)Thenwe
41
matchthemodelclassattributeswithrequestformvalues
42
43
44
print("db table keys are",db.metadata.tables.keys())
45
ifitemcapitalize()notindbmetadatatableskeys():
46
return ("errors/item-not-found.html",item=)
47
if request. == "GET":
48
form = globals()[f"Add{.()}"]()
49
return ("add-item.html",item=,form=)
50
if request. == "POST":
51
table = globals()[item.()]
52
table_fields = inspect.().
53
form_values = {key: request.[] for key in table_fields}
54
print(f"Ready to insert {}")
55
record = (**)
56
db..()
57
db..()
58
item_pk = table..(..()).().
59
(
60
f"Successfully added item #{} to {...()} table.",
61
"info",
62
)
63
return ("/modules")
64
65
66
@modules.route("/delete-<pk>-from-<table>",methods=["POST"])
67
@login_required
68
defdelete_item(pk,table):
69
"""Delete item with Primary Key = pk from corresponding database
70
table.
71
"""
72
model = globals()[table]
73
record = model..(primary_key=).()
74
db..()
75
db..()
76
(f"Successfully removed item #{} from {} table.","info")
77
return ("/modules")
78
79
80
@modules.route("/modules/customers")
81
@login_required
82
defcustomers():
83
modules = Module..()
84
customers = Customer..(..())
85
module = "customers"
86
(f"Successfully accessed module {}.","info")
87
(f"Still fighting against styling in {}.","error")
88
return (
89
f"modules/{}.html",
90
modules=,
91
customers=,
92
)
93
94
95
@modules.route("/modules/products")
96
@login_required
97
defproducts():
98
modules = Module..()
99
products = Product..(..())
100
module = "products"
101
(f"Successfully accessed module {}.","info")
102
(f"Still fighting against styling in {}.","error")
103
return (
104
f"modules/{}.html",
105
modules=,
106
products=,
107
)
108
109
110
@modules.route("/modules/ferti")
111
@login_required
112
defferti():
113
modules = Module..()
114
logs = Fertilog..(..()).()
115
targets = Fertitarget..()
116
module = "ferti"
117
(f"Latest target is {}.","info")
118
(f"Still fighting against styling in {}.","error")
119
return (
120
f"modules/{}.html",
121
modules=,
122
logs=,
123
)
124
125
126
# @main.route("/add-invoice", methods=["GET", "POST"])
127
# def add_invoice():
128
# form = AddInvoice()
129
# if request.method == "GET":
130
# return render_template("add-invoice.html")
131
132
133
# @main.route("/preview-invoice", methods=["GET", "POST"])
134
# def preview_invoice():
135
# if request.method == "GET":
136
# return render_template("preview-invoice.html")
137