|
1 | 1 | from rest_framework import serializers as ser
|
2 |
| -from rest_framework.exceptions import ValidationError |
| 2 | +from api.base.serializers import JSONAPISerializer, LinksField |
| 3 | +from website.util import api_v2_url |
| 4 | +from .fields import FrequencyField |
3 | 5 | from api.nodes.serializers import RegistrationProviderRelationshipField
|
4 | 6 | from api.collections_providers.fields import CollectionProviderRelationshipField
|
5 | 7 | from api.preprints.serializers import PreprintProviderRelationshipField
|
6 |
| -from website.util import api_v2_url |
7 |
| - |
8 |
| - |
9 |
| -from api.base.serializers import JSONAPISerializer, LinksField |
10 |
| - |
11 |
| -NOTIFICATION_TYPES = { |
12 |
| - 'none': 'none', |
13 |
| - 'instant': 'email_transactional', |
14 |
| - 'daily': 'email_digest', |
15 |
| -} |
16 | 8 |
|
17 | 9 |
|
18 |
| -class FrequencyField(ser.Field): |
19 |
| - def to_representation(self, obj): |
20 |
| - user_id = self.context['request'].user.id |
21 |
| - if obj.email_transactional.filter(id=user_id).exists(): |
22 |
| - return 'instant' |
23 |
| - if obj.email_digest.filter(id=user_id).exists(): |
24 |
| - return 'daily' |
25 |
| - return 'none' |
26 |
| - |
27 |
| - def to_internal_value(self, frequency): |
28 |
| - notification_type = NOTIFICATION_TYPES.get(frequency) |
29 |
| - if notification_type: |
30 |
| - return {'notification_type': notification_type} |
31 |
| - raise ValidationError(f'Invalid frequency "{frequency}"') |
32 |
| - |
33 | 10 | class SubscriptionSerializer(JSONAPISerializer):
|
34 |
| - filterable_fields = frozenset([ |
35 |
| - 'id', |
36 |
| - 'event_name', |
37 |
| - ]) |
38 |
| - |
39 |
| - id = ser.CharField(source='_id', read_only=True) |
40 |
| - event_name = ser.CharField(read_only=True) |
41 |
| - frequency = FrequencyField(source='*', required=True) |
42 |
| - links = LinksField({ |
43 |
| - 'self': 'get_absolute_url', |
44 |
| - }) |
| 11 | + filterable_fields = frozenset(['id', 'type']) |
| 12 | + |
| 13 | + id = ser.CharField(read_only=True) |
| 14 | + type = ser.SerializerMethodField() |
| 15 | + email_freq = FrequencyField(required=True) |
| 16 | + links = LinksField({'self': 'get_absolute_url'}) |
45 | 17 |
|
46 | 18 | class Meta:
|
47 | 19 | type_ = 'subscription'
|
48 | 20 |
|
| 21 | + def get_type(self, obj): |
| 22 | + return self.Meta.type_ |
| 23 | + |
49 | 24 | def get_absolute_url(self, obj):
|
50 |
| - return obj.absolute_api_v2_url |
| 25 | + return api_v2_url(f'subscriptions/{obj.pk}') |
51 | 26 |
|
52 | 27 | def update(self, instance, validated_data):
|
53 |
| - user = self.context['request'].user |
54 |
| - notification_type = validated_data.get('notification_type') |
55 |
| - instance.add_user_to_subscription(user, notification_type, save=True) |
| 28 | + instance.message_frequency = validated_data.get('message_frequency', instance.message_frequency) |
| 29 | + instance.save(update_fields=['message_frequency']) |
56 | 30 | return instance
|
57 | 31 |
|
58 |
| - |
59 | 32 | class RegistrationSubscriptionSerializer(SubscriptionSerializer):
|
60 | 33 | provider = RegistrationProviderRelationshipField(
|
61 | 34 | related_view='providers:registration-providers:registration-provider-detail',
|
62 |
| - related_view_kwargs={'provider_id': '<provider._id>'}, |
63 |
| - read_only=False, |
64 |
| - required=False, |
| 35 | + related_view_kwargs={'provider_id': '<subscribed_object._id>'}, |
| 36 | + read_only=True, # We're not modifying the relationship from here |
65 | 37 | )
|
66 | 38 |
|
67 | 39 | def get_absolute_url(self, obj):
|
68 |
| - return api_v2_url(f'registration_subscriptions/{obj._id}') |
| 40 | + return api_v2_url(f'registration_subscriptions/{obj.pk}') |
69 | 41 |
|
70 | 42 | class Meta:
|
71 | 43 | type_ = 'registration-subscription'
|
72 | 44 |
|
73 |
| - |
74 | 45 | class CollectionSubscriptionSerializer(SubscriptionSerializer):
|
75 | 46 | provider = CollectionProviderRelationshipField(
|
76 | 47 | related_view='providers:collection-providers:collection-provider-detail',
|
77 |
| - related_view_kwargs={'provider_id': '<provider._id>'}, |
78 |
| - read_only=False, |
79 |
| - required=False, |
| 48 | + related_view_kwargs={'provider_id': '<subscribed_object._id>'}, |
| 49 | + read_only=True, |
80 | 50 | )
|
81 | 51 |
|
82 | 52 | def get_absolute_url(self, obj):
|
83 |
| - return api_v2_url(f'collection_subscriptions/{obj._id}') |
| 53 | + return api_v2_url(f'collection_subscriptions/{obj.pk}') |
84 | 54 |
|
85 | 55 | class Meta:
|
86 | 56 | type_ = 'collection-subscription'
|
87 | 57 |
|
88 |
| - |
89 | 58 | class PreprintSubscriptionSerializer(SubscriptionSerializer):
|
90 | 59 | provider = PreprintProviderRelationshipField(
|
91 | 60 | related_view='providers:preprint-providers:preprint-provider-detail',
|
92 |
| - related_view_kwargs={'provider_id': '<provider._id>'}, |
93 |
| - read_only=False, |
| 61 | + related_view_kwargs={'provider_id': '<subscribed_object._id>'}, |
| 62 | + read_only=True, |
94 | 63 | )
|
95 | 64 |
|
96 | 65 | def get_absolute_url(self, obj):
|
97 |
| - return api_v2_url(f'preprints_subscriptions/{obj._id}') |
| 66 | + return api_v2_url(f'preprints_subscriptions/{obj.pk}') |
98 | 67 |
|
99 | 68 | class Meta:
|
100 | 69 | type_ = 'preprint-subscription'
|
0 commit comments