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 forum.views.decorators import login_required
11 from forum.forms import FeedbackForm
12 from django.core.urlresolvers import reverse
13 from django.utils.translation import ugettext as _
14 from django.db.models import Count
15 from forum.forms import get_next_url
16 from forum.models import Badge, Award, User, Page
17 from forum.badges.base import BadgesMeta
18 from forum import settings
19 from forum.utils.mail import send_template_email
20 from django.utils.safestring import mark_safe
21 from forum.templatetags.extra_filters import or_preview
26 return HttpResponseRedirect(str(settings.APP_FAVICON))
28 def custom_css(request):
29 return HttpResponse(or_preview(settings.CUSTOM_CSS, request), mimetype="text/css")
31 def static(request, title, content):
32 return render_to_response('static.html', {'content' : content, 'title': title},
33 context_instance=RequestContext(request))
35 def media(request, skin, path):
36 return serve(request, "%s/media/%s" % (skin, path),
37 document_root=os.path.join(os.path.dirname(os.path.dirname(__file__)), 'skins').replace('\\', '/'))
39 def markdown_help(request):
40 return render_to_response('markdown_help.html', context_instance=RequestContext(request))
43 def opensearch(request):
44 return render_to_response('opensearch.html', {'settings' : settings}, context_instance=RequestContext(request))
47 def feedback(request):
48 if request.method == "POST":
49 form = FeedbackForm(request.user, data=request.POST)
53 'email': request.user.is_authenticated() and request.user.email or form.cleaned_data.get('email', None),
54 'message': form.cleaned_data['message'],
55 'name': request.user.is_authenticated() and request.user.username or form.cleaned_data.get('name', None),
56 'ip': request.META['REMOTE_ADDR'],
59 recipients = User.objects.filter(is_superuser=True)
60 send_template_email(recipients, "notifications/feedback.html", context)
62 msg = _('Thanks for the feedback!')
63 request.user.message_set.create(message=msg)
64 return HttpResponseRedirect(get_next_url(request))
66 form = FeedbackForm(request.user, initial={'next':get_next_url(request)})
68 return render_to_response('feedback.html', {'form': form}, context_instance=RequestContext(request))
70 feedback.CANCEL_MESSAGE=_('We look forward to hearing your feedback! Please, give it next time :)')
73 return render_to_response('privacy.html', context_instance=RequestContext(request))
75 @decorate.withfn(login_required)
77 return render_to_response('logout.html', {
78 'next' : get_next_url(request),
79 }, context_instance=RequestContext(request))
81 @decorators.render('badges.html', 'badges', _('badges'), weight=300)
83 badges = [b.ondb for b in sorted(BadgesMeta.by_id.values(), lambda b1, b2: cmp(b1.name, b2.name))]
85 if request.user.is_authenticated():
86 my_badges = Award.objects.filter(user=request.user).values('badge_id').distinct()
92 'mybadges' : my_badges,
95 def badge(request, id, slug):
96 badge = Badge.objects.get(id=id)
97 awards = list(Award.objects.filter(badge=badge).order_by('user', 'awarded_at'))
98 award_count = len(awards)
100 awards = sorted([dict(count=len(list(g)), user=k) for k, g in groupby(awards, lambda a: a.user)],
101 lambda c1, c2: c2['count'] - c1['count'])
103 return render_to_response('badge.html', {
104 'award_count': award_count,
107 }, context_instance=RequestContext(request))
109 def page(request, path):
110 if path in settings.STATIC_PAGE_REGISTRY:
112 page = Page.objects.get(id=settings.STATIC_PAGE_REGISTRY[path])
114 if (not page.published) and (not request.user.is_superuser):
121 template = page.extra.get('template', 'default')
122 sidebar = page.extra.get('sidebar', '')
124 if template == 'default':
125 base = 'base_content.html'
126 elif template == 'sidebar':
129 sidebar_render = page.extra.get('render', 'markdown')
131 if sidebar_render == 'markdown':
132 sidebar = page._as_markdown(sidebar)
133 elif sidebar_render == 'html':
134 sidebar = mark_safe(sidebar)
137 return HttpResponse(page.body, mimetype=page.extra.get('mimetype', 'text/html'))
139 render = page.extra.get('render', 'markdown')
141 if render == 'markdown':
142 body = page.as_markdown()
143 elif render == 'html':
144 body = mark_safe(page.body)
148 return render_to_response('page.html', {
153 }, context_instance=RequestContext(request))