Esempio n. 1
0
def topic_publish(request, category_id=None):
    if category_id:
        Category.objects.get_public_or_404(pk=category_id)

    if request.method == 'POST':
        form = TopicForm(user=request.user, data=request.POST)
        cform = CommentForm(user=request.user, data=request.POST)
        pform = TopicPollForm(data=request.POST)
        pformset = TopicPollChoiceFormSet(can_delete=False, data=request.POST)

        if not request.is_limited and form.is_valid() and cform.is_valid() \
                and pform.is_valid() and pformset.is_valid():
            # wrap in transaction.atomic?
            topic = form.save()

            cform.topic = topic
            comment = cform.save()
            comment_posted.send(sender=comment.__class__, comment=comment, mentions=cform.mentions)

            # Create a poll only if we have choices
            if pformset.is_filled():
                pform.topic = topic
                poll = pform.save()
                pformset.instance = poll
                pformset.save()

            return redirect(topic.get_absolute_url())
    else:
        form = TopicForm(user=request.user, initial={'category': category_id, })
        cform = CommentForm()
        pform = TopicPollForm()
        pformset = TopicPollChoiceFormSet(can_delete=False)

    return render(request, 'foro/topic/topic_publish.html', {'form': form, 'cform': cform,
                                                               'pform': pform, 'pformset': pformset})
Esempio n. 2
0
def comment_update(request, pk):
    comment = Comment.objects.for_update_or_404(pk, request.user)

    if request.method == 'POST':
        form = CommentForm(data=request.POST, instance=comment)

        if form.is_valid():
            comment_pre_update.send(sender=comment.__class__, comment=Comment.objects.get(pk=comment.pk))
            comment = form.save()
            comment_post_update.send(sender=comment.__class__, comment=comment)
            return redirect(request.POST.get('next', comment.get_absolute_url()))
    else:
        form = CommentForm(instance=comment)

    return render(request, 'foro/comment/comment_update.html', {'form': form, })
Esempio n. 3
0
def render_comments_form(topic, next=None):
    form = CommentForm()
    return {
        'form': form,
        'topic_id': topic.pk,
        'next': next,
        'STATIC_URL': settings.STATIC_URL
    }
Esempio n. 4
0
def private_publish(request, user_id=None):
    if request.method == 'POST':
        tform = TopicForPrivateForm(user=request.user, data=request.POST)
        cform = CommentForm(user=request.user, data=request.POST)
        tpform = TopicPrivateManyForm(user=request.user, data=request.POST)

        if not request.is_limited and tform.is_valid() and cform.is_valid() and tpform.is_valid():
            # wrap in transaction.atomic?
            topic = tform.save()
            cform.topic = topic
            comment = cform.save()
            comment_posted.send(sender=comment.__class__, comment=comment, mentions=None)
            tpform.topic = topic
            topics_private = tpform.save_m2m()
            topic_private_post_create.send(sender=TopicPrivate, topics_private=topics_private, comment=comment)
            return redirect(topic.get_absolute_url())

    else:
        tform = TopicForPrivateForm()
        cform = CommentForm()
        initial = None

        if user_id:
            user = get_object_or_404(User, pk=user_id)
            initial = {'users': [user.username, ]}

        tpform = TopicPrivateManyForm(initial=initial)

    return render(request, 'foro/topic_private/private_publish.html', {'tform': tform,
                                                                         'cform': cform,
                                                                         'tpform': tpform})
Esempio n. 5
0
def comment_publish(request, topic_id, pk=None):
    topic = get_object_or_404(Topic.objects.for_access_open(request.user),
                              pk=topic_id)

    if request.method == 'POST':
        form = CommentForm(user=request.user, topic=topic, data=request.POST)

        if not request.is_limited and form.is_valid():
            comment = form.save()
            comment_posted.send(sender=comment.__class__,
                                comment=comment,
                                mentions=form.mentions)
            return redirect(
                request.POST.get('next', comment.get_absolute_url()))
    else:
        initial = None

        if pk:
            comment = get_object_or_404(Comment.objects.for_all(), pk=pk)
            quote = markdown.quotify(comment.comment, comment.user.username)
            initial = {
                'comment': quote,
            }

        form = CommentForm(initial=initial)

    return render(request, 'foro/comment/comment_publish.html', {
        'form': form,
        'topic': topic
    })
