-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathutils.py
139 lines (121 loc) · 4.05 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
from __future__ import annotations
from datetime import datetime
from mimesis import Person, Cryptographic, Text
from project import db, app
from project.models.user import User, UserRole, SocialAuth
from project.models.group import Group
from project.models.user_group_association import UserGroupAssociation
from project import bcrypt
data_generator = Person('en')
data_generator_text = Text()
def add_user(role: UserRole = UserRole.USER,
email: str = None,
username: str = None,
password: str = None,
created_at: datetime = None,
name: str = None) -> User:
"""
Generates a fake user to add in DB
"""
if email is None:
email = data_generator.email()
if username is None:
username = data_generator.email()
if password is None:
password = data_generator.email()
if created_at is None:
created_at = datetime.now()
if name is None:
name = data_generator.full_name()
user = User(email=email,
username=username,
password=password,
name=name,
created_at=created_at,
role=role)
db.session.add(user)
db.session.commit()
return user
def add_user_password(role: UserRole = UserRole.USER,
email: str = None,
username: str = None,
password: str = None,
created_at: datetime = None,
name: str = None) -> tuple(User, str):
"""
Generates a fake user to add in DB and return User, password tuple
"""
if email is None:
email = data_generator.email()
if username is None:
username = data_generator.email()
if password is None:
password = data_generator.email()
if created_at is None:
created_at = datetime.now()
if name is None:
name = data_generator.full_name()
user = User(email=email,
username=username,
password=password,
name=name,
created_at=created_at,
role=role)
db.session.add(user)
db.session.commit()
return user, password
def add_social_user(role: UserRole = UserRole.USER,
email: str = None,
username: str = None,
password: str = None) -> User:
"""
Generates a fake social user to add in DB
"""
if email is None:
email = data_generator.email()
if username is None:
username = data_generator.email()
if password is None:
password = data_generator.email()
user = User(email=email,
username=username,
password=password,
name=data_generator.full_name(),
created_at=datetime.now(),
role=role,
social_type=SocialAuth.FACEBOOK.value,
social_id=data_generator.identifier(),
social_access_token=Cryptographic.token_urlsafe())
db.session.add(user)
db.session.commit()
return user
def add_group(name: str) -> Group:
"""
Add a new group in database
"""
group = Group(name=name)
db.session.add(group)
db.session.commit()
return group
def add_user_group_association(user: User, group: Group) -> UserGroupAssociation:
"""
Add a new user-group association
"""
user_group_association = UserGroupAssociation(user=user, group=group)
db.session.add(user_group_association)
db.session.commit()
return user_group_association
def set_user_token_hash(user: User, token: str) -> User:
"""
Set token hash for user
"""
user.token_hash = bcrypt.generate_password_hash(token, app.config.get('BCRYPT_LOG_ROUNDS')).decode()
db.session.commit()
return user
def set_user_email_token_hash(user: User, token: str) -> User:
"""
Set email token hash for user
"""
user.email_token_hash = bcrypt.generate_password_hash(token, app.config.get('BCRYPT_LOG_ROUNDS')).decode()
db.session.commit()
return user