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_topic_active_view_pinned(self):
"""
Show globally pinned topics first, regular pinned topics are shown as regular topics
"""
category = utils.create_category()
topic_a = utils.create_topic(category=category)
topic_b = utils.create_topic(category=category, is_pinned=True)
topic_c = utils.create_topic(category=category)
topic_d = utils.create_topic(category=category, is_globally_pinned=True)
# show globally pinned first
Topic.objects.filter(pk=topic_d.pk).update(last_active=timezone.now() - datetime.timedelta(days=10))
response = self.client.get(reverse('spirit:topic:index-active'))
self.assertEqual(list(response.context['topics']), [topic_d, topic_c, topic_b, topic_a])
def test_topic_has_new_comments(self):
"""
Should return True when there are new replies
"""
utils.login(self)
category = utils.create_category()
topic = utils.create_topic(category=category, user=self.user, comment_count=1)
self.assertFalse(Topic.objects.filter(pk=topic.pk).with_bookmarks(self.user).first().has_new_comments)
CommentBookmark.objects.create(topic=topic, user=self.user, comment_number=1)
self.assertFalse(Topic.objects.filter(pk=topic.pk).with_bookmarks(self.user).first().has_new_comments)
Topic.objects.filter(pk=topic.pk).update(comment_count=2)
self.assertTrue(Topic.objects.filter(pk=topic.pk).with_bookmarks(self.user).first().has_new_comments)
def test_topic_active_view_bookmark(self):
"""
topics with bookmarks
"""
utils.login(self)
category = utils.create_category()
topic = utils.create_topic(category=category, user=self.user)
bookmark = CommentBookmark.objects.create(topic=topic, user=self.user)
user2 = utils.create_user()
CommentBookmark.objects.create(topic=topic, user=user2)
topic2 = utils.create_topic(category=category, user=self.user)
CommentBookmark.objects.create(topic=topic2, user=self.user)
ten_days_ago = timezone.now() - datetime.timedelta(days=10)
Topic.objects.filter(pk=topic2.pk).update(last_active=ten_days_ago)
response = self.client.get(reverse('spirit:topic:index-active'))
self.assertEqual(list(response.context['topics']), [topic, topic2])
self.assertEqual(response.context['topics'][0].bookmark, bookmark)
def test_comment_publish_on_closed_topic(self):
"""
should not be able to create a comment on a closed topic
"""
Topic.objects.filter(pk=self.topic.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, 404)
def test_topic_get_bookmark_url(self):
"""
Should return the bookmark url
"""
utils.login(self)
category = utils.create_category()
topic = utils.create_topic(category=category, user=self.user)
# No bookmark
topic_with_bookmark = Topic.objects.filter(pk=topic.pk).with_bookmarks(self.user).first()
self.assertEqual(topic_with_bookmark.get_bookmark_url(), topic_with_bookmark.get_absolute_url())
# With bookmark
CommentBookmark.objects.create(topic=topic, user=self.user, comment_number=1)
topic_with_bookmark2 = Topic.objects.filter(pk=topic.pk).with_bookmarks(self.user).first()
self.assertEqual(topic_with_bookmark2.get_bookmark_url(), topic_with_bookmark2.bookmark.get_absolute_url())
# With bookmark and new comment
Topic.objects.filter(pk=topic.pk).update(comment_count=2)
topic_with_bookmark3 = Topic.objects.filter(pk=topic.pk).with_bookmarks(self.user).first()
self.assertEqual(topic_with_bookmark3.get_bookmark_url(), topic_with_bookmark3.bookmark.get_new_comment_url())
def test_topic_active_view_bookmark(self):
"""
topics with bookmarks
"""
utils.login(self)
category = utils.create_category()
topic = utils.create_topic(category=category, user=self.user)
bookmark = CommentBookmark.objects.create(topic=topic, user=self.user)
user2 = utils.create_user()
CommentBookmark.objects.create(topic=topic, user=user2)
topic2 = utils.create_topic(category=category, user=self.user)
CommentBookmark.objects.create(topic=topic2, user=self.user)
ten_days_ago = timezone.now() - datetime.timedelta(days=10)
Topic.objects.filter(pk=topic2.pk).update(last_active=ten_days_ago)
response = self.client.get(reverse('spirit:topic:index-active'))
self.assertEqual(list(response.context['topics']), [topic, topic2])
self.assertEqual(response.context['topics'][0].bookmark, bookmark)
def test_profile_topics_order(self):
"""
topics ordered by date
"""
Topic.objects.all().delete()
category = utils.create_category()
topic_a = utils.create_topic(category=category, user=self.user2)
topic_b = utils.create_topic(category=category, user=self.user2)
topic_c = utils.create_topic(category=category, user=self.user2)
Topic.objects.filter(pk=topic_a.pk).update(date=timezone.now() - datetime.timedelta(days=10))
Topic.objects.filter(pk=topic_c.pk).update(date=timezone.now() - datetime.timedelta(days=5))
utils.login(self)
response = self.client.get(reverse("spirit:user:topics", kwargs={'pk': self.user2.pk,
'slug': self.user2.st.slug}))
self.assertEqual(list(response.context['topics']), [topic_b, topic_c, topic_a])
def _moderate(request, pk, field_name, to_value, action=None):
topic = get_object_or_404(Topic, pk=pk)
if request.method == 'POST':
count = (Topic.objects
.filter(pk=pk)
.exclude(**{field_name: to_value})
.update(**{
field_name: to_value,
'reindex_at': timezone.now()}))
if count and action is not None:
Comment.create_moderation_action(
user=request.user,
topic=topic,
action=action)
return redirect(request.POST.get(
'next', topic.get_absolute_url()))
else:
return render(
def decrease_comment_count(self):
# todo: update last_active to last() comment
Topic.objects\
.filter(pk=self.pk)\
.update(comment_count=F('comment_count') - 1)
def comment_posted_handler(sender, comment, **kwargs):
Topic.objects.filter(pk=comment.topic.pk)\
.update(comment_count=F('comment_count') + 1, last_active=timezone.now())