views.py 6.59 KB
Newer Older
1
from website.models import Question, Comment, Reply
Jean Michel Rouly's avatar
Jean Michel Rouly committed
2
from website.forms import QuestionForm, CommentForm, ReplyForm, FeedbackForm
3
from website.forms import CourseSectionFilterForm
Jean Michel Rouly's avatar
Jean Michel Rouly committed
4
from django.conf import settings
Jean Michel Rouly's avatar
Jean Michel Rouly committed
5
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
Jean Michel Rouly's avatar
Jean Michel Rouly committed
6
from django.core.urlresolvers import reverse
Jean Michel Rouly's avatar
Jean Michel Rouly committed
7
from django.shortcuts import render_to_response, get_object_or_404
Jean Michel Rouly's avatar
Jean Michel Rouly committed
8
from django.shortcuts import render, redirect
9
from django.contrib.auth.decorators import login_required
10
11
12
from django.contrib.auth.models import User
from django.template import RequestContext
from django.http import HttpResponseRedirect
Jean Michel Rouly's avatar
Jean Michel Rouly committed
13
from django.core.mail import EmailMessage
Jean Michel Rouly's avatar
Jean Michel Rouly committed
14
from django.utils import timezone
Jean Michel Rouly's avatar
Jean Michel Rouly committed
15
16
from django.forms import Textarea
from django.forms.models import modelformset_factory
Jean Michel Rouly's avatar
Jean Michel Rouly committed
17
from django.db.models import Count
Jean Michel Rouly's avatar
Jean Michel Rouly committed
18
from django.http import Http404
Jean Michel Rouly's avatar
Jean Michel Rouly committed
19

Jean Michel Rouly's avatar
Jean Michel Rouly committed
20
import os
Jean Michel Rouly's avatar
Jean Michel Rouly committed
21
22
import requests

Jean Michel Rouly's avatar
Jean Michel Rouly committed
23
# Create your views here.
Jean Michel Rouly's avatar
Jean Michel Rouly committed
24
25
26
27
28
def error_404(request):
    return render(request, '404.html', {
    },
    )

Jean Michel Rouly's avatar
Jean Michel Rouly committed
29
30
31
32
33
def error_500(request):
    return render(request, '500.html', {
    },
    )

34
@login_required
Jean Michel Rouly's avatar
Jean Michel Rouly committed
35
def submit_question(request):
36
37
38
39
40
41
42
43
44
45
    if request.method == 'POST':
        form = QuestionForm(request.POST)
        if form.is_valid():
            question = form.save(commit=False)
            question.user = User.objects.get(id=request.user.id)
            question.save()
            return HttpResponseRedirect('/')
    else:
        form = QuestionForm()

46
    return render(request, 'submit_question.html', {
47
        'form' : form,
Jean Michel Rouly's avatar
Jean Michel Rouly committed
48
49
    },
    )
50
51

@login_required
52
def index(request, *args, **kwargs):
53

Jean Michel Rouly's avatar
Jean Michel Rouly committed
54
55
56
    section = kwargs.get('section')
    sort = kwargs.get('sort')

57
58
59
60
61
    if request.method == 'POST':
        form = CourseSectionFilterForm( request.POST )
        if form.is_valid():
            data = form.cleaned_data
            section = data.get("section")
Jean Michel Rouly's avatar
Jean Michel Rouly committed
62
63
64
65
66

            if sort is not None:
                return redirect( 'filter', section, sort )
            else:
                return redirect( 'filter', section )
67
        else:
Jean Michel Rouly's avatar
Jean Michel Rouly committed
68
            return redirect( 'sort', sort )
69
70
71
72
    else:
        form = CourseSectionFilterForm()


Jean Michel Rouly's avatar
Jean Michel Rouly committed
73
    questions = Question.objects.all()
74
75

    if section is not None:
Jean Michel Rouly's avatar
Jean Michel Rouly committed
76
77
        section = section.replace(" ", "")
        section = section.upper()
Jean Michel Rouly's avatar
Jean Michel Rouly committed
78
        questions = Question.objects.all().filter(section__iexact=section)
Jean Michel Rouly's avatar
Jean Michel Rouly committed
79

Jean Michel Rouly's avatar
Jean Michel Rouly committed
80
81
82
    if sort is not None:
        if sort == "comments":
            questions = questions.annotate(comment_count=Count('comments')).order_by('comment_count')
Jean Michel Rouly's avatar
Jean Michel Rouly committed
83
        elif sort == "date":
