Source code for smarter.apps.dashboard.views.dashboard

# pylint: disable=W0613
"""Django views"""

import html

from django import forms
from django.core.handlers.wsgi import WSGIRequest
from django.http import JsonResponse
from django.shortcuts import redirect
from django.urls import reverse
from django.utils.decorators import method_decorator
from django.views.decorators.cache import cache_control

from smarter.apps.dashboard.models import EmailContactList
from smarter.common.helpers.mailchimp_helpers import MailchimpHelper
from smarter.common.utils import is_authenticated_request
from smarter.lib import json
from smarter.lib.django.views import (
    SmarterAuthenticatedWebView,
    SmarterWebHtmlView,
    smarter_cache_page_by_user,
)

DASHBOARD_CACHE_TIMEOUT = 10  # 10 seconds. keeps the dashboard snappy while avoiding appearing stale.


# ------------------------------------------------------------------------------
# Public Access Views
# ------------------------------------------------------------------------------
[docs] class ComingSoon(SmarterWebHtmlView): """Public Access Dashboard view"""
[docs] class EmailForm(forms.Form): """Form for the sign-in page.""" email = forms.EmailField()
template_path = "coming-soon.html"
[docs] def get(self, request, *args, **kwargs): form = ComingSoon.EmailForm() context = {"form": form} return self.clean_http_response(request, template_path=self.template_path, context=context)
[docs] def post(self, request: WSGIRequest): form = ComingSoon.EmailForm(request.POST) if form.is_valid(): email = form.cleaned_data["email"] email_contact_list, created = EmailContactList.objects.get_or_create(email=email) if created: MailchimpHelper().add_list_member(email_contact_list.email) message = "We'll notify you when the launch date nears." else: message = f"{email_contact_list.email} is already in our contact list. We'll keep you updated." return JsonResponse( { "redirect": "/email-added/", "context": { "email_added": { "created": created, "message": message, "email": email_contact_list.email, } }, } ) html_error = html.escape(form.errors.as_text()) return JsonResponse({"error": json.dumps(html_error)})
[docs] class EmailAdded(SmarterWebHtmlView): """Confirmation view for email added to contact list.""" template_path = "dashboard/email-added.html"
[docs] def post(self, request: WSGIRequest): context = json.loads(request.body.decode("utf-8")) return self.clean_http_response(request, template_path=self.template_path, context=context)
[docs] class ChangeLogView(SmarterWebHtmlView): """Notifications view""" template_path = "dashboard/changelog.html"
# ------------------------------------------------------------------------------ # Protected Views # ------------------------------------------------------------------------------
[docs] class NotificationsView(SmarterAuthenticatedWebView): """Notifications view""" template_path = "dashboard/notifications.html"
[docs] @method_decorator(cache_control(max_age=DASHBOARD_CACHE_TIMEOUT), name="dispatch") @method_decorator(smarter_cache_page_by_user(DASHBOARD_CACHE_TIMEOUT), name="dispatch") class DashboardView(SmarterAuthenticatedWebView): """Public Access Dashboard view""" template_path = "dashboard/authenticated.html"
[docs] def get(self, request: WSGIRequest, *args, **kwargs): if kwargs.get("invalidate_cache", False): self.invalidate(request=request, *args, **kwargs) if is_authenticated_request(request): return super().get(request, *args, **kwargs) return redirect(reverse("login_view"))