123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128 |
- import datetime
- from django.utils.translation import gettext as _
- from django.utils.html import escape
- from .models import Participant, Date, Event
- def slots2string( participant : Participant | None, n_slots : int ) -> str :
- """ Convert the slots of a participant to a string. """
- if not participant:
- return '0' * n_slots
- # Get the slots of the participant
- byte_array = participant.slots
- # Convert the slots to a string
- string_value = bin(int.from_bytes(byte_array, byteorder='big'))[2:]
- # Pad the string with 0s if necessary
- return '0' * (n_slots - len(string_value)) + string_value
- def string2slots( string : str, n_slots : int ) -> bytes :
- """ Convert a string to a byte array. """
- if len(string) != n_slots:
- raise ValueError(_("Invalid string length: {0} (expected: {1})").format(len(string), n_slots))
- # Convert the string to a byte array
- return int(string, 2).to_bytes((n_slots + 7) // 8, byteorder='big')
- def get_slot_count( event ) -> int :
- """ Get the number of slots in an event. """
- # Get the timespan of the event
- start_time = datetime.datetime.combine(datetime.date.today(), event.start_time)
- end_time = datetime.datetime.combine(datetime.date.today(), event.end_time)
- if(start_time>end_time):
- end_time += datetime.timedelta(days=1)
- timespan = end_time - start_time
- # Get the number of slots in the event
- days = event.date_set.count()
- slots_per_day = int(timespan.total_seconds() // event.slot_interval.total_seconds())
- return days * slots_per_day
- def slots2grid( event : Event, participants : list[Participant], is_input : bool) -> dict :
- """ Convert the slots of an event to data for the grid. """
- # Get the number of slots in the event
- n_slots = get_slot_count(event)
- # Get the timespan of the event
- start_time = datetime.datetime.combine(datetime.date.today(), event.start_time)
- end_time = datetime.datetime.combine(datetime.date.today(), event.end_time)
- if(start_time>end_time):
- end_time += datetime.timedelta(days=1)
- timespan = end_time - start_time
-
- # Get the slots in a day
- slots_per_day = int(timespan.total_seconds() // event.slot_interval.total_seconds())
- # Get the slots of each day
- participant_slot_strings = [slots2string(participant, n_slots) for participant in participants]
- if is_input:
- html = []
- for n, date in reversed(list(enumerate(event.date_set.all()))):
- day_offset = n * slots_per_day
- dt = datetime.datetime.combine(date.date, datetime.time())
- html += f'<div class="slot-column"><div class="day">{ dt.strftime("%b %d") }</div>'
- slots = []
- # Fill the slots of the day
- for j in range(slots_per_day):
- slot_begin = (start_time + j * event.slot_interval).strftime('%H:%M')
- slot_end = (start_time + (j+1) * event.slot_interval).strftime('%H:%M')
- current_time = start_time + j * event.slot_interval
- if current_time.minute == 0:
- time_label = f'<div class=\"time-label\">{current_time.strftime("%H:%M")}</div>'
- classes = " full-hour"
- elif current_time.minute == 30:
- time_label = ""
- classes = " half-hour"
- else:
- time_label = ""
- classes = ""
- checked = ""
- for i in range(len(participant_slot_strings)):
- if participant_slot_strings[i][day_offset + j] == '1':
- checked = 'checked'
- break
- slots.append(
- f'<div class="slot{classes}" title="{slot_begin} - {slot_end}">{ time_label }<input class="checkable" type="checkbox" id="slot_picker_{ day_offset + j }" name="slot_{day_offset + j}" { checked } /><label class="checkable" for="slot_picker_{day_offset + j}"></label></div>')
- html.append(''.join(slots))
- html.append('</div>')
- return "".join(html)
- else:
- max_occupancy = 1
- html = []
- for n, date in reversed(list(enumerate(event.date_set.all()))):
- day_offset = n * slots_per_day
- dt = datetime.datetime.combine(date.date, datetime.time())
- html.append(f'<div class="slot-column"><div class="day">{ dt.strftime("%b %d") }</div>')
- slots = []
- # Get participants for each slot
- slot_participants = [[] for i in range(slots_per_day)]
- for j in range(slots_per_day):
- for i in range(len(participant_slot_strings)):
- if participant_slot_strings[i][day_offset + j] == '1':
- slot_participants[j].append(i)
- # Fill the slots of the day
- for j, ps in enumerate(slot_participants):
- slot_begin = (start_time + j * event.slot_interval).strftime('%H:%M')
- slot_end = (start_time + (j+1) * event.slot_interval).strftime('%H:%M')
- current_time = start_time + j * event.slot_interval
- if current_time.minute == 0:
- time_label = f'<div class=\"time-label\">{current_time.strftime("%H:%M")}</div>'
- classes = " full-hour"
- elif current_time.minute == 30:
- time_label = ""
- classes = " half-hour"
- else:
- time_label = ""
- classes = ""
- slots.append(
- f'<div class="slot{classes}" id="grid_slot_{ day_offset + j }" title="{slot_begin} - {slot_end} \n{escape(", ".join([participants[p].name for p in ps]))}" style="--color-index:{ len(ps) }">{ time_label }</div>')
- max_occupancy = max(max_occupancy, len(ps))
- html.append(''.join(slots))
- html.append('</div>')
- return f'<div class="occupancy-grid" style="--color-count:{ max_occupancy }">{"".join(html)}</div>'
|