Browse Source

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
changes/07/607307/2
Thierry Carrez 3 years ago
parent
commit
21d77e09f8
  1. 8
      config.json.sample
  2. 6
      html/ptg.html
  3. 40
      html/ptg.js
  4. 41
      ptgbot/db.py

8
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"]
}
}

6
html/ptg.html

@ -92,11 +92,11 @@
<th>{{time.desc}}</th>
{{/each}}
</tr></thead>
{{#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)}}
<tr><td>{{room}}</td>
{{#each (lookup @root.slots day) as |time|}}
<td>{{ roomcode @root.scheduled @root.additional room time.name 0 }}</td>
<td>{{ roomcode @root.schedule room time.name 0 }}</td>
{{/each}}
</tr>
{{/if}}

40
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<times.length; i++) {
if (scheduled[room][times[i]] != "") {
if (schedule[room][times[i]['name']] != undefined) {
return true;
}
if (additional[room]) {
if (additional[room][times[i]['name']] != undefined) {
return true;
}
}
}
return false;
});
Handlebars.registerHelper('roomcode',
function(scheduled, additional, room, timecode, s) {
function(schedule, room, timecode, s) {
var cell = '';
content = scheduled[room][timecode];
if ((content != undefined) && (content != '')) {
cell = '<span class="label label-primary ' + scheduled[room][timecode] +
'">' + 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 = '<small><i>Available for booking</i></small>';
} else {
cell = '<small><i>' + room + "-" + timecode + '</i></small>';
}
} else {
cell = '<span class="label label-primary ' +
additional[room][timecode] +
'">' + additional[room][timecode];
}
if (schedule[room][timecode] != undefined) {
if (schedule[room][timecode] == "") {
if (s == 1) {
cell = '<small><i>Available for booking</i></small>';
} else {
cell = '<small><i>' + room + "-" + timecode + '</i></small>';
}
} else {
cell = '<span class="label label-primary ' +
schedule[room][timecode] +
'">' + schedule[room][timecode];
}
}
return new Handlebars.SafeString(cell);

41
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):

Loading…
Cancel
Save