コード例 #1
0
 def test_absolutifys(self):
     field = AbsoluteOutgoingURLField()
     assert field.to_representation('/foo/baa/') == {
         'url': f'{settings.EXTERNAL_SITE_URL}/foo/baa/',
         'outgoing': f'{settings.EXTERNAL_SITE_URL}/foo/baa/',
     }
     assert field.to_representation('http://foo/baa/') == {
         'url': 'http://foo/baa/',
         'outgoing': get_outgoing_url('http://foo/baa/'),
     }
コード例 #2
0
    def test_allow_internal(self):
        user_input = f'{settings.EXTERNAL_SITE_URL}/foo/baa/'

        # default is allow_internal=True so shouldn't raise
        AbsoluteOutgoingURLField().run_validation(user_input)

        with self.assertRaises(exceptions.ValidationError):
            AbsoluteOutgoingURLField(
                allow_internal=False).run_validation(user_input)

        AbsoluteOutgoingURLField(
            allow_internal=True).run_validation(user_input)
コード例 #3
0
class ShelfFooterField(serializers.Serializer):
    url = AbsoluteOutgoingURLField(source='footer_pathname')
    text = GetTextTranslationSerializerField(source='footer_text')

    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'),
            }
コード例 #4
0
class CTAField(serializers.Serializer):
    url = AbsoluteOutgoingURLField(source='cta_url')
    text = GetTextTranslationSerializerFieldFlat(source='cta_text')

    def to_representation(self, obj):
        data = super().to_representation(obj)
        if data.get('url') or data.get('text'):
            request = self.context.get('request', None)

            if request and is_gate_active(request, 'wrap-outgoing-parameter'):
                # when 'wrap-outgoing-parameter' is on url is a flat string already
                return data
            else:
                url = data.get('url') or {}
                return {
                    **data,
                    'url': url.get('url'),
                    'outgoing': url.get('outgoing'),
                }
        else:
            return None
コード例 #5
0
class ShelfFooterField(CTAField):
    url = AbsoluteOutgoingURLField(source='footer_pathname')
    text = GetTextTranslationSerializerField(source='footer_text')