예제 #1
0
    def test_topics_delete_complete(self):
        topic2 = Topic.objects.create(name='Topic 2', forum=self.forum, user=self.superuser)

        post2 = Post(topic=topic2, user=self.superuser, body='post on topic2')
        post2.save()

        topics_delete_url = reverse('pybb:topics_delete')

        self.login_as(self.staff)

        response = self.client.post(topics_delete_url, data={
            'topic_ids': [self.topic.pk, topic2.pk],
            'form-TOTAL_FORMS': 2,
            'form-INITIAL_FORMS': 0,
            'form-0-topic': self.topic.pk,
            'form-0-confirm': True,
            'form-1-topic': topic2.pk,
            'form-1-confirm': True,
            'submit': 1
        })

        topic2 = Topic.objects.get(pk=topic2.pk)

        self.assertTrue(self.topic.delete)
        self.assertTrue(topic2.delete)

        self.assertRedirects(response, reverse('pybb:index'))
예제 #2
0
파일: forms.py 프로젝트: makinacorpus/pybbm
 def save(self, commit=True):
     if self.instance.pk:
         post = super(PostForm, self).save(commit=False)
         if self.user:
             post.user = self.user
         if post.topic.head == post:
             post.topic.name = self.cleaned_data['name']
             post.topic.updated = datetime.now()
             post.topic.save()
         post.save()
         return post
     allow_post = True
     if defaults.PYBB_PREMODERATION:
             allow_post = defaults.PYBB_PREMODERATION(self.user, self.cleaned_data['body'])
     if self.forum:
         topic = Topic(forum=self.forum,
                       user=self.user,
                       name=self.cleaned_data['name'])
         if not allow_post:
             topic.on_moderation = True
         topic.save()
     else:
         topic = self.topic
     post = Post(topic=topic, user=self.user, user_ip=self.ip,
                 body=self.cleaned_data['body'])
     if not allow_post:
         post.on_moderation = True
     post.save()
     return post
예제 #3
0
파일: tests.py 프로젝트: magatz/pybbm-1
    def test_topic_move_form_errors(self):
        forum = Forum.objects.create(name='temporary forum')

        topic = Topic.objects.create(name='move_topic', forum=forum, user=self.superuser)

        post = Post(topic=topic, user=self.superuser, body='move post')
        post.save()

        form = TopicMoveForm(topic=topic, data={
            'name': topic.name,
            'forum': forum.pk,
            'redirection_type': TopicRedirection.TYPE_PERMANENT_REDIRECT
        })

        self.assertFalse(form.is_valid())
        self.assertIn('name', form.errors)

        Topic.objects.create(name='move_topic', forum=self.forum, user=self.superuser)

        form = TopicMoveForm(topic=topic, data={
            'forum': self.forum.pk,
            'redirection_type': TopicRedirection.TYPE_PERMANENT_REDIRECT
        })

        self.assertFalse(form.is_valid())
        self.assertIn('name', form.errors)

        form = TopicMoveForm(topic=topic, data={
            'forum': self.forum.pk,
            'redirection_type': TopicRedirection.TYPE_EXPIRING_REDIRECT
        })

        self.assertFalse(form.is_valid())
        self.assertIn('expired', form.errors)
예제 #4
0
파일: tests.py 프로젝트: magatz/pybbm-1
    def test_topic_merge_formset(self):
        topic = Topic.objects.create(name='merged_topic', forum=self.forum, user=self.superuser)

        post = Post(topic=topic, user=self.superuser, body='merged post')
        post.save()

        topics = [topic]

        FormSet = get_topic_merge_formset(topics=topics)

        formset = FormSet()

        for i in range(len(topics)):
            self.assertEqual(formset.forms[i].topic, topics[i])

        formset = FormSet(data={
            'form-TOTAL_FORMS': 1,
            'form-INITIAL_FORMS': 0,
            'form-0-topic': self.topic.pk
        })

        self.assertTrue(formset.is_valid())

        for form in formset:
            new_topic = form.save()

        topic = Topic.objects.get(pk=topic.pk)

        self.assertTrue(topic.redirect)

        self.assertEqual(new_topic.redirections.count(), 1)
        self.assertEqual(Post.objects.get(pk=post.pk).topic, self.topic)
        self.assertEqual(topic.posts.count(), 0)
예제 #5
0
파일: test_quotes.py 프로젝트: thoas/pybbm
    def test_simple_quote(self):
        post = Post(user=self.staff, body='[quote="zeus;%d"]Super quote[/quote]' % self.post.pk, topic=self.topic)
        post.save()

        self.assertHTMLEqual(minify_html(post.body_html), u'<blockquote><div class="quote-author">        Posted by <a class="quote-author-name" href="/users/zeus/">zeus</a><a rel="nofollow" class="quote-message-link" href="/xfoo/{}-etopic#post{}"></a></div><div class="quote-message">        Super quote    </div></blockquote>'.format(self.post.topic_id, self.post.id))

        self.assertEqual(Quote.objects.filter(from_post=post).count(), 1)
예제 #6
0
파일: test_quotes.py 프로젝트: thoas/pybbm
    def test_multiple_quote_add(self):
        self.user
        self.login_as(self.staff)

        self.post

        post = Post(topic=self.topic, user=self.superuser, body='[b]do you want my pere nowel?[/b]')
        post.save()

        response = self.client.post(reverse('quote'), data={
            'topic_id': self.topic.pk,
            'post_id': post.pk
        })

        self.assertEqual(response.status_code, 302)

        response = self.client.get(reverse('pybb:post_create',
                                           kwargs={'topic_id': self.topic.id}) + '?quote_id=%d' % self.post.id)

        self.assertEqual(response.context['form'].initial['body'], u'[quote="zeus;{}"]bbcode [b]test[/b][/quote]\n\n[quote="oleiade;{}"][b]do you want my pere nowel?[/b][/quote]\n\n'.format(self.post.pk, post.pk))

        self.assertEqual(response.status_code, 200)

        response = self.client.post(reverse('quote'), data={
            'topic_id': self.topic.pk,
            'post_id': post.pk
        })

        self.assertEqual(response.status_code, 302)

        response = self.client.get(reverse('pybb:post_create',
                                           kwargs={'topic_id': self.topic.id}))
        self.assertTrue('body' not in response.context['form'].initial)
        self.assertEqual(response.status_code, 200)
예제 #7
0
파일: tests.py 프로젝트: magatz/pybbm-1
    def test_topic_merge_form(self):
        topic = Topic.objects.create(name='merged_topic', forum=self.forum, user=self.superuser)

        post = Post(topic=topic, user=self.superuser, body='merged post')
        post.save()

        form = TopicMergeForm(topic=topic, data={
            'topic': topic.pk
        })

        self.assertFalse(form.is_valid())
        self.assertIn('topic', form.errors)

        form = TopicMergeForm(topic=topic, data={
            'topic': self.topic.pk
        })

        self.assertTrue(form.is_valid())

        new_topic = form.save()

        topic = Topic.objects.get(pk=topic.pk)

        self.assertTrue(topic.redirect)

        self.assertEqual(new_topic.redirections.count(), 1)
        self.assertEqual(Post.objects.get(pk=post.pk).topic, self.topic)
        self.assertEqual(topic.posts.count(), 0)
	def create_other_threads(self):
		for page in LessonPage.objects.all():
			if not page.has_own_topic:
				new_topic, topic_created = Topic.objects.get_or_create(
					forum=Forum.objects.get(name='Lessons Forum'),
					name='Урок-' + str(page.lesson_number),
					user=User.objects.get(username='******'),
					slug='lecon' + str(page.lesson_number),
				)
				if topic_created:
					page.topic_id = new_topic.id
					self.stdout.write(str(page.lesson_number) + ' thread created')
					body = 'Не стесняйтесь задавать вопросы и комментировать.'
					new_post = Post(
						body=body,
						topic_id=new_topic.id,
						user_id=new_topic.user_id,
						user_ip='1.1.1.1',
						created=datetime.now(),
					)
					new_post.save()
					self.stdout.write(str(lesson_number) + ' post created')
				else:
					self.stdout.write(str(lesson_number) + ' thread not created')
				page.save()
