helpers.py 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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.end_time)
  19. end_time = datetime.datetime.combine(datetime.date.today(), event.start_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. 'days': [],
  31. 'rows': [],
  32. }
  33. # Get the timespan of the event
  34. start_time = datetime.datetime.combine(datetime.date.today(), event.end_time)
  35. end_time = datetime.datetime.combine(datetime.date.today(), event.start_time)
  36. timespan = abs(end_time - start_time)
  37. # Get the slots in a day
  38. slots_per_day = timespan.total_seconds() // event.slot_interval.total_seconds()
  39. # Fill the rows of the grid
  40. last_hour = None
  41. for i in range(slots_per_day):
  42. current_time = start_time + i * event.slot_interval
  43. data['rows'].append({
  44. 'time': current_time,
  45. 'is_full_hour': current_time.hour != last_hour,
  46. })
  47. last_hour = current_time.hour
  48. # Get the slots of each day
  49. participants = event.participant_set.all()
  50. participant_slot_strings = [slots2string(participant, n_slots) for participant in participants]
  51. max_occupancy = 0
  52. for date in event.date_set.all():
  53. # Get participants for each slot
  54. slot_participants = [[] for i in range(slots_per_day)]
  55. for i, participant in enumerate(participants):
  56. for j in range(slots_per_day):
  57. if participant_slot_strings[i][j] == '1':
  58. slot_participants[j].append(participant)
  59. # Fill the slots of the day
  60. slots = []
  61. for ps in slot_participants:
  62. slots.append({
  63. 'tooltip': ', '.join([p.user.username for p in ps]),
  64. 'occupancy': len(ps),
  65. })
  66. max_occupancy = max(max_occupancy, len(ps))
  67. day = {
  68. 'date': date.date,
  69. 'slots': slots,
  70. }
  71. data['days'].append(day)