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 response = serve(request, "%s/media/%s" % (skin, path),
37 document_root=os.path.join(os.path.dirname(os.path.dirname(__file__)), 'skins').replace('\\', '/'))
38 content_type = response['Content-Type']
39 if ('charset=' not in content_type):
40 if (content_type.startswith('text') or content_type=='application/x-javascript'):
41 content_type += '; charset=utf-8'
42 response['Content-Type'] = content_type
46 def markdown_help(request):
47 return render_to_response('markdown_help.html', context_instance=RequestContext(request))
50 def opensearch(request):
51 return render_to_response('opensearch.html', {'settings' : settings}, context_instance=RequestContext(request))
54 def feedback(request):
55 if request.method == "POST":
56 form = FeedbackForm(request.user, data=request.POST)
60 'email': request.user.is_authenticated() and request.user.email or form.cleaned_data.get('email', None),
61 'message': form.cleaned_data['message'],
62 'name': request.user.is_authenticated() and request.user.username or form.cleaned_data.get('name', None),
63 'ip': request.META['REMOTE_ADDR'],
66 recipients = User.objects.filter(is_superuser=True)
67 send_template_email(recipients, "notifications/feedback.html", context)
69 msg = _('Thanks for the feedback!')
70 request.user.message_set.create(message=msg)
71 return HttpResponseRedirect(get_next_url(request))
73 form = FeedbackForm(request.user, initial={'next':get_next_url(request)})
75 return render_to_response('feedback.html', {'form': form}, context_instance=RequestContext(request))
77 feedback.CANCEL_MESSAGE=_('We look forward to hearing your feedback! Please, give it next time :)')
80 return render_to_response('privacy.html', context_instance=RequestContext(request))
82 @decorate.withfn(login_required)
84 return render_to_response('logout.html', {
85 'next' : get_next_url(request),
86 }, context_instance=RequestContext(request))
88 @decorators.render('badges.html', 'badges', _('badges'), weight=300)
90 badges = [b.ondb for b in sorted(BadgesMeta.by_id.values(), lambda b1, b2: cmp(b1.name, b2.name))]
92 if request.user.is_authenticated():
93 my_badges = Award.objects.filter(user=request.user).values('badge_id').distinct()
99 'mybadges' : my_badges,
102 def badge(request, id, slug):
103 badge = Badge.objects.get(id=id)
104 awards = list(Award.objects.filter(badge=badge).order_by('user', 'awarded_at'))
105 award_count = len(awards)
107 awards = sorted([dict(count=len(list(g)), user=k) for k, g in groupby(awards, lambda a: a.user)],
108 lambda c1, c2: c2['count'] - c1['count'])
110 return render_to_response('badge.html', {
111 'award_count': award_count,
114 }, context_instance=RequestContext(request))
116 def page(request, path):
117 if path in settings.STATIC_PAGE_REGISTRY:
119 page = Page.objects.get(id=settings.STATIC_PAGE_REGISTRY[path])
121 if (not page.published) and (not request.user.is_superuser):
128 template = page.extra.get('template', 'default')
129 sidebar = page.extra.get('sidebar', '')
131 if template == 'default':
132 base = 'base_content.html'
133 elif template == 'sidebar':
136 sidebar_render = page.extra.get('render', 'markdown')
138 if sidebar_render == 'markdown':
139 sidebar = page._as_markdown(sidebar)
140 elif sidebar_render == 'html':
141 sidebar = mark_safe(sidebar)
144 return HttpResponse(page.body, mimetype=page.extra.get('mimetype', 'text/html'))
146 render = page.extra.get('render', 'markdown')
148 if render == 'markdown':
149 body = page.as_markdown()
150 elif render == 'html':
151 body = mark_safe(page.body)
155 return render_to_response('page.html', {
160 }, context_instance=RequestContext(request))