-
Notifications
You must be signed in to change notification settings - Fork 0
/
temp.py
353 lines (322 loc) · 12.1 KB
/
temp.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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
from solana.rpc.api import Client
from spl.token.client import Token
from solders.pubkey import Pubkey
from solders.keypair import Keypair
from flask_sqlalchemy import SQLAlchemy
from flask_login import LoginManager, UserMixin, login_user, logout_user, login_required, current_user
from markupsafe import escape
from flask import redirect
from flask import session
from flask import request
from flask import render_template
from flask import Flask
from flask import blueprints
import flask
from werkzeug.utils import secure_filename
import logging
import colorlog
import json
from datetime import date
import time
import secrets
import requests
import hashlib
import matplotlib
matplotlib.use('Agg')
# Use the non-GUI backend to generate plots in order to fix the bug where system will crash when
# importing matplotlib
import matplotlib.pyplot as plt
import os
from ftplib import FTP
from pathlib import Path
from extensions.language_manager import LanguageManager, _
logger.info("[!] Server started or reloaded")
# requests.post("http://localhost:80/erp_admin_ntfy",
# data="Server started or reloaded".encode(encoding='utf-8'))
# Welcome page / Homepage
# @app.route("/")
# def welcome_page():
# return render_template('html/welcome.html')
#
#
# # Login Page
#
# @login_required
# @app.route("/dashboard", methods=['GET', 'POST'])
# def dashboard():
# # checking if the current user is authenticated and NOT give them access to the dashboard containing classified
# # datas
# if current_user.is_authenticated:
# return render_template("html/dashboard.html")
# else:
# return redirect("/")
#
#
# @login_required
# @app.route("/admin")
# def admin_panel():
# global is_admin, user, email
# is_admin = False
# with open('datas/settings.json', 'r') as s_f:
# settings = json.loads(s_f.read())
# # admin check
# if user and settings["Admin"] == email:
# is_admin = True
# return render_template("html/admin_dashboard.html", user_list=users_online)
# else:
# # requests.post("http://localhost:80/erp_admin_ntfy",
# # data="Illegal login attempt to admin panel detected".encode(encoding='utf-8'))
# logout_user()
# return redirect("/", 403)
# s_f.close()
#
#
# @app.route('/user_manager', methods=['GET', 'POST'])
# def user_manager():
# if is_admin:
# # button detection
# user_id = request.form.get('user_id')
# action = request.form.get('action')
# submit = request.form.get('submit')
#
# all_users = Employee.query.all()
# for l_users in all_users:
# l_users.is_online = l_users.Email in users_online
# if user_id:
# if action == 'kick':
# return kick_user(user_id)
# elif action == 'edit':
# return redirect('/edit_user/' + user_id)
# elif action == 'delete':
# return delete_user(user_id)
# else:
# return redirect("/user_manager")
# return render_template('html/user_manager.html', users=all_users)
# else:
# # requests.post("http://localhost:80/erp_admin_ntfy",
# # data="Illegal login attempt to admin panel detected".encode(encoding='utf-8'))
# logout_user()
# return redirect("/")
#
#
# def kick_user(user_id):
# user_k = Employee.query.get(user_id)
# if user_k:
# user_k.session_token = None
# db.session.commit()
# logger.info("[*] Kicked user with ID: " + str(user_id))
# return redirect("/user_manager")
#
#
# @app.route('/edit_user/<int:user_id>', methods=['GET', 'POST'])
# @login_required
# def edit_user(user_id):
# e_user = Employee.query.get(user_id)
# if request.method == 'POST':
# db.session.add(e_user)
# e_user.Name = request.form.get('name')
# e_user.Dept = request.form.get('dept')
# e_user.Email = request.form.get('email')
# e_user.Password = hashlib.sha256(request.form.get('password').encode('utf-8')).hexdigest()
# e_user.session_token = None
# db.session.commit()
# return redirect('/user_manager')
# return render_template('html/edit_user.html', user=e_user)
#
#
# def delete_user(user_id):
# d_user = Employee.query.get(user_id)
# d_user.Password = 'invalid'
# d_user.session_token = None
# db.session.commit()
# logger.info("[*] Deleted user with ID: " + str(user_id))
# return redirect("/user_manager")
#
#
# @app.route('/create_user', methods=['GET', 'POST'])
# @login_required
# # creating new users
# def create_user():
# if request.method == 'POST':
# id = request.form.get('employeeId')
# name = request.form.get('name')
# dept = request.form.get('dept')
# email = request.form.get('email')
# # hashing the users password before it enters the database
# password = hashlib.sha256(str(request.form.get('password')).encode('utf-8')).hexdigest()
# new_user = Employee(EmployeeID=id, Name=name, Dept=dept, Email=email, Password=password, session_token=None)
# db.session.add(new_user)
# db.session.commit()
# return redirect('/user_management') # Redirect to a user management page
# return render_template('/html/create_user.html')
#
#
# @app.before_request
# def validate_session_token():
# global email
# # check if the user is authenticated
# if current_user.is_authenticated:
# db_token = Employee.query.filter_by(EmployeeID=current_user.get_id()).first().session_token
# session_token = session.get('user_token')
# # validating the user session token
# if not session_token or session_token != db_token:
# # requiring the user to re-login if user's session token is invalid
# logout_user()
# logger.warning("[!] " + str(email) + "\'s session token is invalid!")
# return redirect('/login')
# logger.info("[*] " + str(email) + "\'s session token is valid")
#
def create_space_chart(used_space, free_space):
labels = 'Used Space', 'Free Space'
sizes = [used_space, free_space]
colors = ['red', 'green']
explode = (0.1, 0) # explode the 1st slice (i.e., 'Used Space')
plt.figure(figsize=(6, 4))
plt.pie(sizes, explode=explode, labels=labels, colors=colors, autopct='%1.1f%%', shadow=True, startangle=140)
plt.axis('equal') # Equal aspect ratio ensures that pie is drawn as a circle.
# Save the plot as a .png file
plt.savefig('static/inventory_space.png')
plt.close()
@app.route('/inventory', methods=["GET", "POST"])
@login_required
def inventory():
items = Inventory.query.all()
item_in = []
item_out = []
for i in range(len(items)):
if items[i]["Out_Date"] is None:
item_in.append(items[i])
else:
item_out.append(items[i])
total_items_in = len(items_in)
total_capacity = 1000
used_space = sum(item.Quantity for item in items_in)
free_space = total_capacity - used_space
# generating the chart that's going to be displayed on the webpage
create_space_chart(used_space, free_space)
item_id = request.form.get('item_id')
action = request.form.get('action')
submit = request.form.get('submit')
all_items = Inventory.query.all()
if item_id:
if action == 'i_edit':
return redirect("/edit_item/" + str(item_id))
elif action == 'i_check_out':
return check_out_item(item_id)
elif action == 'o_edit':
return redirect("/edit_item/" + str(item_id))
elif action == 'o_del':
Inventory.query.filter(Inventory.Item_Id == item_id).delete()
return redirect("/inventory")
else:
return redirect("/inventory")
return render_template('html/inventory.html', items=items_in, o_items=item_out, total_items=total_items_in,
available_space=free_space)
def check_out_item(item_id):
c_item = Inventory.query.get(item_id)
c_item.OutDate = str(log_name)
db.session.commit()
logger.info("[*] Checked out item with ID: " + str(item_id))
return redirect("/inventory")
@app.route('/edit_item/<int:item_id>', methods=['GET', 'POST'])
@login_required
def edit_item(item_id):
e_item = Inventory.query.get(item_id)
if request.method == 'POST':
db.session.add(e_item)
if request.form.get('name'):
e_item.I_Name = request.form.get('name')
if request.form.get('disc'):
e_item.I_Disc = request.form.get('disc')
if request.form.get('in_date'):
e_item.InDate = request.form.get('in_date')
if request.form.get('warehouse_num'):
e_item.Warehouse_Num = request.form.get('warehouse_num')
if request.form.get('quantity'):
e_item.Quantity = request.form.get('quantity')
db.session.commit()
return redirect('/inventory')
return render_template('html/edit_item.html', item=e_item)
@app.route('/check_in_item', methods=['GET', 'POST'])
@login_required
# creating new users
def check_in_item():
if request.method == 'POST':
id = request.form.get('ItemId')
name = request.form.get('i_name')
disc = request.form.get('i_disc')
date = str(log_name)
od = None
warehouse_num = request.form.get('warehouse_num')
quantity = request.form.get('quantity')
new_item = Inventory(ItemID=id, I_Name=name, I_Disc=disc, InDate=date, OutDate=od,
Warehouse_Num=warehouse_num,
Quantity=quantity)
db.session.add(new_item)
db.session.commit()
return redirect('/inventory') # Redirect to inventory page
return render_template('/html/item_in.html')
@app.route("/sales")
@login_required
def sales():
pass
@app.route("/purchases")
@login_required
def purchases():
pass
@app.route('/report', methods=['GET', 'POST'])
@login_required
def report():
# global email
# if request.method == 'POST':
# title = request.form['title']
# description = request.form['description']
# severity = request.form['severity'] # 'info', 'warning', or 'fatal'
# # Prepare the card data for Codecks
# card_data = {
# 'content': str(title) + '\n\n' + str(description),
# 'severity': severity,
# "userEmail": str(email)
# }
#
# # Codecks API URL and headers
# url = 'https://api.codecks.io/user-report/v1/create-report?token=rt_ddrASKCqSAXfN9FJsJwOuU4n'
# headers = {
# 'Content-Type': 'application/json'
# }
#
# # Send the request to Codecks
# print('sending...')
# response = requests.post(url, json=json.dumps(card_data), headers=headers)
# print('sent')
# print(response)
# if response.status_code == 201:
# flash('Your request has been submitted successfully!', 'success')
# return redirect('/dashboard')
# else:
# return redirect('/report')
return render_template('html/report.html')
# data model definition
# model for employees
class Employee(UserMixin, db.Model):
__tablename__ = 'employee'
EmployeeID = db.Column(db.Integer, primary_key=True)
Name = db.Column(db.String(20), nullable=True)
Dept = db.Column(db.String(30), nullable=True)
Email = db.Column(db.String(30), unique=True, nullable=True)
Password = db.Column(db.String(90), nullable=True)
session_token = db.Column(db.String(100), nullable=True)
def get_id(self):
return str(self.EmployeeID)
# model for inventory
class Inventory(UserMixin, db.Model):
__tablename__ = 'inventory'
ItemID = db.Column(db.Integer, primary_key=True, unique=True, nullable=False)
I_Name = db.Column(db.String(20), nullable=True)
I_Disc = db.Column(db.String(500), nullable=True)
InDate = db.Column(db.String(10), nullable=True)
OutDate = db.Column(db.String(10), nullable=True)
Warehouse_Num = db.Column(db.Integer, nullable=True)
Quantity = db.Column(db.Integer, nullable=True)
def get_id(self):
return str(self.EmployeeID)