예제 #9
0
    def test_topic_move_complete(self):
        topic = Topic(name='xtopic', forum=self.forum, user=self.user)
        topic.save()
        post = Post(topic=topic, user=self.user, body='one')
        post.save()

        topic_merge_url = reverse('pybb:topic_move')

        self.login_as(self.staff)

        name = 'new name for a topic'

        response = self.client.post(topic_merge_url, data={
            'topic_ids': [topic.pk],
            'form-TOTAL_FORMS': 1,
            'form-INITIAL_FORMS': 0,
            'form-0-name': name,
            'form-0-forum': self.forum.pk,
            'form-0-redirection_type': TopicRedirection.TYPE_PERMANENT_REDIRECT,
            'submit': 1
        })

        self.assertRedirects(response, reverse('pybb:index'))

        topic = Topic.objects.get(pk=topic.pk)

        self.assertTrue(topic.redirect)
        self.failUnless(topic.redirection is not None)

        redirection = topic.redirection

        new_topic = redirection.to_topic

        self.assertEqual(new_topic.name, name)
        self.assertEqual(new_topic.post_count, 1)
예제 #10
0
파일: tests.py 프로젝트: magatz/pybbm-1
    def test_topic_delete_formset(self):
        topic2 = Topic.objects.create(name='Topic 2', forum=self.forum, user=self.superuser)

        post2 = Post(topic=topic2, user=self.superuser, body='post on topic2')
        post2.save()

        topics = Topic.objects.filter(pk__in=[self.topic.pk, topic2.pk])

        FormSet = get_topic_delete_formset(topics=topics)

        formset = FormSet(data={
            'form-TOTAL_FORMS': 2,
            'form-INITIAL_FORMS': 0,
            'form-0-topic': self.topic.pk,
            'form-0-confirm': True,
            'form-1-topic': topic2.pk,
            'form-1-confirm': True,
        })

        self.assertTrue(formset.is_valid())

        for form in formset:
            topic = form.save()

            self.assertTrue(topic.deleted)
예제 #11
0
파일: tests.py 프로젝트: magatz/pybbm-1
    def test_topic_move_form_with_new_forum(self):
        forum = Forum.objects.create(name='temporary forum')

        topic = Topic.objects.create(name='move_topic', forum=forum, user=self.superuser)

        post = Post(topic=topic, user=self.superuser, body='move post')
        post.save()

        name = 'new name for temporary topic'

        form = TopicMoveForm(topic=topic, data={
            'name': name,
            'forum': self.forum.pk,
            'redirection_type': TopicRedirection.TYPE_PERMANENT_REDIRECT
        })

        self.assertTrue(form.is_valid())

        new_topic = form.save()

        topic = Topic.objects.get(pk=topic.pk)

        self.assertTrue(topic.redirect)

        self.failUnless(new_topic.slug is not None)
        self.assertEqual(new_topic.redirections.count(), 1)
        self.assertEqual(Post.objects.get(pk=post.pk).topic, new_topic)
        self.assertEqual(topic.posts.count(), 0)
예제 #12
0
    def test_topic_tracker_redirect_view(self):
        self.login_as(self.staff)

        self.post

        response = self.client.get(reverse('pybb:topic_tracker_redirect', kwargs={
            'topic_id': self.topic.pk
        }))

        self.assertRedirects(response, self.topic.get_absolute_url(), status_code=301)

        TopicReadTracker.objects.create(topic=self.topic,
                                        user=self.staff)

        response = self.client.get(reverse('pybb:topic_tracker_redirect', kwargs={
            'topic_id': self.topic.pk
        }))

        self.client.get(self.topic.get_absolute_url())

        post = Post(topic=self.topic, user=self.user, body='test')
        post.save()

        response = self.client.get(reverse('pybb:topic_tracker_redirect', kwargs={
            'topic_id': self.topic.pk
        }))

        self.assertRedirects(response, post.get_absolute_url(), status_code=301)
예제 #13
0
 def test_forum_updated(self):
     time.sleep(1)
     topic = Topic(name='xtopic', forum=self.forum, user=self.user)
     topic.save()
     post = Post(topic=topic, user=self.user, body='one')
     post.save()
     post = Post.objects.get(id=post.id)
     self.assertTrue(self.forum.updated == post.created)
예제 #14
0
    def test_delete_post_view_own_user(self):
        post = Post(topic=self.topic, user=self.user, body='test to delete')
        post.save()

        self.login()

        response = self.client.post(reverse('pybb:post_delete', args=[post.id]))
        self.assertEqual(response.status_code, 302)

        self.assertRaises(Post.DoesNotExist, lambda: Post.objects.get(pk=post.pk, deleted=False))

        self.assertNotEqual(post.deletion, None)
예제 #15
0
    def test_hidden(self):
        client = Client()
        parent = Forum(name='hcat', hidden=True)
        parent.save()

        forum_in_hidden = Forum(name='in_hidden', forum=parent)
        forum_in_hidden.save()

        topic_in_hidden = Topic(forum=forum_in_hidden, name='in_hidden', user=self.user)
        topic_in_hidden.save()

        forum_hidden = Forum(name='hidden', forum=self.parent_forum, hidden=True)
        forum_hidden.save()

        topic_hidden = Topic(forum=forum_hidden, name='hidden', user=self.user)
        topic_hidden.save()

        post_hidden = Post(topic=topic_hidden, user=self.user, body='hidden')
        post_hidden.save()

        post_in_hidden = Post(topic=topic_in_hidden, user=self.user, body='hidden')
        post_in_hidden.save()

        self.assertFalse(parent.id in [c.id for c in client.get(reverse('pybb:index')).context['forums']])
        self.assertEqual(client.get(parent.get_absolute_url()).status_code, 302)
        self.assertEqual(client.get(forum_in_hidden.get_absolute_url()).status_code, 302)
        self.assertEqual(client.get(topic_in_hidden.get_absolute_url()).status_code, 302)

        self.assertNotContains(client.get(reverse('pybb:index')), forum_hidden.get_absolute_url())
        self.assertNotContains(client.get(reverse('pybb:feed_topics')), topic_hidden.get_absolute_url())
        self.assertNotContains(client.get(reverse('pybb:feed_topics')), topic_in_hidden.get_absolute_url())

        self.assertNotContains(client.get(reverse('pybb:feed_posts')), post_hidden.get_absolute_url())
        self.assertNotContains(client.get(reverse('pybb:feed_posts')), post_in_hidden.get_absolute_url())
        self.assertEqual(client.get(forum_hidden.get_absolute_url()).status_code, 302)
        self.assertEqual(client.get(topic_hidden.get_absolute_url()).status_code, 302)

        client.login(username='******', password='******')

        self.assertTrue(parent.id in [c.id for c in client.get(reverse('pybb:index')).context['forums']])

        self.assertContains(client.get(reverse('pybb:index')), forum_hidden.get_absolute_url())

        self.assertEqual(client.get(parent.get_absolute_url()).status_code, 200)
        self.assertEqual(client.get(forum_in_hidden.get_absolute_url()).status_code, 200)
        self.assertEqual(client.get(topic_in_hidden.get_absolute_url()).status_code, 200)
        self.assertEqual(client.get(forum_hidden.get_absolute_url()).status_code, 200)
        self.assertEqual(client.get(topic_hidden.get_absolute_url()).status_code, 200)
        self.user.is_staff = True
        self.user.save()
        self.assertTrue(parent.id in [c.id for c in client.get(reverse('pybb:index')).context['forums']])
        self.assertContains(client.get(reverse('pybb:index')), forum_hidden.get_absolute_url())

        self.assertEqual(client.get(parent.get_absolute_url()).status_code, 200)
        self.assertEqual(client.get(forum_in_hidden.get_absolute_url()).status_code, 200)
        self.assertEqual(client.get(topic_in_hidden.get_absolute_url()).status_code, 200)
        self.assertEqual(client.get(forum_hidden.get_absolute_url()).status_code, 200)
        self.assertEqual(client.get(topic_hidden.get_absolute_url()).status_code, 200)
