views.py 4.69 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
Jean Michel Rouly's avatar
Jean Michel Rouly committed
3
from django.conf import settings
Jean Michel Rouly's avatar
Jean Michel Rouly committed
4
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
Jean Michel Rouly's avatar
Jean Michel Rouly committed
5
from django.shortcuts import render_to_response, get_object_or_404
Jean Michel Rouly's avatar
Jean Michel Rouly committed
6
from django.shortcuts import render, redirect
7
from django.contrib.auth.decorators import login_required
8
9
10
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
11
from django.core.mail import EmailMessage
Jean Michel Rouly's avatar
Jean Michel Rouly committed
12
from django.utils import timezone
Jean Michel Rouly's avatar
Jean Michel Rouly committed
13
14
from django.forms import Textarea
from django.forms.models import modelformset_factory
Jean Michel Rouly's avatar
Jean Michel Rouly committed
15

Jean Michel Rouly's avatar
Jean Michel Rouly committed
16
import os
Jean Michel Rouly's avatar
Jean Michel Rouly committed
17
18
import requests

Jean Michel Rouly's avatar
Jean Michel Rouly committed
19
# Create your views here.
Jean Michel Rouly's avatar
Jean Michel Rouly committed
20
21
22
23
24
def instructions(request):
    return render_to_response('instructions.html', {
    },
    )

25
@login_required
Jean Michel Rouly's avatar
Jean Michel Rouly committed
26
def submit_question(request):
27
28
29
30
31
32
33
34
35
36
    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()

Jean Michel Rouly's avatar
Jean Michel Rouly committed
37
    return render_to_response('submit_question.html', {
38
        'form' : form,
Jean Michel Rouly's avatar
Jean Michel Rouly committed
39
    },
40
    RequestContext(request),
Jean Michel Rouly's avatar
Jean Michel Rouly committed
41
    )
42
43
44

@login_required
def index(request):
Jean Michel Rouly's avatar
Jean Michel Rouly committed
45
46
47
48
49
50
51
52
53
54
55
56
57
    questions =  Question.objects.all()
    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)

58
    return render_to_response('index.html', {
Jean Michel Rouly's avatar
Jean Michel Rouly committed
59
60
        'questions' : questions,
        'page_range' : range(1, int(questions.paginator.num_pages)+1),
61
62
    },
    )
Jean Michel Rouly's avatar
Jean Michel Rouly committed
63

64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
@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)

    return render_to_response('my_questions.html', {
        'questions' : questions,
        'page_range' : range(1, int(questions.paginator.num_pages)+1),
    },
    )

Jean Michel Rouly's avatar
Jean Michel Rouly committed
83
84
85
86
87
@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
88
89
90
91
92
93
            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
94
95
96
97
98
99
100
101
102
            return HttpResponseRedirect('/')
    else:
        form = FeedbackForm()

    return render_to_response('feedback.html', {
        'form' : form,
    },
    RequestContext(request),
    )
103

Jean Michel Rouly's avatar
Jean Michel Rouly committed
104
@login_required
105
def view_question(request, slug):
Jean Michel Rouly's avatar
Jean Michel Rouly committed
106
107
108
109
110
111
112
113
114
115

    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
116
    if request.method == 'POST':
Jean Michel Rouly's avatar
Jean Michel Rouly committed
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
        # parse the comment form
        if "comment_sub" in request.POST:
            comment_form = CommentForm(request.POST)
            if comment_form.is_valid():
                comment = comment_form.save(commit=False)
                comment.user = current_user
                comment.parent = Question.objects.get(id=slug)
                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
140

141
142
    return render_to_response('question.html', {
        'question' : get_object_or_404(Question, pk=slug),
Jean Michel Rouly's avatar
Jean Michel Rouly committed
143
        'comment_form' : comment_form,
Jean Michel Rouly's avatar
Jean Michel Rouly committed
144
        'reply_forms' : reply_forms,
145
146
147
    },
    RequestContext(request),
    )