-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
258 lines (208 loc) · 9.37 KB
/
app.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
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
#!/usr/bin/env python3
"""
Author: Joshua Singh
"""
import argparse
import configparser
from flask import Flask, jsonify, render_template, request, redirect, url_for
from flask_sqlalchemy import SQLAlchemy
import os
settings = {
"legal_disclaimer_file": "legal.txt",
"legal_disclaimer": "",
"store_name": "",
"refresh_time": 60000
}
display_config = {}
def create_app(config_filename=None):
app = Flask(__name__)
db = SQLAlchemy()
class Section(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(80), nullable=False)
description = db.Column(db.String(255), nullable=True)
available = db.Column(db.Boolean, default=True, nullable=False)
items = db.relationship('Item', backref='section', lazy=True, cascade="all, delete-orphan")
def as_dict(self):
return {
"id": self.id,
"name": self.name,
"description": self.description,
"available": self.available,
"items": self.items
}
class Price(db.Model):
id = db.Column(db.Integer, primary_key=True)
label = db.Column(db.String(50), nullable=False) # e.g., "Small", "Large"
amount = db.Column(db.Float, nullable=False)
item_id = db.Column(db.Integer, db.ForeignKey('item.id'), nullable=False)
def as_dict(self):
return {
"id": self.id,
"label": self.label,
"amount": self.amount,
"item_id": self.item_id
}
class Item(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(80), nullable=False)
description = db.Column(db.String(200), nullable=True)
section_id = db.Column(db.Integer, db.ForeignKey('section.id'), nullable=False)
available = db.Column(db.Boolean, default=True, nullable=False)
prices = db.relationship('Price', backref='item', lazy=True, cascade="all, delete-orphan")
def as_dict(self):
return {
"id": self.id,
"name": self.name,
"description": self.description,
"available": self.available,
"section_id": self.section_id,
"prices": self.prices
}
@app.route('/')
def display_menu():
sections = Section.query.all()
return render_template('display.html',
sections=sections, settings = settings,
style=display_config)
@app.route('/admin', methods=['GET', 'POST'])
def admin():
if request.method == 'POST':
if 'section_name' in request.form:
section_name = request.form['section_name']
section_description = request.form.get('section_description')
new_section = Section(name=section_name, description=section_description)
db.session.add(new_section)
db.session.commit()
elif 'item_name' in request.form:
item_name = request.form['item_name']
item_description = request.form.get('item_description')
section_id = request.form['section_id']
new_item = Item(name=item_name, description=item_description, section_id=int(section_id))
db.session.add(new_item)
db.session.commit()
return redirect(url_for('admin'))
sections = Section.query.all()
return render_template('admin.html', sections=sections)
@app.route('/edit_item/<int:item_id>', methods=['GET', 'POST'])
def edit_item(item_id):
item = Item.query.get_or_404(item_id)
if request.method == 'POST':
item.name = request.form['item_name']
item.description = request.form.get('item_description')
item.section_id = request.form['section_id']
db.session.commit()
return redirect(url_for('edit_item', item_id = item_id))
sections = Section.query.all()
return render_template('edit_item.html', item = item, sections = sections)
@app.route('/edit_section/<int:section_id>', methods=['GET', 'POST'])
def edit_section(section_id):
section = Section.query.get_or_404(section_id)
if request.method == 'POST':
section.name = request.form['section_name']
section.description = request.form.get('section_description')
db.session.commit()
return redirect(url_for('edit_section', section_id = section_id))
return render_template('edit_section.html', section = section)
@app.route('/toggle_availability/<int:item_id>', methods=['POST'])
def toggle_availability(item_id):
item = Item.query.get_or_404(item_id)
item.available = not item.available
db.session.commit()
return redirect(url_for('edit_item', item_id = item_id))
@app.route('/toggle_section/<int:section_id>', methods=['POST'])
def toggle_section(section_id):
section = Section.query.get_or_404(section_id)
section.available = not section.available
db.session.commit()
return redirect(url_for('edit_section', section_id = section_id))
@app.route('/delete_item/<int:item_id>', methods=['POST'])
def delete_item(item_id):
item = Item.query.get_or_404(item_id)
db.session.delete(item)
db.session.commit()
return redirect(url_for('admin'))
@app.route('/delete_section/<int:section_id>', methods=['POST'])
def delete_section(section_id):
section = Section.query.get_or_404(section_id)
db.session.delete(section)
db.session.commit()
return redirect(url_for('admin'))
@app.route('/edit_price/<int:item_id>', methods=['GET', 'POST'])
def edit_price(item_id):
item = Item.query.get_or_404(item_id)
if request.method == 'POST':
price_id = request.form.get('price_id')
if price_id:
price = Price.query.get_or_404(price_id)
price.label = request.form['price_label']
price.amount = request.form['price_amount']
else:
price_label = request.form['price_label']
price_amount = request.form['price_amount']
new_price = Price(label=price_label, amount=float(price_amount), item_id=item.id)
db.session.add(new_price)
db.session.commit()
return redirect(url_for('edit_price', item_id=item.id))
return render_template('edit_price.html', item=item)
@app.route('/delete_price/<int:price_id>', methods=['POST'])
def delete_price(price_id):
price = Price.query.get_or_404(price_id)
item_id = price.item_id
db.session.delete(price)
db.session.commit()
return redirect(url_for('edit_price', item_id=item_id))
def get_legal_disclosure():
if os.path.isfile(settings["legal_disclaimer_file"]) and os.access(settings["legal_disclaimer_file"], os.R_OK):
#print(legal_disclaimer_file)
with open(settings["legal_disclaimer_file"]) as file:
settings["legal_disclaimer"] = file.read()
def load_config(config_file):
print(config_file)
if os.path.isfile(config_file) and os.access(config_file, os.R_OK):
global settings
config = configparser.ConfigParser()
config.read(config_file)
settings["legal_disclaimer_file"] = config.get("Settings","legal_disclosure_location")
app.config['SQLALCHEMY_DATABASE_URI'] = f"sqlite:///{config['Settings']['database_location']}"
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.jinja_env.add_extension('jinja2.ext.do')
settings["store_name"] = config['Settings']['store_name']
settings["refresh_time"] = config['Settings']['refresh_time']
for entry in config['Display']:
#print(entry)
display_config[entry] = config['Display'][entry]
@app.route("/sections", methods=["GET"])
def get_sections():
sections = Section.query.all()
# Manually producing a Dictionary of all values from the DB
# This is NOT ideal but a workaround for experimental functionality to retrieve data as JSON
data = []
for s in sections:
d = s.as_dict()
i = []
for item in d['items']:
p = []
id = item.as_dict()
for price in id['prices']:
p.append(price.as_dict())
id['prices'] = p
i.append(id)
d['items'] = i
data.append(d)
return jsonify(data)
def main():
load_config(config_filename)
db.init_app(app)
with app.app_context():
db.create_all()
get_legal_disclosure()
main()
return app
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Cool script description here.')
parser.add_argument("-c", "--config", action='store', default="config.ini", help="path to configuration file")
parser.add_argument("-d", "--debug", action="store_true", default=False, help="Add debug content to webpage")
args = parser.parse_args()
app = create_app(args.config)
app.run(debug=args.debug)