예제 #16
0
    def test_undelete_post_view_own_user(self):
        post = Post(topic=self.topic, user=self.user, body='test to delete', deleted=True)
        post.save()

        PostDeletion.objects.create(post=post, user=self.user)

        self.login()

        response = self.client.post(reverse('pybb:post_delete', args=[post.id]))
        self.assertEqual(response.status_code, 302)

        post = Post.objects.get(pk=post.pk)

        self.assertRaises(PostDeletion.DoesNotExist, lambda: post.deletion)

        self.assertFalse(post.deleted)
예제 #17
0
    def test_topic_merge_view(self):
        topic = Topic(name='xtopic', forum=self.forum, user=self.user)
        topic.save()
        post = Post(topic=topic, user=self.user, body='one')
        post.save()

        topic_merge_url = reverse('pybb:topic_merge')

        self.login_as(self.staff)

        response = self.client.post(topic_merge_url, data={
            'topic_ids': [topic.pk]
        })

        self.assertEqual(response.status_code, 200)
        self.assertEqual(len(response.context['form'].forms), 1)
        self.assertTemplateUsed(response, 'pybb/topic/merge.html')
예제 #18
0
파일: tests.py 프로젝트: magatz/pybbm-1
    def test_multiple_mentions(self):
        self.user
        self.staff
        self.superuser

        multiple_mentions = Post(topic=self.topic, user=self.user, body='@thoas is right and @oleiade or @zeus!')

        processor = MentionProcessor(multiple_mentions.body, obj=multiple_mentions)

        self.assertEqual(processor.render(), u'[mention=%d]thoas[/mention] is right and [mention=%d]oleiade[/mention] or [mention=%d]zeus[/mention]!' % (
            self.staff.pk,
            self.superuser.pk,
            self.user.pk
        ))
        multiple_mentions.save()

        self.assertEqual(Mention.objects.filter(post=multiple_mentions).count(), 3)
	def handle(self, *args, **options):
		_parser = Markdown(
			extensions=[
				'forum.mdownx.nofollowlinks',
				'markdown.extensions.nl2br',
				'pymdownx.extra',
				'pymdownx.emoji',
				'pymdownx.tasklist',
				'pymdownx.details',
				'pymdownx.superfences',
				'pymdownx.details',
			],
			safe_mode='escape',
		)
		table = self.import_from_csv('narodny_konspekt.csv')
		for lesson_number in table:
			table[lesson_number] = self.rewrite(table[lesson_number])
			if isinstance(table[lesson_number], float):
				pass
			else:
				try:
					page = LessonPage.objects.get(lesson_number=lesson_number)
				except:
					self.stdout.write("Lesson page doesn't exist")
					break
				page.has_own_topic = True
				new_topic, topic_created = Topic.objects.get_or_create(
					forum=Forum.objects.get(name='Lessons Forum'),
					name='Урок-' + str(page.lesson_number),
					user=User.objects.get(username='******'),
					slug='lecon' + str(page.lesson_number),
				)
				page.topic_id = new_topic.id
				self.stdout.write(str(lesson_number) + ' thread created')
				body = table[lesson_number]
				new_post = Post(
					body=body,
					topic_id=new_topic.id,
					user_id=new_topic.user_id,
					user_ip='1.1.1.1',
					created=datetime.now(tz=pytz.timezone(settings.TIME_ZONE)),
				)
				new_post.save()
				self.stdout.write(str(lesson_number) + ' post created')
				page.save()
예제 #20
0
 def test_topic_deletion(self):
     topic = Topic(name='xtopic', forum=self.forum, user=self.user)
     topic.save()
     post = Post(topic=topic, user=self.user, body='one')
     post.save()
     post = Post(topic=topic, user=self.user, body='two')
     post.save()
     post.delete()
     Topic.objects.get(id=topic.id)
     Forum.objects.get(id=self.forum.id)
     topic.delete()
     Forum.objects.get(id=self.forum.id)
예제 #21
0
    def test_delete_post_view_superuser(self):
        post = Post(topic=self.topic, user=self.user, body='test to delete')
        post.save()
        self.user.is_superuser = True
        self.user.save()
        self.login()
        response = self.client.post(reverse('pybb:post_delete', args=[post.id]), follow=True)
        self.assertEqual(response.status_code, 200)
        self.assertEqual(Topic.objects.filter(id=self.topic.id).count(), 1)
        self.assertEqual(Forum.objects.filter(id=self.forum.id).count(), 1)

        response = self.client.post(reverse('pybb:post_delete', args=[self.post.id]), follow=True)
        self.assertEqual(response.status_code, 200)

        self.assertEqual(Post.objects.filter(id=self.post.id,
                                             deleted=False).count(), 0)
        self.assertEqual(Topic.objects.filter(id=self.topic.id,
                                              deleted=False).count(), 0)
        self.assertEqual(Forum.objects.filter(id=self.forum.id).count(), 1)
예제 #22
0
    def test_topic_merge_complete(self):
        topic = Topic(name='xtopic', forum=self.forum, user=self.user)
        topic.save()
        post = Post(topic=topic, user=self.user, body='one')
        post.save()

        topic_merge_url = reverse('pybb:topic_merge')

        self.login_as(self.staff)

        response = self.client.post(topic_merge_url, data={
            'topic_ids': [topic.pk],
            'form-TOTAL_FORMS': 1,
            'form-INITIAL_FORMS': 0,
            'form-0-topic': self.topic.pk,
            'submit': 1
        })

        self.assertRedirects(response, reverse('pybb:index'))
예제 #23
0
파일: forms.py 프로젝트: DylannCordel/pybbm
    def save(self, commit=True):
        if self.instance.pk:
            post = super(PostForm, self).save(commit=False)
            if self.user:
                post.user = self.user
            if post.topic.head == post:
                post.topic.name = self.cleaned_data['name']
                if self.may_create_poll:
                    post.topic.poll_type = self.cleaned_data['poll_type']
                    post.topic.poll_question = self.cleaned_data['poll_question']
                post.topic.updated = tznow()
                if commit:
                    post.topic.save()
            post.updated = tznow()
            if commit:
                post.save()
            return post, post.topic

        allow_post = True
        if defaults.PYBB_PREMODERATION:
            allow_post = defaults.PYBB_PREMODERATION(self.user, self.cleaned_data['body'])
        if self.forum:
            topic = Topic(
                forum=self.forum,
                user=self.user,
                name=self.cleaned_data['name'],
                poll_type=self.cleaned_data.get('poll_type', Topic.POLL_TYPE_NONE),
                poll_question=self.cleaned_data.get('poll_question', None),
                slug=self.cleaned_data.get('slug', None),
            )
            if not allow_post:
                topic.on_moderation = True
        else:
            topic = self.topic
        post = Post(user=self.user, user_ip=self.ip, body=self.cleaned_data['body'])
        if not allow_post:
            post.on_moderation = True
        if commit:
            topic.save()
            post.topic = topic
            post.save()
        return post, topic
예제 #24
0
파일: tests.py 프로젝트: magatz/pybbm-1
    def test_compute(self):
        # initials
        self.assertEqual(self.forum.forum_count, 0)
        self.assertEqual(self.parent_forum.forum_count, 1)

        self.topic
        self.post

        parent_forum = Forum.objects.get(pk=self.parent_forum.pk)

        self.assertEqual(parent_forum.topic_count, 1)
        self.assertEqual(parent_forum.post_count, 1)
        self.assertEqual(self.forum.topic_count, 1)
        self.assertEqual(self.forum.post_count, 1)

        forum = Forum.objects.create(name='bar', description='bar', forum=self.forum)

        topic = Topic(name='bar', forum=forum, user=self.user)
        topic.save()

        post = Post(topic=topic, user=self.user, body='my new post')
        post.save()

        self.assertEqual(topic.post_count, 1)
        self.assertEqual(forum.topic_count, 1)

        self.assertEqual(Forum.objects.get(pk=self.forum.pk).topic_count, 2)
        self.assertEqual(Forum.objects.get(pk=self.parent_forum.pk).topic_count, 2)

        new_topic = Topic(name='foo', forum=self.forum, user=self.user)
        new_topic.save()

        new_post = Post(topic=topic, user=self.user, body='my new post')
        new_post.save()

        self.assertEqual(Forum.objects.get(pk=self.forum.pk).topic_count, 3)
        self.assertEqual(Forum.objects.get(pk=self.parent_forum.pk).topic_count, 3)

        post.mark_as_deleted(commit=True)

        self.assertEqual(Forum.objects.get(pk=self.forum.pk).topic_count, 2)
        self.assertEqual(Forum.objects.get(pk=self.parent_forum.pk).topic_count, 2)

        self.assertTrue(Topic.objects.get(pk=topic.pk).deleted)

        self.assertEqual(Forum.objects.get(pk=forum.pk).topic_count, 0)
        self.assertEqual(Forum.objects.get(pk=forum.pk).post_count, 0)

        new_topic.mark_as_deleted()

        self.assertEqual(Forum.objects.get(pk=self.forum.pk).topic_count, 1)
        self.assertEqual(Forum.objects.get(pk=self.parent_forum.pk).topic_count, 1)
