2 from itertools import groupby
3 from django.shortcuts import render_to_response, get_object_or_404
4 from django.core.urlresolvers import reverse
5 from django.template import RequestContext, loader
6 from django.http import HttpResponseRedirect, HttpResponse, Http404
7 from django.views.static import serve
8 from forum import settings
9 from forum.modules import decorate
10 from django.views.decorators.cache import cache_page
11 from forum.views.decorators import login_required
12 from forum.forms import FeedbackForm
13 from django.core.urlresolvers import reverse
14 from django.utils.translation import ugettext as _
15 from django.db.models import Count
16 from forum.forms import get_next_url
17 from forum.models import Badge, Award, User, Page
18 from forum.badges.base import BadgesMeta
19 from forum.http_responses import HttpResponseNotFound, HttpResponseIntServerError
20 from forum import settings
21 from forum.utils.mail import send_template_email
22 from django.utils.safestring import mark_safe
23 from forum.templatetags.extra_filters import or_preview
25 import re, sys, logging, traceback
28 return HttpResponseRedirect(str(settings.APP_FAVICON))
30 def custom_css(request):
31 return HttpResponse(or_preview(settings.CUSTOM_CSS, request), mimetype="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['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 request.user.message_set.create(message=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 badges = sorted([Badge.objects.get(id=id) for id in BadgesMeta.by_id.keys()], lambda b1, b2: cmp(b1.name, b2.name))
94 if request.user.is_authenticated():
95 my_badges = Award.objects.filter(user=request.user).values('badge_id').distinct()
101 'mybadges' : my_badges,
104 def badge(request, id, slug):
105 badge = Badge.objects.get(id=id)
106 awards = list(Award.objects.filter(badge=badge).order_by('user', 'awarded_at'))
107 award_count = len(awards)
109 awards = sorted([dict(count=len(list(g)), user=k) for k, g in groupby(awards, lambda a: a.user)],
110 lambda c1, c2: c2['count'] - c1['count'])
112 return render_to_response('badge.html', {
113 'award_count': award_count,
116 }, context_instance=RequestContext(request))
119 path = request.path[1:]
121 if path in settings.STATIC_PAGE_REGISTRY:
123 page = Page.objects.get(id=settings.STATIC_PAGE_REGISTRY[path])
125 if (not page.published) and (not request.user.is_superuser):
126 return HttpResponseNotFound(request)
128 return HttpResponseNotFound(request)
130 return HttpResponseNotFound(request)
132 template = page.extra.get('template', 'default')
133 sidebar = page.extra.get('sidebar', '')
135 if template == 'default':
136 base = 'base_content.html'
137 elif template == 'sidebar':
140 sidebar_render = page.extra.get('render', 'markdown')
142 if sidebar_render == 'markdown':
143 sidebar = page._as_markdown(sidebar)
144 elif sidebar_render == 'html':
145 sidebar = mark_safe(sidebar)
148 return HttpResponse(page.body, mimetype=page.extra.get('mimetype', 'text/html'))
150 render = page.extra.get('render', 'markdown')
152 if render == 'markdown':
153 body = page.as_markdown()
154 elif render == 'html':
155 body = mark_safe(page.body)
159 return render_to_response('page.html', {
164 }, context_instance=RequestContext(request))
167 def error_handler(request):
169 stacktrace = "".join(["\t\t%s\n" % l for l in traceback.format_exc().split("\n")])
173 error executing request:
188 'path': request.path,
189 'user': request.user.is_authenticated() and ("%s (%s)" % (request.user.username, request.user.id)) or "<anonymous>",
190 'method': request.method,
191 'post': request.POST and "".join(["\t\t%s: %s\n" % (k, v) for k, v in request.POST.items()]) or "None",
192 'get': request.GET and "".join(["\t\t%s: %s\n" % (k, v) for k, v in request.GET.items()]) or "None",
193 'cookies': request.COOKIES and "".join(["\t\t%s: %s\n" % (k, v) for k, v in request.COOKIES.items()]) or "None",
194 'headers': request.META and "".join(["\t\t%s: %s\n" % (k, v) for k, v in request.META.items()]) or "None",
195 'stacktrace': stacktrace
198 log_msg = "error executing request:\n%s" % stacktrace
201 logging.error(log_msg)
202 return HttpResponseIntServerError(request)