2 from itertools import groupby
4 from django.shortcuts import render_to_response
5 from django.template import RequestContext
6 from django.http import HttpResponseRedirect, HttpResponse
7 from django.views.static import serve
8 from django.views.decorators.cache import cache_page
9 from django.utils.translation import ugettext as _
10 from django.utils.safestring import mark_safe
12 from django.contrib import messages
14 from forum import settings
15 from forum.views.decorators import login_required
16 from forum.forms import FeedbackForm
17 from forum.modules import decorate
18 from forum.forms import get_next_url
19 from forum.models import Badge, Award, User, Page
20 from forum.http_responses import HttpResponseNotFound, HttpResponseIntServerError
21 from forum.utils.mail import send_template_email
22 from forum.templatetags.extra_filters import or_preview
25 import logging, traceback
28 return HttpResponseRedirect(str(settings.APP_FAVICON))
30 def custom_css(request):
31 return HttpResponse(or_preview(settings.CUSTOM_CSS, request), content_type="text/css")
33 def static(request, title, content):
34 return render_to_response('static.html', {'content' : content, 'title': title},
35 context_instance=RequestContext(request))
37 def media(request, skin, path):
38 response = serve(request, "%s/media/%s" % (skin, path),
39 document_root=os.path.join(os.path.dirname(os.path.dirname(__file__)), 'skins').replace('\\', '/'))
40 content_type = response.get('Content-Type', '')
41 if ('charset=' not in content_type):
42 if (content_type.startswith('text') or content_type=='application/x-javascript'):
43 content_type += '; charset=utf-8'
44 response['Content-Type'] = content_type
48 def markdown_help(request):
49 return render_to_response('markdown_help.html', context_instance=RequestContext(request))
51 @cache_page(60 * 60 * 24 * 30) #30 days
52 def opensearch(request):
53 return render_to_response('opensearch.html', {'settings' : settings}, context_instance=RequestContext(request))
56 def feedback(request):
57 if request.method == "POST":
58 form = FeedbackForm(request.user, data=request.POST)
62 'email': request.user.is_authenticated() and request.user.email or form.cleaned_data.get('email', None),
63 'message': form.cleaned_data['message'],
64 'name': request.user.is_authenticated() and request.user.username or form.cleaned_data.get('name', None),
65 'ip': request.META['REMOTE_ADDR'],
68 recipients = User.objects.filter(is_superuser=True)
69 send_template_email(recipients, "notifications/feedback.html", context)
71 msg = _('Thanks for the feedback!')
72 messages.info(request, msg)
73 return HttpResponseRedirect(get_next_url(request))
75 form = FeedbackForm(request.user, initial={'next':get_next_url(request)})
77 return render_to_response('feedback.html', {'form': form}, context_instance=RequestContext(request))
79 feedback.CANCEL_MESSAGE=_('We look forward to hearing your feedback! Please, give it next time :)')
82 return render_to_response('privacy.html', context_instance=RequestContext(request))
84 @decorate.withfn(login_required)
86 return render_to_response('logout.html', {
87 'next' : get_next_url(request),
88 }, context_instance=RequestContext(request))
90 @decorators.render('badges.html', 'badges', _('badges'), weight=300)
92 from forum.badges.base import BadgesMeta
93 badges = sorted([Badge.objects.get(id=id) for id in BadgesMeta.by_id.keys()], lambda b1, b2: cmp(b1.name, b2.name))
95 if request.user.is_authenticated():
96 my_badges = Award.objects.filter(user=request.user).values_list('badge_id', flat=True).distinct()
102 'mybadges' : my_badges,
105 def badge(request, id, slug):
106 badge = Badge.objects.get(id=id)
107 awards = list(Award.objects.filter(badge=badge).order_by('user', 'awarded_at'))
108 award_count = len(awards)
110 awards = sorted([dict(count=len(list(g)), user=k) for k, g in groupby(awards, lambda a: a.user)],
111 lambda c1, c2: c2['count'] - c1['count'])
113 return render_to_response('badge.html', {
114 'award_count': award_count,
117 }, context_instance=RequestContext(request))
120 path = request.path[1:]
122 if path in settings.STATIC_PAGE_REGISTRY:
124 page = Page.objects.get(id=settings.STATIC_PAGE_REGISTRY[path])
126 if (not page.published) and (not request.user.is_superuser):
127 return HttpResponseNotFound(request)
129 return HttpResponseNotFound(request)
131 return HttpResponseNotFound(request)
133 template = page.extra.get('template', 'default')
134 sidebar = page.extra.get('sidebar', '')
136 if template == 'default':
137 base = 'base_content.html'
138 elif template == 'sidebar':
141 sidebar_render = page.extra.get('render', 'markdown')
143 if sidebar_render == 'markdown':
144 sidebar = page._as_markdown(sidebar)
145 elif sidebar_render == 'html':
146 sidebar = mark_safe(sidebar)
149 return HttpResponse(page.body, content_type=page.extra.get('mimetype', 'text/html'))
151 render = page.extra.get('render', 'markdown')
153 if render == 'markdown':
154 body = page.as_markdown()
155 elif render == 'html':
156 body = mark_safe(page.body)
160 return render_to_response('page.html', {
165 }, context_instance=RequestContext(request))
168 def error_handler(request):
170 stacktrace = "".join(["\t\t%s\n" % l for l in traceback.format_exc().split("\n")])
174 error executing request:
189 'path': request.path,
190 'user': request.user.is_authenticated() and ("%s (%s)" % (request.user.username, request.user.id)) or "<anonymous>",
191 'method': request.method,
192 'post': request.POST and "".join(["\t\t%s: %s\n" % (k, v) for k, v in request.POST.items()]) or "None",
193 'get': request.GET and "".join(["\t\t%s: %s\n" % (k, v) for k, v in request.GET.items()]) or "None",
194 'cookies': request.COOKIES and "".join(["\t\t%s: %s\n" % (k, v) for k, v in request.COOKIES.items()]) or "None",
195 'headers': request.META and "".join(["\t\t%s: %s\n" % (k, v) for k, v in request.META.items()]) or "None",
196 'stacktrace': stacktrace
199 log_msg = "error executing request:\n%s" % stacktrace
202 logging.error(log_msg)
203 return HttpResponseIntServerError(request)