예제 #25
0
    def test_undelete_post_topic_view(self):
        post = Post(topic=self.topic, user=self.user, body='test to delete', deleted=True)
        post.save()
        post.mark_as_deleted(user=self.user)

        self.login()

        self.post.mark_as_deleted(user=self.user)

        response = self.client.post(reverse('pybb:post_delete', args=[self.post.id]))

        self.assertEqual(response.status_code, 302)

        self.post = Post.objects.get(pk=self.post.pk)

        self.assertRaises(PostDeletion.DoesNotExist, lambda: self.post.deletion)

        self.assertFalse(self.post.deleted)

        self.assertTrue(Post.objects.get(pk=post.pk).deleted)
예제 #26
0
파일: tests.py 프로젝트: magatz/pybbm-1
    def test_posts_move_new_topic_form(self):
        forum = Forum.objects.create(name='temporary forum')

        topic = Topic.objects.create(name='move_topic', forum=forum, user=self.superuser)

        post = Post(topic=topic, user=self.superuser, body='move post')
        post.save()

        posts = Post.objects.filter(id__in=[self.post.pk, post.pk])

        form = PostsMoveNewTopicForm(posts=posts, user=self.superuser, data={
            'name': 'New topic name',
            'forum': self.forum.pk,
        })

        self.assertTrue(form.is_valid())

        new_topic = form.save()

        self.assertEqual(new_topic.posts.count(), 2)
예제 #27
0
 def test_subscription(self):
     user = get_user_model().objects.create_user(username='******', password='******', email='*****@*****.**')
     client = Client()
     client.login(username='******', password='******')
     response = client.post(reverse('pybb:subscription_create'), data={
         'topic_id': self.topic.pk,
         'type': Subscription.TYPE_INSTANT_ALERT
     })
     self.assertEqual(response.status_code, 302)
     self.assertRedirects(response, self.topic.get_absolute_url())
     self.assertTrue(user in list(self.topic.subscribers.all()))
     new_post = Post(topic=self.topic, user=self.user, body='test subscribtion юникод')
     new_post.save()
     self.assertTrue([msg for msg in mail.outbox if new_post.get_absolute_url() in msg.body])
     response = client.post(reverse('pybb:subscription_delete'), data={
         'topic_id': self.topic.pk
     })
     self.assertEqual(response.status_code, 302)
     self.assertRedirects(response, self.topic.get_absolute_url())
     self.assertTrue(user not in list(self.topic.subscribers.all()))
예제 #28
0
파일: tests.py 프로젝트: magatz/pybbm-1
    def test_posts_move_existing_topic_form(self):
        self.post

        forum = Forum.objects.create(name='temporary forum')

        topic = Topic.objects.create(name='move_topic', forum=forum, user=self.superuser)

        post = Post(topic=topic, user=self.superuser, body='move post')
        post.save()

        posts = Post.objects.filter(id__in=[post.pk])

        form = PostsMoveExistingTopicForm(posts=posts, data={
            'topic': self.topic.pk,
        })

        self.assertTrue(form.is_valid())

        form.save()

        self.assertEqual(self.topic.posts.count(), 2)
예제 #29
0
    def save(self, *args, **kwargs):
        if self.forum:
            topic_is_new = True
            topic = Topic(forum=self.forum,
                          user=self.user,
                          name=self.cleaned_data['name'])
            topic.save(*args, **kwargs)
        else:
            topic_is_new = False
            topic = self.topic

        post = Post(topic=topic,
                    user=self.user,
                    markup=self.cleaned_data['markup'],
                    body=self.cleaned_data['body'])
        post.save(*args, **kwargs)

        if pybb_settings.ATTACHMENT_ENABLE:
            self.save_attachment(post, self.cleaned_data['attachment'])

        return post
예제 #30
0
 def _get_post_form(self, user=None, topic=None):
     if topic is None:
         topic = Topic(forum=self.forum, name="Test topic", user=self.user if not user else user)
     post = Post(topic=topic, user=self.user if not user else user)
     form = PostForm(instance=post, user=self.user if not user else user, topic=topic)
     form.cleaned_data = {
         'name': "Test topic",
         'body': "Test",
         'poll_type': 0,
         'poll_question': None,
     }
     return form
예제 #31
0
    def test_undelete_post_topic_view(self):
        post = Post(topic=self.topic,
                    user=self.user,
                    body='test to delete',
                    deleted=True)
        post.save()
        post.mark_as_deleted(user=self.user)

        self.login()

        self.post.mark_as_deleted(user=self.user)

        response = self.client.post(
            reverse('pybb:post_delete', args=[self.post.id]))

        self.assertEqual(response.status_code, 302)

        self.post = Post.objects.get(pk=self.post.pk)

        self.assertRaises(PostDeletion.DoesNotExist,
                          lambda: self.post.deletion)

        self.assertFalse(self.post.deleted)

        self.assertTrue(Post.objects.get(pk=post.pk).deleted)
예제 #32
0
파일: forms.py 프로젝트: gpetukhov/pybb
    def save(self):

        if self.forum:
            topic = Topic(forum=self.forum,
                          user=self.user,
                          name=self.cleaned_data['name'])
            topic.save()
        else:
            topic = self.topic

        post = Post(topic=topic,
                    user=self.user,
                    user_ip=self.ip,
                    markup=self.user.pybb_profile.markup,
                    body=self.cleaned_data['body'])
        post.save()

        if settings.PYBB_ATTACHMENT_ENABLE:
            for f in self.files:
                self.save_attachment(post, self.files[f])

        return post
예제 #33
0
 def save(self, commit=True):
     if self.instance.pk:
         post = super(PostForm, self).save(commit=False)
         if self.user:
             post.user = self.user
         if post.topic.head == post:
             post.topic.name = self.cleaned_data['name']
             post.topic.poll_type = self.cleaned_data['poll_type']
             post.topic.poll_question = self.cleaned_data['poll_question']
             post.topic.updated = tznow()
             post.topic.save()
         post.save()
         return post
     allow_post = True
     if defaults.PYBB_PREMODERATION:
         allow_post = defaults.PYBB_PREMODERATION(self.user,
                                                  self.cleaned_data['body'])
     if self.forum:
         topic = Topic(
             forum=self.forum,
             user=self.user,
             name=self.cleaned_data['name'],
             poll_type=self.cleaned_data['poll_type'],
             poll_question=self.cleaned_data['poll_question'],
         )
         if not allow_post:
             topic.on_moderation = True
         topic.save()
     else:
         topic = self.topic
     post = Post(topic=topic,
                 user=self.user,
                 user_ip=self.ip,
                 body=self.cleaned_data['body'],
                 reply_to=self.cleaned_data['reply_to'])
     if not allow_post:
         post.on_moderation = True
     post.save()
     return post
