コード例 #1
0
 def dispatch(self, *args, **kwargs):
     if ('service_name' not in self.request.GET
             or helpers.is_draft_requested(self.request)):
         return super().dispatch(*args, **kwargs)
     cached_page = cache.PageCache.get(slug=self.kwargs['slug'],
                                       params={
                                           'service_name':
                                           self.request.GET['service_name'],
                                           'lang':
                                           translation.get_language(),
                                           'region':
                                           self.request.GET.get('region'),
                                       })
     if cached_page:
         cached_response = helpers.CachedResponse(cached_page)
         cached_response['etag'] = cached_page.get('etag', None)
         response = get_conditional_response(
             request=self.request,
             etag=cached_response['etag'],
             response=cached_response,
         )
     else:
         response = super().dispatch(*args, **kwargs)
         if response.status_code == 200:
             # No etag is set in this response. this is because creating an
             # etag is expensive. It will be present on the next retrieved
             # from the cache though.
             cache.CachePopulator.populate_async(self.get_object())
     return response
コード例 #2
0
    def detail_view(self, request, **kwargs):
        # Exit early if there are any issues
        self.check_parameter_validity()

        if helpers.is_draft_requested(request):
            return super().detail_view(request, pk=None)

        # Return a cached response if one is available
        cached_data = cache.PageCache.get(
            page_id=self.get_object_id(),
            lang=translation.get_language(),
            region=request.GET.get('region'),
        )
        if cached_data:
            cached_response = helpers.CachedResponse(cached_data)
            cached_response['etag'] = cached_data.get('etag', None)
            return get_conditional_response(
                request=request,
                etag=cached_response['etag'],
                response=cached_response,
            )

        # No cached response available
        response = super().detail_view(request, pk=None)
        if response.status_code == 200:
            # Reuse the already-fetched object to populate the cache
            cache.CachePopulator.populate_async(self.get_object())

        # No etag is set for this response because creating one is expensive.
        # If API caching is enabled, one will be added to the cached version
        # created above.
        return response
コード例 #3
0
ファイル: fields.py プロジェクト: tomdyson/directory-cms
 def get_attribute(self, instance):
     if 'request' in self.context:
         is_draft = helpers.is_draft_requested(self.context['request'])
     else:
         is_draft = False
     return {
         'languages': [(code, label)
                       for (code, label) in settings.LANGUAGES_LOCALIZED
                       if code in instance.translated_languages],
         'url':
         instance.get_url(
             is_draft=is_draft,
             language_code=settings.LANGUAGE_CODE,
         ),
         'slug':
         instance.slug,
         'localised_urls':
         instance.get_localized_urls(),
         'pk':
         instance.pk,
         'draft_token': (instance.get_draft_token()
                         if instance.has_unpublished_changes else None)
     }
コード例 #4
0
    def detail_view(self, request, **kwargs):
        # Exit early if there are any issues
        self.check_parameter_validity()

        draft_version = helpers.is_draft_requested(request)

        langs = []
        if translation.get_language():
            langs.append(translation.get_language())
        langs.append('en-gb')

        # Return a cached response if one is available
        for lang in langs:
            cached_data = cache.PageCache.get(page_id=self.object_id, lang=lang, draft_version=draft_version)
            if cached_data:
                cached_response = helpers.CachedResponse(cached_data)
                cached_response['etag'] = cached_data.get('etag', None)
                return get_conditional_response(
                    request=request,
                    etag=cached_response['etag'],
                    response=cached_response,
                )

        logger.warn('Page cache miss')

        page = self.get_object()

        if type(page) not in MODELS_SERIALIZERS_MAPPING:
            # page that exists has been requested, but it's not serializable. E.g, it's a folder page
            return Response(status=204)
        else:
            cache.CachePopulator.populate_async(self.get_object())

        # super().detail_view can take several seconds due to unoptimized database reads - so lots of
        # resources get used but ultimately will timeout
        return Response(status=501)
コード例 #5
0
 def handle_serve_draft_object(self, instance):
     if helpers.is_draft_requested(self.request):
         instance = instance.get_latest_nested_revision_as_page()
     elif not instance.live:
         raise Http404()
     return instance
コード例 #6
0
 def permission_classes(self):
     permission_classes = [SignatureCheckPermission]
     if helpers.is_draft_requested(self.request):
         permission_classes.append(permissions.DraftTokenPermisison)
     return permission_classes
コード例 #7
0
 def get_serializer_context(self):
     context = super().get_serializer_context()
     context['is_draft'] = helpers.is_draft_requested(self.request)
     return context