def to_representation(self, obj): data = super().to_representation(obj) request = self.context.get('request', None) # when 'wrap-outgoing-parameter' is on url is a flat string already is_flat_url = request and is_gate_active(request, 'wrap-outgoing-parameter') url = data.get('url') if not url: if obj.endpoint == Shelf.Endpoints.SEARCH: query_string = '&'.join( f'{key}={value}' for key, value in obj.get_param_dict().items()) fallback = absolutify( f'{reverse("search.search")}?{query_string}') elif obj.endpoint == Shelf.Endpoints.RANDOM_TAG: tag_page_url = reverse('tags.detail', kwargs={'tag_name': obj.tag}) query_string = '&'.join( f'{key}={value}' for key, value in obj.get_param_dict().items() if key != 'tag') fallback = absolutify(f'{tag_page_url}?{query_string}') elif obj.endpoint == Shelf.Endpoints.COLLECTIONS: fallback = absolutify( reverse( 'collections.detail', kwargs={ 'user_id': str(settings.TASK_USER_ID), 'slug': obj.criteria, }, )) else: # shouldn't happen fallback = None url = ({ 'url': fallback, 'outgoing': fallback } if not is_flat_url else fallback) # text = data.get('text') if is_flat_url: return { **data, 'url': url, } else: return { **data, 'url': (url or {}).get('url'), 'outgoing': (url or {}).get('outgoing'), }
def test_basic(self): file_upload = FileUpload(version='123', automated_signing=True) data = FileUploadSerializer( instance=file_upload, context={'request': self.request} ).data assert data == { 'uuid': file_upload.uuid.hex, 'channel': 'unlisted', 'processed': False, 'submitted': False, 'url': settings.EXTERNAL_SITE_URL + reverse( 'v5:addon-upload-detail', args=[file_upload.uuid.hex], ), 'valid': False, 'validation': None, 'version': '123', } file_upload.update( automated_signing=False, validation=json.dumps([{'something': 'happened'}]), ) data = FileUploadSerializer( instance=file_upload, context={'request': self.request} ).data assert data['channel'] == 'listed' assert data['validation'] == [{'something': 'happened'}]
def test_footer_url(self): wrap_outgoing_settings_override = { 'DRF_API_GATES': { api_settings.DEFAULT_VERSION: ('wrap-outgoing-parameter',) } } # search self.search_rec_ext.update(footer_pathname='') data = self.serialize(self.search_rec_ext) url = ( 'http://testserver' + reverse('search.search') + '?promoted=recommended&sort=random&type=extension' ) assert data['footer'] == { 'url': url, 'outgoing': url, 'text': {'en-US': 'See more recommended extensions'}, } with override_settings(**wrap_outgoing_settings_override): data = self.serialize(self.search_rec_ext) assert data['footer'] == { 'url': url, 'text': {'en-US': 'See more recommended extensions'}, } # collections self.collections_shelf.update(footer_pathname='') data = self.serialize(self.collections_shelf) url = 'http://testserver' + reverse( 'collections.detail', kwargs={ 'user_id': str(settings.TASK_USER_ID), 'slug': 'privacy-matters', }, ) assert data['footer'] == { 'url': url, 'outgoing': url, 'text': {'en-US': 'See more enhanced privacy extensions'}, } with override_settings(**wrap_outgoing_settings_override): data = self.serialize(self.collections_shelf) assert data['footer'] == { 'url': url, 'text': {'en-US': 'See more enhanced privacy extensions'}, } # random-tag self.tag_shelf.update(footer_pathname='') data = self.serialize(self.tag_shelf) url = 'http://testserver' + reverse('search.search') + '?tag=foo' assert data['footer'] == { 'url': url, 'outgoing': url, 'text': {'en-US': 'See similar add-ons?'}, } with override_settings(**wrap_outgoing_settings_override): data = self.serialize(self.tag_shelf) assert data['footer'] == { 'url': url, 'text': {'en-US': 'See similar add-ons?'}, }