A Python content management system designed for kirsle.net featuring a blog, comments and photo albums. https://rophako.kirsle.net/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

72 lines
1.9KB

  1. # -*- coding: utf-8 -*-
  2. from __future__ import unicode_literals
  3. """Endpoints for contacting the site owner."""
  4. from flask import Blueprint, request, redirect, url_for, flash
  5. from rophako.utils import template, send_email, remote_addr
  6. from rophako.settings import Config
  7. mod = Blueprint("contact", __name__, url_prefix="/contact")
  8. @mod.route("/")
  9. def index():
  10. return template("contact/index.html")
  11. @mod.route("/send", methods=["POST"])
  12. def send():
  13. """Submitting the contact form."""
  14. name = request.form.get("name", "") or "Anonymous"
  15. email = request.form.get("email", "")
  16. subject = request.form.get("subject", "") or "[No Subject]"
  17. message = request.form.get("message", "")
  18. # Spam traps.
  19. trap1 = request.form.get("contact", "x") != ""
  20. trap2 = request.form.get("website", "x") != "http://"
  21. if trap1 or trap2:
  22. flash("Wanna try that again?")
  23. return redirect(url_for(".index"))
  24. # Message is required.
  25. if len(message) == 0:
  26. flash("The message is required.")
  27. return redirect(url_for(".index"))
  28. # Email looks valid?
  29. reply_to = None
  30. if "@" in email and "." in email:
  31. reply_to = email
  32. # Send the e-mail.
  33. send_email(
  34. to=Config.site.notify_address,
  35. reply_to=reply_to,
  36. subject="Contact Form on {}: {}".format(Config.site.site_name, subject),
  37. message="""A visitor to {site_name} has sent you a message!
  38. IP Address: {ip}
  39. User Agent: {ua}
  40. Referrer: {referer}
  41. Name: {name}
  42. E-mail: {email}
  43. Subject: {subject}
  44. {message}""".format(
  45. site_name=Config.site.site_name,
  46. ip=remote_addr(),
  47. ua=request.user_agent.string,
  48. referer=request.headers.get("Referer", ""),
  49. name=name,
  50. email=email,
  51. subject=subject,
  52. message=message,
  53. )
  54. )
  55. flash("Your message has been delivered.")
  56. return redirect(url_for("index"))