Ejemplo n.º 1
0
    def hidden(self):
        """returns all hidden blog posts"""
        if o_settings.CACHE_ENABLED:
            key = get_key('posts_hidden')
            cached = cache.get(key)
            if cached:
                return cached
        posts = super(PostManager, self).get_query_set().filter(
            status=self.model.HIDDEN)

        if o_settings.CACHE_ENABLED:
            cache.set(key, posts, o_settings.CACHE_TIMEOUT)
        return posts
Ejemplo n.º 2
0
    def published(self):
        """returns all published blog post"""
        if o_settings.CACHE_ENABLED:
            key = get_key('posts_published')
            cached = cache.get(key)
            if cached:
                return cached

        posts = super(PostManager, self).get_query_set().filter(
                    status=self.model.PUBLISHED)

        if o_settings.CACHE_ENABLED:
            cache.set(key, posts, o_settings.CACHE_TIMEOUT)
        return posts
Ejemplo n.º 3
0
    def tags_and_counts(self):
        """returns tags with count of how many posts are tagged with it"""
        if o_settings.CACHE_ENABLED:
            key = get_key('tags_and_counts')
            cached = cache.get(key)
            if cached:
                return cached
        tags = []
        tag_models = super(TagManager, self).get_query_set().all()
        for tag in tag_models:
            tags.append((tag,tag.post_set.filter(status=3).count()))
        tags = sorted(tags, key=operator.itemgetter(1), reverse=True)

        if o_settings.CACHE_ENABLED:
            cache.set(key, tags, o_settings.CACHE_TIMEOUT)
        return tags
Ejemplo n.º 4
0
    def _cache_page(request, *args, **kwargs):

        if o_settings.CACHE_ENABLED is False:
            return fn(request, *args, **kwargs)

        if request.user.is_authenticated():
            return fn(request, *args, **kwargs)

        signature = '%s_%s' % (fn.__name__, '_'.join(kwargs.values()))
        key = get_key(signature)
        if 'clearcache' not in request.GET.keys():
            cached = cache.get(key)
            if cached:
                return cached
        response = fn(request, *args, **kwargs)
        cache.set(key, response, o_settings.CACHE_TIMEOUT)
        return response
Ejemplo n.º 5
0
    def dates(self):
        """returns the years and months for which there are posts"""
        if o_settings.CACHE_ENABLED:
            key = get_key('posts_dates')
            cached = cache.get(key)
            if cached:
                return cached

        posts = self.published()
        dates = OrderedDict()
        for post in posts:
            key = post.created.strftime('%Y_%m')
            try:
                dates[key][1] = dates[key][1] + 1
            except KeyError:
                dates[key] = [post.created, 1]

        if o_settings.CACHE_ENABLED:
            cache.set(key, dates, o_settings.CACHE_TIMEOUT)
        return dates
Ejemplo n.º 6
0
def clear_cached_posts(sender, instance, **kwargs):
    """when a model is saved clear the cache on the query
    and the cache on it's rendered page

    - archive_dates
    - posts_published
    - posts_idea
    - posts_draft
    - posts_hidden
    - tags_and_counts
    - view post
    - archive month
    - all tags for post
    """
    try:
        saved_instance = sender.objects.get(pk=instance.pk)
    except sender.DoesNotExist:
        saved_instance = None
    #
    # TODO: only kill the cache if items have changed .
    # e.g if the description, title, slug has changed nuke the
    #       index
    #       archive
    #
    kill_list = []
    # dates
    if getattr(saved_instance, 'created', None) != instance.created:
        kill_list.append(get_key('archive_dates'))
        kill_list.append(
            get_key('month_%s_%s' % (
                instance.created.year,
                instance.created.month)))
    try:
        kill_list.append(
            get_key('month_%s_%s' % (
                saved_instance.created.year,
                saved_instance.created.month)))
    except AttributeError:
        pass

    # Just nuke all of the below for now
    kill_list.append(get_key('posts_published'))
    kill_list.append(get_key('posts_idea'))
    kill_list.append(get_key('posts_draft'))
    kill_list.append(get_key('posts_hidden'))
    kill_list.append(get_key('tags_and_counts'))
    kill_list.append(get_key('index_'))

    # always nuke the post
    kill_list.append(get_key('post_%s' % instance.slug))

    # nuke tags
    try:
        for tag in instance.tags.all():
            kill_list.append(get_key('tag_%s' % tag))
    except ValueError:
        pass
    cache.delete_many(kill_list)