Skip to content
Snippets Groups Projects
Verified Commit 1a504589 authored by Jonathan Weth's avatar Jonathan Weth :keyboard:
Browse files

Build own import references for time periods and breaks as dicts

parent 77732e1e
No related branches found
No related tags found
1 merge request!13Resolve "Support import from MySQL"
......@@ -341,10 +341,10 @@ def import_supervision_areas() -> Dict[int, chronos_models.SupervisionArea]:
return ref
def import_time_periods_and_breaks() -> List[List[chronos_models.TimePeriod]]:
def import_time_periods() -> Dict[int, Dict[int, chronos_models.TimePeriod]]:
""" Import time periods an breaks """
ref = []
time_periods_ref = {}
periods = (
run_default_filter(mysql_models.Commondata.objects, filter_term=False)
.filter(id=40) # Fixed UNTIS constant
......@@ -380,22 +380,30 @@ def import_time_periods_and_breaks() -> List[List[chronos_models.TimePeriod]]:
logger.info(" Time period updated")
# Build index with time periods
if len(ref) < weekday + 1:
ref.append([])
ref[weekday].append(new_time_period)
if weekday not in time_periods_ref:
time_periods_ref[weekday] = {}
time_periods_ref[weekday][period] = new_time_period
return time_periods_ref
def import_breaks(
time_periods_ref: Dict[int, Dict[int, chronos_models.TimePeriod]],
) -> Dict[int, Dict[int, chronos_models.Break]]:
# Build breaks for all weekdays
for weekday, time_periods in enumerate(ref):
breaks_ref = {}
for weekday, time_periods in time_periods_ref.items():
breaks_ref[weekday] = {}
# Add None two times in order to create breaks before first lesson and after last lesson
time_periods = [None] + time_periods + [None]
for i, time_period in enumerate(time_periods):
time_periods_for_breaks = [None] + list(time_periods.values()) + [None]
for i, time_period in enumerate(time_periods_for_breaks):
# If last item (None) is reached, no further break must be created
if i + 1 == len(time_periods):
if i + 1 == len(time_periods_for_breaks):
break
after_period = time_period
before_period = time_periods[i + 1]
before_period = time_periods_for_breaks[i + 1]
short_name = "{}: {}./{}.".format(
weekday,
......@@ -414,4 +422,8 @@ def import_time_periods_and_breaks() -> List[List[chronos_models.TimePeriod]]:
if created:
logger.info(" New break created")
return ref
# Save index with lesson after break
next_period = after_period.period if after_period else before_period.period + 1
breaks_ref[weekday][next_period] = new_break
return breaks_ref
......@@ -68,7 +68,7 @@ def import_lessons(
time_periods = []
rooms_per_periods = []
for el in raw_time_data_2:
day = int(el[1])
weekday = int(el[1]) - 1
hour = int(el[2])
room_ids = untis_split_third(el[3], conv=int)
......@@ -79,7 +79,7 @@ def import_lessons(
rooms.append(r)
# Get time period
time_period = time_periods_ref[day - 1][hour - 1]
time_period = time_periods_ref[weekday][hour]
time_periods.append(time_period)
rooms_per_periods.append(rooms)
......
......@@ -4,8 +4,8 @@ from .importers.common_data import (
import_rooms,
import_supervision_areas,
import_teachers,
import_time_periods_and_breaks,
)
import_time_periods,
import_breaks)
from .importers.lessons import import_lessons
......@@ -13,14 +13,18 @@ def untis_import_mysql():
# Coomon data for Chronos
subjects_ref = import_subjects()
rooms_ref = import_rooms()
supervision_areas_ref = import_supervision_areas()
# Common data for core
teachers_ref = import_teachers()
classes_ref = import_classes(teachers_ref)
# Time periods
time_periods_ref = import_time_periods_and_breaks()
time_periods_ref = import_time_periods()
breaks_ref = import_breaks(time_periods_ref)
# Supervisions
supervision_areas_ref = import_supervision_areas()
# Lessons
import_lessons(time_periods_ref, rooms_ref, subjects_ref, teachers_ref, classes_ref)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment