-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathusers.py
136 lines (100 loc) · 3.68 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
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
"""Managing of user data
The data of the users are stored in the users directory (by default).
The file names are the identifiers of the users.
A new user identifier is generated by finding the maximal value of the identifiers and increasing by one.
The fields of a user object stored in the text file line-by-line as:
first_name
family_name
birth
email
password
"""
import os
import storage_utils
class User(object):
"""User of the document repository"""
def __init__(self, first_name, family_name, birth, email, password):
self._first_name = first_name
self._family_name = family_name
self._birth = birth
self._email = email
self._password = password
@property
def first_name(self):
return self._first_name
@property
def family_name(self):
return self._family_name
@property
def birth(self):
return self._birth
@property
def email(self):
return self._email
@property
def password(self):
return self._password
class Role(object):
"""Represents the roles of the users"""
def __init__(self, role):
if role in ['admin', 'manager', 'author', 'reviewer', 'visitor']:
self._role = role
else:
raise ValueError('The "{}" is an invalid role!'.format(role))
class RoleManager(object):
"""Manage the user roles which are stored in a text file."""
def __init__(self):
pass
def read_roles(self, path):
"""Read roles from the file."""
pass
def write_roles(self):
"""Write roles to the file."""
pass
class UserManager(object):
"""Manage user objects"""
def __init__(self, storage_location):
self._storage_location = storage_location
def save_user(self, user_id, user):
"""Save user to file"""
with open('{}/{}'.format(self._storage_location, user_id), 'w') as user_file:
user_file.write(user.first_name + '\n')
user_file.write(user.last_name + '\n')
user_file.write(str(user.birth) + '\n')
user_file.write(user.email + '\n')
user_file.write(user.password + '\n')
def load_user(self, user_id):
"""Load user from file"""
with open('{}/{}'.format(self._storage_location, user_id)) as user_file:
first_name = user_file.readline().rstrip('\n')
family_name = user_file.readline().rstrip('\n')
birth = user_file.readline().rstrip('\n')
email = user_file.readline().rstrip('\n')
password = user_file.readline().rstrip('\n')
user = User(first_name, family_name, birth, email, password)
return user
def add_user(self, user):
user_id = storage_utils.get_next_id(self._storage_location)
self.save_user(user_id, user)
def update_user(self, user_id, user):
self.remove_user(user_id)
self.save_user(user_id, user)
def remove_user(self, user_id):
user_file_path = '{}/{}'.format(self._storage_location, user_id)
if os.path.exists(user_file_path):
os.remove(user_file_path)
else:
raise ValueError('The user id {} does not exist!'.format(user_id))
def find_user_by_id(self, user_id):
user_file_path = '{}/{}'.format(self._storage_location, user_id)
if os.path.exists(user_file_path):
user = self.load_user(user_id)
return user
else:
raise ValueError('The user id {} does not exist!'.format(user_id))
def find_users_by_name(self, name):
pass
def find_users_by_email(self, email):
pass
def find_users_by_role(self, role):
pass