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.forms import FeedbackForm
10 from django.core.urlresolvers import reverse
11 from django.utils.translation import ugettext as _
12 from django.db.models import Count
13 from forum.forms import get_next_url
14 from forum.models import Badge, Award, User, Page
15 from forum.badges.base import BadgesMeta
16 from forum import settings
17 from forum.utils.mail import send_template_email
18 from django.utils.safestring import mark_safe
19 from forum.templatetags.extra_filters import or_preview
24 return HttpResponseRedirect(str(settings.APP_FAVICON))
26 def custom_css(request):
27 return HttpResponse(or_preview(settings.CUSTOM_CSS, request), mimetype="text/css")
29 def static(request, title, content):
30 return render_to_response('static.html', {'content' : content, 'title': title},
31 context_instance=RequestContext(request))
33 def media(request, skin, path):
34 return serve(request, "%s/media/%s" % (skin, path),
35 document_root=os.path.join(os.path.dirname(os.path.dirname(__file__)), 'skins').replace('\\', '/'))
37 def markdown_help(request):
38 return render_to_response('markdown_help.html', context_instance=RequestContext(request))
41 def opensearch(request):
42 return render_to_response('opensearch.html', {'settings' : settings}, context_instance=RequestContext(request))
45 def feedback(request):
46 if request.method == "POST":
47 form = FeedbackForm(request.user, data=request.POST)
51 'email': request.user.is_authenticated() and request.user.email or form.cleaned_data.get('email', None),
52 'message': form.cleaned_data['message'],
53 'name': request.user.is_authenticated() and request.user.username or form.cleaned_data.get('name', None),
54 'ip': request.META['REMOTE_ADDR'],
57 recipients = User.objects.filter(is_superuser=True)
58 send_template_email(recipients, "notifications/feedback.html", context)
60 msg = _('Thanks for the feedback!')
61 request.user.message_set.create(message=msg)
62 return HttpResponseRedirect(get_next_url(request))
64 form = FeedbackForm(request.user, initial={'next':get_next_url(request)})
66 return render_to_response('feedback.html', {'form': form}, context_instance=RequestContext(request))
68 feedback.CANCEL_MESSAGE=_('We look forward to hearing your feedback! Please, give it next time :)')
71 return render_to_response('privacy.html', context_instance=RequestContext(request))
74 return render_to_response('logout.html', {
75 'next' : get_next_url(request),
76 }, context_instance=RequestContext(request))
78 @decorators.render('badges.html', 'badges', _('badges'), weight=300)
80 badges = [b.ondb for b in sorted(BadgesMeta.by_id.values(), lambda b1, b2: cmp(b1.name, b2.name))]
82 if request.user.is_authenticated():
83 my_badges = Award.objects.filter(user=request.user).values('badge_id').distinct()
89 'mybadges' : my_badges,
92 def badge(request, id, slug):
93 badge = Badge.objects.get(id=id)
94 awards = list(Award.objects.filter(badge=badge).order_by('user', 'awarded_at'))
95 award_count = len(awards)
97 awards = sorted([dict(count=len(list(g)), user=k) for k, g in groupby(awards, lambda a: a.user)],
98 lambda c1, c2: c2['count'] - c1['count'])
100 return render_to_response('badge.html', {
101 'award_count': award_count,
104 }, context_instance=RequestContext(request))
106 def page(request, path):
107 if path in settings.STATIC_PAGE_REGISTRY:
109 page = Page.objects.get(id=settings.STATIC_PAGE_REGISTRY[path])
111 if (not page.published) and (not request.user.is_superuser):
118 template = page.extra.get('template', 'default')
119 sidebar = page.extra.get('sidebar', '')
121 if template == 'default':
122 base = 'base_content.html'
123 elif template == 'sidebar':
126 sidebar_render = page.extra.get('render', 'markdown')
128 if sidebar_render == 'markdown':
129 sidebar = page._as_markdown(sidebar)
130 elif sidebar_render == 'html':
131 sidebar = mark_safe(sidebar)
134 return HttpResponse(page.body, mimetype=page.extra.get('mimetype', 'text/html'))
136 render = page.extra.get('render', 'markdown')
138 if render == 'markdown':
139 body = page.as_markdown()
140 elif render == 'html':
141 body = mark_safe(page.body)
145 return render_to_response('page.html', {
150 }, context_instance=RequestContext(request))