-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbudayaKB_controller.py
More file actions
185 lines (154 loc) · 8.16 KB
/
budayaKB_controller.py
File metadata and controls
185 lines (154 loc) · 8.16 KB
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
from budayaKB_model import BudayaCollection
from flask import Flask, request, render_template
from flask_wtf import FlaskForm
from flask_wtf.file import FileRequired
from wtforms import StringField, SubmitField, SelectField, FileField
from wtforms.validators import InputRequired, URL
# Set some required config for flask
app = Flask(__name__)
app.secret_key = "tp4"
# Initialize model object
database = BudayaCollection()
class ImportForm(FlaskForm):
"""Form used for import function"""
filename = FileField('File Name', validators=[FileRequired()], render_kw={'class': "file-input"})
submit = SubmitField('Import', render_kw={'class': "button is-fullwidth is-hover is-primary "})
class EditForm(FlaskForm):
"""Form used for add and change function"""
name = StringField('Name', validators=[InputRequired()], render_kw={'class': "input"})
type = StringField('Type', validators=[InputRequired()], render_kw={'class': "input"})
prov = StringField('Province', validators=[InputRequired()], render_kw={'class': "input"})
url = StringField('Reference URL', validators=[InputRequired(), URL()], render_kw={'class': "input"})
submit = SubmitField('Submit', render_kw={'class': "button is-fullwidth is-hover is-primary"})
class SearchForm(FlaskForm):
"""Form used for search function"""
option = SelectField(choices=[('name', 'Name'), ('type', 'Type'), ('prov', 'Province')], validators=[InputRequired()], render_kw={'class': "input"})
searched = StringField(validators=[InputRequired()], render_kw={'class': "input"})
submit = SubmitField('Search', render_kw={'class': "button is-hover is-primary"})
class DeleteForm(FlaskForm):
"""Form used for delete function"""
deleted = StringField('Name', validators=[InputRequired()], render_kw={'class': "input"})
submit = SubmitField('Delete', render_kw={'class': "button is-fullwidth is-hover is-danger"})
class StatisticsForm(FlaskForm):
"""Form used for statistics function"""
shown = SelectField(choices=[('all', 'All'), ('type', 'Type'), ('prov', 'Province')], validators=[InputRequired()], render_kw={'class':'input'})
submit = SubmitField('Show', render_kw={'class': 'button is-fullwidth is-hover is-primary'})
class ExportForm(FlaskForm):
"""FOrm used for export function"""
filename = StringField("Filename", validators=[InputRequired()], render_kw={'class': "input"})
submit = SubmitField('Export', render_kw={'class': "button is-hover is-primary is-fullwidth"})
# Render out the default HTML, index.html
@app.route('/')
def index():
return render_template("index.html")
# Implementation of import function
@app.route('/import.html', methods=['GET', 'POST'])
def import_data():
import_form = ImportForm()
if request.method == "GET":
return render_template("import.html", form=import_form)
elif request.method == "POST" and import_form.validate_on_submit():
filename = import_form.filename.data
database.importFromCSV(filename.filename)
data = len(database.collection)
return render_template("import.html", data=data, fname=filename.filename, form=import_form)
# Implementation of statistics
@app.route('/statistics.html', methods=['GET', 'POST'])
def statistics():
statistics_form = StatisticsForm()
if request.method == "GET":
return render_template("statistics.html", form=statistics_form)
elif request.method == "POST" and statistics_form.validate_on_submit():
shown = statistics_form.shown.data
total_type = len(database.statByType())
total_prov = len(database.statByProv())
if shown == "all":
total = len(database.collection)
return render_template("statistics.html", total=total, total_type=total_type, total_prov=total_prov, form=statistics_form)
elif shown == 'type':
stat_type = database.statByType()
return render_template("statistics.html", type=stat_type,total_type=total_type, form=statistics_form)
elif shown == 'prov':
stat_prov = database.statByProv()
return render_template("statistics.html", prov=stat_prov, total_prov=total_prov, form=statistics_form)
# Implementation of search function
@app.route('/search.html', methods=['GET', 'POST'])
def search():
search_form = SearchForm()
if request.method == "GET":
return render_template('search.html', form=search_form)
elif request.method == "POST" and search_form.validate_on_submit():
option = search_form.option.data
if option == "name":
if search_form.searched.data == "*":
data = database.collection.values()
return render_template('search.html', form=search_form, data=data)
else:
data = database.searchByName(search_form.searched.data)
return render_template('search.html', form=search_form, data=data)
elif option == "type":
data = database.searchByType(search_form.searched.data)
return render_template('search.html', form=search_form, data=data)
elif option == "prov":
data = database.searchByProv(search_form.searched.data)
return render_template('search.html', form=search_form, data=data)
# Implementation of add function
@app.route('/add.html', methods=['GET', 'POST'])
def add():
add_form = EditForm()
if request.method == "GET":
return render_template('add.html', form=add_form)
elif request.method == "POST" and add_form.validate_on_submit():
name = add_form.name.data
type = add_form.type.data
prov = add_form.prov.data
url = add_form.url.data
if database.add(name, type, prov, url) == 1:
return render_template('add.html', name=name, form=add_form)
elif database.add(name, type, prov, url) == "invalid":
return render_template('add.html', form=add_form, error="At least one letter is required")
else:
return render_template('add.html', name=name, form=add_form, why="is found, cant make a duplicate object")
return render_template("add.html", error="Data is invalid! Please try again!", form=add_form)
# Implementation of change function
@app.route('/change.html', methods=['GET', 'POST'])
def change():
change_form = EditForm()
if request.method == "GET":
return render_template('change.html', form=change_form)
elif request.method == "POST" and change_form.validate_on_submit():
name = change_form.name.data
type = change_form.type.data
prov = change_form.prov.data
url = change_form.url.data
if database.change(name, type, prov, url) == 1:
return render_template('change.html', name=name, form=change_form)
elif database.add(name, type, prov, url) == "invalid":
return render_template('add.html', form=change_form, error="At least one letter is required")
else:
return render_template('change.html', name=name, form=change_form, why="is not found, cant change a null object")
return render_template("change.html", error="Data is invalid! Please try again!", form=change_form)
# Implementation of delete function
@app.route('/delete.html', methods=['GET', 'POST'])
def delete():
delete_form = DeleteForm()
if request.method == "GET":
return render_template('delete.html', form=delete_form)
elif request.method == "POST" and delete_form.validate_on_submit():
deleted = delete_form.deleted.data
if database.delete(deleted) == 1:
return render_template('delete.html', deleted=deleted, form=delete_form)
else:
return render_template('delete.html', deleted=deleted, why="is not found, can't delete a null object.", form=delete_form)
@app.route('/export.html', methods=['GET', 'POST'])
def export():
export_form = ExportForm()
if request.method == "GET":
return render_template("export.html", form=export_form)
if request.method == "POST" and export_form.validate_on_submit():
filename = export_form.filename.data
database.exportToCSV(filename)
return render_template("export.html", form=export_form, filename=filename)
# Run main app
if __name__ == "__main__":
app.run(debug=True)