Last active
April 29, 2020 09:28
-
-
Save adamledwards/922eb72bb65c7bbbf4046946cff0719f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ContactSerializer(PermittedFieldsModelSerializer): | |
""" | |
Used by | |
POST /v3/contact | |
GET /v3/contact | |
PATCH /v3/contact | |
and CompanySerializer | |
remove accepts_dit_email_marketing from the listing serializer | |
""" | |
def to_representation(self, instance): | |
""" | |
Convert instance to dict. | |
Optionally lookup from consent service if feature flag | |
is enabled. | |
""" | |
representation = super().to_representation(instance) | |
if is_feature_flag_active(GET_CONSENT_FROM_CONSENT_SERVICE) and 'accepts_dit_email_marketing' in representation: | |
representation['accepts_dit_email_marketing'] = consent.get_one(representation['email']) | |
return representation | |
class ContactListSerializer(ContactSerializer): | |
""" | |
Used by | |
GET /v3/contact/ | |
""" | |
class Meta(ContactSerializer.Meta) | |
exclude = ['accepts_dit_email_marketing'] | |
class ContactViewSet: | |
#.... | |
serializer_class = ContactSerializer | |
def get_serializer_class(self): | |
if self.action == 'retrieve' and is_feature_flag_active(GET_CONSENT_FROM_CONSENT_SERVICE): | |
return ContactListSerializer | |
return super(ContactViewSet, self).get_serializer_class() | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import ContactDetailViewSet | |
contact_item = ContactViewSet.as_view({ | |
'get': 'retrieve', | |
'patch': 'partial_update', | |
}) | |
urls = [ | |
path('contact/<uuid:pk>', contact_item, name='detail'), | |
#.... | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment