From 4d1e2725776e860028ce2cd269c716361f77c60a Mon Sep 17 00:00:00 2001 From: Nick Sergeant Date: Wed, 12 Oct 2011 11:29:21 -0400 Subject: [PATCH] Working on dash --- requirements.txt | 1 + settings.py | 2 ++ snipts/views.py | 36 +++++++++++++++++++++++++----------- templates/base.html | 2 +- templates/home.html | 6 +++++- 5 files changed, 34 insertions(+), 13 deletions(-) diff --git a/requirements.txt b/requirements.txt index 49d1956..232eb5d 100644 --- a/requirements.txt +++ b/requirements.txt @@ -7,6 +7,7 @@ -e hg+http://bitbucket.org/dwaiter/django-bcrypt#egg=django-bcrypt -e hg+ssh://hg@bitbucket.org/nicksergeant/django-registration#egg=django-registration -e svn+http://django-grappelli.googlecode.com/svn/trunk#egg=django-grappelli +-e svn+http://django-pagination.googlecode.com/svn/trunk#egg=django-pagination -e hg+https://bitbucket.org/birkenfeld/pygments-main#egg=Pygments # Deployment diff --git a/settings.py b/settings.py index bc65b6c..5e91a70 100644 --- a/settings.py +++ b/settings.py @@ -43,6 +43,7 @@ MIDDLEWARE_CLASSES = ( 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.middleware.locale.LocaleMiddleware', 'django.contrib.redirects.middleware.RedirectFallbackMiddleware', + 'pagination.middleware.PaginationMiddleware', ) ROOT_URLCONF = 'urls' @@ -62,6 +63,7 @@ INSTALLED_APPS = ( 'compressor', 'django_bcrypt', 'south', + 'pagination', 'taggit', 'tastypie', diff --git a/snipts/views.py b/snipts/views.py index d2c0898..e3aa891 100644 --- a/snipts/views.py +++ b/snipts/views.py @@ -1,20 +1,34 @@ -from annoying.decorators import render_to from snipts.api import PublicSniptResource, PublicTagResource +from annoying.decorators import render_to +from django.db.models import Count +from snipts.models import Snipt +from taggit.models import Tag @render_to('home.html') def home(request): - tr = PublicTagResource() - #tags_queryset = tr.cached_obj_get_list()[:20] - tags_queryset = tr.obj_get_list()[:20] - tags_bundles = (tr.build_bundle(request=request, obj=tag) for tag in tags_queryset) - tags = [tr.full_dehydrate(bundle) for bundle in tags_bundles] + if request.user.is_authenticated(): + return home_user(request) - sr = PublicSniptResource() - #snipts_queryset = sr.cached_obj_get_list()[:20] - snipts_queryset = sr.obj_get_list()[:20] - snipts_bundles = (sr.build_bundle(request=request, obj=snipt) for snipt in snipts_queryset) - snipts = [sr.full_dehydrate(bundle) for bundle in snipts_bundles] + tags = Tag.objects.filter(snipt__public=True) + tags = tags.annotate(count=Count('taggit_taggeditem_items__id')) + tags = tags.order_by('-count')[:20] + + snipts = Snipt.objects.filter(public=True).order_by('-created') + + return { + 'snipts': snipts, + 'tags': tags, + } + +@render_to('home.html') +def home_user(request): + + tags = Tag.objects.filter(snipt__user=request.user) + tags = tags.annotate(count=Count('taggit_taggeditem_items__id')) + tags = tags.order_by('-count')[:20] + + snipts = Snipt.objects.filter(public=True).order_by('-created') return { 'snipts': snipts, diff --git a/templates/base.html b/templates/base.html index a61b620..f27225c 100644 --- a/templates/base.html +++ b/templates/base.html @@ -93,7 +93,7 @@ diff --git a/templates/home.html b/templates/home.html index 3537a5c..d7ffcac 100644 --- a/templates/home.html +++ b/templates/home.html @@ -1,11 +1,15 @@ {% extends "base.html" %} +{% load pagination_tags %} + {% block content %}
+ {% autopaginate snipts 20 %} {% for snipt in snipts %}
- {{ snipt.data.title }} + {{ snipt.title }}
{% endfor %} + {% paginate %}
{% endblock %}