widgets.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import datetime
  2. import ctypes
  3. from django.forms.widgets import Widget
  4. from .models import Participant, Date, Event
  5. from .helpers import string2slots, get_slot_count, slots2string, slots2grid
  6. from django.utils.translation import gettext as _
  7. class DatePickerWidget(Widget):
  8. template_name = 'widgets/date-picker.html'
  9. n_weeks = 5
  10. def get_context(self, name, value, attrs):
  11. context = super().get_context(name, value, attrs)
  12. # get first day of the week
  13. today = datetime.date.today()
  14. context['widget']['today'] = today
  15. first_day_of_week = today - datetime.timedelta(days=today.weekday())
  16. # get the weekdays
  17. weekdays = []
  18. for i in range(7):
  19. weekdays.append(_((first_day_of_week + datetime.timedelta(days=i)).strftime('%a')))
  20. context['widget']['weekdays'] = weekdays
  21. # for the next weeks
  22. weeks = []
  23. for i in range(self.n_weeks):
  24. date = first_day_of_week + datetime.timedelta(days=i*7)
  25. week = {
  26. 'days': [],
  27. 'month': _(date.strftime('%b')),
  28. 'year': date.strftime('%Y'),
  29. }
  30. for j in range(7):
  31. day = {
  32. 'date': date + datetime.timedelta(days=j),
  33. 'day': (date + datetime.timedelta(days=j)).strftime('%d'),
  34. }
  35. week['days'].append(day)
  36. weeks.append(week)
  37. context['widget']['weeks'] = weeks
  38. return context
  39. def value_from_datadict(self, data, files, name):
  40. dates = []
  41. for day in self.GetValidDates():
  42. if data.get(f"date_{day.strftime('%Y-%m-%d')}") == "on":
  43. dates.append(day)
  44. return dates
  45. def value_omitted_from_data(self, data, files, name):
  46. return not any(
  47. (data.get(f"date_{day.strftime('%Y-%m-%d')}") == "on")
  48. for day in self.GetValidDates()
  49. )
  50. def GetValidDates(self):
  51. today = datetime.date.today()
  52. return [
  53. (today + datetime.timedelta(days=i))
  54. for i in range(7*self.n_weeks)
  55. ]
  56. class SlotPickerWidget(Widget):
  57. template_name = 'widgets/slot-picker.html'
  58. def get_context(self, name, value, attrs):
  59. context = super().get_context(name, value, attrs)
  60. if self.participant:
  61. context['widget']['grid'] = slots2grid(self.event, [self.participant], True)
  62. else:
  63. context['widget']['grid'] = slots2grid(self.event, [], True)
  64. return context
  65. def value_from_datadict(self, data, files, name):
  66. # create a mutable string buffer
  67. slots = ctypes.create_string_buffer(self.n_slots)
  68. # set the slots
  69. for i in range(self.n_slots):
  70. if data.get(f"slot_{i}") == "on":
  71. slots[i] = b'1'
  72. else:
  73. slots[i] = b'0'
  74. # return the slots as a string
  75. return slots.value
  76. def value_omitted_from_data(self, data, files, name):
  77. return False
  78. def __init__(self, *args, event, n_slots, participant, **kwargs):
  79. super().__init__(*args, **kwargs)
  80. self.event = event
  81. self.participant = participant
  82. self.n_slots = n_slots