From 21d77e09f87b7ab04ce0013985965f4e1c98448a Mon Sep 17 00:00:00 2001 From: Thierry Carrez Date: Tue, 2 Oct 2018 17:36:05 +0200 Subject: [PATCH] Handle all schedule in a single table The bookable rooms functionality was bolted on top of the "normal" schedule table using a separate table in the JSON and the config. Now that bookable rooms are shown together with "regular" rooms, we can merge the two concept and pay back that technical debt. This is an incompatible JSON change so the database will have to be ~reload-ed from configuration. Change-Id: Ia37b6202b363614418b562b693b3dd807621aa97 --- config.json.sample | 8 ++------ html/ptg.html | 6 +++--- html/ptg.js | 40 +++++++++++++--------------------------- ptgbot/db.py | 41 ++++++++++------------------------------- 4 files changed, 28 insertions(+), 67 deletions(-) diff --git a/config.json.sample b/config.json.sample index 89ed1b9..6467f56 100644 --- a/config.json.sample +++ b/config.json.sample @@ -27,10 +27,10 @@ } ] }, - "scheduled": { + "schedule": { "Ontario": { "MonAM": "swift", - "MonPM": "swift", + "MonPM": "", "TueAM": "swift", "TuePM": "swift" }, @@ -38,10 +38,6 @@ "MonAM": "cinder", "MonPM": "cinder", "TueAM": "cinder", - "TuePM": "cinder" } - }, - "extrarooms": { - "Missouri": ["MonAM", "MonPM", "TueAM", "TuePM"] } } diff --git a/html/ptg.html b/html/ptg.html index b860374..0328a30 100644 --- a/html/ptg.html +++ b/html/ptg.html @@ -92,11 +92,11 @@ {{time.desc}} {{/each}} - {{#each @root.scheduled as |schedule room|}} - {{#if (roomactive @root.scheduled @root.additional room times)}} + {{#each @root.schedule as |sched room|}} + {{#if (roomactive @root.schedule room times)}} {{room}} {{#each (lookup @root.slots day) as |time|}} - {{ roomcode @root.scheduled @root.additional room time.name 0 }} + {{ roomcode @root.schedule room time.name 0 }} {{/each}} {{/if}} diff --git a/html/ptg.js b/html/ptg.js index 7270c4d..1ce66c3 100644 --- a/html/ptg.js +++ b/html/ptg.js @@ -22,43 +22,29 @@ Handlebars.registerHelper('trackContentLine', function(options) { }); Handlebars.registerHelper('roomactive', - function(scheduled, additional, room, times) { + function(schedule, room, times) { for (var i=0; i' + scheduled[room][timecode]; - return new Handlebars.SafeString(cell); - } else { - if (additional[room]) { - if (additional[room][timecode] != undefined) { - if (additional[room][timecode] == "") { - if (s == 1) { - cell = 'Available for booking'; - } else { - cell = '' + room + "-" + timecode + ''; - } - } else { - cell = '' + additional[room][timecode]; - } + if (schedule[room][timecode] != undefined) { + if (schedule[room][timecode] == "") { + if (s == 1) { + cell = 'Available for booking'; + } else { + cell = '' + room + "-" + timecode + ''; } + } else { + cell = '' + schedule[room][timecode]; } } return new Handlebars.SafeString(cell); diff --git a/ptgbot/db.py b/ptgbot/db.py index ba16c2d..924336d 100644 --- a/ptgbot/db.py +++ b/ptgbot/db.py @@ -15,7 +15,6 @@ import calendar import copy import datetime -from itertools import chain import json import os import random @@ -24,13 +23,12 @@ import random class PTGDataBase(): BASE = {'tracks': [], 'slots': {}, 'now': {}, 'next': {}, 'colors': {}, - 'location': {}, 'scheduled': {}, 'additional': {}, 'voice': 0} + 'location': {}, 'schedule': {}, 'voice': 0} def __init__(self, config): self.filename = config['db_filename'] self.slots = config['slots'] - self.scheduled = config['scheduled'] - self.extrarooms = config['extrarooms'] + self.schedule = config['schedule'] if os.path.isfile(self.filename): with open(self.filename, 'r') as fp: @@ -38,24 +36,16 @@ class PTGDataBase(): else: self.data = copy.deepcopy(self.BASE) - old_data = copy.deepcopy(self.data['additional']) self.load_data_from_config() - self.merge_additional(old_data) self.save() def load_data_from_config(self): # Copy slots definition and scheduled rooms from configuration self.data['slots'] = self.slots - self.data['scheduled'] = self.scheduled - - # Create additional rooms dictionary from extrarooms definition - for room in self.extrarooms.keys(): - self.data['additional'][room] = {} - for slot in self.extrarooms[room]: - self.data['additional'][room][slot] = '' + self.data['schedule'] = self.schedule # Add tracks mentioned in configuration that are not in track list - for room, bookings in self.scheduled.items(): + for room, bookings in self.schedule.items(): for time, track in bookings.items(): if track not in self.data['tracks']: self.data['tracks'].append(track) @@ -74,15 +64,6 @@ class PTGDataBase(): '#dc0d0e', ]) - def merge_additional(self, old_data): - # Populate 'additional' with saved data where appropriate - for room in self.data['additional'].keys(): - for slot in self.data['additional'][room].keys(): - try: - self.data['additional'][room][slot] = old_data[room][slot] - except KeyError: - pass - def add_now(self, track, session): self.data['now'][track] = session # Update location if none manually provided yet @@ -104,9 +85,7 @@ class PTGDataBase(): today = calendar.day_name[datetime.date.today().weekday()] if today not in self.data['slots']: today = next(iter(self.data['slots'])) - all_schedule = chain(self.data['scheduled'].items(), - self.data['additional'].items()) - for room, bookings in all_schedule: + for room, bookings in self.data['schedule'].items(): for btime, btrack in bookings.items(): for slot in self.data['slots'].get(today, []): if btrack == track and btime == slot['name']: @@ -151,18 +130,18 @@ class PTGDataBase(): def is_slot_valid_and_empty(self, room, timeslot): try: - return not self.data['additional'][room][timeslot] + return not self.data['schedule'][room][timeslot] except KeyError: return False def book(self, track, room, timeslot): - self.data['additional'][room][timeslot] = track + self.data['schedule'][room][timeslot] = track self.save() def unbook(self, room, timeslot): - if room in self.data['additional'].keys(): - if timeslot in self.data['additional'][room].keys(): - self.data['additional'][room][timeslot] = "" + if room in self.data['schedule'].keys(): + if timeslot in self.data['schedule'][room].keys(): + self.data['schedule'][room][timeslot] = "" self.save() def is_voice_required(self):