Skip to content
Newer
Older
100644 134 lines (95 sloc) 3.94 KB
64a757d @brosner added a small bit of worflow for changing logic in how responses can …
brosner authored Aug 31, 2010
1 from django.conf import settings
78a8b35 @brosner original code from CPC
brosner authored Jan 12, 2010
2 from django.http import Http404, HttpResponse, HttpResponseRedirect
0894f0f @brosner added missing imports in views.py
brosner authored Feb 1, 2010
3 from django.http import HttpResponseNotAllowed, HttpResponseForbidden
78a8b35 @brosner original code from CPC
brosner authored Jan 12, 2010
4 from django.template import RequestContext
5 from django.shortcuts import get_object_or_404, render_to_response
64a757d @brosner added a small bit of worflow for changing logic in how responses can …
brosner authored Sep 1, 2010
6 from django.utils.importlib import import_module
78a8b35 @brosner original code from CPC
brosner authored Jan 12, 2010
7
8 from django.contrib.auth.decorators import login_required
9
b2999de @brosner removed questions references
brosner authored Jan 12, 2010
10 from aiteo.forms import AskQuestionForm, AddResponseForm
4cba763 @brosner updated groups support and made some small tweaks
brosner authored Jan 12, 2010
11 from aiteo.groups import group_and_bridge, group_context
b2999de @brosner removed questions references
brosner authored Jan 12, 2010
12 from aiteo.models import Question
78a8b35 @brosner original code from CPC
brosner authored Jan 12, 2010
13
14
64a757d @brosner added a small bit of worflow for changing logic in how responses can …
brosner authored Sep 1, 2010
15 workflow = import_module(getattr(settings, "AITEO_WORKFLOW_MODULE", "aiteo.workflow"))
16
17
4cba763 @brosner updated groups support and made some small tweaks
brosner authored Jan 12, 2010
18 def question_list(request, **kwargs):
78a8b35 @brosner original code from CPC
brosner authored Jan 12, 2010
19
4cba763 @brosner updated groups support and made some small tweaks
brosner authored Jan 12, 2010
20 group, bridge = group_and_bridge(kwargs)
78a8b35 @brosner original code from CPC
brosner authored Jan 12, 2010
21
22 questions = Question.objects.all().order_by("-score", "created", "id")
23
24 if group:
25 questions = group.content_objects(questions)
bd96390 @brosner when no group is present only get non-grouped objects
brosner authored Jan 12, 2010
26 else:
27 questions = questions.filter(group_content_type=None)
78a8b35 @brosner original code from CPC
brosner authored Jan 12, 2010
28
4cba763 @brosner updated groups support and made some small tweaks
brosner authored Jan 12, 2010
29 ctx = group_context(group, bridge)
30 ctx.update({
78a8b35 @brosner original code from CPC
brosner authored Jan 12, 2010
31 "group": group,
32 "questions": questions,
4cba763 @brosner updated groups support and made some small tweaks
brosner authored Jan 12, 2010
33 })
8538c5f @brosner minor clean-ups to style
brosner authored Jan 12, 2010
34
996934d @brosner minor clean-ups
brosner authored Jan 22, 2010
35 ctx = RequestContext(request, ctx)
36 return render_to_response("aiteo/question_list.html", ctx)
78a8b35 @brosner original code from CPC
brosner authored Jan 12, 2010
37
38
39 @login_required
4cba763 @brosner updated groups support and made some small tweaks
brosner authored Jan 12, 2010
40 def question_create(request, **kwargs):
78a8b35 @brosner original code from CPC
brosner authored Jan 12, 2010
41
4cba763 @brosner updated groups support and made some small tweaks
brosner authored Jan 12, 2010
42 group, bridge = group_and_bridge(kwargs)
78a8b35 @brosner original code from CPC
brosner authored Jan 12, 2010
43
44 if request.method == "POST":
45 form = AskQuestionForm(request.POST)
46 if form.is_valid():
47 question = form.save(commit=False)
48 question.user = request.user
49 question.save()
50 return HttpResponseRedirect(question.get_absolute_url())
51 else:
52 form = AskQuestionForm()
53
4cba763 @brosner updated groups support and made some small tweaks
brosner authored Jan 12, 2010
54 ctx = group_context(group, bridge)
55 ctx.update({
78a8b35 @brosner original code from CPC
brosner authored Jan 12, 2010
56 "group": group,
57 "form": form,
4cba763 @brosner updated groups support and made some small tweaks
brosner authored Jan 12, 2010
58 })
8538c5f @brosner minor clean-ups to style
brosner authored Jan 12, 2010
59
7fea4d5 @brosner fixed a couple RequestContext not working correctly
brosner authored Feb 1, 2010
60 ctx = RequestContext(request, ctx)
996934d @brosner minor clean-ups
brosner authored Jan 22, 2010
61 return render_to_response("aiteo/question_create.html", ctx)
78a8b35 @brosner original code from CPC
brosner authored Jan 12, 2010
62
63
4cba763 @brosner updated groups support and made some small tweaks
brosner authored Jan 12, 2010
64 def question_detail(request, question_id, **kwargs):
78a8b35 @brosner original code from CPC
brosner authored Jan 12, 2010
65
4cba763 @brosner updated groups support and made some small tweaks
brosner authored Jan 12, 2010
66 group, bridge = group_and_bridge(kwargs)
78a8b35 @brosner original code from CPC
brosner authored Jan 12, 2010
67
68 questions = Question.objects.all()
69
70 if group:
71 questions = group.content_objects(questions)
bd96390 @brosner when no group is present only get non-grouped objects
brosner authored Jan 12, 2010
72 else:
73 questions = questions.filter(group_content_type=None)
78a8b35 @brosner original code from CPC
brosner authored Jan 12, 2010
74
75 question = get_object_or_404(questions, pk=question_id)
76 responses = question.responses.order_by("-score", "created", "id")
77
78 if question.user == request.user:
79 is_me = True
80 else:
81 is_me = False
82
83 if request.method == "POST":
84 add_response_form = AddResponseForm(request.POST)
8538c5f @brosner minor clean-ups to style
brosner authored Jan 12, 2010
85
78a8b35 @brosner original code from CPC
brosner authored Jan 12, 2010
86 if add_response_form.is_valid():
87 response = add_response_form.save(commit=False)
88 response.question = question
89 response.user = request.user
90 response.save()
91 return HttpResponseRedirect(response.get_absolute_url())
92 else:
93 if not is_me or request.user.is_staff:
94 add_response_form = AddResponseForm()
95 else:
96 add_response_form = None
97
4cba763 @brosner updated groups support and made some small tweaks
brosner authored Jan 12, 2010
98 ctx = group_context(group, bridge)
99 ctx.update({
78a8b35 @brosner original code from CPC
brosner authored Jan 12, 2010
100 "group": group,
874dbfa @brosner remove is_me (only used for checking if mark as accepted can be shown…
brosner authored Aug 31, 2010
101 "can_mark_accepted": workflow.can_mark_accepted(request.user, question),
78a8b35 @brosner original code from CPC
brosner authored Jan 12, 2010
102 "question": question,
103 "responses": responses,
104 "add_response_form": add_response_form,
4cba763 @brosner updated groups support and made some small tweaks
brosner authored Jan 12, 2010
105 })
8538c5f @brosner minor clean-ups to style
brosner authored Jan 12, 2010
106
7fea4d5 @brosner fixed a couple RequestContext not working correctly
brosner authored Feb 1, 2010
107 ctx = RequestContext(request, ctx)
996934d @brosner minor clean-ups
brosner authored Jan 22, 2010
108 return render_to_response("aiteo/question_detail.html", ctx)
78a8b35 @brosner original code from CPC
brosner authored Jan 12, 2010
109
110
4cba763 @brosner updated groups support and made some small tweaks
brosner authored Jan 12, 2010
111 def mark_accepted(request, question_id, response_id, **kwargs):
78a8b35 @brosner original code from CPC
brosner authored Jan 12, 2010
112
113 if request.method != "POST":
433ded0 @brosner use HttpResponseNotAllowed to be more explicit about the failure
brosner authored Feb 1, 2010
114 return HttpResponseNotAllowed(["POST"])
78a8b35 @brosner original code from CPC
brosner authored Jan 12, 2010
115
4cba763 @brosner updated groups support and made some small tweaks
brosner authored Jan 12, 2010
116 group, bridge = group_and_bridge(kwargs)
78a8b35 @brosner original code from CPC
brosner authored Jan 12, 2010
117
118 questions = Question.objects.all()
119
120 if group:
121 questions = group.content_objects(questions)
e7f4bfb @brosner missed place where we need to get non-group objects instead off all
brosner authored Jan 12, 2010
122 else:
123 questions = questions.filter(group_content_type=None)
78a8b35 @brosner original code from CPC
brosner authored Jan 12, 2010
124
e0da87b @brosner this is my little trick — James Tauber doesn't actually know I can't …
brosner authored Feb 1, 2010
125 question = get_object_or_404(questions, pk=question_id)
126
64a757d @brosner added a small bit of worflow for changing logic in how responses can …
brosner authored Sep 1, 2010
127 if not workflow.can_mark_accepted(request.user, question):
07202e3 @brosner moved short-circuit because I was being retarded thinking it'd work
brosner authored Feb 1, 2010
128 return HttpResponseForbidden("You are not allowed to mark this question accepted.")
78a8b35 @brosner original code from CPC
brosner authored Jan 12, 2010
129
1fcf04e @brosner modified mark_accepted to not require @login_required decorator, but …
brosner authored Feb 1, 2010
130 response = question.responses.get(pk=response_id)
131 response.accept()
78a8b35 @brosner original code from CPC
brosner authored Jan 12, 2010
132
133 return HttpResponse("good")
Something went wrong with that request. Please try again.