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
23 return HttpResponseRedirect(str(settings.APP_FAVICON))
25 def custom_css(request):
26 return HttpResponse(or_preview(settings.CUSTOM_CSS, request), mimetype="text/css")
28 def static(request, title, content):
29 return render_to_response('static.html', {'content' : content, 'title': title},
30 context_instance=RequestContext(request))
32 def media(request, skin, path):
33 return serve(request, "%s/media/%s" % (skin, path),
34 document_root=os.path.join(os.path.dirname(os.path.dirname(__file__)), 'skins').replace('\\', '/'))
36 def markdown_help(request):
37 return render_to_response('markdown_help.html', context_instance=RequestContext(request))
40 def opensearch(request):
41 return render_to_response('opensearch.html', {'settings' : settings}, context_instance=RequestContext(request))
44 def feedback(request):
45 if request.method == "POST":
46 form = FeedbackForm(request.POST)
48 context = {'user': request.user}
50 if not request.user.is_authenticated:
51 context['email'] = form.cleaned_data.get('email', None)
52 context['message'] = form.cleaned_data['message']
53 context['name'] = form.cleaned_data.get('name', None)
54 context['ip'] = request.META['REMOTE_ADDR']
56 recipients = User.objects.filter(is_superuser=True)
57 send_template_email(recipients, "notifications/feedback.html", context)
59 msg = _('Thanks for the feedback!')
60 request.user.message_set.create(message=msg)
61 return HttpResponseRedirect(get_next_url(request))
63 form = FeedbackForm(initial={'next':get_next_url(request)})
65 return render_to_response('feedback.html', {'form': form}, context_instance=RequestContext(request))
67 feedback.CANCEL_MESSAGE=_('We look forward to hearing your feedback! Please, give it next time :)')
70 return render_to_response('privacy.html', context_instance=RequestContext(request))
73 return render_to_response('logout.html', {
74 'next' : get_next_url(request),
75 }, context_instance=RequestContext(request))
78 badges = [b.ondb for b in sorted(BadgesMeta.by_id.values(), lambda b1, b2: cmp(b1.name, b2.name))]
80 if request.user.is_authenticated():
81 my_badges = Award.objects.filter(user=request.user).values('badge_id').distinct()
85 return render_to_response('badges.html', {
87 'mybadges' : my_badges,
88 }, context_instance=RequestContext(request))
90 def badge(request, id, slug):
91 badge = Badge.objects.get(id=id)
92 awards = list(Award.objects.filter(badge=badge).order_by('user', 'awarded_at'))
93 award_count = len(awards)
95 awards = sorted([dict(count=len(list(g)), user=k) for k, g in groupby(awards, lambda a: a.user)],
96 lambda c1, c2: c2['count'] - c1['count'])
98 return render_to_response('badge.html', {
99 'award_count': award_count,
102 }, context_instance=RequestContext(request))
104 def page(request, path):
105 if path in settings.STATIC_PAGE_REGISTRY:
107 page = Page.objects.get(id=settings.STATIC_PAGE_REGISTRY[path])
109 if (not page.published) and (not request.user.is_superuser):
116 template = page.extra.get('template', 'default')
117 sidebar = page.extra.get('sidebar', '')
119 if template == 'default':
120 base = 'base_content.html'
121 elif template == 'sidebar':
124 sidebar_render = page.extra.get('render', 'markdown')
126 if sidebar_render == 'markdown':
127 sidebar = page._as_markdown(sidebar)
128 elif sidebar_render == 'html':
129 sidebar = mark_safe(sidebar)
132 return HttpResponse(page.body, mimetype=page.extra.get('mimetype', 'text/html'))
134 render = page.extra.get('render', 'markdown')
136 if render == 'markdown':
137 body = page.as_markdown()
138 elif render == 'html':
139 body = mark_safe(page.body)
143 return render_to_response('page.html', {
148 }, context_instance=RequestContext(request))