2018-08-28 22:02:04 +02:00
|
|
|
from django.views.generic import TemplateView, DetailView, View
|
2018-08-26 21:18:51 +02:00
|
|
|
from django.contrib.auth.mixins import LoginRequiredMixin
|
2018-11-24 16:53:54 +01:00
|
|
|
from django.contrib.auth import get_user_model
|
2018-08-28 22:02:04 +02:00
|
|
|
from django.shortcuts import get_object_or_404, render
|
|
|
|
from django.http import JsonResponse, HttpResponseRedirect
|
|
|
|
from django.urls import reverse
|
2018-08-26 21:18:51 +02:00
|
|
|
|
|
|
|
from .models import Event, Activity
|
|
|
|
from equipment.models import EquipmentAttribution
|
2016-09-30 20:10:35 +02:00
|
|
|
|
2018-11-24 16:53:54 +01:00
|
|
|
User = get_user_model()
|
|
|
|
|
2017-02-18 01:10:30 +01:00
|
|
|
|
|
|
|
class Index(TemplateView):
|
|
|
|
template_name = "event/index.html"
|
2018-08-26 21:18:51 +02:00
|
|
|
|
|
|
|
|
2018-10-12 18:04:27 +02:00
|
|
|
class EventCalendar(LoginRequiredMixin, DetailView):
|
|
|
|
model = Event
|
|
|
|
template_name = 'event/calendar.html'
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super().get_context_data(**kwargs)
|
|
|
|
event = self.object
|
|
|
|
context['activities'] = (Activity.objects.filter(event=event)
|
|
|
|
.order_by('beginning').prefetch_related(
|
|
|
|
'tags', 'places', 'staff', 'parent'))
|
|
|
|
return context
|
|
|
|
|
|
|
|
|
2018-08-26 21:18:51 +02:00
|
|
|
class EventView(LoginRequiredMixin, DetailView):
|
|
|
|
model = Event
|
|
|
|
template_name = 'event/event.html'
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super().get_context_data(**kwargs)
|
|
|
|
event = self.object
|
|
|
|
context['activities'] = (Activity.objects.filter(event=event)
|
|
|
|
.order_by('beginning').prefetch_related(
|
|
|
|
'tags', 'places', 'staff', 'parent'))
|
|
|
|
return context
|
|
|
|
|
|
|
|
|
2018-11-24 16:53:54 +01:00
|
|
|
class EventViewStaff(LoginRequiredMixin, DetailView):
|
|
|
|
model = Event
|
|
|
|
template_name = 'event/event.html'
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super().get_context_data(**kwargs)
|
|
|
|
user = User.objects.get(username=self.kwargs['username'])
|
|
|
|
event = self.object
|
|
|
|
context['staffuser'] = user
|
|
|
|
context['activities'] = (user.in_perm_activities
|
|
|
|
.filter(event=event)
|
|
|
|
.order_by('beginning')
|
|
|
|
.prefetch_related(
|
|
|
|
'tags', 'places', 'staff', 'parent')
|
|
|
|
)
|
|
|
|
return context
|
|
|
|
|
|
|
|
|
2018-08-26 21:18:51 +02:00
|
|
|
class ActivityView(LoginRequiredMixin, DetailView):
|
|
|
|
model = Activity
|
|
|
|
template_name = 'event/activity.html'
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super().get_context_data(**kwargs)
|
|
|
|
activity = self.object
|
|
|
|
context['attributions'] = (EquipmentAttribution.objects
|
|
|
|
.filter(activity=activity)
|
|
|
|
.prefetch_related('equipment'))
|
|
|
|
return context
|
2018-08-28 22:02:04 +02:00
|
|
|
|
|
|
|
|
|
|
|
class EnrolActivityView(LoginRequiredMixin, View):
|
|
|
|
http_method_names = ['post']
|
|
|
|
|
|
|
|
def post(self, request, pk, *args, **kwargs):
|
|
|
|
activity = get_object_or_404(Activity, id=pk)
|
|
|
|
action = request.POST.get("goal", None)
|
|
|
|
success = True
|
|
|
|
if action == "enrol":
|
|
|
|
activity.staff.add(request.user)
|
|
|
|
elif action == "unenrol":
|
|
|
|
activity.staff.remove(request.user)
|
|
|
|
else:
|
|
|
|
success = False
|
|
|
|
if "ajax" in request.GET:
|
2018-11-12 21:44:10 +01:00
|
|
|
if request.GET["ajax"] == "json":
|
|
|
|
enrols = activity.staff
|
|
|
|
return JsonResponse({
|
|
|
|
"enrolled": enrols.filter(id=request.user.id).exists(),
|
|
|
|
"number": enrols.count(),
|
|
|
|
})
|
2018-08-28 22:02:04 +02:00
|
|
|
return render(request, "event/activity_summary.html",
|
|
|
|
{"activity": activity})
|
|
|
|
return HttpResponseRedirect(reverse("event:activity", kwargs={"pk":pk}))
|