Advertisement
Guest User

Untitled

a guest
May 24th, 2017
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.22 KB | None | 0 0
  1. # Create your views here.
  2.  
  3.  
  4. import operator
  5.  
  6.  
  7. from django.views import generic
  8. from .models import Blog, BlogAuthor, PostComment, Post
  9. from django.contrib.auth.models import User # Blog author or commenter
  10. from django.contrib.auth import login, authenticate
  11. from django.contrib.auth.forms import UserCreationForm
  12. from django.shortcuts import render, redirect
  13. from blog.forms import SignUpForm
  14.  
  15.  
  16. def index(request):
  17. """
  18. View function for home page of site.
  19. """
  20. # Render the HTML template index.html
  21. return render(
  22. request,
  23. 'index.html',
  24. )
  25.  
  26.  
  27. def signup(request):
  28. if request.method == 'POST':
  29. form = SignUpForm(request.POST)
  30. if form.is_valid():
  31. form.save()
  32. username = form.cleaned_data.get('username')
  33. raw_password = form.cleaned_data.get('password1')
  34. user = authenticate(username=username, password=raw_password)
  35. login(request, user)
  36. return redirect('index')
  37. else:
  38. form = SignUpForm()
  39. return render(request, 'signup.html', {'form': form})
  40.  
  41.  
  42.  
  43.  
  44.  
  45. class BlogListView(generic.ListView):
  46. """
  47. Generic class-based view for a list of all blogs.
  48. """
  49. model = Blog
  50. paginate_by = 5
  51.  
  52.  
  53. class PostListView(generic.ListView):
  54. model = Post
  55. paginate_by = 15
  56.  
  57.  
  58. from django.shortcuts import get_object_or_404
  59.  
  60.  
  61. class BlogListbyAuthorView(generic.ListView):
  62. """
  63. Generic class-based view for a list of blogs posted by a particular BlogAuthor.
  64. """
  65. model = Blog
  66. paginate_by = 5
  67. template_name = 'blog/blog_list_by_author.html'
  68.  
  69. def get_queryset(self):
  70. """
  71. Return list of Blog objects created by BlogAuthor (author id specified in URL)
  72. """
  73. id = self.kwargs['pk']
  74. target_author = get_object_or_404(BlogAuthor, pk=id)
  75. return Blog.objects.filter(author=target_author)
  76.  
  77. def get_context_data(self, **kwargs):
  78. """
  79. Add BlogAuthor to context so they can be displayed in the template
  80. """
  81. # Call the base implementation first to get a context
  82.  
  83. context = super(BlogListbyAuthorView, self).get_context_data(**kwargs)
  84. # Get the blogger object from the "pk" URL parameter and add it to the context
  85. context['blogger'] = get_object_or_404(BlogAuthor, pk=self.kwargs['pk'])
  86. return context
  87.  
  88.  
  89. class PostListY(generic.ListView):
  90. model = Post
  91. paginate_by = 15
  92. template_name = 'blog/post_list.html'
  93.  
  94. def get_queryset(self):
  95. id = self.kwargs['pk']
  96. target = get_object_or_404(Blog, pk=id)
  97. return Post.objects.filter(author=target)
  98.  
  99. def get_context_data(self, **kwargs):
  100. # Call the base implementation first to get a context
  101. context = super(PostListY, self).get_context_data(**kwargs)
  102. # Get the blogger object from the "pk" URL parameter and add it to the context
  103. context['blogger'] = get_object_or_404(BlogAuthor, pk=self.kwargs['pk'])
  104. return context
  105.  
  106.  
  107. class BlogDetailView(generic.DetailView):
  108. """
  109. Generic class-based detail view for a blog.
  110. """
  111. model = Blog
  112. template_name = 'blog/post_list.html'
  113. def get_context_data(self, **kwargs):
  114. context = super(BlogDetailView,self).get_context_data(**kwargs)
  115. id = self.kwargs['pk']
  116. target = get_object_or_404(Blog, pk=id)
  117. context['post_list'] = Post.objects.filter(blog = target)
  118.  
  119.  
  120.  
  121.  
  122.  
  123. class PostDetailView(generic.DetailView):
  124. """
  125. Generic class-based detail view for a post.
  126. """
  127. model = Post
  128.  
  129.  
  130. class BloggerListView(generic.ListView):
  131. """
  132. Generic class-based view for a list of bloggers.
  133. """
  134. model = BlogAuthor
  135. paginate_by = 5
  136.  
  137.  
  138. from django.contrib.auth.mixins import LoginRequiredMixin
  139. from django.views.generic.edit import CreateView
  140. from django.urls import reverse
  141.  
  142.  
  143. class PostCommentCreate(LoginRequiredMixin, CreateView):
  144. """
  145. Form for adding a blog comment. Requires login.
  146. """
  147. model = PostComment
  148. fields = ['description', ]
  149.  
  150. def get_context_data(self, **kwargs):
  151. """
  152. Add associated blog to form template so can display its title in HTML.
  153. """
  154. # Call the base implementation first to get a context
  155. context = super(PostCommentCreate, self).get_context_data(**kwargs)
  156. # Get the blog from id and add it to the context
  157. context['post'] = get_object_or_404(Blog, pk=self.kwargs['pk'])
  158. return context
  159.  
  160. def form_valid(self, form):
  161. """
  162. Add author and associated blog to form data before setting it as valid (so it is saved to model)
  163. """
  164. # Add logged-in user as author of comment
  165. form.instance.author = self.request.user
  166. # Associate comment with blog based on passed id
  167. form.instance.post = get_object_or_404(Post, pk=self.kwargs['pk'])
  168. # Call super-class form validation behaviour
  169. return super(PostCommentCreate, self).form_valid(form)
  170.  
  171. def get_success_url(self):
  172. """
  173. After posting comment return to associated blog.
  174. """
  175. return reverse('post-detail', kwargs={'pk': self.kwargs['pk'], })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement