Reformat structure
This commit is contained in:
parent
449e0675fa
commit
fe330e4270
|
|
@ -77,14 +77,6 @@ class SubscriberQuerySet(models.QuerySet):
|
|||
class Subscriber(ProjectBaseMixin):
|
||||
"""Subscriber model."""
|
||||
|
||||
UNUSABLE = 0
|
||||
USABLE = 1
|
||||
|
||||
STATE_CHOICES = (
|
||||
(UNUSABLE, _('Unusable')),
|
||||
(USABLE, _('Usable')),
|
||||
)
|
||||
|
||||
user = models.ForeignKey(
|
||||
User,
|
||||
blank=True,
|
||||
|
|
@ -98,7 +90,6 @@ class Subscriber(ProjectBaseMixin):
|
|||
ip_address = models.GenericIPAddressField(blank=True, null=True, default=None, verbose_name=_('IP address'))
|
||||
country_code = models.CharField(max_length=10, blank=True, null=True, default=None, verbose_name=_('Country code'))
|
||||
locale = models.CharField(blank=True, null=True, default=None, max_length=10, verbose_name=_('Locale identifier'))
|
||||
state = models.PositiveIntegerField(choices=STATE_CHOICES, default=USABLE, verbose_name=_('State'))
|
||||
update_code = models.CharField(
|
||||
max_length=254,
|
||||
blank=True,
|
||||
|
|
@ -128,13 +119,12 @@ class Subscriber(ProjectBaseMixin):
|
|||
def unsubscribe(self):
|
||||
"""Unsubscribe user."""
|
||||
|
||||
subscribes = Subscribe.objects.filter(subscriber=self)
|
||||
subscribes = self.subscribe_objects
|
||||
self.subscription_types = []
|
||||
|
||||
subscribes.unsubscribe_date = now()
|
||||
subscribes.save()
|
||||
|
||||
self.state = self.UNUSABLE
|
||||
self.save()
|
||||
|
||||
@property
|
||||
|
|
@ -151,12 +141,25 @@ class Subscriber(ProjectBaseMixin):
|
|||
query = f'?code={self.update_code}'
|
||||
return f'{schema}://{site_domain}{url}{query}'
|
||||
|
||||
@property
|
||||
def subscribe_objects(self):
|
||||
return Subscribe.objects.filter(subscriber=self)
|
||||
|
||||
|
||||
class Subscribe(ProjectBaseMixin):
|
||||
"""Subscribe model."""
|
||||
|
||||
UNUSABLE = 0
|
||||
USABLE = 1
|
||||
|
||||
STATE_CHOICES = (
|
||||
(UNUSABLE, _('Unusable')),
|
||||
(USABLE, _('Usable')),
|
||||
)
|
||||
|
||||
subscribe_date = models.DateTimeField(_('Last subscribe date'), blank=True, null=True, default=now)
|
||||
unsubscribe_date = models.DateTimeField(_('Last unsubscribe date'), blank=True, null=True, default=None)
|
||||
state = models.PositiveIntegerField(choices=STATE_CHOICES, default=USABLE, verbose_name=_('State'))
|
||||
|
||||
subscriber = models.ForeignKey(Subscriber, on_delete=models.CASCADE)
|
||||
subscription_type = models.ForeignKey(SubscriptionType, on_delete=models.CASCADE)
|
||||
|
|
|
|||
|
|
@ -35,10 +35,8 @@ class CreateSubscribeSerializer(serializers.ModelSerializer):
|
|||
fields = (
|
||||
'email',
|
||||
'subscription_types',
|
||||
'state',
|
||||
'link_to_unsubscribe',
|
||||
)
|
||||
read_only_fields = ('state',)
|
||||
|
||||
def validate(self, attrs):
|
||||
"""Validate attrs."""
|
||||
|
|
@ -67,8 +65,8 @@ class CreateSubscribeSerializer(serializers.ModelSerializer):
|
|||
if user.is_authenticated:
|
||||
attrs['user'] = user
|
||||
|
||||
subscription_type_ids = self.context.get('request')\
|
||||
.parser_context.get('kwargs')\
|
||||
subscription_type_ids = self.context.get('request') \
|
||||
.parser_context.get('kwargs') \
|
||||
.get("subscription_types_pk")
|
||||
|
||||
attrs['subscription_types'] = subscription_type_ids
|
||||
|
|
@ -80,11 +78,23 @@ class CreateSubscribeSerializer(serializers.ModelSerializer):
|
|||
return models.Subscriber.objects.make_subscriber(**validated_data)
|
||||
|
||||
|
||||
class SubscribeObjectSerializer(serializers.ModelSerializer):
|
||||
"""Subscribe serializer."""
|
||||
|
||||
class Meta:
|
||||
"""Meta class."""
|
||||
|
||||
model = models.Subscriber
|
||||
fields = ()
|
||||
read_only_fields = ('subscribe_date', 'unsubscribe_date', 'state',)
|
||||
|
||||
|
||||
class SubscribeSerializer(serializers.ModelSerializer):
|
||||
"""Subscribe serializer."""
|
||||
|
||||
email = serializers.EmailField(required=False, source='send_to')
|
||||
subscription_types = SubscriptionTypeSerializer(read_only=True)
|
||||
subscribe_objects = SubscribeObjectSerializer(read_only=True)
|
||||
|
||||
class Meta:
|
||||
"""Meta class."""
|
||||
|
|
@ -93,9 +103,6 @@ class SubscribeSerializer(serializers.ModelSerializer):
|
|||
fields = (
|
||||
'email',
|
||||
'subscription_types',
|
||||
'state',
|
||||
'subscribe_date',
|
||||
'unsubscribe_date'
|
||||
'link_to_unsubscribe',
|
||||
'subscribe_objects',
|
||||
)
|
||||
read_only_fields = ('state',)
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user