forked from sosschs9/2022_SoftwareDesign
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser.py
261 lines (205 loc) · 8.02 KB
/
user.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
import pymongo
from basic import *
# DB 연결
client = pymongo.MongoClient("mongodb+srv://SD:[email protected]/?retryWrites=true&w=majority")
db = client['2022SoftwareDesign']
col_user = db['User']
##### PD Layer Class #####
class User:
# 초기화: id, password, 이름, 전화번호, 성별, myPost, myReqHistory
def __init__(self, id: str, pw: str, name: str, phoneNumber: str, gender: bool, myPost: list, myReqHistory: list):
self.__id = id
self.__password = pw
self.__name = name
self.__phoneNumber = phoneNumber
self.__gender = gender
self.__myPost = myPost
self.__myRequestHistory = myReqHistory
# 로그인 / ret:bool
def login(self, inputID, inputPW):
if self.getID() == inputID and self.__getPassword() == inputPW:
return True
else:
return False
def getUserInfo(self):
return UserCommonInfo(self.__id, self.__getName(), self.__getPhoneNumber(), self.__getGender())
# User Getter
def getID(self):
return self.__id
def __getPassword(self):
return self.__password
def __getName(self):
return self.__name
def __getPhoneNumber(self):
return self.__phoneNumber
def __getGender(self):
return self.__gender
def getMyRequest(self):
return self.__myRequestHistory
def getMyPost(self):
return self.__myPost
def update(self, password, phoneNumber):
self.__password = password
self.__phoneNumber = phoneNumber
def addMyPost(self, postIdx):
self.__myPost.append(postIdx)
def addMyRequest(self, requestIdx):
self.__myRequestHistory.append(requestIdx)
def deleteMyPost(self, postIdx):
self.__myPost.remove(postIdx)
def deleteMyRequest(self, requestIdx):
self.__myRequestHistory.remove(requestIdx)
class Helper(User):
# 초기화: id, password, 이름, 전화번호, 성별, 도우미 유형, myPost, myReqHistory, myHelpHistory
def __init__(self, id: str, pw: str, name: str, phoneNumber: str, gender: bool, helperType: int, myPost: list,
myReqHistory: list, myHelpHistory: list):
self.__id = id
self.__password = pw
self.__name = name
self.__phoneNumber = phoneNumber
self.__gender = gender
self.__myPost = myPost
self.__myRequestHistory = myReqHistory
self.__helperType = helperType
self.__myHelpHistory = myHelpHistory
# 로그인 / ret:bool
def login(self, inputID, inputPW):
if self.getID() == inputID and self.__getPassword() == inputPW:
return True
else:
return False
def getUserInfo(self):
return UserCommonInfo(self.__id, self.__getName(), self.__getPhoneNumber(), self.__getGender())
# Helper Getter
def getID(self):
return self.__id
def __getPassword(self):
return self.__password
def __getName(self):
return self.__name
def __getPhoneNumber(self):
return self.__phoneNumber
def __getGender(self):
return self.__gender
def getHelperType(self):
return self.__helperType
def getMyRequest(self):
return self.__myRequestHistory
def getMyPost(self):
return self.__myPost
def getMyHelp(self):
return self.__myHelpHistory
def update(self, password, phoneNumber):
self.__password = password
self.__phoneNumber = phoneNumber
def addMyPost(self, postIdx):
self.__myPost.append(postIdx)
def addMyRequest(self, requestIdx):
self.__myRequestHistory.append(requestIdx)
def addmyHelp(self, helpIdx):
self.__myHelpHistory.append(helpIdx)
def deleteMyPost(self, postIdx):
self.__myPost.remove(postIdx)
def deleteMyRequest(self, requestIdx):
self.__myRequestHistory.remove(requestIdx)
##### USER DB #####
# 회원 등록 확인 / ret: boolean
def isRegistered(userID: str):
element = col_user.find_one({'_User__id': userID})
ret = False
if element == None:
element = col_user.find_one({'_Helper__id': userID})
if element != None:
ret = True
else:
ret = True
return ret
# 회원 등록
def DB_addUser(nUser):
col_user.insert_one(nUser.__dict__)
element = col_user.find_one({'_User__id':nUser.getID()})
DB_deleteUser(nUser.getID())
element['key'] = str(element['_id'])
col_user.insert_one(element)
# 회원 삭제
def DB_deleteUser(userID: str):
col_user.delete_one({'_User__id': userID})
col_user.delete_one({'_Helper__id': userID})
# 회원 찾기 / ret: User
def DB_getUser(userID: str):
element = col_user.find_one({'_User__id': userID})
if element == None:
element = col_user.find_one({'_Helper__id': userID})
user = Helper(element['_Helper__id'], element['_Helper__password'], element['_Helper__name'],
element['_Helper__phoneNumber'], element['_Helper__gender'], element['_Helper__helperType'],
element['_Helper__myPost'], element['_Helper__myRequestHistory'],
element['_Helper__myHelpHistory'])
else:
user = User(element['_User__id'], element['_User__password'], element['_User__name'],
element['_User__phoneNumber'], element['_User__gender'], element['_User__myPost'],
element['_User__myRequestHistory'])
return user
# key -> userID
def DB_findUser_key(userKey:str):
element = col_user.find_one({'key':userKey})
if element.get('_User__id') != None:
return element['_User__id']
else:
return element['_Helper__id']
def DB_getUserKey(userID:str):
element = col_user.find_one({'_User__id':userID})
if element != None:
return element['key']
element = col_user.find_one({'_Helper__id':userID})
return element['key']
# 회원 정보 수정
def DB_updateUser(user):
element = col_user.find_one({'_User__id': user.getID()})
if element == None:
element = col_user.find_one({'_Helper__id': user.getID()})
nUser = user.__dict__
DB_deleteUser(user.getID())
nUser['key'] = element['key']
col_user.insert_one(nUser)
##### Method(app.py) #####
# 회원 등록 확인 >> isRegistered(userID:str) / ret: boolean
# User 불러오기 >> DB_getUser(userID:str) / ret: User
# 로그인 :: id, password
# 성공시 ret: bool(True), str(userID)
# 실패시 ret: bool(False), str(errorMsg)
def login(inputID, inputPW):
if isRegistered(inputID) == False:
return False, "등록되지 않은 회원입니다."
user = DB_getUser(inputID)
if user.login(inputID, inputPW) == True:
return True, inputID
else:
return False, "아이디, 비밀번호가 일치하지 않습니다."
# 회원 등록 :: id, password, name, gender
def createUser(userID, password, name, phoneNumber, gender):
user = User(userID, password, name, phoneNumber, gender, [], [])
DB_addUser(user)
# 회원정보 수정
def modifyUser(userID, password, phoneNumber):
user = DB_getUser(userID)
user.update(password, phoneNumber)
DB_updateUser(user)
# 도우미 변환
def beHelper(userID, helperType):
user = col_user.find_one({'_User__id': userID})
if user == None:
user = col_user.find_one({'Helper_id': userID})
helper = Helper(user['_Helper__id'], user['_Helper__password'], user['_Helper__name'],
user['_Helper__phoneNumber'], user['_Helper__gender'], user['_Helper__helperType'] | helperType,
user['_Helper__myPost'], user['_Helper__myRequestHistory'], user['_Helper__MyHelpHistory'])
else:
helper = Helper(user['_User__id'], user['_User__password'], user['_User__name'],
user['_User__phoneNumber'], user['_User__gender'], helperType,
user['_User__myPost'], user['_User__myRequestHistory'], [])
DB_updateUser(helper)
# Helper인지확인 / ret:bool
def isHelper(userID):
user = DB_getUser(userID)
if type(user) == Helper:
return True
return False