forked from pedosb/celcombiller
-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
325 lines (293 loc) · 9.22 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
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
import flask
from flask import Flask, session, request, flash, url_for, redirect, \
render_template, abort
from config import db, app, login_manager
from models import CDR, User, Groups, Dates, Ballance
from time import strftime
from datetime import *
from dateutil.rrule import *
from dateutil.parser import *
from dateutil.relativedelta import *
from flask_restless import ProcessingException
from flask.ext.login import login_user , logout_user , current_user ,\
login_required
from passlib.hash import pbkdf2_sha512
import json
from openbts import to_openbts
@app.route('/')
def index():
"""
Index page, just show the logged username
"""
return render_template('index.html')
@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'GET':
return render_template('login.html')
else:
clid = request.form['clid']
password = request.form['password']
registered_user = \
User.query.filter_by(clid=clid).first()
if pbkdf2_sha512.verify(password, registered_user.password.hash) \
or registered_user is None:
print registered_user.password.hash
login_user(registered_user)
json_with_names = check_time()
return "Hello, cross-origin-world!"
else:
flash('Number or Password is invalid', 'error')
return render_template('ERROR.html')
def check_time():
if not current_user.is_admin:
return False
else:
json_need_update = []
groups = Groups.query.all()
for group in groups:
boolean_time = (group.dates_to_update[0].date - datetime.now())\
.total_seconds()
boolean_time = 1 if boolean_time < 0 else 0
if boolean_time == 1:
json_need_update.append(group.name)
if json_need_update == []:
return None
else:
return json_need_update
def already_has_group(data=None, **kargs):
data = request.data
request_body = json.loads(data)
group = Groups.query.\
filter_by(name=request_body['name']).first()
if group is not None:
raise ProcessingException(description='Already has this Group', code=400)
else:
pass
def put_user_id_in_buffer(*args, **kargs):
data = request.data
request_body = json.loads(data)
global buffer_usersId
buffer_usersId = request_body['users']
del kargs['data']['users']
def add_users_to_group(*args, **kargs):
global buffer_usersId
data = request.data
request_body = json.loads(data)
group = Groups.query\
.filter_by(name=request_body['name']).first()
for userId in buffer_usersId:
user = User.query.filter_by(id_=userId).first()
group.tunel.append(user)
db.session.add(group)
db.session.commit()
pass
def add_dates_to_group(*args, **kargs):
global data_count
data = request.data
request_body = json.loads(data)
group = Groups.query.\
filter_by(name=request_body['name']).first()
listOfdates = Dates.query.order_by(Dates.id_.desc()).limit(data_count)
for date in listOfdates:
group.dates_to_update.append(date)
pass
def transform_to_utc(*args, **kargs):
global data_count
data = request.data
request_body = json.loads(data)
data_count = kargs['data']['count']
min_day = int(datetime.now().strftime("%d"))
min_month = int(datetime.now().strftime("%m"))
min_year = int(datetime.now().strftime("%Y"))
day = int(request_body['day'])
year = int(request_body['year'])
month = int(request_body['month'])
how_many = int(request_body['count'])
if year < min_year or ((month < min_month) and (year <= min_year)) or \
(day < min_day and (month <= min_month and year <= min_year)):
raise ProcessingException(description='Date not accept', code=400)
else:
del kargs['data']['day']
del kargs['data']['month']
del kargs['data']['year']
del kargs['data']['count']
start_date = str(month) + " " + str(day) + " " + str(year) + " 0:0:0 "
all_dates = \
list(rrule(MONTHLY, count=how_many, dtstart=parse(start_date)))
for var in all_dates:
db.session.add(Dates(var))
db.session.commit()
pass
def date_now(*args, **kargs):
kargs['data']['date'] = unicode(datetime.now())
global buffer_usersId
buffer_usersId = kargs['data']['userId']
del kargs['data']['userId']
def add_user_balance(*args, **kargs):
global buffer_usersId
data = request.data
request_body = json.loads(data)
request_body['userId']
x = Ballance.query.order_by(Ballance.id_.desc()).first()
x.usersId = request_body['userId']
db.session.add(x)
db.session.commit()
@app.route('/logout')
def logout():
logout_user()
return render_template('logout.html')
@login_manager.user_loader
def load_user(id_):
return User.query.get(int(id_))
@app.route('/check')
@login_required
def check_login():
"""
Index page, just show the logged username
"""
return render_template('check.html')
def auth(*args, **kargs):
"""
Required API request to be authenticated
"""
if not current_user.is_authenticated:
raise ProcessingException(description='Not authenticated', code=401)
def preprocessor_check_adm(*args, **kargs):
if not current_user.is_admin:
raise ProcessingException(description='Forbidden', code=403)
def preprocessors_patch(instance_id=None, data=None, **kargs):
user_cant_change = ["admin", "clid", "id_",
"originated_calls", "received_calls, tunel"]
admin_cant_change = ["id_", "originated_calls", "received_calls"]
if current_user.is_admin:
for x in data.keys():
if x in admin_cant_change:
raise ProcessingException(description='Forbidden', code=403)
elif current_user.username == instance_id:
for x in data.keys():
if x in user_cant_change:
raise ProcessingException(description='Forbidden', code=403)
else:
raise ProcessingException(description='Forbidden', code=403)
def preprocessors_check_adm_or_normal_user(instance_id=None, **kargs):
if not (current_user.is_admin or current_user.username == instance_id):
raise ProcessingException(description='Forbidden', code=403)
manager = flask.ext.restless.APIManager(app, flask_sqlalchemy_db=db)
# Create the Flask-Restless API manager.
# Create API endpoints, which will be available at /api/<tablename> by
# default. Allowed HTTP methods can be specified as well.
manager.create_api(
User,
preprocessors={
'POST': [
auth,
preprocessor_check_adm
],
'GET_MANY': [
auth,
preprocessor_check_adm
],
'GET_SINGLE': [
auth,
preprocessors_check_adm_or_normal_user
],
'PATCH_SINGLE': [
auth,
preprocessors_check_adm_or_normal_user,
preprocessors_patch
],
'PATCH_MANY': [auth, preprocessor_check_adm],
'DELETE_SINGLE': [auth, preprocessor_check_adm],
},
postprocessors={
'POST':[to_openbts]
},
exclude_columns=[
'password'
],
methods=['POST', 'GET', 'PATCH', 'DELETE'],
allow_patch_many=True,
primary_key='clid'
)
manager.create_api(
CDR,
preprocessors={
'POST': [
auth,
preprocessor_check_adm
],
'GET_MANY': [
auth,
preprocessor_check_adm
],
'GET_SINGLE': [
auth,
preprocessors_check_adm_or_normal_user
],
'PATCH_SINGLE': [auth, preprocessors_patch],
'DELETE_SINGLE': [auth, preprocessor_check_adm],
},
methods=['POST','GET','PATCH', 'DELETE']
)
manager.create_api(
Groups,
preprocessors={
'POST': [
auth,
preprocessor_check_adm,
already_has_group,
put_user_id_in_buffer,
transform_to_utc
],
'GET_MANY': [
auth,
preprocessor_check_adm
],
'GET_SINGLE': [auth, preprocessor_check_adm],
'PATCH_SINGLE': [
auth,
preprocessor_check_adm
],
'DELETE_SINGLE': [auth, preprocessor_check_adm],
},
postprocessors={
'POST': [add_dates_to_group, add_users_to_group],
},
exclude_columns=[
'newUser',
'removeUser',
'updateGroup',
'tunel.password'
],
methods=['POST', 'GET', 'PATCH', 'DELETE'],
results_per_page=100,
primary_key='name'
)
manager.create_api(
Ballance,
preprocessors={
'POST': [
auth,
preprocessor_check_adm,
date_now
],
'GET_MANY': [
auth,
preprocessor_check_adm
],
'GET_SINGLE': [auth, preprocessor_check_adm],
'PATCH_SINGLE': [
auth,
preprocessor_check_adm
],
'DELETE_SINGLE': [auth, preprocessor_check_adm],
},
postprocessors={
'POST': [add_user_balance]
},
methods=['POST', 'GET', 'PATCH', 'DELETE'],
results_per_page=100,
)
# start the flask loop
app.debug = True
app.run('0.0.0.0', 5000)