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 = "
" output += "" output += "" # 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"" output += "" output += "" # loop over all dates in the matrix to find a set of unique "daychunks" we # need checkboxes for. We need to represent a daychunk with a table row if # just one of the days need a checkbox for it. chunks = set() for date in matrix.keys(): # loop over the daychunks on this date for daychunk in matrix[date].keys(): if matrix[date][daychunk]: # add this daychunk it to the chunks set chunks.add(render_datetimetzrange(daychunk)) # make chunks a list and sort it chunks = list(chunks) chunks.sort() needsinfo = False # loop over chunks where at least one date needs a checkbox for chunk in chunks: # we need a header row for this chunk output += f"" # 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'

{render_datetime(daychunk.lower.date)} from {render_datetime(daychunk.lower.time)} to {render_datetime(daychunk.upper.time)}.

' popup += "

This time slot is used for:
" for et in matrix[date][daychunk]["event_types"]: popup += f' {et["name"]}s
' popup += "

" # set tdclass and popup message depending on availability info in our db if matrix[date][daychunk]["initial"]: tdclass = "success" tdicon = "check" popup += "

Our records indicate that this person is available during this time slot.

" elif matrix[date][daychunk]["initial"] is None: tdclass = "warning" tdicon = "question" needsinfo = True if form: popup += "

We have no existing records about this persons availability during this time slot. Please submit availability information!

" else: popup += "

We have no existing records about this persons availability during this time slot.

" else: tdclass = "danger" tdicon = "times" popup += "

Our records indicate that this person is not available during this time slot.

" # ok finish the popup and add it to the td popup += "
" # add the " else: output += "" output += "" # finish the table and add the help text output += "
Speaker Availability
{render_datetime(date.lower.date())}
{chunk} 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 += "N/A
" if form: output += "
Please uncheck any boxes where this person is not available
" elif needsinfo: output += '' output += "
" return mark_safe(output)