예제 #1
0
def create_comment(request, post_slug):
    try:
        post = Post.objects.get(slug=post_slug)
    except Post.DoesNotExist:
        post = None

    if post is None:
        return redirect("/")

    user = User.objects.get(id=request.user.id)
    user_profile = UserProfile.objects.get(user=user)

    if request.method == "POST":
        form = CommentForm(request.POST)

        if form.is_valid():
            if post:
                comment = form.save(commit=False)

                comment.post = post
                comment.user = user_profile

                comment.save()

                return redirect(
                    reverse(
                        "website:view_post",
                        kwargs={"post_slug": post_slug},
                    ))

    form = CommentForm()
    context_dict = {"form": form, "post": post}

    return render(request, "SkyView/createComment.html", context=context_dict)
예제 #2
0
def summary(request, subject, category, subcategory, summary_id):
    try:
        summary = Summary.objects.get(pk=summary_id)
    except Summary.DoesNotExist:
        summary = "DOES NOT EXIST!"

    subject = Subject.objects.get(name=subject)

    request.session.save()
    if not View.objects.filter(summary=summary, session=request.session.session_key):
        view = View(summary=summary,
                    ip=request.META['REMOTE_ADDR'],
                    date_created=datetime.datetime.now(),
                    session=request.session.session_key)
        view.save()

    context_dict = {
        'subject': subject,
        'summary': summary,
    }

    if request.method == 'POST':
        comment_form = CommentForm(data=request.POST)
        context_dict["comment_form"] = comment_form
        if comment_form.is_valid():
            if throttle(request):
                return render(request, "summary.html", context_dict)
            comment = comment_form.save(commit=False)
            comment.user = User.objects.get(id=request.user.id)
            comment.summary = summary
            comment.save()
            messages.add_message(request, messages.SUCCESS, 'התגובה שלך נוספה בצלחה')
            return redirect(comment)
        else:
            print(comment_form.errors)
    else:
        comment_form = CommentForm()
        context_dict["comment_form"] = comment_form

    return render(request, 'summary.html', context_dict)
예제 #3
0
def view_question(request, slug):

    question = Question.objects.get(id=slug)
    comments = Comment.objects.filter(parent__pk=slug)
    current_user = User.objects.get(id=request.user.id)

    comment_form = CommentForm()

    reply_forms = {}
    for comment in comments:
        reply_forms[comment] = ReplyForm(prefix=comment.pk)

    if request.method == 'POST':
        # parse the comment form (Good Rating)
        if "comment_sub_a" in request.POST:
            comment_form = CommentForm(request.POST)
            if comment_form.is_valid():

                question.rating = question.rating + 1
                question.save()

                comment = comment_form.save(commit=False)
                comment.user = current_user
                comment.parent = question
                comment.save()
                return redirect('view_question', slug)

        # parse the comment form (Bad Rating)
        elif "comment_sub_b" in request.POST:
            comment_form = CommentForm(request.POST)
            if comment_form.is_valid():

                question.rating = question.rating - 1
                question.save()

                comment = comment_form.save(commit=False)
                comment.user = current_user
                comment.parent = question
                comment.save()
                return redirect('view_question', slug)

        # parse the reply forms
        elif "reply_sub" in request.POST:
            reply_forms = {}
            for comment in comments:
                reply_forms[comment] = ReplyForm(request.POST,prefix=comment.pk)
            for reply_form_index in reply_forms:
                reply_form = reply_forms.get( reply_form_index )
                if reply_form.is_valid():
                    reply = reply_form.save(commit=False)
                    reply.user = current_user
                    reply.parent = Comment.objects.get(id=reply_form.prefix)
                    reply.save()
            return redirect('view_question', slug)

    return render(request, 'question.html', {
        'question' : get_object_or_404(Question, pk=slug),
        'comment_form' : comment_form,
        'reply_forms' : reply_forms,
    },
    )