snipt/snipt/snipts/views.py

60 lines
1.6 KiB
Python
Raw Normal View History

2011-10-12 13:34:01 -07:00
from django.shortcuts import get_object_or_404
from django.http import HttpResponseRedirect
2011-10-12 13:34:01 -07:00
from django.contrib.auth.models import User
2011-10-12 08:29:21 -07:00
from annoying.decorators import render_to
from django.db.models import Count
from snipts.models import Snipt
from taggit.models import Tag
2011-10-10 20:30:56 -07:00
def home(request):
2011-10-12 08:29:21 -07:00
if request.user.is_authenticated():
return HttpResponseRedirect('/%s/' % request.user.username)
2011-10-12 13:34:01 -07:00
else:
return list_public(request)
@render_to('snipts/list-public.html')
def list_public(request, tag=None):
2011-10-12 08:29:21 -07:00
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')
if tag:
snipts = snipts.filter(tags__name__in=[tag])
2011-10-12 08:29:21 -07:00
return {
'snipts': snipts,
'tags': tags,
'tag': tag,
2011-10-12 08:29:21 -07:00
}
2011-10-12 13:34:01 -07:00
@render_to('snipts/list-user.html')
def list_user(request, user, tag=None):
2011-10-12 08:29:21 -07:00
user = get_object_or_404(User, username=user)
2011-10-12 13:34:01 -07:00
tags = Tag.objects
snipts = Snipt.objects
if user == request.user:
tags = tags.filter(snipt__user=user)
snipts = snipts.filter(user=user)
else:
tags = tags.filter(snipt__user=user, snipt__public=True)
snipts = snipts.filter(user=user, public=True)
tags = tags.annotate(count=Count('taggit_taggeditem_items__id'))
tags = tags.order_by('-count')
snipts = snipts.order_by('-created')
2011-10-10 21:13:18 -07:00
if tag:
snipts = snipts.filter(tags__name__in=[tag])
2011-10-10 20:30:56 -07:00
return {
2011-10-10 21:13:18 -07:00
'snipts': snipts,
'tags': tags,
'tag': tag,
2011-10-12 13:34:01 -07:00
'user': user,
2011-10-10 20:30:56 -07:00
}