helpers.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import datetime
  2. from .models import Participant, Date, Event
  3. def slots2string( participant : Participant, n_slots : int ) -> str :
  4. """ Convert the slots of a participant to a string. """
  5. # Get the slots of the participant
  6. byte_array = participant.slots
  7. # Convert the slots to a string
  8. string_value = bin(int.from_bytes(byte_array, byteorder='big'))[2:]
  9. # Pad the string with 0s if necessary
  10. return '0' * (n_slots - len(string_value)) + string_value
  11. def string2slots( string : str, n_slots : int ) -> bytes :
  12. """ Convert a string to a byte array. """
  13. if len(string) != n_slots:
  14. raise ValueError(f"Invalid string length: {len(string)} (expected {n_slots})")
  15. # Convert the string to a byte array
  16. return int(string, 2).to_bytes((n_slots + 7) // 8, byteorder='big')
  17. def get_slot_count( event ) -> int :
  18. """ Get the number of slots in an event. """
  19. # Get the timespan of the event
  20. start_time = datetime.datetime.combine(datetime.date.today(), event.start_time)
  21. end_time = datetime.datetime.combine(datetime.date.today(), event.end_time)
  22. timespan = abs(end_time - start_time)
  23. # Get the number of slots in the event
  24. days = event.date_set.count()
  25. slots_per_day = int(timespan.total_seconds() // event.slot_interval.total_seconds())
  26. return days * slots_per_day
  27. def slots2grid( event : Event ) -> dict :
  28. """ Convert the slots of an event to data for the grid. """
  29. # Get the number of slots in the event
  30. n_slots = get_slot_count(event)
  31. data = {
  32. 'rows': [],
  33. 'days': [],
  34. 'colors': [],
  35. 'n_days': event.date_set.count(),
  36. }
  37. # Get the timespan of the event
  38. start_time = datetime.datetime.combine(datetime.date.today(), event.start_time)
  39. end_time = datetime.datetime.combine(datetime.date.today(), event.end_time)
  40. timespan = abs(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. participants = event.participant_set.all()
  45. participant_slot_strings = [slots2string(participant, n_slots) for participant in participants]
  46. max_occupancy = 1
  47. for n, date in enumerate(event.date_set.all()):
  48. # Get participants for each slot
  49. slot_participants = [[] for i in range(slots_per_day)]
  50. for i, participant in enumerate(participants):
  51. for j in range(slots_per_day):
  52. if participant_slot_strings[i][n*slots_per_day + j] == '1':
  53. slot_participants[j].append(participant)
  54. # Fill the slots of the day
  55. slots = []
  56. last_hour = None
  57. last_half_hour = None
  58. for j, ps in enumerate(slot_participants):
  59. slot_begin = (start_time + j * event.slot_interval).strftime('%H:%M')
  60. slot_end = (start_time + (j+1) * event.slot_interval).strftime('%H:%M')
  61. current_time = start_time + j * event.slot_interval
  62. slots.append({
  63. 'tooltip': f"{slot_begin} - {slot_end} \n{', '.join([p.user.username for p in ps])}",
  64. 'offset': n*slots_per_day + j,
  65. 'date': date.date,
  66. 'time': current_time,
  67. 'class': f'color_{len(ps)}',
  68. 'is_full_hour': current_time.hour != last_hour,
  69. 'is_half_hour': current_time.minute // 30 != last_half_hour,
  70. })
  71. max_occupancy = max(max_occupancy, len(ps))
  72. last_hour = current_time.hour
  73. last_half_hour = current_time.minute // 30
  74. day = {
  75. 'date': date.date,
  76. 'slots': slots,
  77. }
  78. data['days'].append(day)
  79. # Fill the rows of the grid
  80. last_hour = None
  81. for i in range(slots_per_day):
  82. current_time = start_time + i * event.slot_interval
  83. data['rows'].append({
  84. 'time': current_time,
  85. 'is_full_hour': current_time.hour != last_hour,
  86. })
  87. last_hour = current_time.hour
  88. data['rows'].append({
  89. 'time': end_time,
  90. 'is_full_hour': True,
  91. })
  92. # create a color for each slot from white to dark green
  93. for i in range(max_occupancy+1):
  94. data['colors'].append({
  95. 'color': f"hsl(120, 100%, {100 - 70 * i / max_occupancy}%)",
  96. 'name': f'color_{i}'
  97. })
  98. return data