-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add unsubscribe from marketing email functionality
Create UnsubscribedUser model to save unsubscribed users Create a POST API endpoint to unsubscribe user from marketing emails
- Loading branch information
1 parent
17074fa
commit 102d89f
Showing
14 changed files
with
206 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
""" | ||
Configuration for the edraak_marketing_email Django application. | ||
""" | ||
from django.apps import AppConfig | ||
|
||
|
||
class EdraakMarketingEmailConfig(AppConfig): | ||
""" | ||
Configuration class for the edraak_marketing_email Django application. | ||
""" | ||
name = 'edraak_marketing_email' | ||
verbose_name = "Edraak Marketing Email" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
from edraak_marketing_email.models import UnsubscribedUser | ||
|
||
|
||
def unsubscribe_from_marketing_emails(user): | ||
is_unsubscribed = UnsubscribedUser.objects.filter(user=user).exists() | ||
if is_unsubscribed: | ||
return | ||
|
||
UnsubscribedUser.objects.create(user=user) | ||
|
||
|
||
def subscribe_to_marketing_emails(user): | ||
is_unsubscribed = UnsubscribedUser.objects.filter(user=user).exists() | ||
if not is_unsubscribed: | ||
return | ||
|
||
UnsubscribedUser.objects.filter(user=user).delete() |
26 changes: 26 additions & 0 deletions
26
lms/djangoapps/edraak_marketing_email/migrations/0001_initial.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# -*- coding: utf-8 -*- | ||
# Generated by Django 1.11.18 on 2020-03-08 08:16 | ||
from __future__ import unicode_literals | ||
|
||
from django.conf import settings | ||
from django.db import migrations, models | ||
import django.db.models.deletion | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
initial = True | ||
|
||
dependencies = [ | ||
migrations.swappable_dependency(settings.AUTH_USER_MODEL), | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name='UnsubscribedUser', | ||
fields=[ | ||
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), | ||
('user', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)), | ||
], | ||
), | ||
] |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
""" | ||
Edraak-Marketing-Email-related models. | ||
""" | ||
from django.db import models | ||
from django.contrib.auth.models import User | ||
|
||
|
||
class UnsubscribedUser(models.Model): | ||
""" | ||
Stores users that have opted out of receiving marketing emails. | ||
""" | ||
class Meta(object): | ||
app_label = "edraak_marketing_email" | ||
|
||
user = models.OneToOneField(User, db_index=True, on_delete=models.CASCADE) |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
from rest_framework.test import APITestCase | ||
from rest_framework import status | ||
from django.urls import reverse | ||
from student.tests.factories import UserFactory | ||
from edraak_marketing_email.helpers import ( | ||
subscribe_to_marketing_emails, | ||
unsubscribe_from_marketing_emails | ||
) | ||
|
||
|
||
class TestUnsubscribeUserAPIView(APITestCase): | ||
"""Tests for handling edraak marketing email unsubscription""" | ||
|
||
def setUp(self): | ||
super(TestUnsubscribeUserAPIView, self).setUp() | ||
self.client = self.client_class() | ||
self.url = reverse('edraak_marketing_email:unsubscribe') | ||
|
||
def test_user_unsubscribed_successfully(self, *args): # pylint: disable=unused-argument | ||
user = UserFactory() | ||
|
||
data = { | ||
'username': user.username | ||
} | ||
resp = self.client.post(self.url, data, format='json') | ||
|
||
# Expect that the request gets through successfully, | ||
self.assertEqual(resp.status_code, status.HTTP_200_OK) | ||
|
||
def test_already_unsubscribed_user(self, *args): # pylint: disable=unused-argument | ||
user = UserFactory() | ||
unsubscribe_from_marketing_emails(user) | ||
|
||
data = { | ||
'username': user.username | ||
} | ||
resp = self.client.post(self.url, data, format='json') | ||
|
||
# Expect that the request gets through successfully, | ||
self.assertEqual(resp.status_code, status.HTTP_200_OK) | ||
|
||
|
||
class TestSubscribeUserAPIView(APITestCase): | ||
"""Tests for handling edraak marketing email subscription""" | ||
|
||
def setUp(self): | ||
super(TestSubscribeUserAPIView, self).setUp() | ||
self.client = self.client_class() | ||
self.url = reverse('edraak_marketing_email:subscribe') | ||
|
||
def test_user_subscribed_successfully(self, *args): # pylint: disable=unused-argument | ||
user = UserFactory() | ||
|
||
data = { | ||
'username': user.username | ||
} | ||
resp = self.client.post(self.url, data, format='json') | ||
|
||
# Expect that the request gets through successfully, | ||
self.assertEqual(resp.status_code, status.HTTP_200_OK) | ||
|
||
def test_already_subscribed_user(self, *args): # pylint: disable=unused-argument | ||
user = UserFactory() | ||
subscribe_to_marketing_emails(user) | ||
|
||
data = { | ||
'username': user.username | ||
} | ||
resp = self.client.post(self.url, data, format='json') | ||
|
||
# Expect that the request gets through successfully, | ||
self.assertEqual(resp.status_code, status.HTTP_200_OK) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
""" | ||
URLs for edraak marketing email. | ||
""" | ||
from django.conf.urls import url | ||
from edraak_marketing_email import views | ||
|
||
|
||
urlpatterns = [ | ||
url(r"^marketing_email/unsubscribe$", views.UnsubscribeUserAPIView.as_view(), name="unsubscribe"), | ||
url(r"^marketing_email/subscribe$", views.SubscribeUserAPIView.as_view(), name="subscribe"), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
from django.contrib.auth.models import User | ||
from django.shortcuts import get_object_or_404 | ||
from rest_framework import status | ||
from rest_framework.response import Response | ||
from rest_framework.views import APIView | ||
from edraak_marketing_email.helpers import ( | ||
subscribe_to_marketing_emails, | ||
unsubscribe_from_marketing_emails | ||
) | ||
|
||
|
||
class UnsubscribeUserAPIView(APIView): | ||
""" | ||
Unsubscribe user from marketing emails | ||
""" | ||
authentication_classes = [] | ||
permission_classes = [] | ||
|
||
def post(self, request, *args, **kwargs): | ||
username = request.data.get('username') | ||
user = get_object_or_404(User, username=username) | ||
|
||
unsubscribe_from_marketing_emails(user) | ||
return Response(data={}, status=status.HTTP_200_OK) | ||
|
||
|
||
class SubscribeUserAPIView(APIView): | ||
""" | ||
Subscribe user to marketing emails | ||
""" | ||
authentication_classes = [] | ||
permission_classes = [] | ||
|
||
def post(self, request, *args, **kwargs): | ||
username = request.data.get('username') | ||
user = get_object_or_404(User, username=username) | ||
|
||
subscribe_to_marketing_emails(user) | ||
return Response(data={}, status=status.HTTP_200_OK) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters