Exemplo n.º 1
0
def api_add_post(request, opening_post_id):
    """
    Adds a post and return the JSON response for it
    """

    opening_post = get_object_or_404(Post, id=opening_post_id)

    status = STATUS_OK
    errors = []

    if request.method == "POST":
        form = PostForm(request.POST, request.FILES, error_class=PlainErrorList)
        form.session = request.session

        # if form.need_to_ban:
        #    # Ban user because he is suspected to be a bot
        #    _ban_current_user(request)
        #    status = STATUS_ERROR
        if form.is_valid():
            ThreadView().new_post(request, form, opening_post, html_response=False)
        else:
            status = STATUS_ERROR
            errors = form.as_json_errors()

    response = {"status": status, "errors": errors}

    return HttpResponse(content=json.dumps(response))
Exemplo n.º 2
0
def reply_topic(request, pk, topic_pk):
    topic = get_object_or_404(Topic, board__pk=pk, pk=topic_pk)
    if request.method == 'POST':
        form = PostForm(request.POST)
        if form.is_valid():
            post = form.save(commit=False)
            post.topic = topic
            post.created_by = request.user
            post.save()

            topic.last_update = timezone.now()
            topic.save()

            topic_url = reverse('topic_posts',
                                kwargs={
                                    'pk': pk,
                                    'topic_pk': topic_pk
                                })
            topic_post_url = '{url}?page={page}#{id}'.format(
                url=topic_url, id=post.pk, page=topic.get_page_count())

            return redirect(topic_post_url)
    else:
        form = PostForm
    return render(request, 'reply_topic.html', {'topic': topic, 'form': form})
Exemplo n.º 3
0
def reply_topic(request, pk, topic_pk):
    topic = get_object_or_404(Topic, board__pk=pk, pk=topic_pk)
    form = PostForm(request.POST)
    if form.is_valid():
        post = form.save(commit=False)
        post.topic = topic
        post.created_by = request.user
        post.save()

        topic.last_updated = timezone.now()
        topic.save()

        topic_url = reverse('topic_posts',
                            kwargs={
                                'pk': pk,
                                'topic_pk': topic_pk
                            })
        topic_post_url = '{url}?page={page}#{id}'.format(
            url=topic_url, id=post.pk, page=topic.get_page_count())
        send_reply_notification.delay(post.topic.starter.username,
                                      post.topic.starter.email,
                                      post.topic.subject,
                                      post.created_by.username)
        return redirect(topic_post_url)
    return render(request, 'reply_topic.html', {'topic': topic, 'form': form})
Exemplo n.º 4
0
def reply_topic(request, pk, topic_pk):
    topic = get_object_or_404(Topic, pk=topic_pk, board__pk=pk)
    if request.method == 'POST':
        form = PostForm(request.POST)
        post = form.save(commit=False)
        post.topic = topic
        post.created_by = request.user
        post.save()
        return redirect('topic_posts', pk=pk, topic_pk=topic_pk)
    else:
        form = PostForm()

    return render(request, 'reply_topic.html', {'topic': topic, 'form': form})
Exemplo n.º 5
0
    def post(self, request, post_id, mode=MODE_NORMAL):
        opening_post = get_object_or_404(Post, id=post_id)

        # If this is not OP, don't show it as it is
        if not opening_post.is_opening():
            raise Http404

        if not opening_post.get_thread().archived:
            form = PostForm(request.POST, request.FILES,
                            error_class=PlainErrorList)
            form.session = request.session

            if form.is_valid():
                return self.new_post(request, form, opening_post)
            if form.need_to_ban:
                # Ban user because he is suspected to be a bot
                self._ban_current_user(request)

            return self.get(request, post_id, mode, form)
def reply_topic(request, board_id, topic_id):
    try:
        topic = Topic.objects.get(pk=topic_id)
    except Topic.DoesNotExist:
        raise Http404
    if request.method == 'POST':
        form = PostForm(request.POST)
        if form.is_valid():
            post = form.save(commit=False)
            post.topic = topic
            post.created_by = request.user
            post.save()

            topic.last_updated = timezone.now()
            topic.save()

            topic_url = reverse('topic_posts',
                                kwargs={
                                    'board_id': board_id,
                                    'topic_pk': topic_id
                                })
            topic_post_url = '{url}?page={page}#{id}'.format(
                url=topic_url, id=post.pk, page=topic.get_page_count())
            # return redirect('topic_posts', board_id=topic.board_id, topic_pk=topic.pk)
            return redirect(topic_post_url)
    else:
        form = PostForm()
    return render(request, 'reply_topic.html', {'topic': topic, 'form': form})
