-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfirebase_migration.py
121 lines (105 loc) · 4.2 KB
/
firebase_migration.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
import json
import sys
sys.dont_write_bytecode = True
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "perak.settings")
import django
django.setup()
from firebase_utils.db_storage import db, bucket
from match import models
migration_path = os.path.join("firebase_utils", "migration.json")
def check_category(dict):
for key in dict.keys():
try:
dict[key] = models.Category.objects.get(name=key)
print("{} category object found".format(key.title()))
except models.Category.DoesNotExist:
print("{} category object not found".format(key.title()))
dict[key] = models.Category(name=key)
dict[key].save()
print("Done creating {} category".format(key))
collection = {
'futsal': db.collection(u'futsal-team'),
'dota': db.collection(u'dota'),
'csgo': db.collection(u'csgo')
}
teams = {
'dota': [],
'csgo': [],
'futsal': []
}
category = {
'dota': None,
'csgo': None,
'futsal': None
}
if not os.path.exists(migration_path):
print('Initial migrate')
with open(migration_path, 'w') as file:
check_category(category)
data = {
'futsal_teams': [],
'dota_teams': [],
'csgo_teams': []
}
json.dump(data, file)
else:
print("Migration data found")
with open(migration_path, 'r') as file:
check_category(category)
print("Import data.....")
data = json.load(file)
for key in teams.keys():
teams[key] = data['{}_teams'.format(key)]
print("Get data from firebase....")
db_data = {'{}_teams'.format(key): [doc.id for doc in collection[key].stream()] for key in teams.keys()}
print("Checking data....")
change = False
for key in teams.keys():
if key == "futsal":
continue
difference = set(db_data['{}_teams'.format(key)]) - set(teams[key])
if len(difference) != 0:
print("Update data in {} category".format(key))
change = True
for team in difference:
team_name = team.split("-")[0]
print("Get data from team {}".format(team_name))
team_document = collection[key].document(team)
team_document_object = team_document.get().to_dict()
team_data = {
'details': team_document_object,
'logo': bucket.blob(team_document_object['teamLogo']) if "teamLogo" in team_document_object else None,
'players': team_document.collection("player"),
}
if team_data['logo']:
team_data['logo'].make_public()
team_object = models.Team(name=team_name, category=category[key],
team_logo=team_data['logo'].public_url,manager=team_data['details'].get('namaLengkap_manager',""))
else:
team_object = models.Team(name=team_name, category=category[key])
team_object.save()
if key != "futsal":
captain = models.Player(name=team_document_object['namaLengkap_kapten'], category=category[key],
team=team_object, captain=True)
captain.save()
print("Captain {} saved".format(captain))
for player in team_data['players'].stream():
player_data = player.to_dict()
player_picture = bucket.blob(player_data['foto']) if "foto" in player_data else None
if player_picture:
player_picture.make_public()
player_object = models.Player(name=player_data['namaLengkap'], team=team_object,
category=category[key], profile_picture=player_picture.public_url)
else:
player_object = models.Player(name=player_data['namaLengkap'], team=team_object,
category=category[key])
player_object.save()
print("Player {} saved".format(player_object))
print("Team {} saved".format(team_object))
if change:
print("Exporting data...")
with open(migration_path, 'w') as file:
json.dump(db_data, file)
else:
print("No Change")