Advertisement
Guest User

Untitled

a guest
Jul 1st, 2015
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.18 KB | None | 0 0
  1. {% extends "base.html" %}
  2.  
  3. {% block content %}
  4.  
  5. <!-- Forums -->
  6. <div id="list">
  7. <table border="0" cellpadding="4" width="100%">
  8. <tr>
  9. <td></td>
  10. <td>Posts</td>
  11. <td>Last post</td>
  12. <td></td>
  13. </tr>
  14. {% for forum in forums %}
  15. <tr>
  16. <td {% if forloop.last %} class="last" {% endif %}>
  17. {{ forum.title }}
  18. </td>
  19. <td {% if forloop.last %} class="last" {% endif %}>
  20. {{ forum.num_posts }}
  21. </td>
  22. <td {% if forloop.last %} class="last" {% endif %}>
  23. {{ forum.last_post.short|linebreaksbr }}
  24. </td>
  25.  
  26. <td {% if forloop.last %}class="last" {% endif %}>
  27. <a class="button" href="{% url forum.views.forum forum_pk %}">
  28. VIEW</a>
  29. </td> -->
  30.  
  31. </tr>
  32. </table>
  33. {% endfor %}
  34. </div>
  35. {% endblock %}
  36.  
  37. from django.shortcuts import render, render_to_response
  38. from django.core.urlresolvers import reverse
  39. from dogslikeme.settings import MEDIA_ROOT, MEDIA_URL
  40. from django.core.paginator import Paginator, InvalidPage, EmptyPage
  41. from django.core.context_processors import csrf
  42.  
  43. from .models import *
  44.  
  45. # Create your views here.
  46. def main(request):
  47. """Main listing"""
  48. forums = Forum.objects.all()
  49. return render_to_response('forum/list.html', {'forums':forums,
  50. 'user':request.user})
  51.  
  52. def add_csrf(request, ** kwargs):
  53. d = dict(user=request.user, **kwargs)
  54. d.update(csrf(request))
  55. return d
  56.  
  57. def mk_paginator(request, items, num_items):
  58. """Create and return a paginator."""
  59. paginator = Paginator(items, num_items)
  60. try: page = int(request.GET.get("page"))
  61. except ValueError: page = 1
  62.  
  63. try:
  64. items = paginator.page(page)
  65. except(InvalidPage, EmptyPage):
  66. items = paginator.page(paginator.num_pages)
  67. return items
  68.  
  69. def forum(request, pk):
  70. """Listing of threads in a forum"""
  71. threads = Threads.objects.filter(forum=pk).order_by("-created")
  72. threads = mk_paginator(request, threads, 20)
  73. return render_to_response('forum/forum.html', add_csrf(request,
  74. threads=threads, pk=pk))
  75.  
  76. def thread(request, pk):
  77. """Listing of posts in a thread."""
  78. posts = Post.objects.filter(thread=pk).order_by("created")
  79. posts = mk_paginator(request, posts, 15)
  80. title = Thread.objects.get(pk=pk).title
  81. t = Thread.objects.get(pk=pk)
  82. return render_to_response('forum/thread.html', add_csrf(request,
  83. posts=posts, pk=pk, title=title, media_url=MEDIA_URL,
  84. forum_pk=t.forum.pk))
  85.  
  86. def post(request, ptype, pk):
  87. """Display a post form."""
  88. action = reverse("forum.views.%s" % ptype, args=[pk])
  89. if ptype == "new_thread":
  90. title = "Start New Topic"
  91. subject = ''
  92. elif ptype == "reply":
  93. title = "Reply"
  94. subject = "Re: " + Thread.objects.get(pk=pk).title
  95.  
  96. return render_to_response('forum/post.html', add_csrf(request,
  97. subject=subject, action=action, title=title, forum_pk=forum_pk))
  98.  
  99. def new_thread(request, pk):
  100. """Start a new thread."""
  101. p = request.POST
  102. if p["subject"] and p["body"]:
  103. forum = Forum.objects.get(pk=pk)
  104. thread = Thread.objects.create(forum=forum, title=p["subject"],
  105. creator=request.user)
  106. Post.objects.create(thread=thread, title=p["subject"],
  107. body=p["body"], creator=request.user)
  108. return HttpResponseRedirect(reverse("forum.views.forum",
  109. args=[pk]))
  110.  
  111. def reply(request, pk):
  112. """Reply to a thread."""
  113. p = request.POST
  114. if p["body"]:
  115. thread = Thread.objects.get(pk=pk)
  116. post = Post.objects.create(thread=thread, title=p["subject"],
  117. body=p["body"], creator=request.user)
  118. return HttpResponseRedirect(reverse("forum.views.thread",
  119. arg=[pk]) + "?page=last")
  120.  
  121. from django.conf.urls import include, url
  122. from django.contrib import admin
  123.  
  124. from . import views
  125.  
  126. urlpatterns = [
  127. url(r'^admin/', include(admin.site.urls)),
  128. url(r'^$', 'forum.views.main'),
  129. url(r'^forum(d+)/$', 'forum.views.forum'),
  130. url(r'^thread/(d+)/$', 'forum.views.thread'),
  131.  
  132. url(r'^post/(new_thread|reply)/(d+)/$', 'forum.views.post'),
  133. url(r'^reply/(d+)/$', 'forum.views.reply'),
  134. url(r'^new_thread/(d+)/$', 'forum.views.new_thread'),
  135. ]
  136.  
  137. <a class="button" href="{% url 'forum.views.forum' forum.pk %}">
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement