--wip-- [skip ci]
This commit is contained in:
156
course_calendar/views.py
Normal file
156
course_calendar/views.py
Normal file
@@ -0,0 +1,156 @@
|
||||
from django import urls
|
||||
from django.http import JsonResponse
|
||||
from django.shortcuts import redirect, render
|
||||
|
||||
from course_calendar.forms import EventForm
|
||||
|
||||
from .models import CalendarEvent, EventOccurrence
|
||||
|
||||
|
||||
def calendar(request):
|
||||
return render(request, "calendar.html")
|
||||
|
||||
|
||||
def admin_events_dashboard(request):
|
||||
return render(
|
||||
request,
|
||||
"admin_events_dashboard.html",
|
||||
context={
|
||||
"events": EventOccurrence.objects.select_related("event").all(),
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
def admin_add_event(request):
|
||||
if request.method == "POST":
|
||||
form = EventForm(request.POST)
|
||||
if form.is_valid():
|
||||
print("CLEANED DATA:", form.cleaned_data)
|
||||
event = form.save()
|
||||
print("Event created:", event.__dict__)
|
||||
# Save many-to-many data if present
|
||||
if hasattr(form, "save_m2m"):
|
||||
form.save_m2m()
|
||||
return redirect("admin_events_dashboard")
|
||||
else:
|
||||
print("FORM ERRORS:", form.errors)
|
||||
|
||||
form = EventForm()
|
||||
|
||||
return render(request, "admin_add_event.html", {"form": form})
|
||||
|
||||
|
||||
def occurrence_detail(request, occurrence_id):
|
||||
occ = (
|
||||
EventOccurrence.objects.select_related("event").filter(id=occurrence_id).first()
|
||||
)
|
||||
if not occ:
|
||||
return redirect("calendar")
|
||||
event = occ.event.specific
|
||||
return render(
|
||||
request,
|
||||
"occurrence_detail.html",
|
||||
{
|
||||
"occurrence": occ,
|
||||
"event": event,
|
||||
"user_signed_up": occ.user_signed_up(request.user),
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
def get_all_events(request):
|
||||
events = EventOccurrence.objects.select_related("event").all()
|
||||
events_list = []
|
||||
for occ in events:
|
||||
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)
|
||||
|
||||
|
||||
def get_calendar_occurrences(request):
|
||||
# get occurrences from database (EventOccurrence model)
|
||||
start = request.GET.get("start")
|
||||
end = request.GET.get("end")
|
||||
occurrences = EventOccurrence.objects.filter(
|
||||
start__gte=start, end__lte=end
|
||||
).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)
|
||||
|
||||
|
||||
def get_calendar_occurrence(request, occurrence_id):
|
||||
occ = (
|
||||
EventOccurrence.objects.select_related("event").filter(id=occurrence_id).first()
|
||||
)
|
||||
if not occ:
|
||||
return JsonResponse({"error": "Occurrence not found"}, status=404)
|
||||
event = occ.event.specific
|
||||
event_dict = {
|
||||
"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)),
|
||||
"attendees_count": occ.attendees_count,
|
||||
}
|
||||
return JsonResponse(event_dict)
|
||||
|
||||
|
||||
def occurrence_signup(request, occurrence_id):
|
||||
if not request.user.is_authenticated:
|
||||
return redirect("login")
|
||||
|
||||
occ = EventOccurrence.objects.filter(id=occurrence_id).first()
|
||||
if not occ:
|
||||
return redirect("calendar")
|
||||
|
||||
occ.signed_up_users.add(request.user)
|
||||
occ.save()
|
||||
# redirect to calendar page with ?modal=occurrence_id to show modal with event details
|
||||
return redirect(urls.reverse("calendar") + f"?modal={occurrence_id}")
|
||||
|
||||
|
||||
def occurrence_signout(request, occurrence_id):
|
||||
if not request.user.is_authenticated:
|
||||
return redirect("login")
|
||||
|
||||
occ = EventOccurrence.objects.filter(id=occurrence_id).first()
|
||||
if not occ:
|
||||
return redirect("calendar")
|
||||
|
||||
occ.signed_up_users.remove(request.user)
|
||||
occ.save()
|
||||
return redirect(urls.reverse("calendar") + f"?modal={occurrence_id}")
|
||||
Reference in New Issue
Block a user