def contact_us(request): owner = User.objects.get(id=request.GET.get('owner')) if ( request.GET.get('owner') and User.objects.filter(id=request.GET.get('owner')).first()) else None video = Video.objects.get(id=request.GET.get('video')) if ( request.GET.get('video') and Video.objects.filter( id=request.GET.get('video')).first()) else None description = "%s: %s\n%s: %s%s\n\n" % ( _('Title'), video.title, _('Link'), 'https:' if request.is_secure() else 'http:', video.get_full_url(request)) if video else None send_subject = request.GET.get('subject') if ( request.GET.get('subject') and request.GET.get('subject') in [key for key, value in SUBJECT_CHOICES]) else None prefix = 'https://' if request.is_secure() else 'http://' home_page = ''.join([prefix, get_current_site(request).domain]) url_referrer = request.META["HTTP_REFERER"] if request.META.get( "HTTP_REFERER") else home_page form = ContactUsForm(request, initial={ 'url_referrer': url_referrer, 'subject': send_subject, 'description': description }) if request.method == "POST": form = ContactUsForm(request, request.POST) if form.is_valid(): name = form.cleaned_data['name'] form_subject = form.cleaned_data['subject'] if ( form.cleaned_data.get('subject')) else send_subject subject = "[ %s ] %s" % (TITLE_SITE, dict(SUBJECT_CHOICES)[form_subject]) email = form.cleaned_data['email'] message = form.cleaned_data['description'] text_content = loader.get_template('mail/mail.txt').render({ 'name': name, 'email': email, 'TITLE_SITE': TITLE_SITE, 'message': message, 'url_referrer': form.cleaned_data['url_referrer'] }) html_content = loader.get_template('mail/mail.html').render({ 'name': name, 'email': email, 'TITLE_SITE': TITLE_SITE, 'message': message.replace("\n", "<br/>"), 'url_referrer': form.cleaned_data['url_referrer'] }) dest_email = [owner.email] if owner else CONTACT_US_EMAIL msg = EmailMultiAlternatives(subject, text_content, email, dest_email) msg.attach_alternative(html_content, "text/html") msg.send(fail_silently=False) # EMAIL TO SENDER subject = "[ %s ] %s %s" % (TITLE_SITE, _('your message untitled'), dict(SUBJECT_CHOICES)[form_subject]) text_content = loader.get_template('mail/mail_sender.txt').render({ 'TITLE_SITE': TITLE_SITE, 'message': message }) html_content = loader.get_template('mail/mail_sender.html').render( { 'TITLE_SITE': TITLE_SITE, 'message': message.replace("\n", "<br/>") }) msg = EmailMultiAlternatives(subject, text_content, DEFAULT_FROM_EMAIL, [email]) msg.attach_alternative(html_content, "text/html") msg.send(fail_silently=False) messages.add_message(request, messages.INFO, _('Your message have been sent.')) return redirect(form.cleaned_data['url_referrer']) else: messages.add_message( request, messages.ERROR, _(u'One or more errors have been found in the form.')) return render(request, 'contact_us.html', {'form': form, 'owner': owner})
def contact_us(request): """Handle "Contact us" form.""" owner = (User.objects.get(id=request.GET.get("owner")) if (request.GET.get("owner") and User.objects.filter( id=request.GET.get("owner")).first()) else None) video = (Video.objects.get(id=request.GET.get("video"), sites=get_current_site(request)) if (request.GET.get("video") and request.GET.get("video").isdigit() and Video.objects.filter( id=request.GET.get("video"), sites=get_current_site(request)).first()) else None) description = ("%s: %s\n%s: %s%s\n\n" % ( _("Title"), video.title, _("Link"), "https:" if request.is_secure() else "http:", video.get_full_url(request), ) if video else None) send_subject = (request.GET.get("subject") if (request.GET.get("subject") and request.GET.get("subject") in [key for key, value in SUBJECT_CHOICES]) else None) prefix = "https://" if request.is_secure() else "http://" home_page = "".join([prefix, get_current_site(request).domain]) url_referrer = (request.META["HTTP_REFERER"] if request.META.get("HTTP_REFERER") else home_page) form = ContactUsForm( request, initial={ "url_referrer": url_referrer, "subject": send_subject, "description": description, }, ) if request.method == "POST": form = ContactUsForm(request, request.POST) if form.is_valid(): name = form.cleaned_data["name"] form_subject = (form.cleaned_data["subject"] if (form.cleaned_data.get("subject")) else send_subject) subject = "[ %s ] %s" % ( TITLE_SITE, dict(SUBJECT_CHOICES)[form_subject], ) email = form.cleaned_data["email"] message = form.cleaned_data["description"] valid_human = form.cleaned_data["valid_human"] if valid_human: return redirect(form.cleaned_data["url_referrer"]) text_content = loader.get_template("mail/mail.txt").render({ "name": name, "email": email, "TITLE_SITE": TITLE_SITE, "message": message, "url_referrer": form.cleaned_data["url_referrer"], }) html_content = loader.get_template("mail/mail.html").render({ "name": name, "email": email, "TITLE_SITE": TITLE_SITE, "message": message.replace("\n", "<br/>"), "url_referrer": form.cleaned_data["url_referrer"], }) dest_email = [] dest_email = get_dest_email(owner, video, form_subject, request) msg = EmailMultiAlternatives(subject, text_content, email, dest_email) msg.attach_alternative(html_content, "text/html") msg.send(fail_silently=False) # EMAIL TO SENDER subject = "[ %s ] %s %s" % ( TITLE_SITE, _("your message untitled"), dict(SUBJECT_CHOICES)[form_subject], ) text_content = loader.get_template("mail/mail_sender.txt").render({ "TITLE_SITE": TITLE_SITE, "message": message }) html_content = loader.get_template("mail/mail_sender.html").render( { "TITLE_SITE": TITLE_SITE, "message": message.replace("\n", "<br/>"), }) msg = EmailMultiAlternatives(subject, text_content, DEFAULT_FROM_EMAIL, [email]) msg.attach_alternative(html_content, "text/html") msg.send(fail_silently=False) messages.add_message(request, messages.INFO, _("Your message has been sent.")) return redirect(form.cleaned_data["url_referrer"]) else: messages.add_message( request, messages.ERROR, _(u"One or more errors have been found in the form."), ) return render(request, "contact_us.html", {"form": form, "owner": owner})