-
Notifications
You must be signed in to change notification settings - Fork 1
/
encas.py
executable file
·254 lines (214 loc) · 7.51 KB
/
encas.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
#!/usr/bin/python
# Encas Sales Management Server
# Copyright 2013 - Hugo Caille
#
# This file is part of Encas.
#
# Encas is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Encas is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Encas. If not, see <http://www.gnu.org/licenses/>.
from flask import Flask, request, url_for, redirect, render_template
from common import FlaskConfig, file_handler, convert
app = Flask(__name__)
app.config.from_object(FlaskConfig)
app.logger.addHandler(file_handler)
from model.database import db
db.init_app(app)
from flask.ext.login import login_required, login_user, logout_user, current_user
from login import login_manager, UserHandler, login_required_api, admin_required
login_manager.login_view = 'login'
login_manager.init_app(app)
from model import Account, Transaction, User
from errors import errorhandler, ApiError, MissingFieldsError
import forms
@app.route('/')
@login_required
def home():
return render_template("checkout.html")
@app.route('/account')
@login_required
def accounts():
return render_template("account.html")
@app.route('/admin')
@login_required
def admin():
return render_template("admin.html")
@app.route('/login', methods=['GET', 'POST'])
def login():
form = forms.LoginForm()
if forms.SellerLoginForm().validate_on_submit():
login_user(UserHandler(seller=True))
return redirect(request.args.get("next") or url_for("home"))
elif form.validate_on_submit():
user = User.login(form.username.data, form.password.data)
if user is not None:
login_user(UserHandler(id=user.id))
else:
return render_template("login.html", credentials=True)
return redirect(request.args.get("next") or url_for("home"))
else:
credentials = False
if len(form.errors) > 0:
credentials = True
return render_template("login.html", credentials=credentials)
@app.route('/logout')
@login_required
def logout():
logout_user()
return redirect('/')
@app.route('/account/list/<filter>', methods=['GET'])
@login_required_api
@errorhandler
def listAccounts(filter):
if filter == "active":
accounts = Account.list("active")
elif filter == "deleted":
accounts = Account.list("deleted")
elif filter == "debts":
accounts = Account.list("debts")
elif filter == "staff":
accounts = Account.list("staff")
else:
raise ApiError("Wrong filter, must be one of these: active, deleted, debts, staff")
for account in accounts:
account.balance = str(account.balance)
return [acc.serialize() for acc in accounts]
@app.route('/account/<account_id>', methods=['GET'])
@login_required_api
@errorhandler
def getAccount(account_id):
account_id = convert(int, account_id)
return Account.get(account_id).serialize()
@app.route('/account/number/<number>', methods=['GET'])
@login_required_api
@errorhandler
def getAccountByNumber(number):
number = convert(int, number)
return Account.getByNumber(number).serialize()
@app.route('/account/search/<firstname>', methods=['GET'])
@login_required_api
@errorhandler
def searchAccount(firstname):
return [acc.serialize() for acc in Account.search(firstname)]
@app.route('/account/create', methods=['POST'])
@login_required_api
@errorhandler
def createAccount():
form = forms.AccountCreationForm()
if form.validate_on_submit():
account = Account.create(form.firstname.data, form.lastname.data, form.promo.data, form.number.data)
if form.balance.data is not None and form.balance.data != 0:
Transaction.add(account.id, form.balance.data)
return account.serialize()
else:
raise MissingFieldsError(form.errors.keys())
@app.route('/account/<id>/edit', methods=['POST'])
@login_required_api
@errorhandler
def editAccount(id):
id = convert(int, id)
form = forms.AccountEditionForm()
if form.validate_on_submit():
fields = {'firstname' : form.firstname.data, 'lastname' : form.lastname.data, 'promo' : form.promo.data}
return Account(id=id).edit(fields).serialize()
else:
raise MissingFieldsError(form.errors.keys())
@app.route('/account/<id>/staff', methods=['POST'])
@login_required_api
@errorhandler
@admin_required
def staffAccount(id):
id = convert(int, id)
form = forms.StaffStatusForm()
if form.validate_on_submit():
return Account(id).staff(form.staff.data).serialize()
else:
raise MissingFieldsError(form.errors.keys())
@app.route('/account/<id>/delete', methods=['POST'])
@login_required_api
@errorhandler
@admin_required
def deleteAccount(id):
id = convert(int, id)
return Account(id=id).delete().serialize()
@app.route('/account/<id>/balance', methods=['GET'])
@login_required_api
@errorhandler
def getBalance(id):
id = convert(int, id)
return {'balance' : str(Transaction.getBalance(id))}
@app.route('/account/<id>/calculate', methods=['GET'])
@login_required_api
@errorhandler
def calculateBalance(id):
id = convert(int, id)
return {'balance' : str(Transaction.calculateBalance(id))}
@app.route('/account/<int:account_id>/transactions', methods=['GET'])
@login_required_api
@errorhandler
def getTransactions(account_id):
transactions = Transaction.getByAccount(account_id, 5)
return [tr.serialize() for tr in transactions]
@app.route('/account/<int:account_id>/transactions/all', methods=['GET'])
@login_required_api
@errorhandler
def getAllTransactions(account_id):
transactions = Transaction.getByAccount(account_id, None)
return [tr.serialize() for tr in transactions]
@app.route('/transaction/list', methods=['GET'])
@login_required_api
@errorhandler
def listTransaction():
return [tr.serialize() for tr in Transaction.list()]
@app.route('/transaction/add', methods=['POST'])
@login_required_api
@errorhandler
def addTransaction():
form = forms.TransactionAddForm()
if form.validate_on_submit():
return Transaction.add(form.account_id.data, form.cash.data).serialize()
else:
raise MissingFieldsError(form.errors.keys())
@app.route('/transaction/<id>/revoke', methods=['POST'])
@login_required_api
@errorhandler
def revokeTransaction(id):
id = convert(int, id)
return Transaction(id).revoke().serialize()
@app.route('/user/list', methods=['GET'])
@login_required_api
@errorhandler
def userList():
return [user.serialize() for user in User.list()]
@app.route('/user/admin/create', methods=['POST'])
@login_required_api
@errorhandler
@admin_required
def adminCreation():
form = forms.UserCreationForm()
if form.validate_on_submit():
user = User.create(form.username.data, form.password.data)
User(user.id).makeAdmin()
return user.serialize()
else:
raise MissingFieldsError(form.errors.keys())
@app.route('/user/<id>/remove', methods=['POST'])
@login_required_api
@errorhandler
@admin_required
def userRemove(id):
id = convert(int, id)
if id == current_user.get_id():
raise ApiError("You can't delete your own account.")
return User(id).remove().serialize()
if __name__ == '__main__':
app.run()