def handle_noargs(self, **kwargs): CommentModel = comments.get_model() for c in CommentModel.objects.exclude( user__isnull=True).sort('submit_date').iterator(): fc = FlatComment(site_id=c.site_id, content_type_id=c.content_type_id, object_id=c.object_pk, user_id=c.user_id, submit_date=c.submit_date, content=c.comment, is_public=c.is_public and not c.is_removed) if fc.is_public: cl = CommentList(fc.content_type, fc.object_id) cl.post_comment(fc) sys.stdout.write('.') sys.stdout.flush() else: fc.save(force_insert=True) sys.stdout.write('X') sys.stdout.flush() sys.stdout.write('DONE\n') sys.stdout.flush()
def get_comment_list(self, context): reversed = show_reversed(context['request']) if isinstance(self.lookup, template.Variable): return CommentList.for_object(self.lookup.resolve(context), reversed) ct, obj_pk = map(lambda v: v.resolve(context), self.lookup) if isinstance(ct, int): ct = ContentType.objects.get_for_id(ct) return CommentList(ct, obj_pk, reversed)
def add_publishable(cls, category, publishable, score=None, pipe=None, commit=True): if score is None: score = CommentList(publishable.content_type, publishable.pk).count() # no comments yet, pass if not score: return pipe return super(MostCommentedListingHandler, cls).add_publishable(category, publishable, score, pipe=pipe, commit=commit)
def test_get_comment_raises_does_not_exist_on_mismatched_content_object( self): c = self._get_comment() c.post() clist = CommentList.for_object(ContentType.objects.get(pk=2)) tools.assert_raises(FlatComment.DoesNotExist, clist.get_comment, c.pk)
def test_reversed_slice(self): comment_list = CommentList(self.content_type, 1, reversed=True) clist = range(11) tools.assert_equals(clist[0:4], comment_list[0:4]) tools.assert_equals(clist[2:6], comment_list[2:6]) tools.assert_equals(clist[2:60], comment_list[2:60]) tools.assert_equals(clist[3:], comment_list[3:])
def unlock_comments(request, context): clist = CommentList.for_object(context['object']) clist.unlock() if request.is_ajax(): return HttpResponse('{"error": false}', content_type='application/json') return HttpResponseRedirect(context['object'].get_absolute_url())
def post_comment(request, context, comment_id=None): clist = CommentList.for_object(context['object']) if clist.locked(): return TemplateResponse(request, get_template('comments_locked.html', context['object'], request.is_ajax()), context, status=403) comment = None user = request.user if comment_id: try: comment = clist.get_comment(comment_id) except FlatComment.DoesNotExist: raise Http404() # make sure we don't change user when mod is editting a comment user = comment.user # you can only comment your own comments or you have to be a moderator if comment.user != request.user and not comments_settings.IS_MODERATOR_FUNC( request.user): return HttpResponseForbidden( "You cannot edit other people's comments.") data, files = None, None if request.method == 'POST': data, files = request.POST, request.FILES form = FlatCommentMultiForm(context['object'], user, data=data, files=files, instance=comment) if form.is_valid(): comment, success, reason = form.post() if not success: return HttpResponseForbidden(reason) comment_updated.send(sender=comment.__class__, comment=comment, updating_user=request.user, date_updated=now()) if request.is_ajax(): return TemplateResponse( request, get_template('comment_detail_async.html', context['object']), context) return HttpResponseRedirect( comment.get_absolute_url(show_reversed(request))) context.update({'comment': comment, 'form': form}) return TemplateResponse( request, get_template('comment_form.html', context['object'], request.is_ajax()), context)
def add_publishable(cls, category, publishable, score=None, publish_from=None, pipe=None, commit=True): if score is None and publish_from is None: last_comment = CommentList.for_object(publishable).last_comment() # no comment yet, pass if last_comment is None: return pipe publish_from = last_comment.submit_date return super(LastCommentedListingHandler, cls).add_publishable(category, publishable, score=score, publish_from=publish_from, pipe=pipe, commit=commit)
def comment_detail(request, context, comment_id): clist = CommentList.for_object(context['object']) try: comment = clist.get_comment(comment_id) except FlatComment.DoesNotExist: raise Http404() # avoid additional lookup comment.content_object = context['object'] return HttpResponseRedirect(comment.get_absolute_url(show_reversed(request)))
def post_comment(request, context, comment_id=None): clist = CommentList.for_object(context['object']) if clist.locked(): return TemplateResponse(request, get_template('comments_locked.html', context['object'], request.is_ajax()), context, status=403) comment = None user = request.user if comment_id: try: comment = clist.get_comment(comment_id) except FlatComment.DoesNotExist: raise Http404() # make sure we don't change user when mod is editting a comment user = comment.user # you can only comment your own comments or you have to be a moderator if comment.user != request.user and not comments_settings.IS_MODERATOR_FUNC(request.user): return HttpResponseForbidden("You cannot edit other people's comments.") # Don't allow users to edit a comment after the allowed edit time has expired if comment.user == request.user and not comments_settings.IS_MODERATOR_FUNC(request.user) and comment.is_edit_timer_expired(): if request.is_ajax(): context.update({'comment': comment}) return TemplateResponse(request, get_template('comment_detail_async.html', context['object']), context) return HttpResponseForbidden("The allowed time to edit the comment has expired.") data, files = None, None if request.method == 'POST': data, files = request.POST, request.FILES form = FlatCommentMultiForm(context['object'], user, data=data, files=files, instance=comment) if form.is_valid(): comment, success, reason = form.post(request) if not success: return HttpResponseForbidden(reason) comment_updated.send( sender=comment.__class__, comment=comment, updating_user=request.user, date_updated=now() ) if request.is_ajax(): context.update({'comment': comment}) return TemplateResponse(request, get_template('comment_detail_async.html', context['object']), context) return HttpResponseRedirect(comment.get_absolute_url(show_reversed(request))) context.update({ 'comment': comment, 'form': form }) return TemplateResponse(request, get_template('comment_form.html', context['object'], request.is_ajax()), context)
def render(self, context): try: content_object = self.obj_var.resolve(context) except template.VariableDoesNotExist: return '' # locked comments, no form for you! if CommentList.for_object(content_object).locked(): return '' context[self.form_var] = FlatCommentMultiForm(content_object, context['user']) return ''
def render(self, context): try: content_object = self.obj_var.resolve(context) except template.VariableDoesNotExist: return "" # locked comments, no form for you! if CommentList.for_object(content_object).locked(): return "" context[self.form_var] = FlatCommentMultiForm(content_object, context["user"]) return ""
def moderate_comment(request, context, comment_id): clist = CommentList.for_object(context['object']) try: comment = clist.get_comment(comment_id) except FlatComment.DoesNotExist: raise Http404() url = comment.get_absolute_url() comment.moderate(request.user) if request.is_ajax(): return HttpResponse('{"error": false}', content_type='application/json') return HttpResponseRedirect(url)
def list_comments(request, context, reverse=None): if reverse is None: reverse = show_reversed(request) clist = CommentList.for_object(context['object'], reverse) paginator = Paginator(clist, comments_settings.PAGINATE_BY) try: context['page'] = paginator.page(request.GET.get('p', 1)) except (PageNotAnInteger, EmptyPage): raise Http404() context['comment_list'] = context['page'].object_list context['is_paginated'] = context['page'].has_other_pages() return TemplateResponse(request, get_template('comment_list.html', context['object'], request.is_ajax()), context)
def handle_noargs(self, **kwargs): CommentModel = comments.get_model() for c in CommentModel.objects.exclude(user__isnull=True).sort('submit_date').iterator(): fc = FlatComment( site_id=c.site_id, content_type_id=c.content_type_id, object_id=c.object_pk, user_id=c.user_id, submit_date=c.submit_date, content=c.comment, is_public=c.is_public and not c.is_removed ) if fc.is_public: cl = CommentList(fc.content_type, fc.object_id) cl.post_comment(fc) sys.stdout.write('.'); sys.stdout.flush() else: fc.save(force_insert=True) sys.stdout.write('X'); sys.stdout.flush() sys.stdout.write('DONE\n'); sys.stdout.flush()
def list_comments(request, context, reverse=None): if reverse is None: reverse = show_reversed(request) clist = CommentList.for_object(context['object'], reverse) paginator = Paginator(clist, comments_settings.PAGINATE_BY) try: context['page'] = paginator.page(request.GET.get('p', 1)) except (PageNotAnInteger, EmptyPage): raise Http404() context['comment_list'] = context['page'].object_list context['is_paginated'] = context['page'].has_other_pages() return TemplateResponse( request, get_template('comment_list.html', context['object'], request.is_ajax()), context)
def setUp(self): super(CommentTestCase, self).setUp() self.content_object = ContentType.objects.get(pk=1) self.content_type = ContentType.objects.get_for_model(ContentType) self.user = User.objects.create_user('some_user', '*****@*****.**') self.comment_list = CommentList.for_object(self.content_object)
def test_get_comment_works(self): c = self._get_comment() c.post() clist = CommentList.for_object(self.content_object) tools.assert_equals(c, clist.get_comment(c.pk))
def test_get_comment_raises_does_not_exist_on_mismatched_content_object(self): c = self._get_comment() c.post() clist = CommentList.for_object(ContentType.objects.get(pk=2)) tools.assert_raises(FlatComment.DoesNotExist, clist.get_comment, c.pk)
def setUp(self): super(PublishableTestCase, self).setUp() create_basic_categories(self) create_and_place_a_publishable(self) self.comment_list = CommentList.for_object(self.publishable)
def test_reversed_getitem(self): comment_list = CommentList(self.content_type, 1, reversed=True) for i, c in zip(xrange(11), xrange(11)): tools.assert_equals(c, comment_list[i])