From f1e1d05aba895fb4cd35e86c9ce0711102d79023 Mon Sep 17 00:00:00 2001 From: ndu Date: Wed, 15 Jan 2025 19:46:30 +0100 Subject: [PATCH] fix: add fix --- ...ehiiv_id_subscriber_sync_error_and_more.py | 28 +++++++++++++++ ...3_remove_subscriber_beehiiv_id_and_more.py | 25 ++++++++++++++ server/apps/newsletter/views.py | 34 +++++++++++-------- 3 files changed, 73 insertions(+), 14 deletions(-) create mode 100644 server/apps/newsletter/migrations/0012_subscriber_beehiiv_id_subscriber_sync_error_and_more.py create mode 100644 server/apps/newsletter/migrations/0013_remove_subscriber_beehiiv_id_and_more.py diff --git a/server/apps/newsletter/migrations/0012_subscriber_beehiiv_id_subscriber_sync_error_and_more.py b/server/apps/newsletter/migrations/0012_subscriber_beehiiv_id_subscriber_sync_error_and_more.py new file mode 100644 index 0000000..ee0d340 --- /dev/null +++ b/server/apps/newsletter/migrations/0012_subscriber_beehiiv_id_subscriber_sync_error_and_more.py @@ -0,0 +1,28 @@ +# Generated by Django 5.0.8 on 2025-01-15 18:42 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('newsletter', '0011_alter_newsletter_scheduled_send_time'), + ] + + operations = [ + migrations.AddField( + model_name='subscriber', + name='beehiiv_id', + field=models.CharField(blank=True, max_length=255, null=True), + ), + migrations.AddField( + model_name='subscriber', + name='sync_error', + field=models.TextField(blank=True, null=True), + ), + migrations.AddField( + model_name='subscriber', + name='synced_to_beehiiv', + field=models.BooleanField(default=False), + ), + ] diff --git a/server/apps/newsletter/migrations/0013_remove_subscriber_beehiiv_id_and_more.py b/server/apps/newsletter/migrations/0013_remove_subscriber_beehiiv_id_and_more.py new file mode 100644 index 0000000..4267ff1 --- /dev/null +++ b/server/apps/newsletter/migrations/0013_remove_subscriber_beehiiv_id_and_more.py @@ -0,0 +1,25 @@ +# Generated by Django 5.0.8 on 2025-01-15 18:43 + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('newsletter', '0012_subscriber_beehiiv_id_subscriber_sync_error_and_more'), + ] + + operations = [ + migrations.RemoveField( + model_name='subscriber', + name='beehiiv_id', + ), + migrations.RemoveField( + model_name='subscriber', + name='sync_error', + ), + migrations.RemoveField( + model_name='subscriber', + name='synced_to_beehiiv', + ), + ] diff --git a/server/apps/newsletter/views.py b/server/apps/newsletter/views.py index e0e5f5a..c5336ed 100644 --- a/server/apps/newsletter/views.py +++ b/server/apps/newsletter/views.py @@ -1,36 +1,42 @@ -from django.shortcuts import render from django.http import JsonResponse -from .models import Subscriber from django.views.decorators.csrf import csrf_exempt -from django.db import IntegrityError, transaction -import logging - -logger = logging.getLogger(__name__) +from django.db import IntegrityError +from django.shortcuts import render +from .models import Subscriber -@csrf_exempt +@csrf_exempt def subscribe(request): + """ + Handles POST requests to subscribe an email address. + """ if request.method == 'POST': email = request.POST.get('email') + + # Validate email if not email: return JsonResponse({'message': 'Email is required'}, status=400) try: - with transaction.atomic(): - subscriber = Subscriber.objects.create(email=email, is_active=True) - logger.info(f"Triggering Beehiiv sync for subscriber: {subscriber.id}") - + # Create a new subscriber + Subscriber.objects.create(email=email, is_active=True) return JsonResponse({'message': 'Subscription successful'}, status=200) + except IntegrityError: + # Handle case where email is already subscribed return JsonResponse({'message': 'Email already subscribed'}, status=400) - except Exception as e: - logger.error(f"Error in subscription process: {str(e)}") - return JsonResponse({'message': 'An error occurred'}, status=500) + + # If the request method is not POST, return a 405 Method Not Allowed response + return JsonResponse({'message': 'Method not allowed'}, status=405) def unsubscribe(request, email): + """ + Handles unsubscribing an email address. + """ try: subscriber = Subscriber.objects.get(email=email) subscriber.is_active = False subscriber.save() return render(request, 'newsletter/unsubscribe_success.html', {'email': email}) + except Subscriber.DoesNotExist: return render(request, 'newsletter/unsubscribe_fail.html', {'email': None}) \ No newline at end of file