From 9c271816525b4ef5c34cb503fd63ede1a1a2309c Mon Sep 17 00:00:00 2001
From: Jonathan Weth <git@jonathanweth.de>
Date: Wed, 25 Sep 2019 14:04:29 +0200
Subject: [PATCH] Add drive to cache | Refactoring | Add refresh command to use
 in cron

---
 biscuit/apps/untis/drive.py | 11 +++++++++--
 biscuit/apps/untis/parse.py |  8 ++++----
 biscuit/apps/untis/plan.py  | 14 +++++++-------
 biscuit/apps/untis/sub.py   |  6 +-----
 4 files changed, 21 insertions(+), 18 deletions(-)

diff --git a/biscuit/apps/untis/drive.py b/biscuit/apps/untis/drive.py
index 8a47aa0..a576278 100644
--- a/biscuit/apps/untis/drive.py
+++ b/biscuit/apps/untis/drive.py
@@ -1,6 +1,12 @@
+from dashboard.caches import DRIVE_CACHE, Cache
 from .api import *
 
-def build_drive():
+
+def build_drive(force_update=False):
+    cached = DRIVE_CACHE.get()
+    if cached is not False and not force_update:
+        print("Drive come from cache")
+        return cached
     odrive = {
         "teachers": get_all_teachers(),
         "rooms": get_all_rooms(),
@@ -16,7 +22,8 @@ def build_drive():
             id = el.id
             drive[key][id] = el
 
+    DRIVE_CACHE.update(drive)
     return drive
 
 
-drive = build_drive()
\ No newline at end of file
+drive = build_drive()
diff --git a/biscuit/apps/untis/parse.py b/biscuit/apps/untis/parse.py
index c827cb2..7c001bd 100755
--- a/biscuit/apps/untis/parse.py
+++ b/biscuit/apps/untis/parse.py
@@ -118,12 +118,12 @@ class LessonTime(object):
         self.rooms = rooms
 
 
-def parse():
+def parse(force_update=False):
     global drive
 
     cached = caches.PARSED_LESSONS_CACHE.get()
-    if cached is not False:
-        print("Lessons come from cache")
+    if cached is not False and not force_update:
+        # print("Lessons come from cache")
         return cached
     lessons = []
 
@@ -139,7 +139,7 @@ def parse():
 
             lessons.append(lesson_obj)
 
-    print("Lesson cache was refreshed")
+    # print("Lesson cache was refreshed")
     caches.PARSED_LESSONS_CACHE.update(lessons)
 
     return lessons
diff --git a/biscuit/apps/untis/plan.py b/biscuit/apps/untis/plan.py
index f171cf0..d8c34ce 100644
--- a/biscuit/apps/untis/plan.py
+++ b/biscuit/apps/untis/plan.py
@@ -2,7 +2,7 @@ import datetime
 
 from django.utils import timezone
 
-from dashboard import caches
+from dashboard import caches, plan_caches
 from schoolapps import settings
 from schoolapps.settings import LESSONS
 from untisconnect.api import format_classes, TYPE_CLASS, TYPE_TEACHER, TYPE_ROOM
@@ -60,15 +60,15 @@ def parse_lesson_times():
     return times
 
 
-def get_plan(type, id, smart=False, monday_of_week=None):
+def get_plan(type, id, smart=False, monday_of_week=None, force_update=False):
     """ Generates a plan for type (TYPE_TEACHER, TYPE_CLASS, TYPE_ROOM) and a id of the teacher (class, room)"""
     # Check cache
-    cache = caches.get_cache_for_plan(type, id, smart, monday_of_week)
+    cache = plan_caches.get_cache_for_plan(type, id, smart, monday_of_week)
 
     cached = cache.get()
-    print(cached)
-    if cached is not False:
-        print("Plan come from cache", cache.id)
+    # print(cached)
+    if cached is not False and not force_update:
+        # print("Plan come from cache", cache.id)
         return cached
 
     # Get parsed lessons
@@ -229,6 +229,6 @@ def get_plan(type, id, smart=False, monday_of_week=None):
                     for j in range(event.event.from_lesson - 1, event.event.to_lesson):
                         plan[j][0][i].append(element_container)
 
-    print("Refresh plan cache for", cache.id)
+    # print("Refresh plan cache for", cache.id)
     cache.update(plan)
     return plan
diff --git a/biscuit/apps/untis/sub.py b/biscuit/apps/untis/sub.py
index a51afdc..4cb821a 100644
--- a/biscuit/apps/untis/sub.py
+++ b/biscuit/apps/untis/sub.py
@@ -5,7 +5,7 @@ from untisconnect.api import run_default_filter, row_by_row_helper, format_class
     TYPE_TEACHER
 from untisconnect.api_helper import run_using, untis_split_first, untis_date_to_date, date_to_untis_date
 from untisconnect.parse import get_lesson_element_by_id_and_teacher
-from untisconnect.drive import build_drive
+from untisconnect.drive import drive
 
 TYPE_SUBSTITUTION = 0
 TYPE_CANCELLATION = 1
@@ -28,10 +28,6 @@ def parse_type_of_untis_flags(flags):
     return type_
 
 
-# Build cache
-drive = build_drive()
-
-
 class Substitution(object):
     def __init__(self):
         self.filled = False
-- 
GitLab