예제 #34
0
파일: forms.py 프로젝트: xq2537/pybb
    def save(self):

        if self.forum:
            topic = Topic(forum=self.forum, user=self.user, name=self.cleaned_data["name"])
            topic.save()
        else:
            topic = self.topic

        post = Post(
            topic=topic,
            user=self.user,
            user_ip=self.ip,
            markup=self.user.pybb_profile.markup,
            body=self.cleaned_data["body"],
        )
        post.save()

        if settings.PYBB_ATTACHMENT_ENABLE:
            for f in self.files:
                self.save_attachment(post, self.files[f])

        return post
예제 #35
0
 def test_topic_updated(self):
     topic = Topic(name='etopic', forum=self.forum, user=self.user)
     topic.save()
     time.sleep(1)
     post = Post(topic=topic, user=self.user, body='bbcode [b]test[b]')
     post.save()
     client = Client()
     response = client.get(self.forum.get_absolute_url())
     self.assertEqual(response.context['topic_list'][0], topic)
     time.sleep(1)
     post = Post(topic=self.topic, user=self.user, body='bbcode [b]test[b]')
     post.save()
     client = Client()
     response = client.get(self.forum.get_absolute_url())
     self.assertEqual(response.context['topic_list'][0], self.topic)
예제 #36
0
    def test_topic_move_complete(self):
        topic = Topic(name='xtopic', forum=self.forum, user=self.user)
        topic.save()
        post = Post(topic=topic, user=self.user, body='one')
        post.save()

        topic_merge_url = reverse('pybb:topic_move')

        self.login_as(self.staff)

        name = 'new name for a topic'

        response = self.client.post(
            topic_merge_url,
            data={
                'topic_ids': [topic.pk],
                'form-TOTAL_FORMS': 1,
                'form-INITIAL_FORMS': 0,
                'form-0-name': name,
                'form-0-forum': self.forum.pk,
                'form-0-redirection_type':
                TopicRedirection.TYPE_PERMANENT_REDIRECT,
                'submit': 1
            })

        self.assertRedirects(response, reverse('pybb:index'))

        topic = Topic.objects.get(pk=topic.pk)

        self.assertTrue(topic.redirect)
        self.failUnless(topic.redirection is not None)

        redirection = topic.redirection

        new_topic = redirection.to_topic

        self.assertEqual(new_topic.name, name)
        self.assertEqual(new_topic.post_count, 1)
예제 #37
0
    def test_delete_post_view_superuser(self):
        post = Post(topic=self.topic, user=self.user, body='test to delete')
        post.save()
        self.user.is_superuser = True
        self.user.save()
        self.login()
        response = self.client.post(reverse('pybb:post_delete',
                                            args=[post.id]),
                                    follow=True)
        self.assertEqual(response.status_code, 200)
        self.assertEqual(Topic.objects.filter(id=self.topic.id).count(), 1)
        self.assertEqual(Forum.objects.filter(id=self.forum.id).count(), 1)

        response = self.client.post(reverse('pybb:post_delete',
                                            args=[self.post.id]),
                                    follow=True)
        self.assertEqual(response.status_code, 200)

        self.assertEqual(
            Post.objects.filter(id=self.post.id, deleted=False).count(), 0)
        self.assertEqual(
            Topic.objects.filter(id=self.topic.id, deleted=False).count(), 0)
        self.assertEqual(Forum.objects.filter(id=self.forum.id).count(), 1)
예제 #38
0
    def test_topics_delete_view(self):
        topic2 = Topic.objects.create(name='Topic 2', forum=self.forum, user=self.superuser)

        post2 = Post(topic=topic2, user=self.superuser, body='post on topic2')
        post2.save()

        topics_delete_url = reverse('pybb:topics_delete')

        self.login_as(self.staff)

        response = self.client.post(topics_delete_url, data={
            'topic_ids': [self.topic.pk, topic2.pk],
            'topic': topic2,
            'confirm': True
        })

        topic2 = Topic.objects.get(pk=topic2.pk)

        self.assertTrue(topic2.delete)

        self.assertEqual(response.status_code, 200)
        self.assertEqual(len(response.context['form'].forms), 2)
        self.assertTemplateUsed(response, 'pybb/topic/delete.html')
예제 #39
0
class MentionsTest(TestCase):
    def test_processors(self):
        self.user
        self.staff

        self.post = Post(topic=self.topic, user=self.user, body='@thoas is right!')
        processor = MentionProcessor(self.post.body, obj=self.post)

        self.assertEqual(processor.render(), u'[mention=%d]thoas[/mention] is right!' % self.staff.pk)

        self.post.save()

        body = markup(self.post.body, obj=self.post)

        self.assertEqual(body, '@<a class="mention" href="/users/thoas/">thoas</a> is right!')

        self.assertEqual(Mention.objects.filter(post=self.post,
                                                from_user=self.post.user,
                                                to_user=self.staff).count(), 1)

    def test_multiple_mentions(self):
        self.user
        self.staff
        self.superuser

        multiple_mentions = Post(topic=self.topic, user=self.user, body='@thoas is right and @oleiade or @zeus!')

        processor = MentionProcessor(multiple_mentions.body, obj=multiple_mentions)

        self.assertEqual(processor.render(), u'[mention=%d]thoas[/mention] is right and [mention=%d]oleiade[/mention] or [mention=%d]zeus[/mention]!' % (
            self.staff.pk,
            self.superuser.pk,
            self.user.pk
        ))
        multiple_mentions.save()

        self.assertEqual(Mention.objects.filter(post=multiple_mentions).count(), 3)
예제 #40
0
    def test_posts_move_new_topic_form(self):
        forum = Forum.objects.create(name='temporary forum')

        topic = Topic.objects.create(name='move_topic',
                                     forum=forum,
                                     user=self.superuser)

        post = Post(topic=topic, user=self.superuser, body='move post')
        post.save()

        posts = Post.objects.filter(id__in=[self.post.pk, post.pk])

        form = PostsMoveNewTopicForm(posts=posts,
                                     user=self.superuser,
                                     data={
                                         'name': 'New topic name',
                                         'forum': self.forum.pk,
                                     })

        self.assertTrue(form.is_valid())

        new_topic = form.save()

        self.assertEqual(new_topic.posts.count(), 2)
예제 #41
0
    def test_topic_merge_formset(self):
        topic = Topic.objects.create(name='merged_topic',
                                     forum=self.forum,
                                     user=self.superuser)

        post = Post(topic=topic, user=self.superuser, body='merged post')
        post.save()

        topics = [topic]

        FormSet = get_topic_merge_formset(topics=topics)

        formset = FormSet()

        for i in range(len(topics)):
            self.assertEqual(formset.forms[i].topic, topics[i])

        formset = FormSet(
            data={
                'form-TOTAL_FORMS': 1,
                'form-INITIAL_FORMS': 0,
                'form-0-topic': self.topic.pk
            })

        self.assertTrue(formset.is_valid())

        for form in formset:
            new_topic = form.save()

        topic = Topic.objects.get(pk=topic.pk)

        self.assertTrue(topic.redirect)

        self.assertEqual(new_topic.redirections.count(), 1)
        self.assertEqual(Post.objects.get(pk=post.pk).topic, self.topic)
        self.assertEqual(topic.posts.count(), 0)
예제 #42
0
    def test_posts_move_existing_topic_form(self):
        self.post

        forum = Forum.objects.create(name='temporary forum')

        topic = Topic.objects.create(name='move_topic',
                                     forum=forum,
                                     user=self.superuser)

        post = Post(topic=topic, user=self.superuser, body='move post')
        post.save()

        posts = Post.objects.filter(id__in=[post.pk])

        form = PostsMoveExistingTopicForm(posts=posts,
                                          data={
                                              'topic': self.topic.pk,
                                          })

        self.assertTrue(form.is_valid())

        form.save()

        self.assertEqual(self.topic.posts.count(), 2)
