forked from canada-ca/tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUsers.py
More file actions
89 lines (76 loc) · 2.85 KB
/
Copy pathUsers.py
File metadata and controls
89 lines (76 loc) · 2.85 KB
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
import bcrypt
from random import randint
from sqlalchemy.types import Integer, Boolean, Float
from sqlalchemy.orm import relationship
from sqlalchemy.ext.hybrid import hybrid_property, hybrid_method
from sqlalchemy import Column, String
from db import Base
from functions.orm_to_dict import orm_to_dict
from functions.slugify import slugify_value
from models.Organizations import Organizations
from models.User_affiliations import User_affiliations
class Users(Base):
__tablename__ = "users"
id = Column(Integer, primary_key=True, autoincrement=True)
user_name = Column(String)
display_name = Column(String)
user_password = Column(String)
preferred_lang = Column(String)
failed_login_attempts = Column(Integer, default=0)
failed_login_attempt_time = Column(Float, default=0, nullable=True)
tfa_validated = Column(Boolean, default=False)
user_affiliation = relationship(
"User_affiliations", back_populates="user", passive_deletes=True,
)
email_validated = Column(Boolean, default=False)
def __init__(self, **kwargs):
super(Users, self).__init__(**kwargs)
acronym = slugify_value(self.user_name).upper()[:50]
self.user_affiliation.append(
User_affiliations(
permission="admin",
user_organization=Organizations(name=self.user_name, acronym=acronym,),
)
)
@hybrid_method
def find_by_user_name(self, user_name):
return self.query.filter(self.user_name == user_name).first()
@hybrid_method
def find_by_id(self, id):
return self.query.filter(self.id == id).first()
@hybrid_property
def roles(self):
affiliations = orm_to_dict(self.user_affiliation)
roles = []
if len(affiliations):
roles = []
for affiliation in affiliations:
roles.append(
{
"user_id": affiliation["user_id"],
"org_id": affiliation["organization_id"],
"permission": affiliation["permission"],
}
)
return roles
@hybrid_property
def password(self):
return self.user_password
@password.setter
def password(self, password):
if len(password) < 12:
raise ValueError("Password must be greater than 12 characters")
else:
self.user_password = bcrypt.hashpw(
password.encode("utf8"), bcrypt.gensalt()
).decode("utf8")
@hybrid_method
def verify_account(self):
# Set user email_validated field to true0
self.email_validated = True
@hybrid_method
def update_password(self, password):
""" Used To Update Users Password"""
self.password = password
self.failed_login_attempts = 0
self.failed_login_attempt_time = 0