Added correct creating subscribe
This commit is contained in:
parent
eac841f41f
commit
7baf022a77
|
|
@ -8,6 +8,7 @@ from rest_framework import serializers
|
||||||
from rest_framework import validators as rest_validators
|
from rest_framework import validators as rest_validators
|
||||||
|
|
||||||
from account import models, tasks
|
from account import models, tasks
|
||||||
|
from notification.models import Subscriber
|
||||||
from utils import exceptions as utils_exceptions
|
from utils import exceptions as utils_exceptions
|
||||||
from utils import methods as utils_methods
|
from utils import methods as utils_methods
|
||||||
|
|
||||||
|
|
@ -46,6 +47,12 @@ class UserSerializer(serializers.ModelSerializer):
|
||||||
'newsletter',
|
'newsletter',
|
||||||
]
|
]
|
||||||
|
|
||||||
|
def create(self, validated_data):
|
||||||
|
user = super(UserSerializer, self).create(validated_data)
|
||||||
|
validated_data['user'] = user
|
||||||
|
Subscriber.objects.make_subscriber(**validated_data)
|
||||||
|
return user
|
||||||
|
|
||||||
def validate_email(self, value):
|
def validate_email(self, value):
|
||||||
"""Validate email value"""
|
"""Validate email value"""
|
||||||
if value == self.instance.email:
|
if value == self.instance.email:
|
||||||
|
|
|
||||||
|
|
@ -21,7 +21,7 @@ class SubscriberManager(models.Manager):
|
||||||
"""Extended manager for Subscriber model."""
|
"""Extended manager for Subscriber model."""
|
||||||
|
|
||||||
def make_subscriber(self, email=None, user=None, ip_address=None, country_code=None,
|
def make_subscriber(self, email=None, user=None, ip_address=None, country_code=None,
|
||||||
locale=None, subscription_type=None, *args, **kwargs):
|
locale=None, subscription_types=None, *args, **kwargs):
|
||||||
"""Make subscriber and update info."""
|
"""Make subscriber and update info."""
|
||||||
# search existing object
|
# search existing object
|
||||||
if not user:
|
if not user:
|
||||||
|
|
@ -44,12 +44,12 @@ class SubscriberManager(models.Manager):
|
||||||
obj.locale = locale
|
obj.locale = locale
|
||||||
obj.state = self.model.USABLE
|
obj.state = self.model.USABLE
|
||||||
obj.update_code = generate_string_code()
|
obj.update_code = generate_string_code()
|
||||||
obj.subscription_type = subscription_type
|
obj.subscription_types = subscription_types
|
||||||
obj.save()
|
obj.save()
|
||||||
else:
|
else:
|
||||||
obj = self.model.objects.create(user=user, email=email, ip_address=ip_address,
|
obj = self.model.objects.create(user=user, email=email, ip_address=ip_address,
|
||||||
country_code=country_code, locale=locale,
|
country_code=country_code, locale=locale,
|
||||||
subscription_type=subscription_type)
|
subscription_types=subscription_types)
|
||||||
return obj
|
return obj
|
||||||
|
|
||||||
def associate_user(self, user):
|
def associate_user(self, user):
|
||||||
|
|
@ -127,6 +127,13 @@ class Subscriber(ProjectBaseMixin):
|
||||||
|
|
||||||
def unsubscribe(self):
|
def unsubscribe(self):
|
||||||
"""Unsubscribe user."""
|
"""Unsubscribe user."""
|
||||||
|
|
||||||
|
subscribes = Subscribe.objects.filter(subscriber=self)
|
||||||
|
self.subscription_types = []
|
||||||
|
|
||||||
|
subscribes.unsubscribe_date = now()
|
||||||
|
subscribes.save()
|
||||||
|
|
||||||
self.state = self.UNUSABLE
|
self.state = self.UNUSABLE
|
||||||
self.save()
|
self.save()
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
"""Notification app serializers."""
|
"""Notification app serializers."""
|
||||||
from django.utils.translation import ugettext_lazy as _
|
from django.utils.translation import ugettext_lazy as _
|
||||||
from rest_framework import serializers
|
from rest_framework import serializers
|
||||||
|
|
||||||
from notification import models
|
from notification import models
|
||||||
from utils.methods import get_user_ip
|
from utils.methods import get_user_ip
|
||||||
from utils.serializers import TranslatedField
|
from utils.serializers import TranslatedField
|
||||||
|
|
@ -21,11 +22,11 @@ class SubscriptionTypeSerializer(serializers.ModelSerializer):
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
class SubscribeSerializer(serializers.ModelSerializer):
|
class CreateSubscribeSerializer(serializers.ModelSerializer):
|
||||||
"""Subscribe serializer."""
|
"""Create Subscribe serializer."""
|
||||||
|
|
||||||
email = serializers.EmailField(required=False, source='send_to')
|
email = serializers.EmailField(required=False, source='send_to')
|
||||||
subscription_type = SubscriptionTypeSerializer(read_only=True)
|
subscription_types = SubscriptionTypeSerializer(read_only=True)
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
"""Meta class."""
|
"""Meta class."""
|
||||||
|
|
@ -33,10 +34,9 @@ class SubscribeSerializer(serializers.ModelSerializer):
|
||||||
model = models.Subscriber
|
model = models.Subscriber
|
||||||
fields = (
|
fields = (
|
||||||
'email',
|
'email',
|
||||||
'subscription_type',
|
'subscription_types',
|
||||||
'state',
|
'state',
|
||||||
'subscribe_date',
|
'link_to_unsubscribe',
|
||||||
'unsubscribe_date'
|
|
||||||
)
|
)
|
||||||
read_only_fields = ('state',)
|
read_only_fields = ('state',)
|
||||||
|
|
||||||
|
|
@ -47,6 +47,10 @@ class SubscribeSerializer(serializers.ModelSerializer):
|
||||||
|
|
||||||
# validate email
|
# validate email
|
||||||
email = attrs.get('send_to')
|
email = attrs.get('send_to')
|
||||||
|
|
||||||
|
if attrs.get('email'):
|
||||||
|
email = attrs.get('email')
|
||||||
|
|
||||||
if user.is_authenticated:
|
if user.is_authenticated:
|
||||||
if email is not None and email != user.email:
|
if email is not None and email != user.email:
|
||||||
raise serializers.ValidationError(_('Does not match user email'))
|
raise serializers.ValidationError(_('Does not match user email'))
|
||||||
|
|
@ -59,18 +63,39 @@ class SubscribeSerializer(serializers.ModelSerializer):
|
||||||
attrs['country_code'] = request.country_code
|
attrs['country_code'] = request.country_code
|
||||||
attrs['locale'] = request.locale
|
attrs['locale'] = request.locale
|
||||||
attrs['ip_address'] = get_user_ip(request)
|
attrs['ip_address'] = get_user_ip(request)
|
||||||
|
|
||||||
if user.is_authenticated:
|
if user.is_authenticated:
|
||||||
attrs['user'] = user
|
attrs['user'] = user
|
||||||
|
|
||||||
subscription_type_id = self.context.get('request').parser_context.get('kwargs').get("subscription_type_pk")
|
subscription_type_ids = self.context.get('request')\
|
||||||
subscription_type_qs = models.SubscriptionType.objects.filter(id=subscription_type_id)
|
.parser_context.get('kwargs')\
|
||||||
if not subscription_type_qs.exists():
|
.get("subscription_types_pk")
|
||||||
raise serializers.ValidationError({'detail': _(f'SubscriptionType not found.')})
|
|
||||||
attrs["subscription_type"] = subscription_type_qs.first()
|
attrs['subscription_types'] = subscription_type_ids
|
||||||
|
|
||||||
return attrs
|
return attrs
|
||||||
|
|
||||||
def create(self, validated_data):
|
def create(self, validated_data):
|
||||||
"""Create obj."""
|
"""Create obj."""
|
||||||
subscriber = models.Subscriber.objects.make_subscriber(**validated_data)
|
return models.Subscriber.objects.make_subscriber(**validated_data)
|
||||||
return subscriber
|
|
||||||
|
|
||||||
|
class SubscribeSerializer(serializers.ModelSerializer):
|
||||||
|
"""Subscribe serializer."""
|
||||||
|
|
||||||
|
email = serializers.EmailField(required=False, source='send_to')
|
||||||
|
subscription_types = SubscriptionTypeSerializer(read_only=True)
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
"""Meta class."""
|
||||||
|
|
||||||
|
model = models.Subscriber
|
||||||
|
fields = (
|
||||||
|
'email',
|
||||||
|
'subscription_types',
|
||||||
|
'state',
|
||||||
|
'subscribe_date',
|
||||||
|
'unsubscribe_date'
|
||||||
|
'link_to_unsubscribe',
|
||||||
|
)
|
||||||
|
read_only_fields = ('state',)
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@ from notification.views import common
|
||||||
app_name = "notification"
|
app_name = "notification"
|
||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
|
path('subscribe/', common.CreateSubscribeView.as_view(), name='create-subscribe'),
|
||||||
path('subscribe/<int:subscription_type_pk>', common.SubscribeView.as_view(), name='subscribe'),
|
path('subscribe/<int:subscription_type_pk>', common.SubscribeView.as_view(), name='subscribe'),
|
||||||
path('subscribe-info/', common.SubscribeInfoAuthUserView.as_view(), name='check-code-auth'),
|
path('subscribe-info/', common.SubscribeInfoAuthUserView.as_view(), name='check-code-auth'),
|
||||||
path('subscribe-info/<code>/', common.SubscribeInfoView.as_view(), name='check-code'),
|
path('subscribe-info/<code>/', common.SubscribeInfoView.as_view(), name='check-code'),
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@
|
||||||
from django.shortcuts import get_object_or_404
|
from django.shortcuts import get_object_or_404
|
||||||
from rest_framework import generics, permissions
|
from rest_framework import generics, permissions
|
||||||
from rest_framework.response import Response
|
from rest_framework.response import Response
|
||||||
|
|
||||||
from notification import models
|
from notification import models
|
||||||
from notification.serializers import common as serializers
|
from notification.serializers import common as serializers
|
||||||
|
|
||||||
|
|
@ -20,6 +21,20 @@ class SubscribeView(generics.GenericAPIView):
|
||||||
return Response(data=serializer.data)
|
return Response(data=serializer.data)
|
||||||
|
|
||||||
|
|
||||||
|
class CreateSubscribeView(generics.GenericAPIView):
|
||||||
|
"""Create subscribe View."""
|
||||||
|
|
||||||
|
queryset = models.Subscriber.objects.all()
|
||||||
|
permission_classes = (permissions.AllowAny,)
|
||||||
|
serializer_class = serializers.CreateSubscribeSerializer
|
||||||
|
|
||||||
|
def post(self, request, *args, **kwargs):
|
||||||
|
serializer = self.get_serializer(data=request.data)
|
||||||
|
serializer.is_valid(raise_exception=True)
|
||||||
|
serializer.save()
|
||||||
|
return Response(data=serializer.data)
|
||||||
|
|
||||||
|
|
||||||
class SubscribeInfoView(generics.RetrieveAPIView):
|
class SubscribeInfoView(generics.RetrieveAPIView):
|
||||||
"""Subscribe info view."""
|
"""Subscribe info view."""
|
||||||
|
|
||||||
|
|
@ -78,4 +93,3 @@ class SubscriptionTypesView(generics.ListAPIView):
|
||||||
permission_classes = (permissions.AllowAny,)
|
permission_classes = (permissions.AllowAny,)
|
||||||
queryset = models.SubscriptionType.objects.all()
|
queryset = models.SubscriptionType.objects.all()
|
||||||
serializer_class = serializers.SubscriptionTypeSerializer
|
serializer_class = serializers.SubscriptionTypeSerializer
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user