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
22 return HttpResponseRedirect(str(settings.APP_FAVICON))
24 def custom_css(request):
25 return HttpResponse(unicode(settings.CUSTOM_CSS), mimetype="text/css")
27 def static(request, title, content):
28 return render_to_response('static.html', {'content' : content, 'title': title}, context_instance=RequestContext(request))
30 def media(request, skin, path):
31 return serve(request, "%s/media/%s" % (skin, path),
32 document_root=os.path.join(os.path.dirname(os.path.dirname(__file__)),'skins').replace('\\','/'))
34 def markdown_help(request):
35 return render_to_response('markdown_help.html', context_instance=RequestContext(request))
38 def opensearch(request):
39 return render_to_response('opensearch.html', {'settings' : settings}, context_instance=RequestContext(request))
42 def feedback(request):
43 if request.method == "POST":
44 form = FeedbackForm(request.POST)
46 context = {'user': request.user}
48 if not request.user.is_authenticated:
49 context['email'] = form.cleaned_data.get('email',None)
50 context['message'] = form.cleaned_data['message']
51 context['name'] = form.cleaned_data.get('name',None)
52 context['ip'] = request.META['REMOTE_ADDR']
54 recipients = User.objects.filter(is_superuser=True)
55 send_template_email(recipients, "notifications/feedback.html", context)
57 msg = _('Thanks for the feedback!')
58 request.user.message_set.create(message=msg)
59 return HttpResponseRedirect(get_next_url(request))
61 form = FeedbackForm(initial={'next':get_next_url(request)})
63 return render_to_response('feedback.html', {'form': form}, context_instance=RequestContext(request))
64 feedback.CANCEL_MESSAGE=_('We look forward to hearing your feedback! Please, give it next time :)')
67 return render_to_response('privacy.html', context_instance=RequestContext(request))
70 return render_to_response('logout.html', {
71 'next' : get_next_url(request),
72 }, context_instance=RequestContext(request))
75 badges = [b.ondb for b in sorted(BadgesMeta.by_id.values(), lambda b1, b2: cmp(b1.name, b2.name))]
77 if request.user.is_authenticated():
78 my_badges = Award.objects.filter(user=request.user).values('badge_id').distinct()
82 return render_to_response('badges.html', {
84 'mybadges' : my_badges,
85 }, context_instance=RequestContext(request))
87 def badge(request, id, slug):
88 badge = Badge.objects.get(id=id)
89 awards = list(Award.objects.filter(badge=badge).order_by('user', 'awarded_at'))
90 award_count = len(awards)
92 awards = sorted([dict(count=len(list(g)), user=k) for k, g in groupby(awards, lambda a: a.user)],
93 lambda c1, c2: c2['count'] - c1['count'])
95 return render_to_response('badge.html', {
96 'award_count': award_count,
99 }, context_instance=RequestContext(request))
101 def page(request, path):
102 if path in settings.STATIC_PAGE_REGISTRY:
104 page = Page.objects.get(id=settings.STATIC_PAGE_REGISTRY[path])
106 if not page.published or request.user.is_superuser:
113 template = page.extra.get('template', 'default')
114 sidebar = page.extra.get('sidebar', '')
116 if template == 'default':
117 base = 'base_content.html'
118 elif template == 'sidebar':
121 sidebar_render = page.extra.get('render', 'markdown')
123 if sidebar_render == 'markdown':
124 sidebar = page._as_markdown(sidebar)
125 elif sidebar_render == 'html':
126 sidebar = mark_safe(sidebar)
129 return HttpResponse(page.body)
131 render = page.extra.get('render', 'markdown')
133 if render == 'markdown':
134 body = page.as_markdown()
135 elif render == 'html':
136 body = mark_safe(page.body)
140 return render_to_response('page.html', {
145 }, context_instance=RequestContext(request))