def contact( request, template_path='contact_form.html', template_data=None, initial=None): """This is a fairly run-of-the-mill contact form, except that it can be overridden in various ways so that its logic can be called from other functions. We also use a field called phone_number in place of the subject field to defeat spam. """ if template_data is None: template_data = {} if initial is None: initial = {} if request.method == 'POST': form = ContactForm(request.POST) if form.is_valid(): cd = form.cleaned_data # Uses phone_number as Subject field to defeat spam. If this field # begins with three digits, assume it's spam; fake success. if re.match('\d{3}', cd['phone_number']): logger.info("Detected spam message. Not sending email.") return HttpResponseRedirect(reverse(u'contact_thanks')) default_from = settings.DEFAULT_FROM_EMAIL EmailMessage( subject=u'[CourtListener] Contact: ' u'{phone_number}'.format(**cd), body=u'Subject: {phone_number}\n' u'From: {name} ({email})\n' u'\n\n{message}\n\n' u'Browser: {browser}'.format( browser=request.META.get(u'HTTP_USER_AGENT', u"Unknown"), **cd ), to=['*****@*****.**'], reply_to=[cd.get(u'email', default_from) or default_from], ).send() return HttpResponseRedirect(reverse(u'contact_thanks')) else: # the form is loading for the first time try: initial[u'email'] = request.user.email initial[u'name'] = request.user.get_full_name() form = ContactForm(initial=initial) except AttributeError: # for anonymous users, who lack full_names, and emails form = ContactForm(initial=initial) template_data.update( {u'form': form, u'private': False} ) return render(request, template_path, template_data)
def contact( request: HttpRequest, template_path: str = "contact_form.html", template_data: Optional[Dict[str, Union[ContactForm, str, bool]]] = None, initial: Optional[Dict[str, str]] = None, ) -> HttpResponse: """This is a fairly run-of-the-mill contact form, except that it can be overridden in various ways so that its logic can be called from other functions. We also use a field called phone_number in place of the subject field to defeat spam. """ if template_data is None: template_data = {} if initial is None: initial = {} if request.method == "POST": form = ContactForm(request.POST) if form.is_valid(): cd = form.cleaned_data # Uses phone_number as Subject field to defeat spam. If this field # begins with three digits, assume it's spam; fake success. if re.match(r"\d{3}", cd["phone_number"]): logger.info("Detected spam message. Not sending email.") return HttpResponseRedirect(reverse("contact_thanks")) donation_totals = get_donation_totals_by_email(cd["email"]) default_from = settings.DEFAULT_FROM_EMAIL EmailMessage( subject="[CourtListener] Contact: " "{phone_number}".format(**cd), body="Subject: {phone_number}\n" "From: {name} ({email})\n" "Total donated: {total_donated}\n" "Total last year: {total_last_year}\n\n\n" "{message}\n\n" "Browser: {browser}".format( browser=request.META.get("HTTP_USER_AGENT", "Unknown"), total_donated=donation_totals["total"], total_last_year=donation_totals["last_year"], **cd, ), to=["*****@*****.**"], reply_to=[cd.get("email", default_from) or default_from], ).send() return HttpResponseRedirect(reverse("contact_thanks")) else: # the form is loading for the first time if isinstance(request.user, User): initial["email"] = request.user.email initial["name"] = request.user.get_full_name() form = ContactForm(initial=initial) else: # for anonymous users, who lack full_names, and emails form = ContactForm(initial=initial) template_data.update({"form": form, "private": False}) return render(request, template_path, template_data)
def contact( request, template_path='contact_form.html', template_data=None, initial=None): """This is a fairly run-of-the-mill contact form, except that it can be overridden in various ways so that its logic can be called from other functions. """ if template_data is None: template_data = {} if initial is None: initial = {} if request.method == 'POST': form = ContactForm(request.POST) if form.is_valid(): cd = form.cleaned_data default_from = settings.DEFAULT_FROM_EMAIL EmailMessage( subject=u'[CourtListener] Contact form message: ' u'{subject}'.format(**cd), body=u'Subject: {subject}\n' u'From: {name} ({email})\n' u'Browser: {browser}\n' u'Message: \n\n{message}'.format( browser=request.META.get(u'HTTP_USER_AGENT', u"Unknown"), **cd ), to=[m[1] for m in settings.MANAGERS], reply_to=[cd.get(u'email', default_from) or default_from], ).send() return HttpResponseRedirect(reverse(u'contact_thanks')) else: # the form is loading for the first time try: initial[u'email'] = request.user.email initial[u'name'] = request.user.get_full_name() form = ContactForm(initial=initial) except AttributeError: # for anonymous users, who lack full_names, and emails form = ContactForm(initial=initial) template_data.update( {u'form': form, u'private': False} ) return render_to_response( template_path, template_data, RequestContext(request) )