Jean Michel Rouly's avatar
Jean Michel Rouly committed
84
            questions = questions.order_by('-date')
Jean Michel Rouly's avatar
Jean Michel Rouly committed
85
86
        else:
            raise Http404("Invalid filter.")
87
88


Jean Michel Rouly's avatar
Jean Michel Rouly committed
89
90
91
92
93
94
95
96
97
98
99
100
    paginator = Paginator(questions, 10) # show 25 questions per page

    page = request.GET.get('page')
    try:
        questions = paginator.page(page)
    except PageNotAnInteger:
        # if page is NaN, deliver first page
        questions = paginator.page(1)
    except EmptyPage:
        # if page is empty, deliver last page
        questions = paginator.page(paginator.num_pages)

101
    return render(request, 'index.html', {
Jean Michel Rouly's avatar
Jean Michel Rouly committed
102
        'filter' : section,
103
        'sort' : sort,
Jean Michel Rouly's avatar
Jean Michel Rouly committed
104
105
        'questions' : questions,
        'page_range' : range(1, int(questions.paginator.num_pages)+1),
106
        'form' : form,
107
108
    },
    )
Jean Michel Rouly's avatar
Jean Michel Rouly committed
109

110
111
112
113
114
115
116
117
118
119
120
121
122
@login_required
def my_questions(request):
    questions =  Question.objects.filter(user__id=request.user.id)
    paginator = Paginator(questions, 10) # show 25 questions per page

    page = request.GET.get('page')
    try:
        questions = paginator.page(page)
    except PageNotAnInteger:
        questions = paginator.page(1)
    except EmptyPage:
        questions = paginator.page(paginator.num_pages)

123
    return render(request, 'my_questions.html', {
124
125
126
127
128
        'questions' : questions,
        'page_range' : range(1, int(questions.paginator.num_pages)+1),
    },
    )

Jean Michel Rouly's avatar
Jean Michel Rouly committed
129
130
131
132
133
@login_required
def feedback(request):
    if request.method == 'POST':
        form = FeedbackForm( request.POST )
        if form.is_valid():
Jean Michel Rouly's avatar
Jean Michel Rouly committed
134
135
136
137
138
139
            f = open(os.path.join(settings.MEDIA_ROOT, 'feedback.txt'), 'a')
            data = form.cleaned_data
            f.write( str(timezone.now()) )
            f.write( str('\n') )
            f.write( data['text'] )
            f.write( str('\n\n\n') )
Jean Michel Rouly's avatar
Jean Michel Rouly committed
140
141
142
143
            return HttpResponseRedirect('/')
    else:
        form = FeedbackForm()

144
    return render(request, 'feedback.html', {
Jean Michel Rouly's avatar
Jean Michel Rouly committed
145
146
147
        'form' : form,
    },
    )
148

Jean Michel Rouly's avatar
Jean Michel Rouly committed
149
@login_required
150
def view_question(request, slug):
Jean Michel Rouly's avatar
Jean Michel Rouly committed
151

152
    question = Question.objects.get(id=slug)
Jean Michel Rouly's avatar
Jean Michel Rouly committed
153
154
155
156
157
158
159
160
161
    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)

Jean Michel Rouly's avatar
Jean Michel Rouly committed
162
    if request.method == 'POST':
163
164
        # parse the comment form (Good Rating)
        if "comment_sub_a" in request.POST:
Jean Michel Rouly's avatar
Jean Michel Rouly committed
165
166
            comment_form = CommentForm(request.POST)
            if comment_form.is_valid():
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184

                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()

Jean Michel Rouly's avatar
Jean Michel Rouly committed
185
186
                comment = comment_form.save(commit=False)
                comment.user = current_user
187
                comment.parent = question
Jean Michel Rouly's avatar
Jean Michel Rouly committed
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
                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)
Jean Michel Rouly's avatar
Jean Michel Rouly committed
204

205
    return render(request, 'question.html', {
206
        'question' : get_object_or_404(Question, pk=slug),
Jean Michel Rouly's avatar
Jean Michel Rouly committed
207
        'comment_form' : comment_form,
Jean Michel Rouly's avatar
Jean Michel Rouly committed
208
        'reply_forms' : reply_forms,
209
210
    },
    )
Jean Michel Rouly's avatar
Jean Michel Rouly committed
211
212
213
214
215

def help(request):
    return render(request, 'help.html', {
    },
    )