import logging from django import template from django.template import Context, Template from django.utils.safestring import mark_safe logger = logging.getLogger("bornhack.%s" % __name__) register = template.Library() def render_datetime(datetime): """ Renders a datetime in the users timezone """ t = Template("{{ datetime }}") return t.render(Context({"datetime": datetime})) def render_datetimetzrange(datetimetzrange): """ Renders a datetimetzrange as 14:00-16:00 in the users timezone """ return f"{render_datetime(datetimetzrange.lower.time())}-{render_datetime(datetimetzrange.upper.time())} ({datetimetzrange.lower.tzname()})" @register.simple_tag def availabilitytable(matrix, form=None): """ Build the HTML table to show speaker availability, and hold the checkboxes for the speaker_availability form. """ if not matrix: logger.error("we have no matrix to build a table from") return "" # start bulding the output output = "
Speaker Availability | " # to build the for this table we need to loop over the days (dates) # in the matrix and create a column for each for date in matrix.keys(): output += f"{render_datetime(date.lower.date())} | " output += "" output += "||
---|---|---|---|
{chunk} | " # loop over dates to find out if we need a checkbox for this chunk on this date for date in matrix.keys(): # loop over matrix daychunks and for each date find the chunk/row we are working with for daychunk in matrix[date].keys(): if render_datetimetzrange(daychunk) == chunk: # do we need a checkbox? if matrix[date][daychunk]: popup = f'with label and the field from the form output += f" | " # add the form field? if form: # add the label for this form field, include an # onclick() js event to the label to toggle the td background class output += f"" output += " | " else: output += "N/A | " output += "