1
|
|
|
# -*- coding: utf-8 -*- |
|
|
|
|
2
|
|
|
import threading |
|
|
|
|
3
|
|
|
|
4
|
|
|
import transaction |
5
|
|
|
import typing as typing |
|
|
|
|
6
|
|
|
|
7
|
|
|
from tracim.models.auth import User |
8
|
|
|
|
9
|
|
|
|
10
|
|
|
class UserApi(object): |
|
|
|
|
11
|
|
|
|
12
|
|
|
def __init__(self, current_user: typing.Optional[User], session, config): |
|
|
|
|
13
|
|
|
self._session = session |
14
|
|
|
self._user = current_user |
15
|
|
|
self._config = config |
16
|
|
|
|
17
|
|
|
def get_all(self): |
|
|
|
|
18
|
|
|
return self._session.query(User).order_by(User.display_name).all() |
19
|
|
|
|
20
|
|
|
def _base_query(self): |
21
|
|
|
return self._session.query(User) |
22
|
|
|
|
23
|
|
|
def get_one(self, user_id: int): |
|
|
|
|
24
|
|
|
return self._base_query().filter(User.user_id==user_id).one() |
|
|
|
|
25
|
|
|
|
26
|
|
|
def get_one_by_email(self, email: str): |
|
|
|
|
27
|
|
|
return self._base_query().filter(User.email==email).one() |
|
|
|
|
28
|
|
|
|
29
|
|
|
def get_one_by_id(self, id: int) -> User: |
|
|
|
|
30
|
|
|
return self._base_query().filter(User.user_id==id).one() |
|
|
|
|
31
|
|
|
|
32
|
|
|
def update( |
|
|
|
|
33
|
|
|
self, |
34
|
|
|
user: User, |
35
|
|
|
name: str=None, |
|
|
|
|
36
|
|
|
email: str=None, |
|
|
|
|
37
|
|
|
do_save=True, |
38
|
|
|
timezone: str='', |
|
|
|
|
39
|
|
|
): |
40
|
|
|
if name is not None: |
41
|
|
|
user.display_name = name |
42
|
|
|
|
43
|
|
|
if email is not None: |
44
|
|
|
user.email = email |
45
|
|
|
|
46
|
|
|
user.timezone = timezone |
47
|
|
|
|
48
|
|
|
if do_save: |
49
|
|
|
self.save(user) |
50
|
|
|
|
51
|
|
|
def user_with_email_exists(self, email: str): |
|
|
|
|
52
|
|
|
try: |
53
|
|
|
self.get_one_by_email(email) |
54
|
|
|
return True |
55
|
|
|
# TODO - G.M - 09-04-2018 - Better exception |
|
|
|
|
56
|
|
|
except: |
|
|
|
|
57
|
|
|
return False |
58
|
|
|
|
59
|
|
|
def create_user(self, email=None, groups=[], save_now=False) -> User: |
|
|
|
|
60
|
|
|
user = User() |
61
|
|
|
|
62
|
|
|
if email: |
63
|
|
|
user.email = email |
64
|
|
|
|
65
|
|
|
for group in groups: |
66
|
|
|
user.groups.append(group) |
67
|
|
|
|
68
|
|
|
self._session.add(user) |
69
|
|
|
|
70
|
|
|
if save_now: |
71
|
|
|
self._session.flush() |
72
|
|
|
|
73
|
|
|
return user |
74
|
|
|
|
75
|
|
|
def save(self, user: User): |
|
|
|
|
76
|
|
|
self._session.flush() |
77
|
|
|
|
78
|
|
|
def execute_created_user_actions(self, created_user: User) -> None: |
79
|
|
|
""" |
80
|
|
|
Execute actions when user just been created |
81
|
|
|
:return: |
82
|
|
|
""" |
83
|
|
|
# NOTE: Cyclic import |
84
|
|
|
# TODO - G.M - 28-03-2018 - [Calendar] Reenable Calendar stuff |
|
|
|
|
85
|
|
|
#from tracim.lib.calendar import CalendarManager |
86
|
|
|
#from tracim.model.organisational import UserCalendar |
87
|
|
|
|
88
|
|
|
# TODO - G.M - 04-04-2018 - [auth] |
|
|
|
|
89
|
|
|
# Check if this is already needed with |
90
|
|
|
# new auth system |
91
|
|
|
created_user.ensure_auth_token( |
92
|
|
|
session=self._session, |
93
|
|
|
validity_seconds=self._config.USER_AUTH_TOKEN_VALIDITY |
94
|
|
|
) |
95
|
|
|
|
96
|
|
|
# Ensure database is up-to-date |
97
|
|
|
self._session.flush() |
98
|
|
|
transaction.commit() |
99
|
|
|
|
100
|
|
|
# TODO - G.M - 28-03-2018 - [Calendar] Reenable Calendar stuff |
|
|
|
|
101
|
|
|
# calendar_manager = CalendarManager(created_user) |
102
|
|
|
# calendar_manager.create_then_remove_fake_event( |
103
|
|
|
# calendar_class=UserCalendar, |
104
|
|
|
# related_object_id=created_user.user_id, |
105
|
|
|
# ) |
106
|
|
|
|
The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:
If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.