-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.py
155 lines (111 loc) · 3.95 KB
/
utils.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
from flask import redirect, url_for, session, request
from datetime import datetime
from functools import wraps
from flask.json import jsonify
from sqlalchemy.ext.declarative import DeclarativeMeta
from sqlalchemy.sql.functions import user
from database import Database
import json
def login_required(f):
@wraps(f)
def decorator(*args, **kwargs):
if not session.get('logged'):
return redirect(url_for('auth.login', after=request.path))
return f(*args, **kwargs)
return decorator
class AlchemyEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj.__class__, DeclarativeMeta):
# an SQLAlchemy class
fields = {}
for field in [x for x in dir(obj) if not x.startswith('_') and x != 'metadata']:
data = obj.__getattribute__(field)
try:
json.dumps(data) # this will fail on non-encodable values, like other classes
fields[field] = data
except TypeError:
fields[field] = None
# a json-encodable dict
return fields
return json.JSONEncoder.default(self, obj)
def get_allFrequency():
"""
Restituisce tutte le frequenze presenti sul DB
"""
db = Database()
db_session = db.session
data = db_session.query(db.frequency).all()
data_dumped = json.dumps(data, cls=AlchemyEncoder)
db_session.close()
return data_dumped
def get_FrequencyByUsername(username):
"""
Restituisce tutte le frequenze presenti sul DB con quell'username
"""
db = Database()
db_session = db.session
data = db_session.query(db.frequency).filter(db.frequency.owner == username).all()
data_dumped = json.dumps(data, cls=AlchemyEncoder)
db_session.close()
return data_dumped
def get_Frequency(ids):
"""
Restituisce la frequenze presente sul DB con un ID specifico
"""
db = Database()
db_session = db.session
data = db_session.query(db.frequency).filter(db.frequency.id == ids).all()
data_dumped = json.dumps(data, cls=AlchemyEncoder)
db_session.close()
return data_dumped
def get_lastFrequency():
"""
Restituisce l'ultima frequenza sul DB
"""
allFrequency = get_allFrequency()
allFrequencyLoaded = json.loads(allFrequency)
lastFrequency = allFrequencyLoaded[-1]
return(int(lastFrequency['id']))
def assign_LastFrequencyNumber(username):
"""
Assegna l'ultima frequenza
"""
try:
def find_missing(lst):
return [x for x in range(1000, lst[-1]+1) if x not in lst]
frequencys = []
db = Database()
db_session = db.session
allFrequency = get_allFrequency()
allFrequencyLoaded = json.loads(allFrequency)
for a in allFrequencyLoaded:
frequencys.append(a['id'])
FrequencyMissing = find_missing(frequencys)
frequencys.sort()
if frequencys[-1] == 1999:
# TODO: Impletare che controlla le riallocabili!!!
return False
if FrequencyMissing:
lastFrequencyNumber = FrequencyMissing[0]
try:
db_session.add(db.frequency(id=lastFrequencyNumber,owner=username, reallocable=0, occuped=1))
db_session.commit()
db_session.close()
return lastFrequencyNumber
except Exception as e:
print(e)
return False
else:
lastFrequency = frequencys[-1]
lastFrequencyNumber = lastFrequency+1
try:
db_session.add(db.frequency(id=lastFrequencyNumber,owner=username, reallocable=0, occuped=1))
db_session.commit()
db_session.close()
return lastFrequencyNumber
except Exception as e:
print(e)
return False
except Exception as e:
print(e)
return False