feat(views.py): adapt views after for EventOccurrences
This commit is contained in:
109
kursy/views.py
109
kursy/views.py
@@ -1,10 +1,11 @@
|
|||||||
from django import urls
|
from django import urls
|
||||||
from django.contrib.auth.decorators import login_required
|
from django.contrib.auth.decorators import login_required
|
||||||
from django.http import JsonResponse
|
from django.http import JsonResponse
|
||||||
from django.shortcuts import render, redirect
|
from django.shortcuts import redirect, render
|
||||||
|
|
||||||
|
from home.models import EventOccurrence, EventPage
|
||||||
|
|
||||||
from .forms import SignUpForm
|
from .forms import SignUpForm
|
||||||
from home.models import EventPage
|
|
||||||
|
|
||||||
|
|
||||||
def signup(request):
|
def signup(request):
|
||||||
@@ -28,70 +29,96 @@ def calendar(request):
|
|||||||
return render(request, "calendar.html")
|
return render(request, "calendar.html")
|
||||||
|
|
||||||
|
|
||||||
def get_calendar_events(request):
|
def occurrence_detail(request, occurrence_id):
|
||||||
# get events from database (EventPage model)
|
occ = (
|
||||||
|
EventOccurrence.objects.select_related("event").filter(id=occurrence_id).first()
|
||||||
start = request.GET.get("start")
|
)
|
||||||
end = request.GET.get("end")
|
if not occ:
|
||||||
events = (
|
return redirect("calendar")
|
||||||
EventPage.objects.live()
|
event = occ.event.specific
|
||||||
.values("id", "title", "start", "end", "color", "tags")
|
return render(
|
||||||
.filter(start__gte=start, end__lte=end)
|
request,
|
||||||
|
"occurrence_detail.html",
|
||||||
|
{
|
||||||
|
"occurrence": occ,
|
||||||
|
"event": event,
|
||||||
|
"user_signed_up": occ.user_signed_up(request.user),
|
||||||
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
# to json
|
|
||||||
events_list = list(events)
|
|
||||||
|
|
||||||
# add page url to each event
|
def get_calendar_occurrences(request):
|
||||||
for event in events_list:
|
# get occurrences from database (EventOccurrence model)
|
||||||
event_page = EventPage.objects.live().filter(title=event["title"]).first()
|
start = request.GET.get("start")
|
||||||
if event_page:
|
end = request.GET.get("end")
|
||||||
event["url"] = event_page.url
|
occurrences = EventOccurrence.objects.filter(
|
||||||
if event_page.is_past:
|
start__gte=start, end__lte=end
|
||||||
event["color"] = "#666666"
|
).select_related("event")
|
||||||
|
|
||||||
|
events_list = []
|
||||||
|
for occ in occurrences:
|
||||||
|
event = occ.event.specific
|
||||||
|
events_list.append(
|
||||||
|
{
|
||||||
|
"id": occ.id,
|
||||||
|
"event_id": event.id,
|
||||||
|
"title": event.title,
|
||||||
|
"start": occ.start,
|
||||||
|
"end": occ.end,
|
||||||
|
"location": event.location,
|
||||||
|
"url": event.url,
|
||||||
|
"color": "#666666" if occ.is_past else event.color,
|
||||||
|
"tags": list(event.tags.values_list("name", flat=True)),
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
return JsonResponse(events_list, safe=False)
|
return JsonResponse(events_list, safe=False)
|
||||||
|
|
||||||
|
|
||||||
def get_calendar_event(request, event_id):
|
def get_calendar_occurrence(request, occurrence_id):
|
||||||
event = EventPage.objects.live().filter(id=event_id).first()
|
occ = (
|
||||||
if not event:
|
EventOccurrence.objects.select_related("event").filter(id=occurrence_id).first()
|
||||||
return JsonResponse({"error": "Event not found"}, status=404)
|
)
|
||||||
|
if not occ:
|
||||||
|
return JsonResponse({"error": "Occurrence not found"}, status=404)
|
||||||
|
event = occ.event.specific
|
||||||
event_dict = {
|
event_dict = {
|
||||||
"id": event.id,
|
"id": occ.id,
|
||||||
|
"event_id": event.id,
|
||||||
"title": event.title,
|
"title": event.title,
|
||||||
"start": event.start,
|
"start": occ.start,
|
||||||
"end": event.end,
|
"end": occ.end,
|
||||||
"location": event.location,
|
"location": event.location,
|
||||||
"url": event.url,
|
"url": event.url,
|
||||||
"color": event.color,
|
"color": "#666666" if occ.is_past else event.color,
|
||||||
"tags": list(event.tags.values_list("name", flat=True)),
|
"tags": list(event.tags.values_list("name", flat=True)),
|
||||||
|
"attendees_count": occ.attendees_count,
|
||||||
}
|
}
|
||||||
|
|
||||||
return JsonResponse(event_dict)
|
return JsonResponse(event_dict)
|
||||||
|
|
||||||
|
|
||||||
def event_signup(request, event_id):
|
def occurrence_signup(request, occurrence_id):
|
||||||
if not request.user.is_authenticated:
|
if not request.user.is_authenticated:
|
||||||
return redirect("login")
|
return redirect("login")
|
||||||
|
|
||||||
event = EventPage.objects.live().filter(id=event_id).first()
|
occ = EventOccurrence.objects.filter(id=occurrence_id).first()
|
||||||
if not event:
|
if not occ:
|
||||||
return redirect("calendar")
|
return redirect("calendar")
|
||||||
|
|
||||||
event.signed_up_users.add(request.user)
|
occ.signed_up_users.add(request.user)
|
||||||
# redirect to calendar page with ?modal=event_id to show modal with event details
|
occ.save()
|
||||||
return redirect(urls.reverse("calendar") + f"?modal={event_id}")
|
# redirect to calendar page with ?modal=occurrence_id to show modal with event details
|
||||||
|
return redirect(urls.reverse("calendar") + f"?modal={occurrence_id}")
|
||||||
|
|
||||||
|
|
||||||
def event_signout(request, event_id):
|
def occurrence_signout(request, occurrence_id):
|
||||||
if not request.user.is_authenticated:
|
if not request.user.is_authenticated:
|
||||||
return redirect("login")
|
return redirect("login")
|
||||||
|
|
||||||
event = EventPage.objects.live().filter(id=event_id).first()
|
occ = EventOccurrence.objects.filter(id=occurrence_id).first()
|
||||||
if not event:
|
if not occ:
|
||||||
return redirect("calendar")
|
return redirect("calendar")
|
||||||
|
|
||||||
event.signed_up_users.remove(request.user)
|
occ.signed_up_users.remove(request.user)
|
||||||
return redirect(urls.reverse("calendar") + f"?modal={event_id}")
|
occ.save()
|
||||||
|
return redirect(urls.reverse("calendar") + f"?modal={occurrence_id}")
|
||||||
|
|||||||
Reference in New Issue
Block a user