def get(self, id = None, slug = None, cache_key = None): if cache_key: return cache.get(cache_key) elif slug: slug_cache_key = get_model_slug_cache_key(self.model, slug, settings.SITE_ID) id_cache_key = cache.get(slug_cache_key) return cache.get(id_cache_key) # lookup and return elif id: # lookup and return cache_key = get_model_cache_key(self.model, id, settings.SITE_ID) return cache.get(cache_key) else: return None
def content_display(request, slug, year, month, day, content_type, id): """ Main view function. @param request: Django HttpRequest object. @type request: L{HttpRequest} @param slug: URL safe version of content's title. @type slug: string @param year: 4-digit year content was published. @type year: int @param month: 3-letter abbreviation of month content was published. @type month: string @param day: 2-digit day content was published. @type day: int @param id: Database PK of content. @type id: int @rtype: L{HttpResponse} @return: HttpResponse encompassing the content's detail page or its generic views. """ # Validate the url contains a valid date try: valid_date = date(*time.strptime(year+month+day, '%Y%b%d')[:3]) # if no valid date, return a 404 error except ValueError: raise Http404('Invalid date.') # Determine the content type. This will help us look up the cache key for the object. ctype_cache_key = "%s::contenttype::%s::%s" % (settings.GROUP_NAME, str(content_type).lower(), settings.GROUP_CACHE_VERSION) content_type_name = cache.get(ctype_cache_key) if not content_type_name: content_type_name = get_object_or_404(GFContentType, short_name__iexact=content_type).model_name cache.set(ctype_cache_key, content_type_name, settings.CACHE_LONG_SECONDS) try: content_type = get_model('content', content_type_name.lower()) except Exception: return HttpResponseServerError # Object Cache Key cache_key = get_model_cache_key(content_type, id, settings.SITE_ID) # If the URL has ?clear_cache on it, and the user is staff, clear the cache despite any other timers/settings. # Otherwise, check the settings/timers normally. if request.GET.has_key('clear_cache') and request.user.is_staff: clear_cache = True else: clear_cache = False content_obj = None if not clear_cache: # does the settings.CACHE_HTML global variable not equal None. If so, check HTML cache and return. if settings.CACHE_HTML: # If settings for caching entrie HTML page is set then build the html page cache key html_cache_key = "%s.html" % cache_key # after building the html page cache key, check the cache to see if page exists html = cache.get(html_cache_key) # if page exists, return the cached page if html: return html # if page does not exits, fall through and build html that will be cached and rendered # Try cache retrieval for object content_obj = content_type.cache.get(id=id) # If cache get failed or cache was ordered to clear, find the content object from the DB. if not content_obj: # if the user is not staff, only select from published, public articles if not request.user.is_staff: # get the obj content_obj = get_object_or_404(content_type.published, id=id, sites__id__exact=settings.SITE_ID) # Don't cache staff requests, or else they'll be visible to all once cached. model_key = get_model_cache_key(content_type, content_obj.id, settings.SITE_ID) slug_key = get_model_slug_cache_key(content_type, content_obj.slug, settings.SITE_ID) cache.set(model_key, content_obj, settings.CACHE_LONG_SECONDS) cache.set(slug_key, model_key, settings.CACHE_LONG_SECONDS) # if the user is staff, they are allowed to see draft/non-published articles else: content_obj = get_object_or_404(content_type.objects, id=id, sites__id__exact=settings.SITE_ID) # Axe any cache for the object to be sure it is all clear. if clear_cache: model_key = get_model_cache_key(content_type, content_obj.id, settings.SITE_ID) slug_key = get_model_slug_cache_key(content_type, content_obj.slug, settings.SITE_ID) cache.delete(model_key) cache.delete(slug_key) # Get the absolute URL. try: absolute_url = content_obj.get_absolute_url() except Exception, e: raise Http404(str(e))