Added method subscribe with code
This commit is contained in:
parent
1ed384ded2
commit
a447dfa471
|
|
@ -33,6 +33,7 @@ class CreateSubscribeSerializer(serializers.ModelSerializer):
|
|||
|
||||
email = serializers.EmailField(required=False, source='send_to')
|
||||
subscription_types = serializers.PrimaryKeyRelatedField(many=True, queryset=models.SubscriptionType.objects.all())
|
||||
country_code = serializers.CharField(required=False, allow_blank=True)
|
||||
|
||||
class Meta:
|
||||
"""Meta class."""
|
||||
|
|
@ -42,7 +43,10 @@ class CreateSubscribeSerializer(serializers.ModelSerializer):
|
|||
'email',
|
||||
'subscription_types',
|
||||
'link_to_unsubscribe',
|
||||
'country_code',
|
||||
'update_code'
|
||||
)
|
||||
read_only_fields = ('link_to_unsubscribe', 'update_code')
|
||||
|
||||
def validate(self, attrs):
|
||||
"""Validate attrs."""
|
||||
|
|
@ -64,7 +68,13 @@ class CreateSubscribeSerializer(serializers.ModelSerializer):
|
|||
|
||||
# append info
|
||||
attrs['email'] = email
|
||||
attrs['country_code'] = request.country_code
|
||||
|
||||
if request.country_code:
|
||||
attrs['country_code'] = request.country_code
|
||||
|
||||
else:
|
||||
attrs['country_code'] = attrs.get('country_code')
|
||||
|
||||
attrs['locale'] = request.locale
|
||||
attrs['ip_address'] = get_user_ip(request)
|
||||
|
||||
|
|
@ -94,6 +104,50 @@ class CreateSubscribeSerializer(serializers.ModelSerializer):
|
|||
return super().update(instance, validated_data)
|
||||
|
||||
|
||||
class UpdateSubscribeSerializer(serializers.ModelSerializer):
|
||||
"""Create Subscribe serializer."""
|
||||
|
||||
subscription_types = serializers.PrimaryKeyRelatedField(many=True, queryset=models.SubscriptionType.objects.all())
|
||||
|
||||
class Meta:
|
||||
"""Meta class."""
|
||||
|
||||
model = models.Subscriber
|
||||
fields = (
|
||||
'subscription_types',
|
||||
'link_to_unsubscribe',
|
||||
'update_code'
|
||||
)
|
||||
read_only_fields = ('link_to_unsubscribe', 'update_code')
|
||||
|
||||
def validate(self, attrs):
|
||||
"""Validate attrs."""
|
||||
request = self.context.get('request')
|
||||
user = request.user
|
||||
|
||||
if request.country_code:
|
||||
attrs['country_code'] = request.country_code
|
||||
|
||||
else:
|
||||
attrs['country_code'] = attrs.get('country_code')
|
||||
|
||||
attrs['locale'] = request.locale
|
||||
attrs['ip_address'] = get_user_ip(request)
|
||||
|
||||
if user.is_authenticated:
|
||||
attrs['user'] = user
|
||||
|
||||
return attrs
|
||||
|
||||
def update(self, instance, validated_data):
|
||||
if settings.USE_CELERY:
|
||||
send_subscribes_update_email.delay(instance.pk)
|
||||
else:
|
||||
send_subscribes_update_email(instance.pk)
|
||||
|
||||
return super().update(instance, validated_data)
|
||||
|
||||
|
||||
class SubscribeObjectSerializer(serializers.ModelSerializer):
|
||||
"""Subscribe serializer."""
|
||||
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ app_name = "notification"
|
|||
|
||||
urlpatterns = [
|
||||
path('subscribe/', common.CreateSubscribeView.as_view(), name='create-subscribe'),
|
||||
path('subscribe/<code>/', common.UpdateSubscribeView.as_view(), name='update-subscribe'),
|
||||
path('subscribe-info/', common.SubscribeInfoAuthUserView.as_view(), name='check-code-auth'),
|
||||
path('subscribe-info/<code>/', common.SubscribeInfoView.as_view(), name='check-code'),
|
||||
path('unsubscribe/', common.UnsubscribeAuthUserView.as_view(), name='unsubscribe-auth'),
|
||||
|
|
|
|||
|
|
@ -15,6 +15,16 @@ class CreateSubscribeView(generics.CreateAPIView):
|
|||
serializer_class = serializers.CreateSubscribeSerializer
|
||||
|
||||
|
||||
class UpdateSubscribeView(generics.UpdateAPIView):
|
||||
"""Subscribe info view."""
|
||||
|
||||
lookup_field = 'update_code'
|
||||
lookup_url_kwarg = 'code'
|
||||
permission_classes = (permissions.AllowAny,)
|
||||
queryset = models.Subscriber.objects.all()
|
||||
serializer_class = serializers.UpdateSubscribeSerializer
|
||||
|
||||
|
||||
class SubscribeInfoView(generics.RetrieveAPIView):
|
||||
"""Subscribe info view."""
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user