helpers.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 ) -> bytes :
  12. """ Convert a string to a byte array. """
  13. # Convert the string to a byte array
  14. return int(string, 2).to_bytes((len(string) + 7) // 8, byteorder='big')
  15. def get_slot_count( event ) -> int :
  16. """ Get the number of slots in an event. """
  17. # Get the timespan of the event
  18. start_time = datetime.datetime.combine(datetime.date.today(), event.start_time)
  19. end_time = datetime.datetime.combine(datetime.date.today(), event.end_time)
  20. timespan = abs(end_time - start_time)
  21. # Get the number of slots in the event
  22. days = event.date_set.count()
  23. slots_per_day = timespan.total_seconds() // event.slot_interval.total_seconds()
  24. return int(days * slots_per_day)
  25. def slots2grid( event : Event ) -> dict :
  26. """ Convert the slots of an event to data for the grid. """
  27. # Get the number of slots in the event
  28. n_slots = get_slot_count(event)
  29. data = {
  30. 'rows': [],
  31. 'days': [],
  32. 'slot_height': 30 * (event.slot_interval.total_seconds() / 3600),
  33. 'n_days': event.date_set.count(),
  34. }
  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. timespan = abs(end_time - start_time)
  39. # Get the slots in a day
  40. slots_per_day = int(timespan.total_seconds() // event.slot_interval.total_seconds())
  41. # Get the slots of each day
  42. participants = event.participant_set.all()
  43. participant_slot_strings = [slots2string(participant, n_slots) for participant in participants]
  44. max_occupancy = 1
  45. for n, date in enumerate(event.date_set.all()):
  46. # Get participants for each slot
  47. slot_participants = [[] for i in range(slots_per_day)]
  48. for i, participant in enumerate(participants):
  49. for j in range(slots_per_day):
  50. if participant_slot_strings[i][n*slots_per_day + j] == '1':
  51. slot_participants[j].append(participant)
  52. # Fill the slots of the day
  53. slots = []
  54. for j, ps in enumerate(slot_participants):
  55. slot_begin = (start_time + j * event.slot_interval).strftime('%H:%M')
  56. slot_end = (start_time + (j+1) * event.slot_interval).strftime('%H:%M')
  57. slots.append({
  58. 'tooltip': f"{slot_begin} - {slot_end} \n{', '.join([p.user.username for p in ps])}",
  59. 'occupancy': len(ps),
  60. 'offset': n*slots_per_day + j,
  61. 'date': date.date,
  62. })
  63. max_occupancy = max(max_occupancy, len(ps))
  64. day = {
  65. 'date': date.date,
  66. 'slots': slots,
  67. }
  68. data['days'].append(day)
  69. # Fill the rows of the grid
  70. last_hour = None
  71. for i in range(slots_per_day):
  72. current_time = start_time + i * event.slot_interval
  73. data['rows'].append({
  74. 'days': [day['slots'][i] for day in data['days']],
  75. 'time': current_time,
  76. 'is_full_hour': current_time.hour != last_hour,
  77. })
  78. last_hour = current_time.hour
  79. data['rows'].append({
  80. 'days': [None for day in data['days']],
  81. 'time': end_time,
  82. 'is_full_hour': True,
  83. })
  84. data['max_value'] = max_occupancy
  85. # create a color for each slot from white to dark green
  86. for row in data['rows']:
  87. for day in row['days']:
  88. if day is not None:
  89. day['color'] = f"hsl(120, 100%, {100 - 70 * day['occupancy'] / max_occupancy}%)"
  90. return data