-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpopulate_foerderschwerpunkte.py
More file actions
219 lines (172 loc) · 6.92 KB
/
populate_foerderschwerpunkte.py
File metadata and controls
219 lines (172 loc) · 6.92 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
"""
Modul zum Befüllen des Katalogs 'Foerderschwerpunkte' im SVWS-Server.
Basierend auf der Schulform aus den Stammdaten.
"""
import requests
import json
import urllib3
from pathlib import Path
from datetime import datetime
# SSL-Warnungen deaktivieren (für selbstsignierte Zertifikate)
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
def load_config(config_file='config.json'):
"""Lädt die Konfiguration aus der JSON-Datei."""
config_path = Path(__file__).parent / config_file
with open(config_path, 'r', encoding='utf-8') as f:
return json.load(f)
def get_school_data(config):
"""
Ruft die Schuldaten vom Server ab, um die Schulform zu ermitteln.
Args:
config: Konfigurationsdictionary mit Verbindungsdaten
Returns:
dict: Schuldaten mit Schulform (schulform), oder None bei Fehler
"""
db_config = config['database']
server = db_config['server']
port = db_config['httpsport']
schema = db_config['schema']
username = db_config['username']
password = db_config['password']
url = f"https://{server}:{port}/db/{schema}/schule/stammdaten"
try:
response = requests.get(
url,
auth=(username, password),
headers={
'Accept': 'application/json'
},
verify=False,
timeout=30
)
if response.status_code == 200:
return response.json()
else:
print(f"Fehler beim Abrufen der Schuldaten: HTTP {response.status_code}")
return None
except requests.exceptions.RequestException as e:
print(f"Fehler beim Abrufen der Schuldaten: {e}")
return None
def load_foerderschwerpunkt_data():
"""Lädt die Foerderschwerpunkte aus der JSON-Datei."""
data_path = Path(__file__).parent / 'statistikdaten' / 'Foerderschwerpunkt.json'
with open(data_path, 'r', encoding='utf-8') as f:
data = json.load(f)
return data['daten']
def get_current_year():
"""Gibt das aktuelle Jahr zurück."""
return datetime.now().year
def filter_foerderschwerpunkte_for_schulform(foerderschwerpunkte_data, schulform):
"""
Filtert die Foerderschwerpunkte für eine bestimmte Schulform.
Verwendet die aktuellste gültige Version basierend auf dem aktuellen Jahr.
Args:
foerderschwerpunkte_data: Die Daten aus Foerderschwerpunkt.json
schulform: Die Schulform (z.B. "GE")
Returns:
list: Liste der gültigen Foerderschwerpunkte
"""
current_year = get_current_year()
result = []
for fs_group in foerderschwerpunkte_data:
# Finde die aktuellste gültige Version
best_entry = None
for entry in fs_group['historie']:
# Prüfe ob Schulform in dieser Variante unterstützt wird
if schulform not in entry['schulformen']:
continue
# Prüfe ob diese Variante für das aktuelle Jahr gültig ist
gueltig_von = entry.get('gueltigVon')
gueltig_bis = entry.get('gueltigBis')
is_valid = True
if gueltig_von is not None and current_year < gueltig_von:
is_valid = False
if gueltig_bis is not None and current_year > gueltig_bis:
is_valid = False
if is_valid:
# Wähle die neueste Variante (höchste gueltigVon)
if best_entry is None or (entry.get('gueltigVon') or 0) > (best_entry.get('gueltigVon') or 0):
best_entry = entry
if best_entry:
result.append({
'kuerzel': best_entry['kuerzel'],
'kuerzelStatistik': best_entry['schluessel'],
'sortierung': len(result) + 1
})
return result
def populate_foerderschwerpunkte(config):
"""
Befüllt den Katalog 'Foerderschwerpunkte' basierend auf der Schulform.
Args:
config: Konfigurationsdictionary mit Verbindungsdaten
"""
db_config = config['database']
server = db_config['server']
port = db_config['httpsport']
schema = db_config['schema']
username = db_config['username']
password = db_config['password']
# Abrufen der Schuldaten
print("Rufe Schuldaten ab...")
school_data = get_school_data(config)
if not school_data:
print("Fehler: Schuldaten konnten nicht abgerufen werden.")
return 0, 1
schulform = school_data.get('schulform')
print(f"Schulform: {schulform}")
print()
# Laden der Foerderschwerpunkt-Daten
try:
foerderschwerpunkte_data = load_foerderschwerpunkt_data()
except Exception as e:
print(f"Fehler beim Laden der Foerderschwerpunkt-Datei: {e}")
return 0, 1
# Filtern für diese Schulform
foerderschwerpunkte = filter_foerderschwerpunkte_for_schulform(foerderschwerpunkte_data, schulform)
url = f"https://{server}:{port}/db/{schema}/foerderschwerpunkte/create"
print(f"Befülle Katalog 'Foerderschwerpunkte' mit {len(foerderschwerpunkte)} Einträgen...")
print(f"URL: {url}")
print(f"Using username: {username}")
print()
created_count = 0
failed_count = 0
for i, fs in enumerate(foerderschwerpunkte, 1):
body = {
"kuerzel": fs['kuerzel'],
"kuerzelStatistik": fs['kuerzelStatistik'],
"istSichtbar": True,
"sortierung": fs['sortierung']
}
try:
response = requests.post(
url,
auth=(username, password),
json=body,
headers={
'Content-Type': 'application/json',
'Accept': 'application/json'
},
verify=False,
timeout=30
)
print(f"[{i}/{len(foerderschwerpunkte)}] {fs['kuerzel']} ({fs['kuerzelStatistik']}): ", end="")
if response.status_code == 200 or response.status_code == 201:
print(f"✓ (HTTP {response.status_code})")
created_count += 1
else:
print(f"✗ (HTTP {response.status_code})")
print(f" Response: {response.text[:200]}")
failed_count += 1
except requests.exceptions.RequestException as e:
print(f"[{i}/{len(foerderschwerpunkte)}] {fs['kuerzel']}: ✗ Fehler: {e}")
failed_count += 1
print()
print(f"Ergebnis: {created_count} erfolgreich, {failed_count} fehlgeschlagen")
if failed_count == 0:
print("✓ Alle Foerderschwerpunkte erfolgreich erstellt!")
else:
print(f"⚠ {failed_count} Einträge konnten nicht erstellt werden.")
return created_count, failed_count
if __name__ == '__main__':
config = load_config()
populate_foerderschwerpunkte(config)