예제 #43
0
    def test_double_quote(self):
        post_1 = Post(user=self.staff,
                      body='[quote="%s;%d"]%s[/quote]' %
                      (self.post.user.username, self.post.pk, self.post.body),
                      topic=self.topic)
        post_1.save()

        post_2 = Post(user=self.superuser,
                      body='[quote="%s;%d"]%s[/quote]' %
                      (post_1.user.username, post_1.pk, post_1.body),
                      topic=self.topic)
        post_2.save()
        self.assertHTMLEqual(
            minify_html(post_2.body_html),
            u'<blockquote><div class="quote-author">        Posted by <a class="quote-author-name" href="/users/thoas/">thoas</a><a rel="nofollow" class="quote-message-link" href="/xfoo/{}-etopic#post{}"></a></div><div class="quote-message"><blockquote><div class="quote-author">        Posted by <a class="quote-author-name" href="/users/zeus/">zeus</a><a rel="nofollow" class="quote-message-link" href="/xfoo/{}-etopic#post{}"></a></div><div class="quote-message">        bbcode <strong>test</strong></div></blockquote></div></blockquote>'
            .format(post_1.topic_id, post_1.id, self.post.topic_id,
                    self.post.id))

        self.assertEqual(Quote.objects.filter(from_post=post_2).count(), 2)
예제 #44
0
    def save(self, commit=True):
        if self.instance.pk:
            post = super(PostForm, self).save(commit=False)
            if self.user:
                post.user = self.user
            if post.topic.head == post:
                post.topic.name = self.cleaned_data['name']
                if self.may_create_poll:
                    post.topic.poll_type = self.cleaned_data['poll_type']
                    post.topic.poll_question = self.cleaned_data[
                        'poll_question']
                post.topic.updated = tznow()
                if commit:
                    post.topic.save()
            post.updated = tznow()
            if commit:
                post.save()
                # Subscribe to topic
                if self.cleaned_data['subscribe']:
                    post.topic.subscribers.add(self.user)
                else:
                    post.topic.subscribers.remove(self.user)
            return post, post.topic

        allow_post = True
        if defaults.PYBB_PREMODERATION:
            allow_post = defaults.PYBB_PREMODERATION(self.user,
                                                     self.cleaned_data['body'])
        if self.forum:
            topic = Topic(
                forum=self.forum,
                user=self.user,
                name=self.cleaned_data['name'],
                poll_type=self.cleaned_data.get('poll_type',
                                                Topic.POLL_TYPE_NONE),
                poll_question=self.cleaned_data.get('poll_question', None),
                slug=self.cleaned_data.get('slug', None),
            )
            if not allow_post:
                topic.on_moderation = True
        else:
            topic = self.topic
        post = Post(user=self.user,
                    user_ip=self.ip,
                    body=self.cleaned_data['body'])
        if not allow_post:
            post.on_moderation = True
        if commit:
            topic.save()
            post.topic = topic
            post.save()
        return post, topic
예제 #45
0
    def clean(self):
        """Checks if Topic is open and user's post cooldown.
        """
        if self.topic.is_locked and not \
           pybb_can_administer_forum(self.user, self.topic.forum):
            # Disallow non-moderators
            raise ValidationError(
                _('The topic you are trying to post in is closed'))

        if not self.instance or not getattr(self.instance, 'pk', None):
            # Adding new post: check cooldown
            cooldown_remaining = Post.get_user_cooldown(self.user)

            if cooldown_remaining > 0:
                raise ValidationError(
                    _n(
                        'You have to wait %(seconds)d more second before you can place another post',
                        'You have to wait %(seconds)d more seconds before you can place another post',
                        cooldown_remaining) % {'seconds': cooldown_remaining})

        return super(PostForm, self).clean()
예제 #46
0
 def test_post_deletion(self):
     post = Post(topic=self.topic, user=self.user, body='bbcode [b]test[b]')
     post.save()
     post.delete()
     Topic.objects.get(id=self.topic.id)
     Forum.objects.get(id=self.forum.id)
예제 #47
0
    def test_read_tracking_multi_user(self):
        self.post

        topic_1 = self.topic
        topic_2 = Topic(name='topic_2', forum=self.forum, user=self.user)
        topic_2.save()

        Post(topic=topic_2, user=self.user, body='one').save()

        user_ann = get_user_model().objects.create_user('ann', 'ann@localhost', 'ann')
        client_ann = Client()
        client_ann.login(username='******', password='******')

        user_bob = get_user_model().objects.create_user('bob', 'bob@localhost', 'bob')
        client_bob = Client()
        client_bob.login(username='******', password='******')

        #  Two topics, each with one post. everything is unread, so the db should reflect that:
        self.assertEqual(TopicReadTracker.objects.all().count(), 0)
        self.assertEqual(ForumReadTracker.objects.all().count(), 0)

        #  user_ann reads topic_1, she should get one topic read tracker, there should be no forum read trackers
        client_ann.get(topic_1.get_absolute_url())
        self.assertEqual(TopicReadTracker.objects.all().count(), 1)
        self.assertEqual(TopicReadTracker.objects.filter(user=user_ann).count(), 1)
        self.assertEqual(TopicReadTracker.objects.filter(user=user_ann, topic=topic_1).count(), 1)
        self.assertEqual(ForumReadTracker.objects.all().count(), 0)

        #  user_bob reads topic_1, he should get one topic read tracker, there should be no forum read trackers
        client_bob.get(topic_1.get_absolute_url())
        self.assertEqual(TopicReadTracker.objects.all().count(), 2)
        self.assertEqual(TopicReadTracker.objects.filter(user=user_bob).count(), 1)
        self.assertEqual(TopicReadTracker.objects.filter(user=user_bob, topic=topic_1).count(), 1)
        self.assertEqual(ForumReadTracker.objects.all().count(), 0)

        #  user_bob reads topic_2, he should get a forum read tracker,
        #  there should be no topic read trackers for user_bob
        client_bob.get(topic_2.get_absolute_url())
        self.assertEqual(TopicReadTracker.objects.all().count(), 1)
        self.assertEqual(ForumReadTracker.objects.all().count(), 1)
        self.assertEqual(ForumReadTracker.objects.filter(user=user_bob).count(), 1)
        self.assertEqual(ForumReadTracker.objects.filter(user=user_bob, forum=self.forum).count(), 1)

        #  user_ann creates topic_3, there should be a new topic read tracker in the db
        topic_create_url = reverse('pybb:topic_create', kwargs={'forum_id': self.forum.id})
        response = client_ann.get(topic_create_url)
        values = self.get_form_values(response)
        values['body'] = 'topic_3'
        values['name'] = 'topic_3'
        values['poll_type'] = 0
        response = client_ann.post(topic_create_url, data=values, follow=True)

        self.assertEqual(TopicReadTracker.objects.all().count(), 2)
        self.assertEqual(TopicReadTracker.objects.filter(user=user_ann).count(), 2)
        self.assertEqual(ForumReadTracker.objects.all().count(), 1)

        topic_3 = Topic.objects.order_by('-updated')[0]
        self.assertEqual(topic_3.name, 'topic_3')

        #  user_ann posts to topic_1, a topic they've already read, no new trackers should be created (existing one is updated)
        post_create_url = reverse('pybb:post_create', kwargs={'topic_id': topic_1.id})
        response = client_ann.get(post_create_url)
        values = self.get_form_values(response)
        values['body'] = 'test tracking'
        response = client_ann.post(post_create_url, values, follow=True)
        self.assertEqual(TopicReadTracker.objects.all().count(), 2)
        self.assertEqual(TopicReadTracker.objects.filter(user=user_ann).count(), 2)
        self.assertEqual(ForumReadTracker.objects.all().count(), 1)

        self.assertEqual(ForumReadTracker.objects.all().count(), 1)
        previous_time = ForumReadTracker.objects.all()[0].time_stamp

        # user bob reads topic 1 which he already read, topic tracker recreated, forum tracker untouched (topic 3 still unread)
        client_bob.get(topic_1.get_absolute_url())
        self.assertEqual(ForumReadTracker.objects.all().count(), 1)
        self.assertEqual(ForumReadTracker.objects.all()[0].time_stamp, previous_time)
        self.assertEqual(TopicReadTracker.objects.all().count(), 3)
        self.assertEqual(TopicReadTracker.objects.filter(user=user_bob).count(), 1)

        self.assertEqual(ForumReadTracker.objects.all().count(), 1)
        previous_time = ForumReadTracker.objects.all()[0].time_stamp

        # user bob reads topic 3, topic tracker purged, forum tracker updated
        client_bob.get(topic_3.get_absolute_url())
        self.assertEqual(ForumReadTracker.objects.all().count(), 1)
        self.assertGreater(ForumReadTracker.objects.all()[0].time_stamp, previous_time)
        self.assertEqual(TopicReadTracker.objects.all().count(), 2)
        self.assertEqual(TopicReadTracker.objects.filter(user=user_bob).count(), 0)
