views.py 2.55 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
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

Jean Michel Rouly's avatar
Jean Michel Rouly committed
13
import os
Jean Michel Rouly's avatar
Jean Michel Rouly committed
14
15
import requests

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

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

@login_required
def index(request):
42
43
44
    if request.method == 'POST':
        pass

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

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

    return render_to_response('feedback.html', {
        'form' : form,
    },
    RequestContext(request),
    )
70
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
82
    if request.method == 'POST':
        comment_form = CommentForm(request.POST)
        if comment_form.is_valid():
            comment = comment_form.save(commit=False)
            comment.user = User.objects.get(id=request.user.id)
            comment.parent = Question.objects.get(id=slug)
            comment.save()
            return HttpResponseRedirect('')
    else:
        comment_form = CommentForm()

83
84
    return render_to_response('question.html', {
        'question' : get_object_or_404(Question, pk=slug),
Jean Michel Rouly's avatar
Jean Michel Rouly committed
85
        'comment_form' : comment_form,
86
87
88
    },
    RequestContext(request),
    )