import datetime from django.shortcuts import get_object_or_404 from django.urls import reverse from django.views import generic from gym.models import Room, Equipment, Setting, Session class RoomListView(generic.ListView): queryset = Room.objects.all().order_by('name') context_object_name = 'rooms' template_name = 'gym/rooms.html' class RoomDetailView(generic.DetailView): model = Room context_object_name = 'room' template_name = 'gym/room.html' class EquipmentDetailView(generic.DetailView): model = Equipment context_object_name = 'equipment' template_name = 'gym/equipment.html' class EquipmentCreateView(generic.CreateView): model = Equipment fields = ['room', 'name'] template_name = 'gym/equipment_edit.html' room = None def get_success_url(self): return reverse('equipment-detail', kwargs={'pk': self.object.pk}) def dispatch(self, request, *args, **kwargs): default_room_id = self.request.GET.get('room') if default_room_id: self.room = get_object_or_404(Room, pk=default_room_id) return super().dispatch(request, *args, **kwargs) def get_initial(self): initial = super().get_initial() initial['room'] = self.room return initial class SettingCreateView(generic.CreateView): model = Setting fields = ['equipment', 'name', 'value'] template_name = 'gym/setting_edit.html' equipment = None def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) context['title'] = 'Ajouter' context['equipment'] = self.equipment return context def dispatch(self, request, *args, **kwargs): default_equipment_id = self.request.GET.get('equipment') if default_equipment_id: self.equipment = get_object_or_404(Equipment, pk=default_equipment_id) return super().dispatch(request, *args, **kwargs) def get_initial(self): initial = super().get_initial() initial['equipment'] = self.equipment return initial def get_success_url(self): return reverse('equipment-detail', kwargs={'pk': self.object.equipment.pk}) class SettingUpdateView(generic.UpdateView): model = Setting fields = ['equipment', 'name', 'value'] template_name = 'gym/setting_edit.html' def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) context['edit'] = True context['title'] = 'Modifier' context['equipment'] = self.object.equipment return context def get_success_url(self): return reverse('equipment-detail', kwargs={'pk': self.object.equipment.pk}) class SettingDeleteView(generic.DeleteView): model = Setting template_name = 'gym/setting_confirm_delete.html' context_object_name = 'setting' def get_success_url(self): return reverse('equipment-detail', kwargs={'pk': self.object.equipment.pk}) class SessionCreateView(generic.CreateView): model = Session fields = ['room', 'start'] template_name = 'gym/session_edit.html' room = None def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) context['title'] = 'Démarrer' context['room'] = self.room return context def get_success_url(self): return reverse('session-detail', kwargs={'pk': self.object.pk}) def dispatch(self, request, *args, **kwargs): self.room = get_object_or_404(Room, pk=self.request.GET.get('room')) return super().dispatch(request, *args, **kwargs) def get_initial(self): initial = super().get_initial() initial['room'] = self.room initial['start'] = datetime.datetime.now() return initial class SessionDetailView(generic.DetailView): model = Session context_object_name = 'session' template_name = 'gym/session_detail.html'