view contact/views.py @ 1195:7fc6c42b2f5b

Adding a local user photo upload option.
author Brian Neal <bgneal@gmail.com>
date Sun, 07 May 2023 16:22:13 -0500
parents b957e4829a03
children
line wrap: on
line source
"""Views for the contact application."""

from django.conf import settings
from django.shortcuts import redirect, render
from django.views.generic import TemplateView

from contact.forms import ContactForm
from core.functions import get_full_name


def contact_form(request):
    if request.method == 'POST':
        form = ContactForm(request.POST, request=request)
        if form.is_valid():
            form.save()
            return redirect('contact-thanks')
    else:
        initial_data = {}
        if request.user.is_authenticated():
            name = get_full_name(request.user).strip()
            if name != request.user.username:
                name = '{} ({})'.format(name, request.user.username)

            initial_data = {'name': name, 'email': request.user.email}

        subject = request.GET.get('subject')
        if subject:
            initial_data['subject'] = subject

        form = ContactForm(initial=initial_data, request=request)

    return render(request, 'contact/contact_form.html', {
        'form': form,
        'RECAPTCHA_SITE_KEY': settings.RECAPTCHA_SITE_KEY,
        'V3_DESIGN': True,
    })


class ContactThanksView(TemplateView):
    template_name = 'contact/contact_thanks.html'

    def get_context_data(self, **kwargs):
        context = super(ContactThanksView, self).get_context_data(**kwargs)
        context['V3_DESIGN'] = True
        return context