111 lines
3.9 KiB
Python
111 lines
3.9 KiB
Python
from datetime import datetime
|
|
|
|
from django.conf import settings
|
|
from django.contrib import messages
|
|
from django.contrib.auth.decorators import permission_required
|
|
from django.contrib.auth.mixins import PermissionRequiredMixin
|
|
from django.shortcuts import render
|
|
from django.views.generic.dates import MonthArchiveView
|
|
|
|
from dal import autocomplete
|
|
|
|
from .models import Member, EventExt
|
|
from membershipworks import MembershipWorks
|
|
|
|
|
|
class MemberAutocomplete(autocomplete.Select2QuerySetView):
|
|
model = Member
|
|
search_fields = ["account_name"]
|
|
|
|
def get_queryset(self):
|
|
if not self.request.user.has_perm("membershipworks.view_member"):
|
|
return Member.objects.none()
|
|
else:
|
|
return super().get_queryset()
|
|
|
|
|
|
@permission_required("membershipworks.view_eventext")
|
|
def upcoming_events(request):
|
|
now = datetime.now()
|
|
|
|
membershipworks = MembershipWorks()
|
|
membershipworks.login(
|
|
settings.MEMBERSHIPWORKS_USERNAME, settings.MEMBERSHIPWORKS_PASSWORD
|
|
)
|
|
|
|
events = membershipworks.get_events_list(now)
|
|
if "error" in events:
|
|
messages.add_message(
|
|
request,
|
|
messages.ERROR,
|
|
f"MembershipWorks Error: {events['error']}",
|
|
)
|
|
# TODO: this should probably be an HTTP 500 response
|
|
return render(request, "base.dj.html")
|
|
|
|
ongoing_events = []
|
|
full_events = []
|
|
upcoming_events = []
|
|
for event in events["evt"]:
|
|
try:
|
|
# ignore hidden events
|
|
if event["cal"] == 0:
|
|
continue
|
|
event_details = membershipworks.get_event_by_eid(event["eid"])
|
|
|
|
# Convert timestamps to datetime objects
|
|
event_details["sdp_dt"] = datetime.fromtimestamp(event_details["sdp"])
|
|
event_details["edp_dt"] = datetime.fromtimestamp(event_details["edp"])
|
|
|
|
# registration has already ended
|
|
if (
|
|
"erd" in event_details
|
|
and datetime.fromtimestamp(event_details["erd"]) < now
|
|
):
|
|
ongoing_events.append(event_details)
|
|
# class is full
|
|
elif event_details["cnt"] >= event_details["cap"]:
|
|
full_events.append(event_details)
|
|
else:
|
|
upcoming_events.append(event_details)
|
|
|
|
except KeyError as e:
|
|
messages.add_message(
|
|
request,
|
|
messages.ERROR,
|
|
f"Event '{event.get('ttl')}' missing required property: '{e.args[0]}'",
|
|
)
|
|
# TODO: this should probably be an HTTP 500 response
|
|
return render(request, "base.dj.html")
|
|
|
|
context = {
|
|
"event_sections": [
|
|
{
|
|
"title": "Upcoming Events",
|
|
"blurb": "Events that are currently open for registration.",
|
|
"events": upcoming_events,
|
|
"truncate": False,
|
|
},
|
|
{
|
|
"title": "Just Missed",
|
|
"blurb": "These classes are currently full at time of writing. If you are interested, please check the event's page; spots occasionally open up. Keep an eye on this newsletter to see when these classes are offered again.",
|
|
"events": full_events,
|
|
"truncate": True,
|
|
},
|
|
{
|
|
"title": "Ongoing Events",
|
|
"blurb": "These events are ongoing. Registration is currently closed, but these events may be offered again in the future.",
|
|
"events": ongoing_events,
|
|
"truncate": True,
|
|
},
|
|
]
|
|
}
|
|
return render(request, "membershipworks/upcoming_events.dj.html", context)
|
|
|
|
|
|
class EventMonthReport(PermissionRequiredMixin, MonthArchiveView):
|
|
permission_required = "membershipworks.view_eventext"
|
|
queryset = EventExt.objects.select_related("category", "instructor").all()
|
|
date_field = "start"
|
|
template_name = "membershipworks/event_month_report.dj.html"
|