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-03-11 20:50:06 -07:00
|
|
|
from pygments.lexers import get_all_lexers
|
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():
|
2011-12-02 21:19:01 -08:00
|
|
|
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:
|
2012-03-10 18:52:46 -08:00
|
|
|
snipts = snipts.filter(tags__slug__in=[tag_slug])
|
|
|
|
tag = get_object_or_404(Tag, slug=tag_slug)
|
2012-03-10 20:36:44 -08:00
|
|
|
else:
|
|
|
|
tag = None
|
2011-12-02 21:19:01 -08:00
|
|
|
|
2011-10-12 08:29:21 -07:00
|
|
|
return {
|
2012-03-06 08:29:43 -08:00
|
|
|
'has_snipts': True,
|
2012-03-11 20:50:06 -07:00
|
|
|
'lexers': sorted(get_all_lexers()),
|
2012-02-12 18:13:13 -08:00
|
|
|
'public': True,
|
2011-10-12 08:29:21 -07:00
|
|
|
'snipts': snipts,
|
|
|
|
'tags': tags,
|
2012-03-10 18:52:46 -08:00
|
|
|
'tag': tag,
|
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:
|
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))
|
2012-04-12 19:04:37 -07:00
|
|
|
|
|
|
|
tags = tags.filter(Q(snipt__user=user) | Q(snipt__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:
|
2012-03-10 18:52:46 -08:00
|
|
|
snipts = snipts.filter(tags__slug__in=[tag_slug])
|
|
|
|
tag = get_object_or_404(Tag, slug=tag_slug)
|
2012-03-10 20:36:44 -08:00
|
|
|
else:
|
|
|
|
tag = None
|
2011-12-02 21:19:01 -08:00
|
|
|
|
2011-10-10 20:30:56 -07:00
|
|
|
return {
|
2012-03-06 08:29:43 -08:00
|
|
|
'has_snipts': True,
|
2012-03-11 20:50:06 -07:00
|
|
|
'lexers': sorted(get_all_lexers()),
|
2012-02-12 21:42:25 -08:00
|
|
|
'public': public,
|
2012-04-13 13:26:58 -07:00
|
|
|
'public_user': (public and user),
|
2011-10-10 21:13:18 -07:00
|
|
|
'snipts': snipts,
|
|
|
|
'tags': tags,
|
2012-03-10 18:52:46 -08:00
|
|
|
'tag': tag,
|
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)
|
2012-02-19 10:01:28 -08:00
|
|
|
public = False
|
2012-01-16 15:47:09 -08:00
|
|
|
else:
|
|
|
|
tags = tags.filter(snipt__user=user, snipt__public=True)
|
2012-02-19 10:01:28 -08:00
|
|
|
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 {
|
2012-03-06 08:29:43 -08:00
|
|
|
'has_snipts': True,
|
2012-03-11 20:50:06 -07:00
|
|
|
'lexers': sorted(get_all_lexers()),
|
2012-02-19 10:01:28 -08:00
|
|
|
'public': public,
|
2012-01-16 15:47:09 -08:00
|
|
|
'snipt': snipt,
|
|
|
|
'tags': tags,
|
2012-02-19 10:01:28 -08:00
|
|
|
'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)
|
|
|
|
|
|
|
|
lines = snipt.embedded.split('\n')
|
|
|
|
return render_to_response('snipts/embed.html',
|
|
|
|
{'lines': lines, 'snipt': snipt},
|
|
|
|
context_instance=RequestContext(request),
|
|
|
|
mimetype='application/javascript')
|