-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroutine_zero.py
More file actions
67 lines (54 loc) · 2.34 KB
/
routine_zero.py
File metadata and controls
67 lines (54 loc) · 2.34 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
import os
import firebase_admin
from firebase_admin import firestore, credentials
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "key.json"
cred = credentials.Certificate('key.json')
firebase_admin.initialize_app(cred, {
'projectId': 'momo-89849',
})
db = firestore.client()
user_list = [] #전체 유저 리스트
routine_list = [] #전체 루틴 리스트
finished_all_users = [] #전체 루틴 완료한 유저
finished_not_users = [] #전체 루틴 완료하지 않은 유저
completed_routine = [] #완료된 루틴
not_completed_routine = [] #완료되지 않은 루틴
@firestore.transactional
def routine_zero(transaction, routine_ref):
new_streak = 0
transaction.update(routine_ref, {
'streak': new_streak,
'finished': False
})
def hello_pubsub(event,context):
transaction = db.transaction()
docs = db.collection('User_Collection').stream()
for doc in docs:
user_list.append(doc.id)
# 전체 루틴 리스트 가져오기
for user in user_list:
routine_ref = db.collection('User_Collection').document(user).collection('Routine_Collection').stream()
for routine in routine_ref:
user_routine_dict = routine.to_dict()
routine_list.append(routine)
for user in user_list:
routine_ref = db.collection('User_Collection').document(user).collection('Routine_Collection').stream()
is_all_finished = True
user_routine_dict=[]
# 현재 user의 user_routine 순회
for routine in routine_ref:
user_routine_dict = routine.to_dict()
# 루틴을 완료하지 않은 경우
if user_routine_dict['finished'] == False:
finished_not_users.append(user) #user streak 초기화
not_completed_routine.append(routine) #user streak 초기화 & finished:False로 초기화
is_all_finished = False
# 루틴을 완료한 경우
elif user_routine_dict['finished'] == True:
completed_routine.append(routine) #routine streak +1 & finished:False 초기화
#사용자가 전체 루틴을 완료한 경우
if is_all_finished == True:
finished_all_users.append(user)
# #유저 streak 추가
for r in not_completed_routine:
routine_zero(transaction, r.reference)