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_private_publishvalidate(self):
"""
Should validate all forms even when errors
"""
self.assertEqual(len(Topic.objects.all()), 0)
utils.login(self)
no_data = {}
response = self.client.post(reverse('spirit:topic:private:publish'), no_data)
self.assertEqual(len(Topic.objects.all()), 0)
self.assertTrue(bool(response.context['tform'].errors))
self.assertTrue(bool(response.context['cform'].errors))
self.assertTrue(bool(response.context['tpform'].errors))
self.assertEqual(len(list(response.context['messages'])), 0)
# No rate-limit
form_data = {'comment': 'foo', 'title': 'foobar', 'users': self.user2.username}
self.client.post(reverse('spirit:topic:private:publish'), form_data)
self.assertEqual(len(Topic.objects.all()), 1)
self.assertEqual(response.status_code, 404)
# removed subcategory
Category.objects.all().update(is_removed=False)
subcategory = utils.create_category(parent=self.category, is_removed=True)
topic2 = utils.create_topic(subcategory)
utils.login(self)
form_data = {'comment': 'foobar', }
response = self.client.post(reverse('spirit:comment:publish', kwargs={'topic_id': topic2.pk, }),
form_data)
self.assertEqual(response.status_code, 404)
# removed topic
Category.objects.all().update(is_removed=False)
Topic.objects.all().update(is_removed=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_private_publish(self):
"""
POST, create private topic
"""
self.assertEqual(len(Topic.objects.all()), 0)
utils.login(self)
form_data = {'comment': 'foo', 'title': 'foobar', 'users': self.user2.username}
response = self.client.post(reverse('spirit:topic:private:publish'), form_data)
private = TopicPrivate.objects.last()
expected_url = private.get_absolute_url()
self.assertRedirects(response, expected_url, status_code=302)
self.assertEqual(len(Topic.objects.all()), 1)
# ratelimit
form_data['title'] = 'new foobar'
form_data['comment'] = 'new foo'
response = self.client.post(reverse('spirit:topic:private:publish'), form_data)
self.assertEqual(response.status_code, 200)
self.assertEqual(len(Topic.objects.all()), 1)
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])
utils.login(self)
category = utils.create_category()
topic = utils.create_topic(category=category, user=self.user, comment_count=1)
self.assertEqual(
Topic.objects.filter(pk=topic.pk).with_bookmarks(self.user).first().new_comments_count,
0
)
CommentBookmark.objects.create(topic=topic, user=self.user, comment_number=1)
self.assertEqual(
Topic.objects.filter(pk=topic.pk).with_bookmarks(self.user).first().new_comments_count,
0
)
Topic.objects.filter(pk=topic.pk).update(comment_count=2)
self.assertEqual(
Topic.objects.filter(pk=topic.pk).with_bookmarks(self.user).first().new_comments_count,
1
)
# topic.comment_count greater than bookmark.comment_number should return 0
Topic.objects.filter(pk=topic.pk).update(comment_count=0)
self.assertEqual(
Topic.objects.filter(pk=topic.pk).with_bookmarks(self.user).first().new_comments_count,
0
)
def deleted(request):
# Private topics cant be deleted, closed or pinned so we are ok
return _index(
request,
queryset=Topic.objects.filter(is_removed=True),
template='spirit/topic/admin/deleted.html'
)
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)
def index(request):
# TODO: add button to clean up read topics? or read all?
# redirect to first page if empty
topics = (
Topic.objects
.for_access(user=request.user)
.for_unread(user=request.user)
.with_bookmarks(user=request.user))
page = paginate(
request,
query_set=topics,
lookup_field="last_active",
page_var='p')
context = {
'page': page,
'next_page': to_page_key(**page.next_page())
}
return render(request, 'spirit/topic/unread/index.html', context)
def topics(request, pk, slug):
user_topics = (
Topic.objects
.visible()
.with_bookmarks(user=request.user)
.filter(user_id=pk)
.select_related('category', 'user__st')
.order_by('-date', '-pk'))
return _activity(
request, pk, slug,
queryset=user_topics,
template='spirit/user/profile_topics.html',
reverse_to='spirit:user:topics',
context_name='topics',
per_page=config.topics_per_page)