예제 #48
0
    def test_compute(self):
        # initials
        self.assertEqual(self.forum.forum_count, 0)
        self.assertEqual(self.parent_forum.forum_count, 1)

        self.topic
        self.post

        parent_forum = Forum.objects.get(pk=self.parent_forum.pk)

        self.assertEqual(parent_forum.topic_count, 1)
        self.assertEqual(parent_forum.post_count, 1)
        self.assertEqual(self.forum.topic_count, 1)
        self.assertEqual(self.forum.post_count, 1)

        # Add (forum --> topic --> post) in self.forum
        forum = Forum.objects.create(name='bar',
                                     description='bar',
                                     forum=self.forum)

        topic = Topic(name='bar', forum=forum, user=self.user)
        topic.save()

        post = Post(topic=topic, user=self.user, body='my new post')
        post.save()

        self.assertEquals(Topic.objects.get(pk=topic.pk).first_post, post)
        self.assertEqual(topic.post_count, 1)
        self.assertEqual(forum.topic_count, 1)

        self.assertEqual(Forum.objects.get(pk=self.forum.pk).post_count, 2)
        self.assertEqual(Forum.objects.get(pk=self.forum.pk).topic_count, 2)
        self.assertEqual(
            Forum.objects.get(pk=self.parent_forum.pk).post_count, 2)
        self.assertEqual(
            Forum.objects.get(pk=self.parent_forum.pk).topic_count, 2)

        # Add (topic --> post) in self.forum
        new_topic = Topic(name='foo', forum=self.forum, user=self.user)
        new_topic.save()

        new_post = Post(topic=new_topic, user=self.user, body='my new post')
        new_post.save()
        self.assertEquals(
            Topic.objects.get(pk=new_topic.pk).first_post, new_post)

        self.assertEqual(Forum.objects.get(pk=self.forum.pk).post_count, 3)
        self.assertEqual(Forum.objects.get(pk=self.forum.pk).topic_count, 3)
        self.assertEqual(
            Forum.objects.get(pk=self.parent_forum.pk).post_count, 3)
        self.assertEqual(
            Forum.objects.get(pk=self.parent_forum.pk).topic_count, 3)

        # delete post in (self.parent_forum --> self.forum --> forum --> topic)
        post.mark_as_deleted(commit=True)

        self.assertTrue(Topic.objects.get(pk=topic.pk).deleted)
        self.assertEquals(Topic.objects.get(pk=topic.pk).first_post, post)
        self.assertTrue(Topic.objects.get(pk=topic.pk).first_post.deleted)

        self.assertEqual(Forum.objects.get(pk=forum.pk).topic_count, 0)
        self.assertEqual(Forum.objects.get(pk=forum.pk).post_count, 0)

        self.assertEqual(Forum.objects.get(pk=self.forum.pk).topic_count, 2)
        self.assertEqual(
            Forum.objects.get(pk=self.parent_forum.pk).topic_count, 2)

        # delete new_topic in (self.parent_forum --> self.forum)
        new_topic.mark_as_deleted()

        self.assertTrue(Topic.objects.get(pk=new_topic.pk).deleted)
        self.assertEquals(
            Topic.objects.get(pk=new_topic.pk).first_post, new_post)
        self.assertTrue(Topic.objects.get(pk=new_topic.pk).first_post.deleted)

        self.assertEqual(Forum.objects.get(pk=self.forum.pk).topic_count, 1)
        self.assertEqual(
            Forum.objects.get(pk=self.parent_forum.pk).topic_count, 1)

        # undelete new_topic in (self.parent_forum --> self.forum)
        new_topic.mark_as_undeleted()

        self.assertFalse(Topic.objects.get(pk=new_topic.pk).deleted)
        self.assertEquals(
            Topic.objects.get(pk=new_topic.pk).first_post, new_post)
        self.assertFalse(Topic.objects.get(pk=new_topic.pk).first_post.deleted)

        self.assertEqual(Forum.objects.get(pk=self.forum.pk).topic_count, 2)
        self.assertEqual(
            Forum.objects.get(pk=self.parent_forum.pk).topic_count, 2)

        # undelete post in (self.parent_forum --> self.forum --> forum --> topic)
        post.mark_as_undeleted(commit=True)

        self.assertFalse(Topic.objects.get(pk=topic.pk).deleted)
        self.assertEquals(Topic.objects.get(pk=topic.pk).first_post, post)
        self.assertFalse(Topic.objects.get(pk=topic.pk).first_post.deleted)

        self.assertEqual(Forum.objects.get(pk=forum.pk).post_count, 1)
        self.assertEqual(Forum.objects.get(pk=forum.pk).topic_count, 1)
        self.assertEqual(Forum.objects.get(pk=self.forum.pk).post_count, 3)
        self.assertEqual(Forum.objects.get(pk=self.forum.pk).topic_count, 3)
        self.assertEqual(
            Forum.objects.get(pk=self.parent_forum.pk).post_count, 3)
        self.assertEqual(
            Forum.objects.get(pk=self.parent_forum.pk).topic_count, 3)
예제 #49
0
    def test_compute_active_members(self):
        # Initials
        self.assertEquals(self.parent_forum.member_count, 0)
        self.assertEquals(self.forum.member_count, 0)
        self.assertEquals(self.topic.member_count, 0)

        # Add first post by self.user (count += 1)
        user_post_1 = self.post
        self.assertEquals(self.parent_forum.member_count, 1)
        self.assertEquals(self.forum.member_count, 1)
        self.assertEquals(self.topic.member_count, 1)

        # Add first post by self.staff (count += 1)
        staff_post_1 = Post(topic=self.topic,
                            user=self.staff,
                            body='my new post')
        staff_post_1.save()
        self.assertEquals(self.parent_forum.member_count, 2)
        self.assertEquals(self.forum.member_count, 2)
        self.assertEquals(self.topic.member_count, 2)

        # Add second post by self.user (count += 0)
        user_post_2 = Post(topic=self.topic,
                           user=self.user,
                           body='my new post')
        user_post_2.save()
        self.assertEquals(self.parent_forum.member_count, 2)
        self.assertEquals(self.forum.member_count, 2)
        self.assertEquals(self.topic.member_count, 2)

        # Delete staff_post_1 (count -= 1)
        staff_post_1.mark_as_deleted(commit=True)
        self.assertEquals(self.parent_forum.member_count, 1)
        self.assertEquals(self.forum.member_count, 1)
        self.assertEquals(self.topic.member_count, 1)

        # Restore staff_post_1 (count += 1)
        staff_post_1.mark_as_undeleted(commit=True)
        self.assertEquals(self.parent_forum.member_count, 2)
        self.assertEquals(self.forum.member_count, 2)
        self.assertEquals(self.topic.member_count, 2)

        # Delete user_post_2 (count -= 0)
        user_post_2.mark_as_deleted(commit=True)
        self.assertEquals(self.parent_forum.member_count, 2)
        self.assertEquals(self.forum.member_count, 2)
        self.assertEquals(self.topic.member_count, 2)

        # Add third post by self.user in parent_forum (count += 0)
        new_topic = Topic(name='foo2', forum=self.parent_forum, user=self.user)
        new_topic.save()
        user_post_3 = Post(topic=new_topic, user=self.user, body='my new post')
        user_post_3.save()
        self.assertEquals(self.parent_forum.member_count, 2)
        self.assertEquals(new_topic.member_count, 1)
        self.assertEquals(self.forum.member_count, 2)
        self.assertEquals(self.topic.member_count, 2)
예제 #50
0
 def create_post(self, topic):
     return Post(topic=topic,
                 user=self.user,
                 user_ip=self.ip,
                 body=self.cleaned_data['body'],
                 hash=self.cleaned_data['hash'])
