Skip to content

Instantly share code, notes, and snippets.

@adamledwards
Last active April 29, 2020 09:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adamledwards/922eb72bb65c7bbbf4046946cff0719f to your computer and use it in GitHub Desktop.
Save adamledwards/922eb72bb65c7bbbf4046946cff0719f to your computer and use it in GitHub Desktop.
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()
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