from django.shortcuts import render, redirect, get_object_or_404 from .forms import MultipleShiftForm, ShiftForm, EventForm from .models import Shift, Employee, Location, Event from django.utils.timezone import datetime, timedelta from django.utils import timezone import calendar def create_multiple_shifts(request): date_str = request.GET.get('date') initial_data = {} if date_str: initial_data['date'] = date_str if request.method == 'POST': form = MultipleShiftForm(request.POST) if form.is_valid(): employees = form.cleaned_data['employees'] date = form.cleaned_data['date'] start = form.cleaned_data['start'] end = form.cleaned_data['end'] shifttype = form.cleaned_data['shifttype'] for employee in employees: Shift.objects.create( employee=employee, date=date, start=start, end=end, shifttype=shifttype ) return redirect('current_week_shifts') # Annahme, dass Sie eine Erfolgsmeldung anzeigen möchten else: form = MultipleShiftForm(initial=initial_data) return render(request, 'main/create_multiple_shifts.html', {'form': form}) def create_event(request): date_str = request.GET.get('date') initial_data = {} if date_str: initial_data['date'] = date_str if request.method == 'POST': form = EventForm(request.POST) if form.is_valid(): name = form.cleaned_data['name'] date = form.cleaned_data['date'] event_type = form.cleaned_data['event_type'] location = form.cleaned_data['location'] cvd = form.cleaned_data['cvd'] cvt = form.cleaned_data['cvt'] Event.objects.create( name=name, date=date, event_type=event_type, location=location, cvd=cvd, cvt=cvt ) return redirect('current_week_shifts') # Annahme, dass Sie eine Erfolgsmeldung anzeigen möchten else: form = EventForm(initial=initial_data) return render(request, 'main/create_event.html', {'form': form}) def current_week_shifts(request): # Standardmäßig die aktuelle Woche anzeigen today = timezone.now().date() # Überprüfen, ob ein Startdatum in der URL angegeben ist start_date_str = request.GET.get('start_date') if start_date_str: try: start_of_week = datetime.strptime(start_date_str, '%Y-%m-%d').date() except ValueError: start_of_week = today - timedelta(days=today.weekday()) # Fallback auf die aktuelle Woche bei Fehler else: start_of_week = today - timedelta(days=today.weekday()) # Montag der aktuellen Woche end_of_week = start_of_week + timedelta(days=6) # Sonntag der aktuellen Woche # Berechnung für die nächste und vorherige Woche previous_week = start_of_week - timedelta(days=7) next_week = start_of_week + timedelta(days=7) # Berechnung der Kalenderwoche calendar_week = start_of_week.isocalendar()[1] # Initialisiere ein Dictionary für die Schichten der Mitarbeiter shifts_by_employee = {} events_by_location = {} employees = Employee.objects.all() locations = Location.objects.all() for employee in employees: shifts_by_employee[employee] = {day: None for day in range(7)} for location in locations: events_by_location[location] = {day: None for day in range(7)} # Hole alle Schichten für die aktuelle Woche shifts = Shift.objects.filter(date__range=[start_of_week, end_of_week]) events = Event.objects.filter(date__range=[start_of_week, end_of_week]) print(events) # Fülle das Dictionary mit den Schichtdaten for shift in shifts: employee = shift.employee day_of_week = (shift.date - start_of_week).days shifts_by_employee[employee][day_of_week] = shift for event in events: location = event.location day_of_week = (event.date - start_of_week).days events_by_location[location][day_of_week] = event # Bereite die Daten der Woche für das Template vor week_dates = [(start_of_week + timedelta(days=i)).strftime("%d.%m.%Y") for i in range(7)] days_of_week = ['Mo', 'Di', 'Mi', 'Do', 'Fr', 'Sa', 'So'] days_with_dates = list(zip(days_of_week, week_dates)) context = { 'shifts_by_employee': shifts_by_employee, 'events_by_location': events_by_location, 'start_of_week': start_of_week, 'end_of_week': end_of_week, 'days_with_dates': days_with_dates, 'range_days': range(7), 'previous_week': previous_week.strftime('%Y-%m-%d'), 'next_week': next_week.strftime('%Y-%m-%d'), 'calendar_week': calendar_week, } print(shifts_by_employee) print(events_by_location) return render(request, 'main/current_week_shifts.html', context) def public(request): # Standardmäßig die aktuelle Woche anzeigen today = timezone.now().date() # Überprüfen, ob ein Startdatum in der URL angegeben ist start_date_str = request.GET.get('start_date') if start_date_str: try: start_of_week = datetime.strptime(start_date_str, '%Y-%m-%d').date() except ValueError: start_of_week = today - timedelta(days=today.weekday()) # Fallback auf die aktuelle Woche bei Fehler else: start_of_week = today - timedelta(days=today.weekday()) # Montag der aktuellen Woche end_of_week = start_of_week + timedelta(days=6) # Sonntag der aktuellen Woche # Berechnung für die nächste und vorherige Woche previous_week = start_of_week - timedelta(days=7) next_week = start_of_week + timedelta(days=7) # Berechnung der Kalenderwoche calendar_week = start_of_week.isocalendar()[1] # Initialisiere ein Dictionary für die Schichten der Mitarbeiter shifts_by_employee = {} events_by_location = {} employees = Employee.objects.all() locations = Location.objects.all() for employee in employees: shifts_by_employee[employee] = {day: None for day in range(7)} for location in locations: events_by_location[location] = {day: None for day in range(7)} # Hole alle Schichten für die aktuelle Woche shifts = Shift.objects.filter(date__range=[start_of_week, end_of_week]) events = Event.objects.filter(date__range=[start_of_week, end_of_week]) print(events) # Fülle das Dictionary mit den Schichtdaten for shift in shifts: employee = shift.employee day_of_week = (shift.date - start_of_week).days shifts_by_employee[employee][day_of_week] = shift for event in events: location = event.location day_of_week = (event.date - start_of_week).days events_by_location[location][day_of_week] = event # Bereite die Daten der Woche für das Template vor week_dates = [(start_of_week + timedelta(days=i)).strftime("%d.%m.%Y") for i in range(7)] days_of_week = ['Mo', 'Di', 'Mi', 'Do', 'Fr', 'Sa', 'So'] days_with_dates = list(zip(days_of_week, week_dates)) context = { 'shifts_by_employee': shifts_by_employee, 'events_by_location': events_by_location, 'start_of_week': start_of_week, 'end_of_week': end_of_week, 'days_with_dates': days_with_dates, 'range_days': range(7), 'previous_week': previous_week.strftime('%Y-%m-%d'), 'next_week': next_week.strftime('%Y-%m-%d'), 'calendar_week': calendar_week, } return render(request, 'main/public.html', context) def edit_shift(request, pk): shift = get_object_or_404(Shift, pk=pk) if request.method == 'POST': form = ShiftForm(request.POST, instance=shift) if form.is_valid(): form.save() return redirect('current_week_shifts') else: form = ShiftForm(instance=shift) return render(request, 'main/edit_shift.html', {'form': form, 'shift': shift}) def edit_event(request, pk): event = get_object_or_404(Event, pk=pk) if request.method == 'POST': form = EventForm(request.POST, instance=event) if form.is_valid(): form.save() return redirect('current_week_shifts') else: form = EventForm(instance=event) return render(request, 'main/edit_event.html', {'form': form, 'event': event}) def delete_shift(request, pk): shift = get_object_or_404(Shift, pk=pk) if request.method == 'POST': shift.delete() return redirect('current_week_shifts') return render(request, 'main/delete_shift.html', {'shift': shift}) def delete_event(request, pk): event = get_object_or_404(Event, pk=pk) if request.method == 'POST': event.delete() return redirect('current_week_shifts') return render(request, 'main/delete_event.html', {'event': event})