예제 #51
0
    def test_hidden(self):
        client = Client()
        parent = Forum(name='hcat', hidden=True)
        parent.save()

        forum_in_hidden = Forum(name='in_hidden', forum=parent)
        forum_in_hidden.save()

        topic_in_hidden = Topic(forum=forum_in_hidden,
                                name='in_hidden',
                                user=self.user)
        topic_in_hidden.save()

        forum_hidden = Forum(name='hidden',
                             forum=self.parent_forum,
                             hidden=True)
        forum_hidden.save()

        topic_hidden = Topic(forum=forum_hidden, name='hidden', user=self.user)
        topic_hidden.save()

        post_hidden = Post(topic=topic_hidden, user=self.user, body='hidden')
        post_hidden.save()

        post_in_hidden = Post(topic=topic_in_hidden,
                              user=self.user,
                              body='hidden')
        post_in_hidden.save()

        self.assertFalse(parent.id in [
            c.id for c in client.get(reverse('pybb:index')).context['forums']
        ])
        self.assertEqual(
            client.get(parent.get_absolute_url()).status_code, 302)
        self.assertEqual(
            client.get(forum_in_hidden.get_absolute_url()).status_code, 302)
        self.assertEqual(
            client.get(topic_in_hidden.get_absolute_url()).status_code, 302)

        self.assertNotContains(client.get(reverse('pybb:index')),
                               forum_hidden.get_absolute_url())
        self.assertNotContains(client.get(reverse('pybb:feed_topics')),
                               topic_hidden.get_absolute_url())
        self.assertNotContains(client.get(reverse('pybb:feed_topics')),
                               topic_in_hidden.get_absolute_url())

        self.assertNotContains(client.get(reverse('pybb:feed_posts')),
                               post_hidden.get_absolute_url())
        self.assertNotContains(client.get(reverse('pybb:feed_posts')),
                               post_in_hidden.get_absolute_url())
        self.assertEqual(
            client.get(forum_hidden.get_absolute_url()).status_code, 302)
        self.assertEqual(
            client.get(topic_hidden.get_absolute_url()).status_code, 302)

        client.login(username='******', password='******')

        self.assertTrue(parent.id in [
            c.id for c in client.get(reverse('pybb:index')).context['forums']
        ])

        self.assertContains(client.get(reverse('pybb:index')),
                            forum_hidden.get_absolute_url())

        self.assertEqual(
            client.get(parent.get_absolute_url()).status_code, 200)
        self.assertEqual(
            client.get(forum_in_hidden.get_absolute_url()).status_code, 200)
        self.assertEqual(
            client.get(topic_in_hidden.get_absolute_url()).status_code, 200)
        self.assertEqual(
            client.get(forum_hidden.get_absolute_url()).status_code, 200)
        self.assertEqual(
            client.get(topic_hidden.get_absolute_url()).status_code, 200)
        self.user.is_staff = True
        self.user.save()
        self.assertTrue(parent.id in [
            c.id for c in client.get(reverse('pybb:index')).context['forums']
        ])
        self.assertContains(client.get(reverse('pybb:index')),
                            forum_hidden.get_absolute_url())

        self.assertEqual(
            client.get(parent.get_absolute_url()).status_code, 200)
        self.assertEqual(
            client.get(forum_in_hidden.get_absolute_url()).status_code, 200)
        self.assertEqual(
            client.get(topic_in_hidden.get_absolute_url()).status_code, 200)
        self.assertEqual(
            client.get(forum_hidden.get_absolute_url()).status_code, 200)
        self.assertEqual(
            client.get(topic_hidden.get_absolute_url()).status_code, 200)
예제 #52
0
    def test_staff(self):
        client = Client()
        parent = Forum(name='hcat', staff=True)
        parent.save()

        forum_in_staff = Forum(name='in_staff', forum=parent)
        forum_in_staff.save()

        topic_in_staff = Topic(forum=forum_in_staff,
                               name='in_staff',
                               user=self.user)
        topic_in_staff.save()

        forum_staff = Forum(name='staff', forum=self.parent_forum, staff=True)
        forum_staff.save()

        topic_staff = Topic(forum=forum_staff, name='staff', user=self.user)
        topic_staff.save()

        post_staff = Post(topic=topic_staff, user=self.user, body='staff')
        post_staff.save()

        post_in_staff = Post(topic=topic_in_staff,
                             user=self.user,
                             body='staff')
        post_in_staff.save()

        self.assertFalse(parent.id in [
            c.id for c in client.get(reverse('pybb:index')).context['forums']
        ])
        self.assertEqual(
            client.get(parent.get_absolute_url()).status_code, 404)
        self.assertEqual(
            client.get(forum_in_staff.get_absolute_url()).status_code, 404)
        self.assertEqual(
            client.get(topic_in_staff.get_absolute_url()).status_code, 404)

        self.assertEqual(
            client.get(forum_staff.get_absolute_url()).status_code, 404)
        self.assertEqual(
            client.get(topic_staff.get_absolute_url()).status_code, 404)

        client.login(username='******', password='******')

        self.assertFalse(parent.id in [
            c.id for c in client.get(reverse('pybb:index')).context['forums']
        ])

        self.assertNotContains(client.get(reverse('pybb:index')),
                               forum_staff.get_absolute_url())

        self.assertEqual(
            client.get(parent.get_absolute_url()).status_code, 404)
        self.assertEqual(
            client.get(forum_in_staff.get_absolute_url()).status_code, 404)
        self.assertEqual(
            client.get(topic_in_staff.get_absolute_url()).status_code, 404)
        self.assertEqual(
            client.get(forum_staff.get_absolute_url()).status_code, 404)
        self.assertEqual(
            client.get(topic_staff.get_absolute_url()).status_code, 404)

        self.user.is_staff = True
        self.user.save()

        self.assertTrue(parent.id in [
            c.id for c in client.get(reverse('pybb:index')).context['forums']
        ])

        self.assertEqual(
            client.get(parent.get_absolute_url()).status_code, 200)
        self.assertEqual(
            client.get(forum_in_staff.get_absolute_url()).status_code, 200)
        self.assertEqual(
            client.get(topic_in_staff.get_absolute_url()).status_code, 200)
        self.assertEqual(
            client.get(forum_staff.get_absolute_url()).status_code, 200)
        self.assertEqual(
            client.get(topic_staff.get_absolute_url()).status_code, 200)
예제 #53
0
    def save(self, commit=True):
        related_object = None
        if self.cleaned_data.get('related_object', None):
            try:
                related_object_pk = int(self.cleaned_data['related_object'].pk)
            except ValueError:
                pass
            else:
                related_object = self.related_object_type.get_object_for_this_type(
                    pk=related_object_pk)

        if self.instance.pk:
            now = datetime.now()
            topic = super(TopicForm, self).save(commit=False)
            topic.related_object = related_object
            if 'name' in self.fields.keys():
                topic.name = self.cleaned_data['name']
            if 'importance' in self.fields.keys():
                topic.importance = self.cleaned_data['importance']
            topic.updated = now

            if self.cleaned_data.get('subscribe', False):
                topic.subscribers.add(self.user)
            else:
                topic.subscribers.remove(self.user)

            if commit:
                topic.save()

            post = topic.head
            post.body = self.cleaned_data['body']
            post.updated = now

            if commit:
                if self.post_form:
                    # Post form has some additional save logic (add edit user,
                    # topic subscription)
                    post = self.post_form.save(commit=True)
                else:
                    post.save()

            return post

        topic = Topic(forum=self.forum,
                      user=self.user,
                      name=self.cleaned_data['name'])
        if self.cleaned_data.has_key('importance'):
            topic.importance = self.cleaned_data['importance']
        topic.related_object = related_object
        if commit:
            topic.save()
            if self.cleaned_data['subscribe']:
                topic.subscribers.add(self.user)
                topic.save()

        post = Post(topic=topic,
                    user=self.user,
                    user_ip=self.ip,
                    body=self.cleaned_data['body'])
        if commit:
            post.save()
        return post