helpers.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import datetime
  2. from django.utils.translation import gettext as _
  3. from django.utils.html import escape
  4. from .models import Participant, Date, Event
  5. def slots2string( participant : Participant, n_slots : int ) -> str :
  6. """ Convert the slots of a participant to a string. """
  7. # Get the slots of the participant
  8. byte_array = participant.slots
  9. # Convert the slots to a string
  10. string_value = bin(int.from_bytes(byte_array, byteorder='big'))[2:]
  11. # Pad the string with 0s if necessary
  12. return '0' * (n_slots - len(string_value)) + string_value
  13. def string2slots( string : str, n_slots : int ) -> bytes :
  14. """ Convert a string to a byte array. """
  15. if len(string) != n_slots:
  16. raise ValueError(_("Invalid string length: {0} (expected: {1})").format(len(string), n_slots))
  17. # Convert the string to a byte array
  18. return int(string, 2).to_bytes((n_slots + 7) // 8, byteorder='big')
  19. def get_slot_count( event ) -> int :
  20. """ Get the number of slots in an event. """
  21. # Get the timespan of the event
  22. start_time = datetime.datetime.combine(datetime.date.today(), event.start_time)
  23. end_time = datetime.datetime.combine(datetime.date.today(), event.end_time)
  24. if(start_time>end_time):
  25. end_time += datetime.timedelta(days=1)
  26. timespan = end_time - start_time
  27. # Get the number of slots in the event
  28. days = event.date_set.count()
  29. slots_per_day = int(timespan.total_seconds() // event.slot_interval.total_seconds())
  30. return days * slots_per_day
  31. def slots2grid( event : Event, participants : list[Participant], is_input : bool) -> dict :
  32. """ Convert the slots of an event to data for the grid. """
  33. # Get the number of slots in the event
  34. n_slots = get_slot_count(event)
  35. # Get the timespan of the event
  36. start_time = datetime.datetime.combine(datetime.date.today(), event.start_time)
  37. end_time = datetime.datetime.combine(datetime.date.today(), event.end_time)
  38. if(start_time>end_time):
  39. end_time += datetime.timedelta(days=1)
  40. timespan = end_time - start_time
  41. # Get the slots in a day
  42. slots_per_day = int(timespan.total_seconds() // event.slot_interval.total_seconds())
  43. # Get the slots of each day
  44. participant_slot_strings = [slots2string(participant, n_slots) for participant in participants]
  45. if is_input:
  46. html = []
  47. for n, date in reversed(list(enumerate(event.date_set.all()))):
  48. day_offset = n * slots_per_day
  49. dt = datetime.datetime.combine(date.date, datetime.time())
  50. html += f'<div class="slot-column"><div class="day">{ dt.strftime("%b %d") }</div>'
  51. slots = []
  52. # Fill the slots of the day
  53. for j in range(slots_per_day):
  54. slot_begin = (start_time + j * event.slot_interval).strftime('%H:%M')
  55. slot_end = (start_time + (j+1) * event.slot_interval).strftime('%H:%M')
  56. current_time = start_time + j * event.slot_interval
  57. if current_time.minute == 0:
  58. time_label = f'<div class=\"time-label\">{current_time.strftime("%H:%M")}</div>'
  59. classes = " full-hour"
  60. elif current_time.minute == 30:
  61. time_label = ""
  62. classes = " half-hour"
  63. else:
  64. time_label = ""
  65. classes = ""
  66. checked = ""
  67. for i in range(len(participant_slot_strings)):
  68. if participant_slot_strings[i][day_offset + j] == '1':
  69. checked = 'checked'
  70. break
  71. slots.append(
  72. 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>')
  73. html.append(''.join(slots))
  74. html.append('</div>')
  75. return "".join(html)
  76. else:
  77. max_occupancy = 1
  78. html = []
  79. for n, date in reversed(list(enumerate(event.date_set.all()))):
  80. day_offset = n * slots_per_day
  81. dt = datetime.datetime.combine(date.date, datetime.time())
  82. html.append(f'<div class="slot-column"><div class="day">{ dt.strftime("%b %d") }</div>')
  83. slots = []
  84. # Get participants for each slot
  85. slot_participants = [[] for i in range(slots_per_day)]
  86. for j in range(slots_per_day):
  87. for i in range(len(participant_slot_strings)):
  88. if participant_slot_strings[i][day_offset + j] == '1':
  89. slot_participants[j].append(i)
  90. # Fill the slots of the day
  91. for j, ps in enumerate(slot_participants):
  92. slot_begin = (start_time + j * event.slot_interval).strftime('%H:%M')
  93. slot_end = (start_time + (j+1) * event.slot_interval).strftime('%H:%M')
  94. current_time = start_time + j * event.slot_interval
  95. if current_time.minute == 0:
  96. time_label = f'<div class=\"time-label\">{current_time.strftime("%H:%M")}</div>'
  97. classes = " full-hour"
  98. elif current_time.minute == 30:
  99. time_label = ""
  100. classes = " half-hour"
  101. else:
  102. time_label = ""
  103. classes = ""
  104. slots.append(
  105. f'<div class="slot{classes}" id="grid_slot_{ day_offset + j }" title="{slot_begin} - {slot_end} \n{escape(", ".join([participants[p].user.username for p in ps]))}" style="--color-index:{ len(ps) }">{ time_label }</div>')
  106. max_occupancy = max(max_occupancy, len(ps))
  107. html.append(''.join(slots))
  108. html.append('</div>')
  109. return f'<div class="occupancy-grid" style="--color-count:{ max_occupancy }">{"".join(html)}</div>'