Exemplo n.º 7
0
Arquivo: views.py Projeto: 2018lak/MAF
def post(request):
    """
    posting questions
    """
    if request.method == "POST":
        postform = PostForm(request.POST)
        if postform.is_valid():
            _post = postform.save(commit=False)
            _post.user = request.user
            _post.save()

            # tags(ManyToManyField)
            for _tagname in request.POST.get('tags', '').split(','):
                _tagname = _tagname.strip()

                # When only _tagname is not blank
                if _tagname:
                    _tag, _created = Tag.objects.get_or_create(
                        name=_tagname, defaults={'name': _tagname})
                    _post.tags.add(_tag)

            return HttpResponseRedirect(reverse('home'))
    elif request.method == "GET":
        postform = PostForm()

    return render(request, "post.html", {
        'postform': postform,
    })
Exemplo n.º 8
0
def reply_topic(request, board_id, topic_id):
    topic = get_object_or_404(Topic, board_id=board_id, id=topic_id)

    if request.method == 'POST':
        form = PostForm(request.POST)
        if form.is_valid():
            post = form.save(commit=False)
            post.topic = topic
            post.created_by = request.user
            post.save()

            # update topic's last_updated
            topic.last_updated = datetime.now()
            topic.save()

            topic_url = reverse('topic_posts',
                                kwargs={
                                    'board_id': board_id,
                                    'topic_id': topic_id
                                })
            topic_post_url = f'{topic_url}?page={topic.get_page_count()}#{post.id}'

            return redirect(topic_post_url)
    else:
        form = PostForm()

    return render(request, 'reply_topic.html', {'topic': topic, 'form': form})
Exemplo n.º 9
0
def create_post(request):
    if request.method == "POST":
        # use this to add users to DB TABLE TODO
        post = Post(
            user=request.user,
            pub_date=timezone.localtime(),
            winner_selected=False,
        )
        form = PostForm(instance=post, data=request.POST)
        if form.is_valid():
            form.save()
            return redirect('boards:index')
    else:
        form = PostForm()
    return render(request, "boards/new_post_form.html", {'form': form})
Exemplo n.º 10
0
def new_post_view(request, board_pk, topic_pk):
    topic = get_object_or_404(Topic, pk=topic_pk)
    if request.method == 'POST':
        form = PostForm(request.POST)
        if form.is_valid():
            post = form.save(commit=False)
            post.topic = topic
            post.authored_by = request.user
            post.save()
            return redirect('topic_posts',
                            board_pk=board_pk,
                            topic_pk=topic.pk)
    form = PostForm()
    return render(request, 'reply_topic.html', {'topic': topic, 'form': form})
Exemplo n.º 11
0
def reply_topic(request, pk, topic_pk):
    topic = get_object_or_404(Topic, board__pk=pk, pk=topic_pk)
    if request.method == 'POST':
        form = PostForm(request.POST)
        if form.is_valid():
            post = form.save(commit=False)
            post.topic = topic
            post.created_by = request.user
            post.save()
            # IMPORTANT: using topic_pk as referring to the keyword argument of the function.
            return redirect('topic_posts', pk=pk, topic_pk=topic_pk)
    else:
        form = PostForm()
    return render(request, 'reply_topic.html', {'topic': topic, 'form': form})
Exemplo n.º 12
0
def reply_topic(request, id, topic_id):
    topic = get_object_or_404(Topic, board__pk=id, pk=topic_id)
    if request.method == "POST":
        form = PostForm(request.POST)
        if form.is_valid():
            post = form.save(commit=False)
            post.topic = topic
            post.created_by = request.user
            post.save()
            topic.updated_by = request.user
            topic.updated_dt = timezone.now()
            topic.save()

            return redirect('topics_posts', id=id, topic_id=topic_id)
    else:
        form = PostForm()
    return render(request, 'reply_topic.html', {'topic': topic, 'form': form})
Exemplo n.º 13
0
def reply_topic(request, pk, topic_pk):
    topic = get_object_or_404(Topic, board__pk=pk, pk=topic_pk)
    if request.method == "POST":
        form = PostForm(request.POST)
        if form.is_valid():
            post = form.save(commit=False)
            post.topic = topic
            post.created_by = request.user
            post.save()
            return redirect("boards:topic_posts", pk=pk, topic_pk=topic_pk)
    else:
        form = PostForm()

    return render(request, "boards/reply_topic.html", {
        "topic": topic,
        "form": form
    })