Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_comment_publish_on_closed_cateory(self):
"""
should be able to create a comment on a closed category (if topic is not closed)
"""
Category.objects.filter(pk=self.category.pk).update(is_closed=True)
utils.login(self)
form_data = {'comment': 'foobar', }
response = self.client.post(reverse('spirit:comment:publish', kwargs={'topic_id': self.topic.pk, }),
form_data)
self.assertEqual(response.status_code, 302)
self.assertEqual(len(Comment.objects.all()), 1)
def test_comments_move(self):
comment = utils.create_comment(user=self.user, topic=self.topic)
comment2 = utils.create_comment(user=self.user, topic=self.topic)
to_topic = utils.create_topic(category=self.category)
form_data = {'topic': to_topic.pk,
'comments': [comment.pk, comment2.pk], }
form = CommentMoveForm(topic=self.topic, data=form_data)
self.assertEqual(form.is_valid(), True)
self.assertEqual(form.save(), list(Comment.objects.filter(topic=to_topic)))
def publish(request, topic_id, pk=None):
user = request.user
topic = get_object_or_404(
Topic.objects.opened().for_access(user),
pk=topic_id)
if request.method == 'POST':
form = CommentForm(user=user, topic=topic, data=request.POST)
if not request.is_limited() and form.is_valid():
if not user.st.update_post_hash(form.get_comment_hash()):
# Hashed comment may have not been saved yet
return redirect(
request.POST.get('next', None) or
Comment
.get_last_for_topic(topic_id)
.get_absolute_url())
comment = form.save()
comment_posted(comment=comment, mentions=form.mentions)
return redirect(request.POST.get('next', comment.get_absolute_url()))
else:
initial = None
if pk: # todo: move to form
comment = get_object_or_404(Comment.objects.for_access(user=user), pk=pk)
quote = markdown.quotify(comment.comment, comment.user.st.nickname)
initial = {'comment': quote}
form = CommentForm(initial=initial)
def create_moderation_action(cls, user, topic, action):
# TODO: better comment_html text (map to actions), use default language
return cls.objects.create(
user=user,
topic=topic,
action=action,
comment="action",
comment_html="action"
)
def increase_user_profile_comment_count(sender, instance, created, **kwargs):
if created and not instance.topic.category.is_private:
instance.user.st.increase_comment_count()
post_save.connect(increase_user_profile_comment_count, sender=Comment, dispatch_uid='Comment:increase_user_profile_comment_count')
def decrease_user_profile_comment_count(sender, instance, **kwargs):
if not instance.topic.category.is_private:
instance.user.st.decrease_comment_count()
post_delete.connect(decrease_user_profile_comment_count, sender=Comment, dispatch_uid='Comment:decrease_user_profile_comment_count')
def comments(request, pk, slug):
# todo: test with_polls!
user_comments = (
Comment.objects
.filter(user_id=pk)
.visible()
.with_polls(user=request.user)
.select_related('topic'))
return _activity(
request, pk, slug,
queryset=user_comments,
template='spirit/user/profile_comments.html',
reverse_to='spirit:user:detail',
context_name='comments',
per_page=config.comments_per_page)
def update(request, pk):
topic = Topic.objects.for_update_or_404(pk, request.user)
if request.method == 'POST':
form = TopicForm(user=request.user, data=request.POST, instance=topic)
category_id = topic.category_id
if form.is_valid():
topic = form.save()
if topic.category_id != category_id:
Comment.create_moderation_action(
user=request.user, topic=topic, action=MOVED)
return redirect(request.POST.get('next', topic.get_absolute_url()))
else:
form = TopicForm(user=request.user, instance=topic)
context = {'form': form}
return render(request, 'spirit/topic/update.html', context)
def find(request, pk):
comment = get_object_or_404(Comment.objects.select_related('topic'), pk=pk)
comment_number = Comment.objects.filter(topic=comment.topic, date__lte=comment.date).count()
url = paginator.get_url(
comment.topic.get_absolute_url(),
comment_number,
config.comments_per_page,
'page')
return redirect(url)
def detail(request, pk, slug):
topic = Topic.objects.get_public_or_404(pk, request.user)
if topic.slug != slug:
return HttpResponsePermanentRedirect(topic.get_absolute_url())
utils.topic_viewed(request=request, topic=topic)
comments = (
Comment.objects
.for_topic(topic=topic)
.with_likes(user=request.user)
.with_polls(user=request.user)
.order_by('date'))
comments = paginate(
comments,
per_page=config.comments_per_page,
page_number=request.GET.get('page', 1))
context = {
'topic': topic,
'comments': comments}
return render(request, 'spirit/topic/detail.html', context)