2024-01-11 23:36:15 +02:00
|
|
|
import logging
|
|
|
|
|
|
|
|
from django.shortcuts import render, redirect
|
|
|
|
from django.db import IntegrityError
|
|
|
|
|
|
|
|
from lib.anonymize_ip import anonymize_ip
|
|
|
|
|
|
|
|
from .models import Signup
|
|
|
|
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2023-12-14 18:27:56 +02:00
|
|
|
|
|
|
|
def index(request):
|
2024-01-11 23:36:15 +02:00
|
|
|
if request.method == "POST":
|
|
|
|
x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')
|
|
|
|
if x_forwarded_for:
|
|
|
|
ip = x_forwarded_for.split(',')[0]
|
|
|
|
else:
|
|
|
|
ip = request.META.get('REMOTE_ADDR')
|
|
|
|
anonymous_ip = anonymize_ip(ip)
|
|
|
|
|
|
|
|
e = Email(
|
|
|
|
email = request.POST.get("email"),
|
|
|
|
anonymized_ip = anonymous_ip,
|
|
|
|
user_agent = request.META["HTTP_USER_AGENT"],
|
|
|
|
)
|
|
|
|
|
|
|
|
try:
|
|
|
|
e.clean_fields()
|
|
|
|
except ValidationError as e:
|
|
|
|
return e.render(request, "signup/index.html",
|
|
|
|
{"error_message": e.message})
|
|
|
|
|
|
|
|
logging.info("registering email={}".format(request.POST.get("email")))
|
|
|
|
try:
|
|
|
|
e.save()
|
|
|
|
except IntegrityError:
|
|
|
|
# email already registered, presumably
|
|
|
|
return redirect("index") + "?already_subscribed"
|
|
|
|
except DatabaseError:
|
|
|
|
logger.exception("database error when registering an email")
|
|
|
|
err = "Sorry, database error. Please come back later."
|
|
|
|
return render(request, "signup/index.html",
|
|
|
|
{"error_message": err})
|
|
|
|
return redirect("index") + "?success"
|
|
|
|
|
2023-12-14 22:41:20 +02:00
|
|
|
return render(request, "signup/index.html", {})
|