import structlog from django.conf import settings from django.core.cache import cache from django.core.exceptions import PermissionDenied from django.http import Http404, HttpResponse from django.shortcuts import render from django.views import View from django.views.generic import TemplateView from core.calendar import extract_calendar_events, events_by_month, get_calendar from libraries.constants import LATEST_RELEASE_URL_PATH_STR from libraries.mixins import ContributorMixin from news.models import Entry logger = structlog.get_logger() class HomepageView(ContributorMixin, TemplateView): """ Define all the pieces that will be displayed on the home page """ template_name = "homepage.html" def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) context["entries"] = Entry.objects.published().order_by("-publish_at")[:3] context["events"] = self.get_events() if context["events"]: context["num_months"] = len(context["events"]) else: context["num_months"] = 0 context["LATEST_RELEASE_URL_PATH_STR"] = LATEST_RELEASE_URL_PATH_STR return context def get_events(self): """Returns the events from the Boost Google Calendar.""" cached_events = cache.get(settings.EVENTS_CACHE_KEY) if cached_events: return cached_events try: raw_event_data = get_calendar() except Exception: logger.info("Error getting events") return if not raw_event_data: return events = extract_calendar_events(raw_event_data) sorted_events = events_by_month(events) cache.set( settings.EVENTS_CACHE_KEY, dict(sorted_events), settings.EVENTS_CACHE_TIMEOUT, ) return dict(sorted_events) class ForbiddenView(View): """ This view raises an exception to test our 403.html template """ def get(self, *args, **kwargs): raise PermissionDenied("403 Forbidden") class InternalServerErrorView(View): """ This view raises an exception to test our 500.html template """ def get(self, *args, **kwargs): raise ValueError("500 Internal Server Error") class NotFoundView(View): """ This view raises an exception to test our 404.html template """ def get(self, *args, **kwargs): raise Http404("404 Not Found") def custom_404_view(request, exception=None): return render(request, "404.html", status=404) class OKView(View): def get(self, *args, **kwargs): return HttpResponse("200 OK", status=200)