]> git.openstreetmap.org Git - osqa.git/blob - forum/views/meta.py
97c852f0a7e5d6194eb07367ab254391bb085f1b
[osqa.git] / forum / views / meta.py
1 from itertools import groupby
2 from django.shortcuts import render_to_response, get_object_or_404
3 from django.core.urlresolvers import reverse
4 from django.template import RequestContext, loader
5 from django.http import HttpResponseRedirect, HttpResponse
6 from forum import settings
7 from forum.forms import FeedbackForm
8 from django.core.urlresolvers import reverse
9 from django.utils.translation import ugettext as _
10 from django.db.models import Count
11 from forum.utils.forms import get_next_url
12 from forum.models import Badge, Award, User
13 from forum.badges.base import BadgesMeta
14 from forum import settings
15 from forum.utils.mail import send_email
16 import re
17
18 def favicon(request):
19     return HttpResponseRedirect(str(settings.APP_FAVICON))
20
21 def static(request, title, content):
22     return render_to_response('static.html', {'content' : content, 'title': title}, context_instance=RequestContext(request))
23
24 def markdown_help(request):
25     # md = markdown.Markdown([SettingsExtension({})])
26     # text = md.convert(settings.FAQ_PAGE_TEXT.value)
27
28     return render_to_response('markdown_help.html', context_instance=RequestContext(request))
29
30
31 def opensearch(request):   
32     return render_to_response('opensearch.html', {'settings' : settings}, context_instance=RequestContext(request))
33     
34
35 def feedback(request):
36     if request.method == "POST":
37         form = FeedbackForm(request.POST)
38         if form.is_valid():
39             context = {'user': request.user}
40
41             if not request.user.is_authenticated:
42                 context['email'] = form.cleaned_data.get('email',None)
43             context['message'] = form.cleaned_data['message']
44             context['name'] = form.cleaned_data.get('name',None)
45             context['ip'] = request.META['REMOTE_ADDR']
46
47             recipients = [(adm.username, adm.email) for adm in User.objects.filter(is_superuser=True)]
48
49             send_email(settings.EMAIL_SUBJECT_PREFIX + _("Feedback message from %(site_name)s") % {'site_name': settings.APP_SHORT_NAME},
50                        recipients, "notifications/feedback.html", context)
51             
52             msg = _('Thanks for the feedback!')
53             request.user.message_set.create(message=msg)
54             return HttpResponseRedirect(get_next_url(request))
55     else:
56         form = FeedbackForm(initial={'next':get_next_url(request)})
57
58     return render_to_response('feedback.html', {'form': form}, context_instance=RequestContext(request))
59 feedback.CANCEL_MESSAGE=_('We look forward to hearing your feedback! Please, give it next time :)')
60
61 def privacy(request):
62     return render_to_response('privacy.html', context_instance=RequestContext(request))
63
64 def logout(request):
65     return render_to_response('logout.html', {
66         'next' : get_next_url(request),
67     }, context_instance=RequestContext(request))
68
69 def badges(request):
70     badges = [b.ondb for b in sorted(BadgesMeta.by_id.values(), lambda b1, b2: cmp(b1.name, b2.name))]
71     
72     if request.user.is_authenticated():
73         my_badges = Award.objects.filter(user=request.user).values('badge_id').distinct()
74     else:
75         my_badges = []
76
77     return render_to_response('badges.html', {
78         'badges' : badges,
79         'mybadges' : my_badges,
80     }, context_instance=RequestContext(request))
81
82 def badge(request, id, slug):
83     badge = Badge.objects.get(id=id)
84     awards = list(Award.objects.filter(badge=badge).order_by('user', 'awarded_at'))
85     award_count = len(awards)
86     
87     awards = sorted([dict(count=len(list(g)), user=k) for k, g in groupby(awards, lambda a: a.user)],
88                     lambda c1, c2: c2['count'] - c1['count'])
89
90     return render_to_response('badge.html', {
91         'award_count': award_count,
92         'awards' : awards,
93         'badge' : badge,
94     }, context_instance=RequestContext(request))
95