def page(request, slug, template="pages/page.html"): """ Display content for a page. First check for any matching page processors and handle them. Secondly, build the list of template names to choose from given the slug and type of page being viewed. """ page = get_object_or_404(Page.objects.published(request.user), slug=slug) if page.login_required and not request.user.is_authenticated(): return redirect("%s?%s=%s" % (settings.LOGIN_URL, REDIRECT_FIELD_NAME, urlquote(request.get_full_path()))) context = {"page": page} for processor in page_processors.processors[page.content_model]: response = processor(request, page) if isinstance(response, HttpResponse): return response elif response: if isinstance(response, dict): context.update(response) else: raise ValueError("The page processor %s.%s returned %s but " "must return HttpResponse or dict." % ( processor.__module__, processor.__name__, type(response))) templates = ["pages/%s.html" % slug] if page.content_model is not None: templates.append("pages/%s.html" % page.content_model) templates.append(template) request_context = RequestContext(request, context) t = select_template(templates, request_context) return HttpResponse(t.render(request_context))
def page(request, slug, template="pages/page.html", extra_context=None): """ Display content for a page. First check for any matching page processors and handle them. Secondly, build the list of template names to choose from given the slug and type of page being viewed. """ page = get_object_or_404(Page.objects.published(request.user), slug=slug) if page.login_required and not request.user.is_authenticated(): path = urlquote(request.get_full_path()) url = "%s?%s=%s" % (settings.LOGIN_URL, REDIRECT_FIELD_NAME, path) return redirect(url) context = {"page": page} if extra_context is not None: context.update(extra_context) model_processors = page_processors.processors[page.content_model] slug_processors = page_processors.processors["slug:%s" % page.slug] for processor in model_processors + slug_processors: response = processor(request, page) if isinstance(response, HttpResponse): return response elif response: try: context.update(response) except (TypeError, ValueError): name = "%s.%s" % (processor.__module__, processor.__name__) error = ("The page processor %s returned %s but must return " "HttpResponse or dict." % (name, type(response))) raise ValueError(error) templates = [u"pages/%s.html" % slug] if page.content_model is not None: templates.append(u"pages/%s.html" % page.content_model) templates.append(template) request_context = RequestContext(request, context) t = select_template(templates, request_context) return HttpResponse(t.render(request_context))
def blog_post_list(request, tag=None, year=None, month=None, username=None, category=None, template="blog/blog_post_list.html"): """ Display a list of blog posts that are filtered by tag, year, month, author or category. Custom templates are checked for using the name ``blog/blog_post_list_XXX.html`` where ``XXX`` is either the category slug or author's username if given. """ settings.use_editable() templates = [] blog_posts = BlogPost.objects.published(for_user=request.user) if tag is not None: tag = get_object_or_404(Keyword, slug=tag) blog_posts = blog_posts.filter(keywords__in=tag.assignments.all()) if year is not None: blog_posts = blog_posts.filter(publish_date__year=year) if month is not None: blog_posts = blog_posts.filter(publish_date__month=month) month = month_name[int(month)] if category is not None: category = get_object_or_404(BlogCategory, slug=category) blog_posts = blog_posts.filter(categories=category) templates.append("blog/blog_post_list_%s.html" % category.slug) author = None if username is not None: author = get_object_or_404(User, username=username) blog_posts = blog_posts.filter(user=author) templates.append("blog/blog_post_list_%s.html" % username) # Create dicts mapping blog post IDs to lists of categories and # keywords, and assign these to each blog post, to avoid querying # the database inside the template loop for posts. blog_posts = list(blog_posts.select_related("user")) categories = defaultdict(list) for cat in BlogCategory.objects.raw( "SELECT * FROM blog_blogcategory " "JOIN blog_blogpost_categories " "ON blog_blogcategory.id = blog_blogpost_categories.blogcategory_id " "WHERE blogpost_id IN (%s)" % ",".join([str(p.id) for p in blog_posts])): categories[cat.blogpost_id].append(cat) keywords = defaultdict(list) blogpost_type = ContentType.objects.get(app_label="blog", model="blogpost") assigned = AssignedKeyword.objects.filter(blogpost__in=blog_posts, content_type=blogpost_type).select_related("keyword") for a in assigned: keywords[a.object_pk].append(a.keyword) for i, post in enumerate(blog_posts): setattr(blog_posts[i], "category_list", categories[post.id]) setattr(blog_posts[i], "keyword_list", keywords[post.id]) blog_posts = paginate(blog_posts, request.GET.get("page", 1), settings.BLOG_POST_PER_PAGE, settings.BLOG_POST_MAX_PAGING_LINKS) context = {"blog_page": blog_page(), "blog_posts": blog_posts, "year": year, "month": month, "tag": tag, "category": category, "author": author} templates.append(template) request_context = RequestContext(request, context) t = select_template(templates, request_context) return HttpResponse(t.render(request_context))
def news_post_detail(request, slug, template="news/news_post_detail.html"): """ Display a news post. """ news_posts = NewsPost.objects.published(for_user=request.user) news_post = get_object_or_404(news_posts, slug=slug) settings.use_editable() context = {"news_post": news_post, "news_page": news_page(),} request_context = RequestContext(request, context) t = select_template(["news/%s.html" % slug, template], request_context) return HttpResponse(t.render(request_context))
def podcast_detail(request, slug, template="podcast/podcast_detail.html"): """ Display a particular podcast and handle comment submission. Custom templates are checked for using the name ``podcast_detail_XXX.html`` where ``XXX`` is the podcast's slug. """ podcasts = Podcast.objects.published(for_user=request.user) pc = get_object_or_404(podcasts, slug=slug) context = {"podcast": pc} templates = [u"podcast/podcast_detail_%s.html" % slug, template] request_context = RequestContext(request, context) t = select_template(templates, request_context) return HttpResponse(t.render(request_context))
def podcast_list( request, tag=None, year=None, month=None, username=None, category=None, template="podcast/podcast_list.html" ): """ Provide a listing of podcasts """ templates = [] podcasts = Podcast.objects.published() context = {"page": podcast_page(), "podcasts": podcasts} templates.append(template) request_context = RequestContext(request, context) t = select_template(templates, request_context) return HttpResponse(t.render(request_context))
def blog_post_list(request, tag=None, year=None, month=None, username=None, category=None, template="blog/blog_post_list.html"): """ Display a list of blog posts that are filtered by tag, year, month, author or category. Custom templates are checked for using the name ``blog/blog_post_list_XXX.html`` where ``XXX`` is either the category slug or author's username if given. """ settings.use_editable() templates = [] blog_posts = BlogPost.objects.published(for_user=request.user) if tag is not None: tag = get_object_or_404(Keyword, slug=tag) blog_posts = blog_posts.filter(keywords=tag) if year is not None: blog_posts = blog_posts.filter(publish_date__year=year) if month is not None: blog_posts = blog_posts.filter(publish_date__month=month) month = month_name[int(month)] if category is not None: category = get_object_or_404(BlogCategory, slug=category) blog_posts = blog_posts.filter(categories=category) templates.append("blog/blog_post_list_%s.html" % category.slug) author = None if username is not None: author = get_object_or_404(User, username=username) blog_posts = blog_posts.filter(user=author) templates.append("blog/blog_post_list_%s.html" % username) blog_posts = paginate(blog_posts, request.GET.get("page", 1), settings.BLOG_POST_PER_PAGE, settings.BLOG_POST_MAX_PAGING_LINKS) context = { "blog_page": blog_page(), "blog_posts": blog_posts, "year": year, "month": month, "tag": tag, "category": category, "author": author } templates.append(template) request_context = RequestContext(request, context) t = select_template(templates, request_context) return HttpResponse(t.render(request_context))
def render(self, context): if not getattr(self, "nodelist", False): if not isinstance(file_name, basestring) and \ is_iterable(file_name): t = select_template(file_name, context) else: t = get_template(file_name, context) self.nodelist = t.nodelist parts = [template.Variable(part).resolve(context) for part in token.split_contents()[1:]] if takes_context: parts.insert(0, context) result = tag_func(*parts) autoescape = context.autoescape context = context_class(result, autoescape=autoescape) return self.nodelist.render(context)
def render_to_response(template_name, dictionary=None, context_instance=None, mimetype=None): """ Mimics ``django.shortcuts.render_to_response`` but uses Mezzanine's ``get_template`` which handles device specific template directories. """ dictionary = dictionary or {} if context_instance: context_instance.update(dictionary) else: context_instance = Context(dictionary) if isinstance(template_name, (list, tuple)): t = select_template(template_name, context_instance) else: t = get_template(template_name, context_instance) return HttpResponse(t.render(context_instance), mimetype=mimetype)
def podcast_list(request, tag=None, year=None, month=None, username=None, category=None, template="podcast/podcast_list.html"): """ Provide a listing of podcasts """ templates = [] podcasts = Podcast.objects.published() context = {"page": podcast_page(), "podcasts": podcasts} templates.append(template) request_context = RequestContext(request, context) t = select_template(templates, request_context) return HttpResponse(t.render(request_context))
def blog_post_detail(request, slug, template="blog/blog_post_detail.html"): """ Display a blog post. """ # Create two comment forms - one with posted data and errors that will be # matched to the form submitted via comment_id, and an empty one for all # other instances. commenter_cookie_prefix = "mezzanine-blog-" commenter_cookie_fields = ("name", "email", "website") comment_data = {} for f in commenter_cookie_fields: comment_data[f] = request.COOKIES.get(commenter_cookie_prefix + f, "") blog_posts = BlogPost.objects.published(for_user=request.user) blog_post = get_object_or_404(blog_posts, slug=slug) posted_comment_form = CommentForm(request.POST or None, initial=comment_data) unposted_comment_form = CommentForm(initial=comment_data) if request.method == "POST" and posted_comment_form.is_valid(): comment = posted_comment_form.save(commit=False) comment.blog_post = blog_post comment.by_author = (request.user == blog_post.user and request.user.is_authenticated) comment.ip_address = request.META.get("HTTP_X_FORWARDED_FOR", request.META["REMOTE_ADDR"]) comment.replied_to_id = request.POST.get("replied_to") comment.save() response = HttpResponseRedirect(comment.get_absolute_url()) # Store commenter's details in a cookie for 90 days. cookie_expires = 60 * 60 * 24 * 90 for f in commenter_cookie_fields: cookie_name = commenter_cookie_prefix + f cookie_value = request.POST.get(f, "") set_cookie(response, cookie_name, cookie_value, cookie_expires) return response settings.use_editable() context = {"blog_post": blog_post, "blog_page": blog_page(), "use_disqus": bool(settings.COMMENTS_DISQUS_SHORTNAME), "posted_comment_form": posted_comment_form, "unposted_comment_form": unposted_comment_form} request_context = RequestContext(request, context) t = select_template(["blog/%s.html" % slug, template], request_context) return HttpResponse(t.render(request_context))
def blog_post_detail(request, slug, template="blog/blog_post_detail.html"): """ Display a blog post and handle comment submission. Custom templates are checked for using the name ``blog/blog_post_detail_XXX.html`` where ``XXX`` is the blog posts's slug. """ blog_posts = BlogPost.objects.published(for_user=request.user) blog_post = get_object_or_404(blog_posts, slug=slug) # Handle comments comment_parts = handle_comments(blog_post, request) posted_comment_form, unposted_comment_form, response = comment_parts if response is not None: return response context = {"blog_page": blog_page(), "blog_post": blog_post, "posted_comment_form": posted_comment_form, "unposted_comment_form": unposted_comment_form} templates = ["blog/blog_post_detail_%s.html" % slug, template] request_context = RequestContext(request, context) t = select_template(templates, request_context) return HttpResponse(t.render(request_context))
def blog_post_list(request, tag=None, year=None, month=None, username=None, category=None, template="blog/blog_post_list.html"): """ Display a list of blog posts that are filtered by tag, year, month, author or category. Custom templates are checked for using the name ``blog/blog_post_list_XXX.html`` where ``XXX`` is either the category slug or author's username if given. """ settings.use_editable() templates = [] blog_posts = BlogPost.objects.published(for_user=request.user) if tag is not None: tag = get_object_or_404(Keyword, slug=tag) blog_posts = blog_posts.filter(keywords=tag) if year is not None: blog_posts = blog_posts.filter(publish_date__year=year) if month is not None: blog_posts = blog_posts.filter(publish_date__month=month) month = month_name[int(month)] if category is not None: category = get_object_or_404(BlogCategory, slug=category) blog_posts = blog_posts.filter(categories=category) templates.append("blog/blog_post_list_%s.html" % category.slug) author = None if username is not None: author = get_object_or_404(User, username=username) blog_posts = blog_posts.filter(user=author) templates.append("blog/blog_post_list_%s.html" % username) blog_posts = paginate(blog_posts, request.GET.get("page", 1), settings.BLOG_POST_PER_PAGE, settings.BLOG_POST_MAX_PAGING_LINKS) context = {"blog_page": blog_page(), "blog_posts": blog_posts, "year": year, "month": month, "tag": tag, "category": category, "author": author} templates.append(template) request_context = RequestContext(request, context) t = select_template(templates, request_context) return HttpResponse(t.render(request_context))
def blog_post_detail(request, slug, template="blog/blog_post_detail.html"): """ Display a blog post and handle comment submission. Custom templates are checked for using the name ``blog/blog_post_detail_XXX.html`` where ``XXX`` is the blog posts's slug. """ blog_posts = BlogPost.objects.published(for_user=request.user) blog_post = get_object_or_404(blog_posts, slug=slug) # Handle comments comment_parts = handle_comments(blog_post, request) posted_comment_form, unposted_comment_form, response = comment_parts if response is not None: return response context = { "blog_page": blog_page(), "blog_post": blog_post, "posted_comment_form": posted_comment_form, "unposted_comment_form": unposted_comment_form } templates = ["blog/blog_post_detail_%s.html" % slug, template] request_context = RequestContext(request, context) t = select_template(templates, request_context) return HttpResponse(t.render(request_context))
def blog_post_list(request, tag=None, year=None, month=None, username=None, category=None, template="blog/blog_post_list.html"): """ Display a list of blog posts that are filtered by tag, year, month, author or category. Custom templates are checked for using the name ``blog/blog_post_list_XXX.html`` where ``XXX`` is either the category slug or author's username if given. """ settings.use_editable() templates = [] blog_posts = BlogPost.objects.published(for_user=request.user) if tag is not None: tag = get_object_or_404(Keyword, slug=tag) blog_posts = blog_posts.filter(keywords__in=tag.assignments.all()) if year is not None: blog_posts = blog_posts.filter(publish_date__year=year) if month is not None: blog_posts = blog_posts.filter(publish_date__month=month) month = month_name[int(month)] if category is not None: category = get_object_or_404(BlogCategory, slug=category) blog_posts = blog_posts.filter(categories=category) templates.append(u"blog/blog_post_list_%s.html" % category.slug) author = None if username is not None: author = get_object_or_404(User, username=username) blog_posts = blog_posts.filter(user=author) templates.append(u"blog/blog_post_list_%s.html" % username) # Create dicts mapping blog post IDs to lists of categories and # keywords, and assign these to each blog post, to avoid querying # the database inside the template loop for posts. blog_posts = list(blog_posts.select_related("user")) categories = defaultdict(list) if blog_posts: ids = ",".join([str(p.id) for p in blog_posts]) for cat in BlogCategory.objects.raw( "SELECT * FROM blog_blogcategory " "JOIN blog_blogpost_categories " "ON blog_blogcategory.id = blogcategory_id " "WHERE blogpost_id IN (%s)" % ids): categories[cat.blogpost_id].append(cat) keywords = defaultdict(list) blogpost_type = ContentType.objects.get(app_label="blog", model="blogpost") assigned = AssignedKeyword.objects.filter( blogpost__in=blog_posts, content_type=blogpost_type).select_related("keyword") for a in assigned: keywords[a.object_pk].append(a.keyword) for i, post in enumerate(blog_posts): setattr(blog_posts[i], "category_list", categories[post.id]) setattr(blog_posts[i], "keyword_list", keywords[post.id]) blog_posts = paginate(blog_posts, request.GET.get("page", 1), settings.BLOG_POST_PER_PAGE, settings.BLOG_POST_MAX_PAGING_LINKS) context = { "blog_page": blog_page(), "blog_posts": blog_posts, "year": year, "month": month, "tag": tag, "category": category, "author": author } templates.append(template) request_context = RequestContext(request, context) t = select_template(templates, request_context) return HttpResponse(t.render(request_context))