views.py 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. from __future__ import unicode_literals
  2. from future.builtins import str, int
  3. from calendar import month_name
  4. from django.contrib.auth import get_user_model
  5. from django.http import Http404
  6. from django.shortcuts import get_object_or_404
  7. from django.template.response import TemplateResponse
  8. from django.utils.translation import ugettext_lazy as _
  9. from mezzanine.blog.models import BlogPost, BlogCategory
  10. from mezzanine.blog.feeds import PostsRSS, PostsAtom
  11. from mezzanine.conf import settings
  12. from mezzanine.generic.models import Keyword
  13. from mezzanine.utils.views import paginate
  14. User = get_user_model()
  15. def blog_post_list(request, tag=None, year=None, month=None, username=None,
  16. category=None, template="blog/blog_post_list.html",
  17. extra_context=None):
  18. """
  19. Display a list of blog posts that are filtered by tag, year, month,
  20. author or category. Custom templates are checked for using the name
  21. ``blog/blog_post_list_XXX.html`` where ``XXX`` is either the
  22. category slug or author's username if given.
  23. """
  24. templates = []
  25. blog_posts = BlogPost.objects.published(for_user=request.user)
  26. if tag is not None:
  27. tag = get_object_or_404(Keyword, slug=tag)
  28. blog_posts = blog_posts.filter(keywords__keyword=tag)
  29. if year is not None:
  30. blog_posts = blog_posts.filter(publish_date__year=year)
  31. if month is not None:
  32. blog_posts = blog_posts.filter(publish_date__month=month)
  33. try:
  34. month = _(month_name[int(month)])
  35. except IndexError:
  36. raise Http404()
  37. if category is not None:
  38. category = get_object_or_404(BlogCategory, slug=category)
  39. blog_posts = blog_posts.filter(categories=category)
  40. templates.append(u"blog/blog_post_list_%s.html" %
  41. str(category.slug))
  42. author = None
  43. if username is not None:
  44. author = get_object_or_404(User, username=username)
  45. blog_posts = blog_posts.filter(user=author)
  46. templates.append(u"blog/blog_post_list_%s.html" % username)
  47. prefetch = ("categories", "keywords__keyword")
  48. blog_posts = blog_posts.select_related("user").prefetch_related(*prefetch)
  49. blog_posts = paginate(blog_posts, request.GET.get("page", 1),
  50. settings.BLOG_POST_PER_PAGE,
  51. settings.MAX_PAGING_LINKS)
  52. context = {"blog_posts": blog_posts, "year": year, "month": month,
  53. "tag": tag, "category": category, "author": author}
  54. context.update(extra_context or {})
  55. templates.append(template)
  56. return TemplateResponse(request, templates, context)
  57. def blog_post_detail(request, slug, year=None, month=None, day=None,
  58. template="blog/blog_post_detail.html",
  59. extra_context=None):
  60. """. Custom templates are checked for using the name
  61. ``blog/blog_post_detail_XXX.html`` where ``XXX`` is the blog
  62. posts's slug.
  63. """
  64. blog_posts = BlogPost.objects.published(
  65. for_user=request.user).select_related()
  66. blog_post = get_object_or_404(blog_posts, slug=slug)
  67. related_posts = blog_post.related_posts.published(for_user=request.user)
  68. context = {"blog_post": blog_post, "editable_obj": blog_post,
  69. "related_posts": related_posts}
  70. context.update(extra_context or {})
  71. templates = [u"blog/blog_post_detail_%s.html" % str(slug), template]
  72. return TemplateResponse(request, templates, context)
  73. def blog_post_feed(request, format, **kwargs):
  74. """
  75. Blog posts feeds - maps format to the correct feed view.
  76. """
  77. try:
  78. return {"rss": PostsRSS, "atom": PostsAtom}[format](**kwargs)(request)
  79. except KeyError:
  80. raise Http404()