Snipt: Glitch Edition
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

77 lines
1.9KB

  1. import hashlib
  2. from accounts.models import UserProfile
  3. from annoying.decorators import ajax_request, render_to
  4. from blogs.views import blog_list
  5. from django.contrib.auth.models import User
  6. from django.db.models import Count
  7. from django.http import HttpResponseRedirect, HttpResponseBadRequest
  8. from snipts.models import Snipt
  9. from snipts.utils import get_lexers_list
  10. from taggit.models import Tag
  11. @render_to('homepage.html')
  12. def homepage(request):
  13. if request.blog_user:
  14. return blog_list(request)
  15. return {
  16. 'snipts_count': Snipt.objects.all().count(),
  17. }
  18. @ajax_request
  19. def lexers(request):
  20. lexers = get_lexers_list()
  21. objects = []
  22. for l in lexers:
  23. try:
  24. filters = l[2]
  25. except IndexError:
  26. filters = []
  27. try:
  28. mimetypes = l[3]
  29. except IndexError:
  30. mimetypes = []
  31. objects.append(
  32. {"name": l[0], "lexers": l[1], "filters": filters, "mimetypes": mimetypes}
  33. )
  34. return {"objects": objects}
  35. def login_redirect(request):
  36. if request.user.is_authenticated():
  37. return HttpResponseRedirect("/" + request.user.username + "/")
  38. else:
  39. return HttpResponseRedirect("/")
  40. @render_to("tags.html")
  41. def tags(request):
  42. all_tags = Tag.objects.filter(snipt__public=True).order_by("name")
  43. all_tags = all_tags.annotate(count=Count("taggit_taggeditem_items__id"))
  44. popular_tags = Tag.objects.filter(snipt__public=True)
  45. popular_tags = popular_tags.annotate(count=Count("taggit_taggeditem_items__id"))
  46. popular_tags = popular_tags.order_by("-count")[:20]
  47. popular_tags = sorted(popular_tags, key=lambda tag: tag.name)
  48. return {"all_tags": all_tags, "tags": popular_tags}
  49. @ajax_request
  50. def user_api_key(request):
  51. if not request.user.is_authenticated():
  52. return HttpResponseBadRequest()
  53. return {"api_key": request.user.api_key.key}