]> git.openstreetmap.org Git - osqa.git/blob - forum/views/meta.py
Fixing OSQA 258, Original templates can be directly accessed by public viewers.
[osqa.git] / forum / views / meta.py
1 import os
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
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.utils.forms import get_next_url
14 from forum.models import Badge, Award, User
15 from forum.badges.base import BadgesMeta
16 from forum import settings
17 from forum.utils.mail import send_email
18 import re
19
20 def favicon(request):
21     return HttpResponseRedirect(str(settings.APP_FAVICON))
22
23 def static(request, title, content):
24     return render_to_response('static.html', {'content' : content, 'title': title}, context_instance=RequestContext(request))
25
26 def media(request, skin, path):
27     return serve(request, "%s/media/%s" % (skin, path), 
28                  document_root=os.path.join(os.path.dirname(os.path.dirname(__file__)),'skins').replace('\\','/'))
29
30 def markdown_help(request):
31     # md = markdown.Markdown([SettingsExtension({})])
32     # text = md.convert(settings.FAQ_PAGE_TEXT.value)
33
34     return render_to_response('markdown_help.html', context_instance=RequestContext(request))
35
36
37 def opensearch(request):   
38     return render_to_response('opensearch.html', {'settings' : settings}, context_instance=RequestContext(request))
39     
40
41 def feedback(request):
42     if request.method == "POST":
43         form = FeedbackForm(request.POST)
44         if form.is_valid():
45             context = {'user': request.user}
46
47             if not request.user.is_authenticated:
48                 context['email'] = form.cleaned_data.get('email',None)
49             context['message'] = form.cleaned_data['message']
50             context['name'] = form.cleaned_data.get('name',None)
51             context['ip'] = request.META['REMOTE_ADDR']
52
53             recipients = [(adm.username, adm.email) for adm in User.objects.filter(is_superuser=True)]
54
55             send_email(settings.EMAIL_SUBJECT_PREFIX + _("Feedback message from %(site_name)s") % {'site_name': settings.APP_SHORT_NAME},
56                        recipients, "notifications/feedback.html", context)
57             
58             msg = _('Thanks for the feedback!')
59             request.user.message_set.create(message=msg)
60             return HttpResponseRedirect(get_next_url(request))
61     else:
62         form = FeedbackForm(initial={'next':get_next_url(request)})
63
64     return render_to_response('feedback.html', {'form': form}, context_instance=RequestContext(request))
65 feedback.CANCEL_MESSAGE=_('We look forward to hearing your feedback! Please, give it next time :)')
66
67 def privacy(request):
68     return render_to_response('privacy.html', context_instance=RequestContext(request))
69
70 def logout(request):
71     return render_to_response('logout.html', {
72         'next' : get_next_url(request),
73     }, context_instance=RequestContext(request))
74
75 def badges(request):
76     badges = [b.ondb for b in sorted(BadgesMeta.by_id.values(), lambda b1, b2: cmp(b1.name, b2.name))]
77     
78     if request.user.is_authenticated():
79         my_badges = Award.objects.filter(user=request.user).values('badge_id').distinct()
80     else:
81         my_badges = []
82
83     return render_to_response('badges.html', {
84         'badges' : badges,
85         'mybadges' : my_badges,
86     }, context_instance=RequestContext(request))
87
88 def badge(request, id, slug):
89     badge = Badge.objects.get(id=id)
90     awards = list(Award.objects.filter(badge=badge).order_by('user', 'awarded_at'))
91     award_count = len(awards)
92     
93     awards = sorted([dict(count=len(list(g)), user=k) for k, g in groupby(awards, lambda a: a.user)],
94                     lambda c1, c2: c2['count'] - c1['count'])
95
96     return render_to_response('badge.html', {
97         'award_count': award_count,
98         'awards' : awards,
99         'badge' : badge,
100     }, context_instance=RequestContext(request))
101