helpers.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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, participants : list[Participant]) -> 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. participant_slot_strings = [slots2string(participant, n_slots) for participant in participants]
  45. max_occupancy = 1
  46. for n, date in enumerate(event.date_set.all()):
  47. # Get participants for each slot
  48. slot_participants = [[] for i in range(slots_per_day)]
  49. for i, participant in enumerate(participants):
  50. for j in range(slots_per_day):
  51. if participant_slot_strings[i][n*slots_per_day + j] == '1':
  52. slot_participants[j].append(participant)
  53. # Fill the slots of the day
  54. slots = []
  55. last_hour = None
  56. last_half_hour = None
  57. for j, ps in enumerate(slot_participants):
  58. slot_begin = (start_time + j * event.slot_interval).strftime('%H:%M')
  59. slot_end = (start_time + (j+1) * event.slot_interval).strftime('%H:%M')
  60. current_time = start_time + j * event.slot_interval
  61. slots.append({
  62. 'tooltip': f"{slot_begin} - {slot_end} \n{', '.join([p.user.username for p in ps])}",
  63. 'offset': n*slots_per_day + j,
  64. 'date': date.date,
  65. 'time': current_time,
  66. 'color': len(ps),
  67. 'is_full_hour': current_time.hour != last_hour,
  68. 'is_half_hour': current_time.minute // 30 != last_half_hour,
  69. })
  70. max_occupancy = max(max_occupancy, len(ps))
  71. last_hour = current_time.hour
  72. last_half_hour = current_time.minute // 30
  73. day = {
  74. 'date': date.date,
  75. 'slots': slots,
  76. }
  77. data['days'].append(day)
  78. # Fill the rows of the grid
  79. last_hour = None
  80. for i in range(slots_per_day):
  81. current_time = start_time + i * event.slot_interval
  82. data['rows'].append({
  83. 'time': current_time,
  84. 'is_full_hour': current_time.hour != last_hour,
  85. })
  86. last_hour = current_time.hour
  87. data['rows'].append({
  88. 'time': end_time,
  89. 'is_full_hour': True,
  90. })
  91. data['colors'] = max_occupancy
  92. return data