-
Notifications
You must be signed in to change notification settings - Fork 0
/
Users.py
96 lines (86 loc) · 4.11 KB
/
Users.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
from passlib.handlers.sha2_crypt import sha256_crypt
from Database import session, UsersDbTable
from flask import abort, jsonify
import random
class Users:
field_for_register = ['name', 'surname', 'email', 'password', 'pseudo', 'type']
def register(self, data):
customer = session.query(UsersDbTable).filter_by(email=data['email']).first()
if customer is None:
bits = random.getrandbits(126)
token = hex(bits)
customer = UsersDbTable(name=data['name'],
surname=data['surname'],
email=data['email'],
password=sha256_crypt.encrypt(data['password']),
pseudo=data['pseudo'],
type=data['type'],
token=token)
session.add(customer)
session.commit()
return customer.token
else:
abort(409)
def update(self, data):
customer = session.query(UsersDbTable).filter_by(email=data['email']).first()
if customer is None:
abort(400)
else:
customer.name = customer.name if (data['name'] == '' or data['name'] == None) else data['name']
customer.surname = customer.surname if (data['surname'] == '' or data['surname'] == None) else data['surname']
customer.email = customer.email if (data['email'] == '' or data['email'] == None) else data['email']
customer.password = customer.password if (data['password'] == '' or data['password'] == None) else sha256_crypt.encrypt(data['password'])
customer.pseudo = customer.pseudo if (data['pseudo'] == '' or data['pseudo'] == None) else data['pseudo']
customer.type = customer.type if (data['type'] == '' or data['type'] == None) else data['type']
session.add(customer)
session.commit()
return (
{'name': customer.name, 'surname': customer.surname, 'email': customer.email, 'pseudo': customer.pseudo,
'type': customer.type, 'token': customer.token, 'id': customer.id})
def login(self, data):
customer = session.query(UsersDbTable).filter_by(email=data['email']).first()
if customer is not None:
if sha256_crypt.verify(data['password'], customer.password) is False:
abort(401)
else:
return (
{'name': customer.name, 'surname': customer.surname, 'email': customer.email,
'pseudo': customer.pseudo,
'type': customer.type, 'token': customer.token})
else:
abort(401)
def check_token(self, token):
customer = session.query(UsersDbTable).filter_by(email=data['email']).first()
if customer is not None:
if sha256_crypt.verify(data['password'], customer.password) is False:
abort(401)
else:
return (
{'name': customer.name, 'surname': customer.surname, 'email': customer.email,
'pseudo': customer.pseudo,
'type': customer.type, 'token': customer.token})
else:
abort(401)
def search_user_by_name(self, name):
customer = session.query(UsersDbTable).filter_by(name=name).all()
if customer is not None:
return customer
else:
abort(401)
def delete(self, data):
customer = session.query(UsersDbTable).filter_by(token=data['token']).first()
if customer is not None:
session.delete(customer)
session.commit()
else:
abort(409)
def get_manager(self, data):
customer = session.query(UsersDbTable).filter_by(token=data['token']).first()
if customer is not None:
json = jsonify({'first_name': customer.first_name,
'last_name': customer.last_name,
'email_adress': customer.email_adress
})
else:
abort(409)
return json