snipt/snipts/views.py

115 lines
3.3 KiB
Python
Raw Normal View History

2012-02-26 17:46:28 -08:00
from django.shortcuts import get_object_or_404, render_to_response
2012-02-26 20:52:02 -08:00
from django.http import Http404, HttpResponseRedirect
2011-10-12 13:34:01 -07:00
from django.contrib.auth.models import User
2012-02-26 17:46:28 -08:00
from django.template import RequestContext
2011-10-12 08:29:21 -07:00
from annoying.decorators import render_to
2012-02-13 19:36:59 -08:00
from snipts.models import Favorite, Snipt
2011-10-12 08:29:21 -07:00
from django.db.models import Count
2012-02-13 19:36:59 -08:00
from django.db.models import Q
2011-10-12 08:29:21 -07:00
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')
2012-01-16 15:47:09 -08:00
def list_public(request, tag_slug=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'))
2012-01-16 15:47:09 -08:00
tags = tags.order_by('-count', 'name')[:20]
2011-10-12 08:29:21 -07:00
snipts = Snipt.objects.filter(public=True).order_by('-created')
2012-01-16 15:47:09 -08:00
if tag_slug:
snipts = snipts.filter(tags__name__in=[tag_slug])
2011-10-12 08:29:21 -07:00
return {
2012-02-12 18:13:13 -08:00
'public': True,
2011-10-12 08:29:21 -07:00
'snipts': snipts,
'tags': tags,
2012-01-16 15:47:09 -08:00
'tag': tag_slug,
2011-10-12 08:29:21 -07:00
}
2011-10-12 13:34:01 -07:00
@render_to('snipts/list-user.html')
2012-01-16 15:47:09 -08:00
def list_user(request, username, tag_slug=None):
2011-10-12 08:29:21 -07:00
2012-01-16 15:47:09 -08:00
user = get_object_or_404(User, username=username)
2011-10-12 13:34:01 -07:00
tags = Tag.objects
snipts = Snipt.objects
if user == request.user:
tags = tags.filter(snipt__user=user)
2012-02-12 21:42:25 -08:00
public = False
2012-02-13 19:36:59 -08:00
favorites = Favorite.objects.filter(user=user).values('snipt')
favorites = [f['snipt'] for f in favorites]
snipts = snipts.filter(Q(user=user) | Q(pk__in=favorites))
2011-10-12 13:34:01 -07:00
else:
tags = tags.filter(snipt__user=user, snipt__public=True)
snipts = snipts.filter(user=user, public=True)
2012-02-12 21:42:25 -08:00
public = True
2011-10-12 13:34:01 -07:00
tags = tags.annotate(count=Count('taggit_taggeditem_items__id'))
2012-01-16 15:47:09 -08:00
tags = tags.order_by('-count', 'name')
2011-10-12 13:34:01 -07:00
snipts = snipts.order_by('-created')
2011-10-10 21:13:18 -07:00
2012-01-16 15:47:09 -08:00
if tag_slug:
snipts = snipts.filter(tags__name__in=[tag_slug])
2011-10-10 20:30:56 -07:00
return {
2012-02-12 21:42:25 -08:00
'public': public,
2011-10-10 21:13:18 -07:00
'snipts': snipts,
'tags': tags,
2012-01-16 15:47:09 -08:00
'tag': tag_slug,
2011-10-12 13:34:01 -07:00
'user': user,
2011-10-10 20:30:56 -07:00
}
2012-01-16 15:04:24 -08:00
@render_to('snipts/detail.html')
2012-01-16 15:47:09 -08:00
def detail(request, username, snipt_slug):
snipt = get_object_or_404(Snipt, user__username=username, slug=snipt_slug)
user = snipt.user
2012-02-26 20:52:02 -08:00
if user != request.user:
if not snipt.public:
if 'key' not in request.GET:
raise Http404
else:
if request.GET.get('key') != snipt.key:
raise Http404
2012-01-16 15:47:09 -08:00
tags = Tag.objects
if user == request.user:
tags = tags.filter(snipt__user=user)
public = False
2012-01-16 15:47:09 -08:00
else:
tags = tags.filter(snipt__user=user, snipt__public=True)
public = True
2012-01-16 15:47:09 -08:00
tags = tags.annotate(count=Count('taggit_taggeditem_items__id'))
tags = tags.order_by('-count', 'name')
2012-01-16 15:04:24 -08:00
return {
'public': public,
2012-01-16 15:47:09 -08:00
'snipt': snipt,
'tags': tags,
'user': user,
2012-01-16 15:04:24 -08:00
}
2012-02-26 14:57:02 -08:00
def embed(request, snipt_key):
2012-02-26 19:20:37 -08:00
snipt = get_object_or_404(Snipt, key=snipt_key)
# TODO: Remove these two lines
if not snipt.embedded:
snipt.save()
lines = snipt.embedded.split('\n')
return render_to_response('snipts/embed.html',
{'lines': lines, 'snipt': snipt},
context_instance=RequestContext(request),
mimetype='application/javascript')