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

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

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

24
@login_required
Jean Michel Rouly's avatar
Jean Michel Rouly committed
25
def submit_question(request):
26
27
28
29
30
31
32
33
34
35
    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
36
    return render_to_response('submit_question.html', {
37
        'form' : form,
Jean Michel Rouly's avatar
Jean Michel Rouly committed
38
    },
39
    RequestContext(request),
Jean Michel Rouly's avatar
Jean Michel Rouly committed
40
    )
41
42
43
44

@login_required
def index(request):
    return render_to_response('index.html', {
45
        'questions' : Question.objects.all(),
46
47
    },
    )
Jean Michel Rouly's avatar
Jean Michel Rouly committed
48
49
50
51
52
53

@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
54
55
56
57
58
59
            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
60
61
62
63
64
65
66
67
68
            return HttpResponseRedirect('/')
    else:
        form = FeedbackForm()

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

Jean Michel Rouly's avatar
Jean Michel Rouly committed
70
@login_required
71
def view_question(request, slug):
Jean Michel Rouly's avatar
Jean Michel Rouly committed
72
73
74
75
76
77
78
79
80
81

    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
82
    if request.method == 'POST':
Jean Michel Rouly's avatar
Jean Michel Rouly committed
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
        # 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
106

107
108
    return render_to_response('question.html', {
        'question' : get_object_or_404(Question, pk=slug),
Jean Michel Rouly's avatar
Jean Michel Rouly committed
109
        'comment_form' : comment_form,
Jean Michel Rouly's avatar
Jean Michel Rouly committed
110
        'reply_forms' : reply_forms,
111
112
113
    },
    RequestContext(request),
    )