summaryrefslogtreecommitdiff
path: root/comments/views.py
blob: 260678addbc4b7e80363de4420fcfd2fec5d414b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import json

from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render
from django.core.context_processors import csrf
from django.views.decorators.csrf import csrf_exempt

from comments.forms import CommentForm, ReplyForm
from comments.models import Comment, Reply

def get_comments(request):
    # retriving comment parameters
    book = request.GET.get('book', '')
    chapter = request.GET.get('chapter', '')
    example = request.GET.get('example', '')
    page = request.GET.get('page', '')
    comments = Comment.objects.filter(book=book).filter(chapter=chapter).filter(example=example)
    context = {
        'comments': comments,
        'book': book,
        'chapter': chapter,
        'example': example,
        'page': page
    }
    return render(request, "comments/get_comments.html", context)

def new_comment(request):
    # saving the poted comment
    if request.method == 'POST':
        form = CommentForm(request.POST)
        if form.is_valid():
            comment = Comment()
            comment.book = form.cleaned_data.get("book")
            comment.chapter = form.cleaned_data.get("chapter")
            comment.example = form.cleaned_data.get("example")
            comment.page = form.cleaned_data.get("page")
            comment.title = form.cleaned_data.get("title")
            comment.body = form.cleaned_data.get("body")
            comment.email = form.cleaned_data.get("email", "Anonymous")
            comment.save()
            return HttpResponseRedirect(
                '/comments/get/?book={0}&chapter={1}&example={2}&page={3}'.format(
                    comment.book, comment.chapter, comment.example, comment.page
                )
            )
        else:
            book = request.POST.get('book', '')
            chapter = request.POST.get('chapter', '')
            example = request.POST.get('example', '')
            page = request.POST.get('page', '')
            return HttpResponseRedirect(
                '/comments/new/?book={0}&chapter={1}&example={2}&page={3}'.format(
                    book, chapter, example, page
                )
            )

    # retriving comment parameters
    book = request.GET.get('book', '')
    chapter = request.GET.get('chapter', '')
    example = request.GET.get('example', '')
    page = request.GET.get('page', '')
    initial_values = {
        'book': book,
        'chapter': chapter,
        'example': example,
        'page': page
    }
    form = CommentForm(initial = initial_values)
    context = {
        'form': form,
        'book': book,
        'chapter': chapter,
        'example': example,
        'page': page
    }
    context.update(csrf(request))
    return render(request, 'comments/new_comment.html', context)

def new_reply(request):
    if request.method == 'POST':
        form = ReplyForm(request.POST)
        if form.is_valid():
            comment_id  = form.cleaned_data.get('comment_id')
            comment = Comment.objects.get(id=comment_id)
            reply = Reply()
            reply.comment = comment
            reply.body = form.cleaned_data.get('body')
            reply.email = form.cleaned_data.get('email', 'Anonymous')
            reply.save()
            return HttpResponseRedirect(
                '/comments/get/?book={0}&chapter={1}&example={2}&page={3}'.format(
                    comment.book, comment.chapter, comment.example, comment.page
                )
            )
        else:
            comment_id = request.POST.get('comment_id', '')
            return HttpResponseRedirect(
                '/comments/new-reply/?comment_id={0}'.format(
                    comment_id
                )
            )
    comment_id = request.GET.get('comment_id', '')
    comment = Comment.objects.get(id=comment_id)
    initial_values = {
        'comment_id': comment_id
    }
    form = ReplyForm(initial = initial_values)
    context = {
        'form': form,
        'comment': comment
    }
    return render(request, 'comments/new_reply.html', context)