-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathviews.py
128 lines (100 loc) · 4.07 KB
/
views.py
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
from flask import current_app, render_template, request, redirect, url_for
from services.service import *
# Context Class holds the context of the application
class Context():
def __init__(self):
self.isEdit = False
self.selectedID = 0
self.selectedIDColumn = 0
self.page = 1
#Table class is the parent class for all the tables
class Table():
def __init__(self, type, service, data_class):
self.type = type
self.service = service
self.data_class = data_class
self.page = 1
self.sort_by = None
def get_table(self, page):
service = self.service()
if page <= 0:
return redirect(url_for(self.type+"_page", page=1))
self.sort_by = request.args.get("sortby")
data = service.get_data(page, self.sort_by)
self.page = page
context = Context()
context.page = self.page
return render_template("generic_list.html", title=self.type, table=data, context=context)
def add_row(self):
row = []
columns = self.data_class.getColumns()
for column in columns:
if column in self.data_class.getNonKeyColumns():
row.append(request.form[column])
else:
row.append(None)
obj = self.data_class(row)
service = self.service()
service.add_row(obj)
return redirect(url_for(self.type + "_page", page=self.page))
def update_row(self):
idString = request.args.get("update_id")
idString = idString.replace("'", "")
id = idString.split("[")[1].split(",")[0]
idColumn = self.data_class.getColumns()[0]
if request.method == "GET":
service = self.service()
data = service.get_data(self.page, self.sort_by)
context = Context()
try:
context.isEdit = True
context.selectedID = int(id)
context.selectedIDColumn = idColumn
except:
pass
return render_template("generic_list.html", title=self.type, table=data, context=context)
else:
row = []
columns = self.data_class.getColumns()
for column in columns:
if column in self.data_class.getNonKeyColumns():
row.append(request.form[column])
else:
row.append(None)
obj = self.data_class(row)
service = self.service()
service.update_row(obj, id, idColumn)
return redirect(url_for(self.type + "_page", page=self.page))
def delete_row(self):
idString = request.args.get("delete_id")
idString.replace("'", "")
id = idString.split("[")[1].split(",")[0]
idColumn = self.data_class.getColumns()[0]
service = self.service()
service.delete_row(id, idColumn)
return redirect(url_for(self.type + "_page", page=self.page))
def search(self):
if request.method == "POST":
form = {}
for column in self.data_class.getColumns():
if request.form[column] != "":
form[column] = request.form[column]
service = self.service()
search_data = service.search_and_list(form)
if len(search_data) == 0:
row = []
for column in self.data_class.getColumns():
row.append("")
data = self.data_class(row)
search_data.append(data)
return render_template("generic_list.html", title=self.type, table=search_data, context=Context())
def get_table_goto_row(self, id):
return render_template("generic_list.html", title=self.type, table=self.service().get_data(), context=context)
def home_page():
return render_template("home.html")
class Fed_Table(Table):
def __init__(self):
super().__init__("fed_data", Fed_Service, fed_data)
class Price_Table(Table):
def __init__(self):
super().__init__("price_data", Price_Service, price_data)