diff --git a/app/signup/migrations/0002_signup_referrer.py b/app/signup/migrations/0002_signup_referrer.py new file mode 100644 index 0000000..2a4d2b7 --- /dev/null +++ b/app/signup/migrations/0002_signup_referrer.py @@ -0,0 +1,18 @@ +# Generated by Django 5.0.2 on 2024-02-27 16:29 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('signup', '0001_initial'), + ] + + operations = [ + migrations.AddField( + model_name='signup', + name='referrer', + field=models.CharField(blank=True, max_length=255), + ), + ] diff --git a/app/signup/models.py b/app/signup/models.py index b7ee79d..656053f 100644 --- a/app/signup/models.py +++ b/app/signup/models.py @@ -7,6 +7,7 @@ class Signup(models.Model): anonymized_ip = models.GenericIPAddressField() user_agent = models.CharField(max_length=255) geoip = models.JSONField(null=True, blank=True) + referrer = models.CharField(max_length=255, blank=True) class Meta: constraints = [ diff --git a/app/signup/tests.py b/app/signup/tests.py index 8b1eb5f..d32cb2a 100644 --- a/app/signup/tests.py +++ b/app/signup/tests.py @@ -17,10 +17,12 @@ class SignupViewTest(TestCase): resp = self.client.post(reverse("index"), {"email": "foo@example.com"}, follow=True, HTTP_USER_AGENT="foo-agent", - REMOTE_ADDR="127.0.0.2") + REMOTE_ADDR="127.0.0.2", + HTTP_REFERRER="example.org/good-place") self.assertEqual(resp.redirect_chain, [('/?success=subscribed', 302)]) self.assertContains(resp, "You are now subscribed!") obj = Signup.objects.get() self.assertEqual(obj.email, "foo@example.com") self.assertEqual(obj.anonymized_ip, "127.0.0.0") self.assertEqual(obj.user_agent, "foo-agent") + self.assertEqual(obj.referrer, "example.org/good-place") diff --git a/app/signup/views.py b/app/signup/views.py index 7515d4b..d9687e0 100644 --- a/app/signup/views.py +++ b/app/signup/views.py @@ -33,8 +33,9 @@ def index(request): s = Signup( email=request.POST.get("email"), anonymized_ip=anonymous_ip, - user_agent=request.META["HTTP_USER_AGENT"], + user_agent=request.META['HTTP_USER_AGENT'], geoip=geoip, + referrer=request.META.get('HTTP_REFERRER', ''), ) try: