Created
April 12, 2011 00:36
-
-
Save sorl/914688 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
coding=utf-8 | |
from django import http | |
from django.db.models import Q | |
from django.contrib import messages | |
from django.utils.translation import ugettext as _ | |
from cbv import TemplateView | |
from faq.models import FAQ, FAQGroup | |
from itertools import groupby | |
class FAQList(TemplateView): | |
template_name = 'faq/faq_list.html' | |
def get_context_data(self, **kwargs): | |
search = self.request.GET.get('search', '').strip() | |
q = Q(title__icontains=search) if search else Q() | |
def group_faqs(): | |
for k, g in groupby(FAQ.objects.filter(q), lambda obj: obj.group): | |
yield k, list(g) | |
return { | |
'group_faqs': group_faqs() | |
} | |
class FAQDetail(TemplateView): | |
template_name = 'faq/faq_detail.html' | |
def post(self, request, *args, **kwargs): | |
context = self.get_context_data(**kwargs) | |
if request.user.is_authenticated(): | |
CommentForm = context['faq'].get_comment_form() | |
comment_form = CommentForm(request.POST) | |
if comment_form.is_valid(): | |
obj = comment_form.save(commit=False) | |
obj.author = request.user.profile | |
obj.item = context['faq'] | |
obj.save() | |
messages.add_message(request, messages.INFO, | |
_(u'Tack för din kommentar')) | |
return http.HttpResponseRedirect(request.path) | |
return self.render_to_response(context) | |
def get_context_data(self, **kwargs): | |
try: | |
faq = FAQ.objects.get(slug=kwargs['slug']) | |
except FAQ.DoesNotExist: | |
raise http.Http404 | |
CommentForm = faq.get_comment_form() | |
context = { | |
'params': kwargs, | |
'faq': faq, | |
'comment_form': CommentForm(), | |
} | |
return context |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment