views.py 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. from django.shortcuts import render, redirect, get_object_or_404
  2. from .forms import MultipleShiftForm, ShiftForm, EventForm
  3. from .models import Shift, Employee, Location, Event
  4. from django.utils.timezone import datetime, timedelta
  5. from django.utils import timezone
  6. import calendar
  7. def create_multiple_shifts(request):
  8. date_str = request.GET.get('date')
  9. initial_data = {}
  10. if date_str:
  11. initial_data['date'] = date_str
  12. if request.method == 'POST':
  13. form = MultipleShiftForm(request.POST)
  14. if form.is_valid():
  15. employees = form.cleaned_data['employees']
  16. date = form.cleaned_data['date']
  17. start = form.cleaned_data['start']
  18. end = form.cleaned_data['end']
  19. shifttype = form.cleaned_data['shifttype']
  20. for employee in employees:
  21. Shift.objects.create(
  22. employee=employee,
  23. date=date,
  24. start=start,
  25. end=end,
  26. shifttype=shifttype
  27. )
  28. return redirect('current_week_shifts') # Annahme, dass Sie eine Erfolgsmeldung anzeigen möchten
  29. else:
  30. form = MultipleShiftForm(initial=initial_data)
  31. return render(request, 'main/create_multiple_shifts.html', {'form': form})
  32. def create_event(request):
  33. date_str = request.GET.get('date')
  34. initial_data = {}
  35. if date_str:
  36. initial_data['date'] = date_str
  37. if request.method == 'POST':
  38. form = EventForm(request.POST)
  39. if form.is_valid():
  40. name = form.cleaned_data['name']
  41. date = form.cleaned_data['date']
  42. event_type = form.cleaned_data['event_type']
  43. location = form.cleaned_data['location']
  44. cvd = form.cleaned_data['cvd']
  45. cvt = form.cleaned_data['cvt']
  46. Event.objects.create(
  47. name=name,
  48. date=date,
  49. event_type=event_type,
  50. location=location,
  51. cvd=cvd,
  52. cvt=cvt
  53. )
  54. return redirect('current_week_shifts') # Annahme, dass Sie eine Erfolgsmeldung anzeigen möchten
  55. else:
  56. form = EventForm(initial=initial_data)
  57. return render(request, 'main/create_event.html', {'form': form})
  58. def current_week_shifts(request):
  59. # Standardmäßig die aktuelle Woche anzeigen
  60. today = timezone.now().date()
  61. # Überprüfen, ob ein Startdatum in der URL angegeben ist
  62. start_date_str = request.GET.get('start_date')
  63. if start_date_str:
  64. try:
  65. start_of_week = datetime.strptime(start_date_str, '%Y-%m-%d').date()
  66. except ValueError:
  67. start_of_week = today - timedelta(days=today.weekday()) # Fallback auf die aktuelle Woche bei Fehler
  68. else:
  69. start_of_week = today - timedelta(days=today.weekday()) # Montag der aktuellen Woche
  70. end_of_week = start_of_week + timedelta(days=6) # Sonntag der aktuellen Woche
  71. # Berechnung für die nächste und vorherige Woche
  72. previous_week = start_of_week - timedelta(days=7)
  73. next_week = start_of_week + timedelta(days=7)
  74. # Berechnung der Kalenderwoche
  75. calendar_week = start_of_week.isocalendar()[1]
  76. # Initialisiere ein Dictionary für die Schichten der Mitarbeiter
  77. shifts_by_employee = {}
  78. events_by_location = {}
  79. employees = Employee.objects.all()
  80. locations = Location.objects.all()
  81. for employee in employees:
  82. shifts_by_employee[employee] = {day: None for day in range(7)}
  83. for location in locations:
  84. events_by_location[location] = {day: None for day in range(7)}
  85. # Hole alle Schichten für die aktuelle Woche
  86. shifts = Shift.objects.filter(date__range=[start_of_week, end_of_week])
  87. events = Event.objects.filter(date__range=[start_of_week, end_of_week])
  88. print(events)
  89. # Fülle das Dictionary mit den Schichtdaten
  90. for shift in shifts:
  91. employee = shift.employee
  92. day_of_week = (shift.date - start_of_week).days
  93. shifts_by_employee[employee][day_of_week] = shift
  94. for event in events:
  95. location = event.location
  96. day_of_week = (event.date - start_of_week).days
  97. events_by_location[location][day_of_week] = event
  98. # Bereite die Daten der Woche für das Template vor
  99. week_dates = [(start_of_week + timedelta(days=i)).strftime("%d.%m.%Y") for i in range(7)]
  100. days_of_week = ['Mo', 'Di', 'Mi', 'Do', 'Fr', 'Sa', 'So']
  101. days_with_dates = list(zip(days_of_week, week_dates))
  102. context = {
  103. 'shifts_by_employee': shifts_by_employee,
  104. 'events_by_location': events_by_location,
  105. 'start_of_week': start_of_week,
  106. 'end_of_week': end_of_week,
  107. 'days_with_dates': days_with_dates,
  108. 'range_days': range(7),
  109. 'previous_week': previous_week.strftime('%Y-%m-%d'),
  110. 'next_week': next_week.strftime('%Y-%m-%d'),
  111. 'calendar_week': calendar_week,
  112. }
  113. print(shifts_by_employee)
  114. print(events_by_location)
  115. return render(request, 'main/current_week_shifts.html', context)
  116. def public(request):
  117. # Standardmäßig die aktuelle Woche anzeigen
  118. today = timezone.now().date()
  119. # Überprüfen, ob ein Startdatum in der URL angegeben ist
  120. start_date_str = request.GET.get('start_date')
  121. if start_date_str:
  122. try:
  123. start_of_week = datetime.strptime(start_date_str, '%Y-%m-%d').date()
  124. except ValueError:
  125. start_of_week = today - timedelta(days=today.weekday()) # Fallback auf die aktuelle Woche bei Fehler
  126. else:
  127. start_of_week = today - timedelta(days=today.weekday()) # Montag der aktuellen Woche
  128. end_of_week = start_of_week + timedelta(days=6) # Sonntag der aktuellen Woche
  129. # Berechnung für die nächste und vorherige Woche
  130. previous_week = start_of_week - timedelta(days=7)
  131. next_week = start_of_week + timedelta(days=7)
  132. # Berechnung der Kalenderwoche
  133. calendar_week = start_of_week.isocalendar()[1]
  134. # Initialisiere ein Dictionary für die Schichten der Mitarbeiter
  135. shifts_by_employee = {}
  136. events_by_location = {}
  137. employees = Employee.objects.all()
  138. locations = Location.objects.all()
  139. for employee in employees:
  140. shifts_by_employee[employee] = {day: None for day in range(7)}
  141. for location in locations:
  142. events_by_location[location] = {day: None for day in range(7)}
  143. # Hole alle Schichten für die aktuelle Woche
  144. shifts = Shift.objects.filter(date__range=[start_of_week, end_of_week])
  145. events = Event.objects.filter(date__range=[start_of_week, end_of_week])
  146. print(events)
  147. # Fülle das Dictionary mit den Schichtdaten
  148. for shift in shifts:
  149. employee = shift.employee
  150. day_of_week = (shift.date - start_of_week).days
  151. shifts_by_employee[employee][day_of_week] = shift
  152. for event in events:
  153. location = event.location
  154. day_of_week = (event.date - start_of_week).days
  155. events_by_location[location][day_of_week] = event
  156. # Bereite die Daten der Woche für das Template vor
  157. week_dates = [(start_of_week + timedelta(days=i)).strftime("%d.%m.%Y") for i in range(7)]
  158. days_of_week = ['Mo', 'Di', 'Mi', 'Do', 'Fr', 'Sa', 'So']
  159. days_with_dates = list(zip(days_of_week, week_dates))
  160. context = {
  161. 'shifts_by_employee': shifts_by_employee,
  162. 'events_by_location': events_by_location,
  163. 'start_of_week': start_of_week,
  164. 'end_of_week': end_of_week,
  165. 'days_with_dates': days_with_dates,
  166. 'range_days': range(7),
  167. 'previous_week': previous_week.strftime('%Y-%m-%d'),
  168. 'next_week': next_week.strftime('%Y-%m-%d'),
  169. 'calendar_week': calendar_week,
  170. }
  171. return render(request, 'main/public.html', context)
  172. def edit_shift(request, pk):
  173. shift = get_object_or_404(Shift, pk=pk)
  174. if request.method == 'POST':
  175. form = ShiftForm(request.POST, instance=shift)
  176. if form.is_valid():
  177. form.save()
  178. return redirect('current_week_shifts')
  179. else:
  180. form = ShiftForm(instance=shift)
  181. return render(request, 'main/edit_shift.html', {'form': form, 'shift': shift})
  182. def edit_event(request, pk):
  183. event = get_object_or_404(Event, pk=pk)
  184. if request.method == 'POST':
  185. form = EventForm(request.POST, instance=event)
  186. if form.is_valid():
  187. form.save()
  188. return redirect('current_week_shifts')
  189. else:
  190. form = EventForm(instance=event)
  191. return render(request, 'main/edit_event.html', {'form': form, 'event': event})
  192. def delete_shift(request, pk):
  193. shift = get_object_or_404(Shift, pk=pk)
  194. if request.method == 'POST':
  195. shift.delete()
  196. return redirect('current_week_shifts')
  197. return render(request, 'main/delete_shift.html', {'shift': shift})
  198. def delete_event(request, pk):
  199. event = get_object_or_404(Event, pk=pk)
  200. if request.method == 'POST':
  201. event.delete()
  202. return redirect('current_week_shifts')
  203. return render(request, 'main/delete_event.html', {'event': event})