Esempio n. 6
0
def comment_publish(request, topic_id, pk=None):
    topic = get_object_or_404(Topic.objects.for_access_open(request.user),
                              pk=topic_id)

    if request.method == 'POST':
        form = CommentForm(user=request.user, topic=topic, data=request.POST)

        if not request.is_limited and form.is_valid():
            comment = form.save()
            comment_posted.send(sender=comment.__class__, comment=comment, mentions=form.mentions)
            return redirect(request.POST.get('next', comment.get_absolute_url()))
    else:
        initial = None

        if pk:
            comment = get_object_or_404(Comment.objects.for_all(), pk=pk)
            quote = markdown.quotify(comment.comment, comment.user.username)
            initial = {'comment': quote, }

        form = CommentForm(initial=initial)

    return render(request, 'foro/comment/comment_publish.html', {'form': form, 'topic': topic})
 def test_comment_markdown(self):
     form_data = {
         'comment': '**foro unicode: áéíóú** '
         '<script>alert();</script>',
     }
     form = CommentForm(data=form_data)
     self.assertEqual(form.is_valid(), True)
     form.user = self.user
     form.topic = self.topic
     comment = form.save()
     self.assertEqual(
         comment.comment_html, '<p><strong>foro unicode: áéíóú</strong> '
         '&lt;script&gt;alert();&lt;/script&gt;</p>')
Esempio n. 8
0
def topic_publish(request, category_id=None):
    if category_id:
        Category.objects.get_public_or_404(pk=category_id)

    if request.method == 'POST':
        form = TopicForm(user=request.user, data=request.POST)
        cform = CommentForm(user=request.user, data=request.POST)
        pform = TopicPollForm(data=request.POST)
        pformset = TopicPollChoiceFormSet(can_delete=False, data=request.POST)

        if not request.is_limited and form.is_valid() and cform.is_valid() \
                and pform.is_valid() and pformset.is_valid():
            # wrap in transaction.atomic?
            topic = form.save()

            cform.topic = topic
            comment = cform.save()
            comment_posted.send(sender=comment.__class__,
                                comment=comment,
                                mentions=cform.mentions)

            # Create a poll only if we have choices
            if pformset.is_filled():
                pform.topic = topic
                poll = pform.save()
                pformset.instance = poll
                pformset.save()

            return redirect(topic.get_absolute_url())
    else:
        form = TopicForm(user=request.user,
                         initial={
                             'category': category_id,
                         })
        cform = CommentForm()
        pform = TopicPollForm()
        pformset = TopicPollChoiceFormSet(can_delete=False)

    return render(request, 'foro/topic/topic_publish.html', {
        'form': form,
        'cform': cform,
        'pform': pform,
        'pformset': pformset
    })
Esempio n. 9
0
def comment_update(request, pk):
    comment = Comment.objects.for_update_or_404(pk, request.user)

    if request.method == 'POST':
        form = CommentForm(data=request.POST, instance=comment)

        if form.is_valid():
            comment_pre_update.send(sender=comment.__class__,
                                    comment=Comment.objects.get(pk=comment.pk))
            comment = form.save()
            comment_post_update.send(sender=comment.__class__, comment=comment)
            return redirect(
                request.POST.get('next', comment.get_absolute_url()))
    else:
        form = CommentForm(instance=comment)

    return render(request, 'foro/comment/comment_update.html', {
        'form': form,
    })
Esempio n. 10
0
 def test_comment_create(self):
     form_data = {
         'comment': 'foo',
     }
     form = CommentForm(data=form_data)
     self.assertEqual(form.is_valid(), True)