Skip to content

Commit

Permalink
Merge pull request #43 from dreidev/EnhancementsAly
Browse files Browse the repository at this point in the history
Enhancements aly
  • Loading branch information
dreidev committed Aug 31, 2015
2 parents 8d9f89d + 56d890a commit 75a8492
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 40 deletions.
3 changes: 1 addition & 2 deletions comments/urls.py
@@ -1,11 +1,10 @@
from django.conf.urls import patterns, url
from .views import (
CommentListView, CommentCreateView, CommentDeleteView,
CommentCreateView, CommentDeleteView,
LikeComment, UnlikeComment, CommentUpdateView)

urlpatterns = patterns(
'',
url(r'^$', CommentListView.as_view(), name='comment-list'),
url(r'^create/$', CommentCreateView.as_view(), name='comment-create'),
url(
r'update/(?P<pk>[0-9]+)/$',
Expand Down
42 changes: 4 additions & 38 deletions comments/views.py
@@ -1,5 +1,5 @@
from django.views.generic import (
CreateView, ListView, DeleteView, FormView,
CreateView, DeleteView, FormView,
UpdateView)
from django.core.urlresolvers import reverse_lazy
from django.template.loader import render_to_string
Expand Down Expand Up @@ -56,40 +56,6 @@ def form_valid(self, form):
return response


class CommentListView(ListView):

"""
Class that lists all instances of model:comment.Comment
"""
model = Comment
template_name = "comments/comments.html"

def get_context_data(self, **kwargs):
context = super(CommentListView, self).get_context_data(**kwargs)
context['form'] = CommentForm()
context['comment_liked'] = self.get_comments_liked_zipped_list()
return context

def get_comments_liked_zipped_list(self):
"""
Returns a zipped list containing each comment and whether
the current user liked it or not.
"""
try:
user = self.request.user
except:
return
comments = Comment.objects.all()
liked = []
for comment in comments:
try:
Like.objects.get(user=user, comment=comment)
liked.append(True)
except:
liked.append(False)
return zip(comments, liked)


class CommentCreateView(AjaxableResponseMixin, CreateView):

"""
Expand All @@ -99,7 +65,7 @@ class CommentCreateView(AjaxableResponseMixin, CreateView):
form_class = CommentForm
model = Comment
template_name = 'comments/comment_form.html'
success_url = reverse_lazy('comment-list')
success_url = reverse_lazy('comment-create')

def form_valid(self, form):
comment = form.save(commit=False)
Expand All @@ -123,7 +89,7 @@ class CommentDeleteView(DeleteView):
"""
model = Comment
success_url = reverse_lazy('comment-list')
success_url = reverse_lazy('comment-create')

def get(self, request, *args, **kwargs):
"""
Expand Down Expand Up @@ -242,7 +208,7 @@ class CommentUpdateView(AjaxableResponseMixin, UpdateView):
form_class = CommentForm
model = Comment
template_name = 'comments/comment_edit_form.html'
success_url = reverse_lazy('comment-list')
success_url = reverse_lazy('comment-create')

def form_valid(self, form):
if not self.object.user:
Expand Down

0 comments on commit 75a8492

Please sign in to comment.