forked from sosschs9/2022_SoftwareDesign
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatching.py
More file actions
543 lines (424 loc) · 21.5 KB
/
matching.py
File metadata and controls
543 lines (424 loc) · 21.5 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
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
import pymongo
from basic import *
from user import *
from messagesender import *
# app.py 작성시 맨 아래쪽의 Method(app.py) 부분 참고
# messageSender 부분 수정 필요(requireMatching)
# DB 연결
client = pymongo.MongoClient("mongodb+srv://SD:1234@cluster0.kkapwcd.mongodb.net/?retryWrites=true&w=majority")
db = client['2022SoftwareDesign']
col_match = db['Matching']
# messageSender 객체 생성
messageSender = MessageSender()
##### PD Layer Class #####
class Matching:
# Super Class
def __init__(self, matchingIdx: int, success: bool, helpee: UserCommonInfo, helper: UserCommonInfo, reqDate: str,
address: Address):
self.__matchingIdx = matchingIdx
self.__requestDate = reqDate
self.__address = address
self.__matchingSuccess = success
self.__matchedHelpeeInfo = helpee
self.__matchedHelperInfo = helper
def requireMatching(self, helper: UserCommonInfo):
pass
def delete():
pass
# Matching Getter
def getMatchingIdx(self):
return self.__matchingIdx
def getMatchingSuccess(self):
return self.__matchingSuccess
def getRequestDate(self):
return self.__requestDate
def getAddress(self):
return self.__address
def getHelperInfo(self):
return self.__matchedHelperInfo
def getHelpeeInfo(self):
return self.__matchedHelpeeInfo
# Accompany (병원 동행)
class Accompany(Matching):
# 초기화: 매칭번호, 매칭완료여부, 헬피userInfo, 헬퍼userInfo, 주소, 병원주소, 동행이유
def __init__(self, matchingIdx: int, success: bool, helpee: UserCommonInfo, helper: UserCommonInfo, reqDate: str,
address: Address, hospitalLocation: Address, reason: str):
self.__matchingIdx = matchingIdx
self.__requestDate = reqDate
self.__address = address
self.__hospitalLocation = hospitalLocation
self.__reason = reason
self.__matchingSuccess = success
self.__matchedHelpeeInfo = helpee
self.__matchedHelperInfo = helper
def requireMatching(self, helper: UserCommonInfo):
self.__matchingSuccess = True
self.__matchedHelperInfo = helper
messageSender.sendMatching(self, self.getHelperInfo(), self.getHelpeeInfo(), "병원 동행")
def update(self, reqDate: str, address: Address, hospitalLoc: Address, reason: str):
self.__requestDate = reqDate
self.__address = address
self.__hospitalLocation = hospitalLoc
self.__reason = reason
self.__matchedHelpeeInfo = DB_getUser(self.__matchedHelpeeInfo.ID).getUserInfo()
def delete(self):
deleteMatching(self.__matchingIdx)
# Accompany Getter
def getMatchingIdx(self):
return self.__matchingIdx
def getMatchingSuccess(self):
return self.__matchingSuccess
def getRequestDate(self):
return self.__requestDate
def getAddress(self):
return self.__address
def getHospitalLocation(self):
return self.__hospitalLocation
def getReason(self):
return self.__reason
def getHelperInfo(self):
return self.__matchedHelperInfo
def getHelpeeInfo(self):
return self.__matchedHelpeeInfo
class Counsel(Matching):
# 초기화: 매칭번호, 매칭완료여부, 헬피userInfo, 헬퍼userInfo, 요청날짜(+시간), 주소, 상담유형
def __init__(self, matchingIdx: int, success: bool, helpee: UserCommonInfo, helper: UserCommonInfo, reqDate: str,
address: Address, category: str):
self.__matchingIdx = matchingIdx
self.__requestDate = reqDate
self.__address = address
self.__category = category
self.__matchingSuccess = success
self.__matchedHelpeeInfo = helpee
self.__matchedHelperInfo = helper
def requireMatching(self, helper: UserCommonInfo):
self.__matchingSuccess = True
self.__matchedHelperInfo = helper
messageSender.sendMatching(self, self.getHelperInfo(), self.getHelpeeInfo(), "심리 상담")
# MessageSender.sendAppointment(self, helper.getUserInfo(), helpee.getUserInfo(), "심리 상담")
def update(self, reqDate: str, address: Address, category: str):
self.__requestDate = reqDate
self.__address = address
self.__category = category
self.__matchedHelpeeInfo = DB_getUser(self.__matchedHelpeeInfo.ID).getUserInfo()
def delete(self):
deleteMatching(self.__matchingIdx)
# Counsel Getter
def getMatchingIdx(self):
return self.__matchingIdx
def getMatchingSuccess(self):
return self.__matchingSuccess
def getRequestDate(self):
return self.__requestDate
def getAddress(self):
return self.__address
def getCategory(self):
return self.__category
def getHelperInfo(self):
return self.__matchedHelperInfo
def getHelpeeInfo(self):
return self.__matchedHelpeeInfo
class SafetyCheck(Matching):
# 초기화: 매칭번호, 매칭완료여부, 헬피userInfo, 헬퍼userInfo, 요청날짜(+시간), 주소, 점검부분
def __init__(self, matchingIdx: int, success: bool, helpee: UserCommonInfo, helper: UserCommonInfo, reqDate: str,
address: Address, checkPart: str):
self.__matchingIdx = matchingIdx
self.__requestDate = reqDate
self.__address = address
self.__checkPart = checkPart
self.__matchingSuccess = success
self.__matchedHelpeeInfo = helpee
self.__matchedHelperInfo = helper
def requireMatching(self, helper: UserCommonInfo):
self.__matchingSuccess = True
self.__matchedHelperInfo = helper
messageSender.sendMatching(self, self.getHelperInfo(), self.getHelpeeInfo(), "안전 점검")
# MessageSender.sendAppointment(self, helper.getUserInfo(), helpee.getUserInfo(), "안전 점검")
def update(self, reqDate: str, address: Address, checkPart: str):
self.__requestDate = reqDate
self.__address = address
self.__checkPart = checkPart
self.__matchedHelpeeInfo = DB_getUser(self.__matchedHelpeeInfo.ID).getUserInfo()
def delete(self):
deleteMatching(self.__matchingIdx)
# Counsel Getter
def getMatchingIdx(self):
return self.__matchingIdx
def getMatchingSuccess(self):
return self.__matchingSuccess
def getRequestDate(self):
return self.__requestDate
def getAddress(self):
return self.__address
def getCheckPart(self):
return self.__checkPart
def getHelperInfo(self):
return self.__matchedHelperInfo
def getHelpeeInfo(self):
return self.__matchedHelpeeInfo
##### Matching DB #####
# 새로운 매칭 번호 / ret: int
def getMatchingNumber():
result = col_match.find().sort('matchingIdx', -1).limit(1)
for i in result:
return i['matchingIdx'] + 1
return 1
# 매칭 추가
def DB_addMatching(matching):
element = matching.__dict__
if type(matching) == Accompany:
element['matchingIdx'] = element['_Accompany__matchingIdx']
element['_Accompany__address'] = matching.getAddress().__dict__
element['_Accompany__matchedHelpeeInfo'] = matching.getHelpeeInfo().__dict__
if element['_Accompany__matchedHelperInfo'] != None:
element['_Accompany__matchedHelperInfo'] = matching.getHelperInfo().__dict__
element['_Accompany__hospitalLocation'] = matching.getHospitalLocation().__dict__
elif type(matching) == Counsel:
element['matchingIdx'] = element['_Counsel__matchingIdx']
element['_Counsel__address'] = matching.getAddress().__dict__
element['_Counsel__matchedHelpeeInfo'] = matching.getHelpeeInfo().__dict__
if element['_Counsel__matchedHelperInfo'] != None:
element['_Counsel__matchedHelperInfo'] = matching.getHelperInfo().__dict__
else:
element['matchingIdx'] = element['_SafetyCheck__matchingIdx']
element['_SafetyCheck__address'] = matching.getAddress().__dict__
element['_SafetyCheck__matchedHelpeeInfo'] = matching.getHelpeeInfo().__dict__
if element['_SafetyCheck__matchedHelperInfo'] != None:
element['_SafetyCheck__matchedHelperInfo'] = matching.getHelperInfo().__dict__
col_match.insert_one(element)
# 매칭 삭제
def DB_deleteMatching(matchingIdx: int):
col_match.delete_one({'matchingIdx': matchingIdx})
# 매칭 불러오기 / ret: Matching(Accompany or Cousel or SafetyCheck)
def getMatching(matchingIdx: int):
result = col_match.find_one({'matchingIdx': matchingIdx})
if result.get('_Accompany__matchingIdx') != None:
helpee = data_to_UserInfo(result, '_Accompany__matchedHelpeeInfo')
if result['_Accompany__matchedHelperInfo'] == None:
helper = None
else:
helper = data_to_UserInfo(result, '_Accompany__matchedHelperInfo')
addr = data_to_Address(result, '_Accompany__address')
hospitalAddr = data_to_Address(result, '_Accompany__hospitalLocation')
matching = Accompany(result['_Accompany__matchingIdx'], result['_Accompany__matchingSuccess'], helpee, helper,
result['_Accompany__requestDate'], addr, hospitalAddr, result['_Accompany__reason'])
elif result.get('_Counsel__matchingIdx') != None:
helpee = data_to_UserInfo(result, '_Counsel__matchedHelpeeInfo')
if result['_Counsel__matchedHelperInfo'] == None:
helper = None
else:
helper = data_to_UserInfo(result, '_Counsel__matchedHelperInfo')
addr = data_to_Address(result, '_Counsel__address')
matching = Counsel(result['_Counsel__matchingIdx'], result['_Counsel__matchingSuccess'], helpee, helper,
result['_Counsel__requestDate'], addr, result['_Counsel__category'])
else:
print(result)
matching = 0
helpee = data_to_UserInfo(result, '_SafetyCheck__matchedHelpeeInfo')
if result['_SafetyCheck__matchedHelperInfo'] == None:
helper = None
else:
helper = data_to_UserInfo(result, '_SafetyCheck__matchedHelperInfo')
addr = data_to_Address(result, '_SafetyCheck__address')
matching = SafetyCheck(result['_SafetyCheck__matchingIdx'], result['_SafetyCheck__matchingSuccess'], helpee,
helper,
result['_SafetyCheck__requestDate'], addr, result['_SafetyCheck__checkPart'])
return matching
# 매칭 업데이트
def DB_updateMatching(matching):
DB_deleteMatching(matching.getMatchingIdx())
DB_addMatching(matching)
# 총 매칭 수 / ret: int
def getAllMatchingPageCount(helpType: int):
result = col_match.find({})
cnt = 0
for element in result:
if (element.get('_Accompany__matchingIdx') != None) and (helpType & HelperType.ACCOMPANY):
if element['_Accompany__matchingSuccess'] == False:
cnt += 1
elif (element.get('_Counsel__matchingIdx') != None) and (helpType & HelperType.COUNSEL):
if element['_Counsel__matchingSuccess'] == False:
cnt += 1
elif (element.get('_SafetyCheck__matchingIdx') != None) and (helpType & HelperType.SAFETYCHECK):
if element['_SafetyCheck__matchingSuccess'] == False:
cnt += 1
if cnt % 20 != 0:
return int(cnt / 20) + 1
else:
return int(cnt / 20)
# 매칭 목록 불러오기(페이지 번호별 20개) / ret: list
# list: {'matchingIdx':매칭번호, 'helpType':매칭유형, 'title':제목, 'writerID':작성자ID, 'reqDate':요청일자}
def getMatchingList(pageNumber: int, helpType: int):
matchingIdx = getMatchingNumber() - 1
ret = []
matchingCnt = 0
while matchingIdx > 0 and len(ret) < 20:
element = col_match.find_one({'matchingIdx': matchingIdx})
matchingIdx -= 1
if element == None:
continue
if (element.get('_Accompany__matchingIdx') != None) and (helpType & HelperType.ACCOMPANY):
if element['_Accompany__matchingSuccess'] == True:
continue
matchingCnt += 1
if matchingCnt <= (pageNumber - 1) * 20:
continue
else:
title = '[' + element['_Accompany__hospitalLocation']['region'] + ':' + \
element['_Accompany__hospitalLocation']['placeName'] + '] ' + element['_Accompany__reason']
if (len(title) > 30):
title = title[:30] + '...'
ret.append({'matchingIdx': element['_Accompany__matchingIdx'], 'helpType': '병원동행',
'title': title, 'writerID': element['_Accompany__matchedHelpeeInfo']['ID'],
'reqDate': element['_Accompany__requestDate']})
elif (element.get('_Counsel__matchingIdx') != None) and (helpType & HelperType.COUNSEL):
if element['_Counsel__matchingSuccess'] == True:
continue
matchingCnt += 1
if matchingCnt <= (pageNumber - 1) * 20:
continue
else:
title = '[' + element['_Counsel__address']['region'] + '] ' + element['_Counsel__category']
if (len(title) > 30):
title = title[:30] + '...'
ret.append({'matchingIdx': element['_Counsel__matchingIdx'], 'helpType': '심리상담',
'title': title, 'writerID': element['_Counsel__matchedHelpeeInfo']['ID'],
'reqDate': element['_Counsel__requestDate']})
elif (element.get('_SafetyCheck__matchingIdx') != None) and (helpType & HelperType.SAFETYCHECK):
if element['_SafetyCheck__matchingSuccess'] == True:
continue
matchingCnt += 1
if matchingCnt <= (pageNumber - 1) * 20:
continue
else:
title = '[' + element['_SafetyCheck__address']['region'] + '] ' + element['_SafetyCheck__checkPart']
if (len(title) > 30):
title = title[:30] + '...'
ret.append({'matchingIdx': element['_SafetyCheck__matchingIdx'], 'helpType': '안전점검',
'title': title, 'writerID': element['_SafetyCheck__matchedHelpeeInfo']['ID'],
'reqDate': element['_SafetyCheck__requestDate']})
return ret
# 해당 유저의 매칭요청 목록 불러오기 / ret: list
# list 형태는 getMatchingList에 'status':상태(매칭완료 여부) 추가
def getUserRequestList(postNumList: list):
ret = []
for i in postNumList:
element = col_match.find_one({'_Post__postIdx': i})
if element.get('_Accompany__matchingIdx') != None:
title = '[' + element['_Accompany__hospitalLocation']['region'] + ':' + \
element['_Accompany__hospitalLocation']['placeName'] + '] ' + element['_Accompany__reason']
if (len(title) > 30):
title = title[:30] + '...'
ret.append({'matchingIdx': element['_Accompany__matchingIdx'], 'helpType': '병원동행',
'title': title, 'writerID': element['_Accompany__matchedHelpeeInfo']['ID'],
'reqDate': element['_Accompany__requestDate'],
'status': element['_Accompany__matchingSuccess']})
elif element.get('_Counsel__matchingIdx') != None:
title = '[' + element['_Counsel__address']['region'] + '] ' + element['_Counsel__category']
if (len(title) > 30):
title = title[:30] + '...'
ret.append({'matchingIdx': element['_Counsel__matchingIdx'], 'helpType': '심리상담',
'title': title, 'writerID': element['_Counsel__matchedHelpeeInfo']['ID'],
'reqDate': element['_Counsel__requestDate'], 'status': element['_Counsel__matchingSuccess']})
elif element.get('_SafetyCheck__matchingIdx') != None:
title = '[' + element['_SafetyCheck__address']['region'] + '] ' + element['_SafetyCheck__checkPart']
if (len(title) > 30):
title = title[:30] + '...'
ret.append({'matchingIdx': element['_SafetyCheck__matchingIdx'], 'helpType': '안전점검',
'title': title, 'writerID': element['_SafetyCheck__matchedHelpeeInfo']['ID'],
'reqDate': element['_SafetyCheck__requestDate'],
'status': element['_SafetyCheck__matchingSuccess']})
return ret
# 해당 유저(도우미)의 매칭 목록 불러오기 / ret: list
# list 형태는 getMatchingList와 같음
def getUserHelpList(postNumList: list):
ret = []
for i in postNumList:
element = col_match.find_one({'_Post__postIdx': i})
if element.get('_Accompany__matchingIdx') != None:
title = '[' + element['_Accompany__hospitalLocation']['region'] + ':' + \
element['_Accompany__hospitalLocation']['placeName'] + '] ' + element['_Accompany__reason']
if (len(title) > 30):
title = title[:30] + '...'
ret.append({'matchingIdx': element['_Accompany__matchingIdx'], 'helpType': '병원동행',
'title': title, 'writerID': element['_Accompany__matchedHelpeeInfo']['ID'],
'reqDate': element['_Accompany__requestDate']})
elif element.get('_Counsel__matchingIdx') != None:
title = '[' + element['_Counsel__address']['region'] + '] ' + element['_Counsel__category']
if (len(title) > 30):
title = title[:30] + '...'
ret.append({'matchingIdx': element['_Counsel__matchingIdx'], 'helpType': '심리상담',
'title': title, 'writerID': element['_Counsel__matchedHelpeeInfo']['ID'],
'reqDate': element['_Counsel__requestDate']})
elif element.get('_SafetyCheck__matchingIdx') != None:
title = '[' + element['_SafetyCheck__address']['region'] + '] ' + element['_SafetyCheck__checkPart']
if (len(title) > 30):
title = title[:30] + '...'
ret.append({'matchingIdx': element['_SafetyCheck__matchingIdx'], 'helpType': '안전점검',
'title': title, 'writerID': element['_SafetyCheck__matchedHelpeeInfo']['ID'],
'reqDate': element['_SafetyCheck__requestDate']})
return ret
##### Method(app.py) #####
# Matching 불러오기 >> getMatching(matchingIdx:int)
# 매칭 페이지 수 >> getAllPostPageCount(helpType:int)
# 매칭 목록 불러오기 >> getMatchingList(pageNumber:int, helpType:int)
# 유저 매칭요청 목록 불러오기 >> getUserRequestList(postNumList:list)
# 유저(도우미) 매칭 목록 불러오기 >> getUserHelpList(postNumList:list)
# 새로운 매칭(병원동행) 생성 :: 헬피ID, 요청날짜, 주소, 병원주소, 동행이유
def createAccompany(helpeeID: str, reqDate: str, address: Address, hospitalLoc: Address, reason: str):
user = DB_getUser(helpeeID)
userInfo = user.getUserInfo()
matchingIdx = getMatchingNumber()
matching = Accompany(matchingIdx, False, userInfo, None, reqDate, address, hospitalLoc, reason)
DB_addMatching(matching)
user.addMyRequest(matchingIdx)
DB_updateUser(user)
# 매칭 수정(병원동행) :: 요청날짜, 주소, 병원주소, 동행이유
def modifyAccompany(matchingIdx: int, reqDate: str, address: Address, hospitalLoc: Address, reason: str):
matching = getMatching(matchingIdx)
matching.update(reqDate, address, hospitalLoc, reason)
DB_updateMatching(matching)
# 새로운 매칭(심리상담) 생성 :: 헬피ID, 요청날짜, 주소, 상담유형
def createCounsel(helpeeID: str, reqDate: str, address: Address, category: str):
user = DB_getUser(helpeeID)
userInfo = user.getUserInfo()
matchingIdx = getMatchingNumber()
matching = Counsel(matchingIdx, False, userInfo, None, reqDate, address, category)
DB_addMatching(matching)
user.addMyRequest(matchingIdx)
DB_updateUser(user)
# 매칭 수정(심리상담) :: 요청날짜, 주소, 유형
def modifyCounsel(matchingIdx: int, reqDate: str, address: Address, category: str):
matching = getMatching(matchingIdx)
matching.update(reqDate, address, category)
DB_updateMatching(matching)
# 새로운 매칭(안전점검) 생성 :: 헬피ID, 요청날짜, 주소, 점검부분
def createSafetyCheck(helpeeID: str, reqDate: str, address: Address, checkPart: str):
user = DB_getUser(helpeeID)
userInfo = user.getUserInfo()
matchingIdx = getMatchingNumber()
matching = SafetyCheck(matchingIdx, False, userInfo, None, reqDate, address, checkPart)
DB_addMatching(matching)
user.addMyRequest(matchingIdx)
DB_updateUser(user)
# 매칭 수정(안전점검) :: 요청날짜, 주소, 점검부분
def modifySafetyCheck(matchingIdx: int, reqDate: str, address: Address, checkPart: str):
matching = getMatching(matchingIdx)
matching.update(reqDate, address, checkPart)
DB_updateMatching(matching)
# 매칭 삭제 :: 매칭 번호
def deleteMatching(matchingIdx: int):
matching = getMatching(matchingIdx)
DB_deleteMatching(matchingIdx)
user = DB_getUser(matching.getHelpeeInfo().ID)
user.deleteMyRequest(matchingIdx)
DB_updateUser(user)
# 매칭 하기 :: 매칭 번호, 헬퍼ID
def consentMatching(matchingIdx: int, helperID: str):
matching = getMatching(matchingIdx)
helper = DB_getUser(helperID)
userInfo = helper.getUserInfo()
matching.requireMatching(userInfo)
DB_updateMatching(matching)
helper.addmyHelp(matchingIdx)
DB_updateUser(helper)