This repository has been archived by the owner on Dec 8, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice.py
263 lines (183 loc) · 8.05 KB
/
service.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
from typing import Union, List, Tuple
from database_handler import DatabaseHandler
from utils import *
import config
import json
import shutil
import os
database_handler_ = DatabaseHandler(config.DATABASE_PATH)
def check_login_exists(data: dict) -> bool:
"""Check is user created account"""
return database_handler_.check_login_exists(data.get('email'), data.get('password'))
def create_local_avatar_file_name() -> str:
"""Creating file name for user avatar"""
user_email = hash_data(get_local_user_email())
return f'user.{user_email}.avatar.png'
def create_local_portfolio_file_name() -> str:
"""Creating file name for user portfolio photo"""
user_email = hash_data(get_local_user_email())
photo_index = get_user_data_by_column('portfolio_count', user_email)
photo_index = 0 if not photo_index else photo_index
return f'{user_email}_{photo_index}.png'
def create_user_path() -> None:
"""Create own user path"""
user_email = hash_data(get_local_user_email())
user_path = os.path.join(config.USERS_PATH, user_email)
user_portfolio_path = os.path.join(user_path, 'portfolio')
if not os.path.exists(user_path):
os.mkdir(user_path)
os.mkdir(user_portfolio_path)
def create_account(data: dict) -> bool:
"""Creating account"""
if not check_login_exists(data):
save_current_user_to_local(data)
create_user_path()
data['avatar_photo'] = copy_avatar_photo_to_local(data.get('avatar_photo'))
data['post'] = get_post_id(data['post'])
created = database_handler_.create_account(list(data.values()))
return created
else:
return False
def get_user_data_by_column(column: str, email: str) -> Union[int, float, str, bool]:
"""Get user info from column"""
data = database_handler_.get_user_data_by_column(column, email)
return data if data else False
def get_user_data_by_columns(columns: Union[list, tuple], email: str) -> Union[int, float, str, bool]:
"""Get user info from columns"""
data = database_handler_.get_user_data_by_columns(columns, email)
return data if data else False
def is_user_logged_in_local() -> bool:
"""Check is user logged in in his computer"""
with open('user.json', 'r', encoding='utf-8') as file:
data = json.load(file)
file.close()
if 'user' in data:
if 'email' in data['user']:
return True
return False
def get_local_user_email() -> Union[str, bool]:
"""Get user email which logged in in this computer (local)"""
with open('user.json', 'r', encoding='utf-8') as json_:
json_data = json.load(json_)
json_.close()
if is_user_logged_in_local():
return json_data['user']['email']
return False
def copy_avatar_photo_to_local(file_name: str) -> str:
"""Copy user avatar photo to application local path"""
user_email = hash_data(get_local_user_email())
new_file_name = create_local_avatar_file_name()
new_file_path = os.path.join(os.path.join(config.USERS_PATH, user_email), new_file_name)
shutil.copyfile(file_name, new_file_path)
return new_file_path
def copy_portfolio_photo_to_local(file_name: str) -> str:
"""Copy user portfolio photo to application local path"""
user_email = hash_data(get_local_user_email())
user_portfolio_path = os.path.join(os.path.join(config.USERS_PATH, user_email), 'portfolio')
new_file_name = create_local_portfolio_file_name()
new_file_path = os.path.join(user_portfolio_path, new_file_name)
shutil.copyfile(file_name, new_file_path)
return new_file_path
def save_current_user_to_local(data: dict) -> None:
"""Save in local, that user is logged in"""
with open('user.json', 'w', encoding='utf-8') as json_file:
json.dump(
{
'user': {
'email': data.get('email'),
}
}, json_file)
json_file.close()
create_user_path()
def edit_profile(name: str, surname: str, gender: str, birthday: int, photo: str) -> None:
"""Edit profile"""
user_email = get_local_user_email()
if photo:
photo = copy_avatar_photo_to_local(photo)
else:
photo = get_user_avatar_photo()
database_handler_.edit_user_data_by_columns(['name', 'surname', 'birthday', 'gender', 'avatar_photo'],
[name, surname, birthday, gender, photo],
user_email)
def delete_profile():
"""Delete user"""
user_email = get_local_user_email()
database_handler_.delete_profile(user_email)
def increase_photos_count() -> None:
"""Increase portfolio photo count (in database)"""
user_email = get_local_user_email()
database_handler_.increase_portfolio_photo_count(user_email)
def add_to_portfolio(competitions_name: str, place: str, date: int, photo: str) -> bool:
"""Add portfolio"""
user_email = get_local_user_email()
photo = copy_portfolio_photo_to_local(photo)
increase_photos_count()
database_handler_.add_portfolio_to_portfolios(user_email, competitions_name, place, date, photo)
return True
def get_user_portfolio() -> Union[list, tuple]:
"""Get user portfolio"""
user_email = get_local_user_email()
return database_handler_.get_portfolio(user_email)
def get_user_avatar_photo() -> str:
"""Get path to user avatar"""
user_email = get_local_user_email()
file_name = get_user_data_by_column('avatar_photo', user_email)
if not os.path.exists(file_name):
file_name = config.DEFAULT_AVATAR_PATH
return file_name
def get_full_user_data() -> Union[list, tuple]:
"""Get full user data"""
user_email = get_local_user_email()
return database_handler_.get_full_user_data(user_email)
def check_login_data_correctness(login_data: dict) -> bool:
"""Check login data correctness"""
return database_handler_.check_login_data_correctness(login_data)
def get_static_interface_texts(name: str) -> str:
"""Read program messages file, and return messages"""
with open('program_messages.json', 'r', encoding='utf-8') as json_file:
errors_message = json.load(json_file)
json_file.close()
if name in errors_message['texts']:
return errors_message['texts'].get(name)
else:
return ''
def create_task(title: str, text: str, date: int, photo: Union[str, None]) -> bool:
"""Load task to database"""
user_email = get_local_user_email()
class_ = database_handler_.get_class_id_by_director_email(user_email)
database_handler_.create_task(title, text, date, photo, class_)
return True
def get_tasks() -> List[Tuple]:
"""Get tasks"""
user_email = get_local_user_email()
return database_handler_.get_tasks(user_email)
def get_user_post_id() -> str:
"""Get user post"""
user_email = get_local_user_email()
return get_user_data_by_column('post', user_email)
def get_post_id(post_name: str) -> int:
"""Get post id from table, because post column is foreign key (table: posts)"""
return database_handler_.get_post_id(post_name)
def get_children_list() -> list:
"""Get list of children (if post = 2)"""
return database_handler_.get_children_list()
def create_class(children_email: List[str]) -> bool:
"""Create children class -> (insert or replace classes database and
set class columns at children)"""
user_email = get_local_user_email()
database_handler_.create_class(user_email, children_email)
return True
def get_class() -> list:
"""This method user for get children list from class by director email"""
user_email = get_local_user_email()
return database_handler_.get_class_by_director_email(user_email)
def exit_from_local():
with open('user.json', 'w') as json_file:
json.dump({}, json_file)
json_file.close()
def delete_portfolio_item(data):
user_email = get_local_user_email()
competitions_name = data.get('competitions_name')
place = data.get('place')
datetime_ = data.get('datetime')
database_handler_.delete_portfolio_item(user_email, competitions_name, place, datetime_)