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