-
Notifications
You must be signed in to change notification settings - Fork 0
/
routes.py
executable file
·181 lines (147 loc) · 5.31 KB
/
routes.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
# IMPORTS
from database import sql
from flask import jsonify
import json
import time
from helpers.functions import isSpace
# to prevent various requests at same time
requesting = False
def wait(function):
def wrapper(*args, **kwargs):
global requesting
while requesting:
time.sleep(0.5)
requesting = True
result = function(*args, **kwargs)
requesting = False
return result
return wrapper
# to send all the data from the detailed view
@wait
def selectElements(section, userID):
query = "SELECT * FROM {}View WHERE userID={}".format(section.lower(), userID)
result = sql.run(query)
try:
resultJSON = json.loads(result)
except:
resultJSON = result
return jsonify(resultJSON)
# to send the graph data
@wait
def selectGraph(section, userID):
query = "SELECT * FROM {}Graph WHERE userID={}".format(section, userID)
result = sql.run(query)
try:
resultJSON = json.loads(result)
except:
resultJSON = result
return jsonify(resultJSON)
# to send info of one element
@wait
def selectInfo(section, elementID):
query = "SELECT * FROM {}View WHERE id={}".format(section.lower(), elementID)
result = sql.run(query)
try:
resultJSON = json.loads(result)
except:
resultJSON = result
return jsonify(resultJSON)
# to send all gruped elements of the table
@wait
def selectGroupedElements(section, userID):
query = "SELECT * FROM grouped{}View WHERE userID={}".format(section.capitalize(), userID)
result = sql.run(query)
try:
resultJSON = json.loads(result)
except:
resultJSON = result
return jsonify(resultJSON)
# to send all elements of the table
@wait
def selectToSelect(select, userID):
query = "SELECT * FROM {}SelectView WHERE userID={}".format(select, userID)
result = sql.run(query)
try:
resultJSON = json.loads(result)
except:
resultJSON = result
return jsonify(resultJSON)
# to send all the equal elements to another
@wait
def selectEqualElements(section, userID, element):
if section.lower() == "sales":
client = " IS NULL" if isSpace(element["client"]) else " = '{}'".format(element['client'])
date = " IS NULL" if isSpace(element["date"]) else " = '{}'".format(element['date'])
query = "SELECT id, product, quantity, obtained, profit, discount, date, type, client, clientID, productID, orderID FROM salesView WHERE client{} and date{} and userID={}".format(
client, date, userID)
elif section.lower() == "products":
name = " IS NULL" if isSpace(element["name"]) else " = '{}'".format(element['name'])
char1 = " IS NULL" if isSpace(element["char1"]) else " = '{}'".format(element['char1'])
char2 = " IS NULL" if isSpace(element["char2"]) else " = '{}'".format(element['char2'])
query = "SELECT id, `order`, name, char1, char2, initialStock, available, sold, retailPrice, wholesalePrice, purchasePrice, obtained, profit, invested, orderID, userID FROM productsView WHERE name{} and char1{} and char2{} and userID={}".format(
name, char1, char2, userID)
result = sql.run(query)
try:
resultJSON = json.loads(result)
except:
resultJSON = result
return jsonify(resultJSON)
# to insert one or more elements
@wait
def insertElements(section, userID, elements):
query = "INSERT INTO {} ".format(section.lower())
query += "(userID, "
for item in elements[0]:
query += "{},".format(item)
query = query.rstrip(",") + ") VALUES "
for element in elements:
query += "({}, ".format(userID)
for value in element.values():
value = 'NULL' if isSpace(value) else "'{}'".format(value)
query += "{},".format(value)
query = query.rstrip(",") + "),"
query = query.rstrip(",")
result = sql.run(query, fetch=False)
return result
# to delete one ore more elements
@wait
def deleteElements(section, ids):
if len(ids) > 1:
query = "DELETE FROM {} WHERE id IN {}".format(section.lower(), ids)
else:
query = "DELETE FROM {} WHERE id={}".format(section.lower(), ids[0])
result = sql.run(query, fetch=False)
return result
# to update one element
@wait
def updateElement(section, updatedElement):
query = "UPDATE {} SET ".format(section.lower())
for key, value in updatedElement.items():
newValue = "NULL" if isSpace(value) else "'{}'".format(value)
query += "{}={},".format(key, newValue)
query = query.rstrip(",")
query += " WHERE id={}".format(updatedElement["id"])
result = sql.run(query, fetch=False)
return result
# to sign in
@wait
def login(user):
query = "SELECT id as userID, username FROM users WHERE username='{}' and password='{}'".format(user["username"], user["password"])
result = sql.run(query, fetch=True)
try:
resultJSON = json.loads(result)[0]
return resultJSON
except IndexError:
return {
"username": False,
"userID": False
}
except Exception as e:
return result
#to sign up
@wait
def signup(user):
query = "INSERT INTO users VALUES (null, '{}', '{}')".format(user['username'], user['password'])
result = sql.run(query, fetch=False)
result['username'] = user['username']
return result