-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathread_init_data.py
More file actions
347 lines (281 loc) · 14 KB
/
read_init_data.py
File metadata and controls
347 lines (281 loc) · 14 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
import os
import sys, inspect
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'api.settings')
import django
django.setup()
from django.apps import apps
from api.models import *
import pandas as pd
# todo: replace file links with media upload server
# todo: Refactor read Functions into one Function
# todo: maybe convert ids also to int beforehand
# todo: add support for logo file upload
def getDFfromXLXS(filePath):
df = pd.read_excel(filePath, sheet_name=None)['Sheet1']
# delete first row (the row which explains in which format the data should be)
df = df.drop(0)
#strip all newlines and whitespaces in beginning and end
cols = df.select_dtypes(object).columns
df[cols] = df[cols].apply(lambda x: x.str.strip('\n') if type(x)==str else x)
df = df.applymap(lambda x: x.strip() if type(x)==str else x)
#convert PLZ to int
if 'Postleitzahl' in df:
df['Postleitzahl'] = pd.to_numeric(df['Postleitzahl'], errors='coerce') #to float
df['Postleitzahl'] = df['Postleitzahl'].fillna(0) #fill missing values with nan
df = df.astype({'Postleitzahl':'int'}) # convert to int
#replace NaN Values with empty String
df = df.fillna('')
#find columns that are not ID
data = df.columns
for d in data:
if 'ID' in d: data = data.drop(d)
#drop rows that are empty
for index, row in df.iterrows():
if not any((row[d] for d in data)):
df = df.drop(index)
#change dateformat
try:
df['Datum'] = pd.to_datetime(df.Datum, format='%d.%m.%Y')
df['Datum'] = df['Datum'].dt.strftime('%Y-%m-%d')
except AttributeError: pass
return df
def delete_db():
modelMembers = inspect.getmembers(sys.modules[__name__], inspect.isclass)
print('clear db')
for model in modelMembers:
try:
if model[1].objects.all().count() > 0:
model[1].objects.all().delete()
except:
pass
def addMultiValueAttribute(cell, column, cellName = 'entry', tableName = 'table'):
for ID in str(cell).split(","):
if ID in ('nan', ''): continue
ID = int(float(ID.strip()))
try:
column.add(ID)
except:
print(f"Warning: {cellName} with ID {ID} doesn't exist! {tableName} is beeing created without that {cellName}.")
def insertAdress(state, street, zip, city, country, id = None):
#check if some value is nan todo: check if necessary
try:
if [x for x in (street, zip, city, country) if x == '']: #
print(f's:{street}, p:{zip}, c:{city}, co:{country}')
return
except:
pass
#search for address, if it already exists
query = Address.objects.filter(state=state, street=street,
zip=zip, city=city, country=country)
if query: return query[0]
# else insert it in db
address = Address(id=id, state=state, street=street,
zip=zip, city=city, country=country)
address.save()
return address
def readUser(df):
for index, row in df.iterrows():
#check if it already exists
query = User.objects.filter(title=row['Titel'],
firstname=row['Vorname'],
lastname=row['Nachname'],
email=row['Email'],
position=row['Position'])
if query.exists(): continue
#check if new entry overrides old entry on same ID
try:
old = User.objects.get(id = row['UserID'])
print(old)
print(f"Warning: Override existing {old.firstname} {old.lastname} with {row['Vorname']} {row['Nachname']}")
except User.DoesNotExist:
pass
user = User(id=row['UserID'],
title=row['Titel'],
firstname=row['Vorname'],
lastname=row['Nachname'],
email=row['Email'],
position=row['Position'],
photo=row['Foto'])
user.save()
def readEvents(df):
for index, row in df.iterrows():
#check if it already exists
query = ResearchNetworkEvent.objects.filter(name=row['Name'],
photo=row['Foto'],
date=row['Datum'])
if query.exists(): continue
#check if new entry overrides old entry on same ID
try:
old = ResearchNetworkEvent.objects.get(id = row['EventID'])
print(f"Warning: Override existing {old.name} with {row['Name']}")
except ResearchNetworkEvent.DoesNotExist:
pass
event = ResearchNetworkEvent(id=row['EventID'],
name=row['Name'],
photo=row['Foto'],
date=row['Datum'])
event.save()
def readNews(df):
for index, row in df.iterrows():
#check if it already exists
query = ResearchNetworkNews.objects.filter(title=row['Titel'],
photo=row['Foto'],
description=row['Beschreibung'],
eyecatcher=['Eyecatcher'])
if query.exists(): continue
#check and warn if new entry overrides old entry on same ID
try:
old = ResearchNetworkNews.objects.get(id = row['NewsID'])
print(f"Warning: Override existing {old.title} with {row['Titel']}")
except ResearchNetworkNews.DoesNotExist:
pass
news = ResearchNetworkNews(id=row['NewsID'],
title=row['Titel'],
photo=row['Foto'],
description=row['Beschreibung'],
eyecatcher=['Eyecatcher'])
news.save()
def readProjects(df):
for index, row in df.iterrows():
#search if it already exists
query = ResearchProject.objects.filter(name=row['Projektname'],
description=row['Projektbeschreibung'])
if query.exists():
print(f"Warning: Found duplicate Network Entry under ID {query[0].id} and {int(row['ProjektID'])}")
continue
#check if new entry overrides some old entry on same ID
try:
old = ResearchProject.objects.get(id = row['ProjektID'])
print(f"Warning: Override existing {old.name} with {row['Projektname']}")
except ResearchProject.DoesNotExist:
pass
project = ResearchProject(id=row['ProjektID'],
name=row['Projektname'],
description=row['Projektbeschreibung'])
project.save()
def readClusters(df):
for index, row in df.iterrows():
#search if it already exists
query = ResearchCluster.objects.filter(name=row['Clustername'],
description=row['Clusterbeschreibung'])
if query.exists():
print(f"Warning: Found duplicate Cluster Entry under ID {query[0].id} and {int(row['ClusterID'])}")
continue
#check if new entry overrides some old entry on same ID
try:
old = ResearchCluster.objects.get(id = row['ClusterID'])
print(f"Warning: Override existing {old.name} with {row['Clustername']}")
except ResearchCluster.DoesNotExist:
pass
project = ResearchCluster(id=row['ClusterID'],
name=row['Clustername'],
description=row['Clusterbeschreibung'])
project.save()
def readNetworks(df):
for index, row in df.iterrows():
#check if it already exists
query = ResearchNetwork.objects.filter(name=row['Name'],
shortcut=row['Kuerzel'],
description=row['Information'],
research_description=row['Forschung'])
if query.exists():
print(f"Warning: Found duplicate Network Entry under ID {query[0].id} and {int(row['NetworkID'])}")
continue
#check if new entry overrides old entry on same ID
try:
old = ResearchNetwork.objects.get(id = row['NetworkID'])
print(f"Warning: Override existing {old.name} ({old.shortcut}) with {row['Name']} ({row['Kuerzel']})")
except ResearchNetwork.DoesNotExist:
pass
address = insertAdress(state=row['Bundesland'], street=row['Strasse'],
zip=row['Postleitzahl'], city=row['Ort'], country=row['Land'])
rn = ResearchNetwork(id=row['NetworkID'],
name=row['Name'],
shortcut=row['Kuerzel'],
description=row['Information'],
research_description=row['Forschung'],
logo=row['Logo'],
address = address)
rn.save()
addMultiValueAttribute(cell = row['EventIDs'], column = rn.events, cellName = 'Event', tableName = 'ResearchNetwork '+ rn.name + rn.shortcut)
addMultiValueAttribute(cell = row['NewsIDs'], column = rn.news, cellName = 'News', tableName = 'ResearchNetwork ' + rn.name + rn.shortcut)
def readProfiles(df):
for index, row in df.iterrows():
address = insertAdress(state=row['Bundesland'], street=row['Strasse'],
zip=row['Postleitzahl'], city=row['Ort'], country=row['Land'])
institute = None
if row['Organisationsname'] and str(row['Organisationsname']).strip() != '':
try:
institute = Institute.objects.get(title=row['Organisationsname'])
except Institute.DoesNotExist:
print(f"No institute found for '{row['Organisationsname']}' - profile '{row['Institutsname']}' will have no institute")
cp = CompetenceProfile(author = None,
title=row['Institutsname'],
organization=row['Organisationsname'],
description=row['Zusammenfassung'],
shortcut=row['Kuerzel'],
research_focus=row['Forschungsfokus'],
publications = row['Veroeffentlichungen'],
logo = row['Logo'],
address = address,
institute = institute)
cp.save()
addMultiValueAttribute(cell = row['ProjektIDs'],
column = cp.research_projects,
cellName = 'Project', tableName = 'CompetenceProfile '+ cp.shortcut)
addMultiValueAttribute(cell = row['RepresentativeIDs'], column = cp.representatives,
cellName = 'User', tableName = 'CompetenceProfile '+ cp.shortcut)
addMultiValueAttribute(cell = row['NetworkIDs'],
column = cp.research_networks,
cellName = 'Network', tableName = 'CompetenceProfile '+ cp.shortcut)
addMultiValueAttribute(cell = row['ClusterIDs'],
column = cp.research_clusters,
cellName = 'Cluster', tableName = 'CompetenceProfile '+ cp.shortcut)
def readInstitutes(df):
for index, row in df.iterrows():
if not row['Organisationsname'] or str(row['Organisationsname']).strip() == '':
continue
address = insertAdress(state=row['Bundesland'], street=row['Strasse'],
zip=row['Postleitzahl'], city=row['Ort'], country=row['Land'])
institute = Institute(title=row['Organisationsname'],
shortcut=row['institute_short'],
description=row['Zusammenfassung'],
logo=row['Logo'],
address=address)
institute.save()
def synchronize_primary_keys():
with django.db.connection.cursor() as cursor:
# Get a list of all installed app models
app_models = apps.get_models()
print(app_models)
print()
for model in app_models:
if model._meta.managed and model._meta.app_label == 'api': # Check if the model is managed by Django and if it is from our app
table_name = model._meta.db_table
primary_key_column = model._meta.pk.column
query = f"SELECT MAX({primary_key_column}) FROM {table_name};"
cursor.execute(query)
max_id = cursor.fetchone()[0]
print(f"Table: {table_name}, PK: {primary_key_column}, MAX ID: {max_id}")
if max_id is not None:
sequence_name = f"{table_name}_{primary_key_column}_seq"
update_sequence_query = f"SELECT setval('{sequence_name}', {max_id});"
cursor.execute(update_sequence_query)
print(f"Sequence {sequence_name} updated to {max_id}")
else:
print(f"No records in {table_name}")
if __name__ == '__main__':
print("Starting Init_data script...")
delete_db()
FOLDER = "init_data"
readUser (getDFfromXLXS(FOLDER + '/User.xlsx'))
readNews (getDFfromXLXS(FOLDER + '/News.xlsx'))
readEvents (getDFfromXLXS(FOLDER + '/Events.xlsx'))
readNetworks(getDFfromXLXS(FOLDER + '/Research_Networks.xlsx'))
readProjects(getDFfromXLXS(FOLDER + '/Research_Projects.xlsx'))
readClusters(getDFfromXLXS(FOLDER + '/Research_Clusters.xlsx'))
readInstitutes(getDFfromXLXS(FOLDER + '/CompetenceProfile.xlsx'))
readProfiles(getDFfromXLXS(FOLDER + '/CompetenceProfile.xlsx'))
#sync up primary key sequences for all tables
print("synchronizing primary keys now")
synchronize_primary_keys()