]> git.openstreetmap.org Git - osqa.git/blob - forum/views/meta.py
General cleanup. Moved many hardcoded stuff still left in the code to an admin option...
[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
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 from forum.settings.settingsmarkdown import SettingsExtension, markdown
17 import re
18
19 def favicon(request):
20     return HttpResponseRedirect(str(settings.APP_FAVICON))
21
22 def about(request):
23     return render_to_response('about.html', {'text': settings.ABOUT_PAGE_TEXT.value }, context_instance=RequestContext(request))
24
25 def faq(request):
26     md = markdown.Markdown([SettingsExtension({})])
27     text = md.convert(settings.FAQ_PAGE_TEXT.value)
28
29     return render_to_response('faq.html', {'text' : text}, context_instance=RequestContext(request))
30
31 def feedback(request):
32     if request.method == "POST":
33         form = FeedbackForm(request.POST)
34         if form.is_valid():
35             context = {'user': request.user}
36
37             if not request.user.is_authenticated:
38                 context['email'] = form.cleaned_data.get('email',None)
39             context['message'] = form.cleaned_data['message']
40             context['name'] = form.cleaned_data.get('name',None)
41
42             recipients = [(adm.username, adm.email) for adm in User.objects.filter(is_superuser=True)]
43
44             send_email(settings.EMAIL_SUBJECT_PREFIX + _("Feedback message from %(site_name)s") % {'site_name': settings.APP_SHORT_NAME},
45                        recipients, "notifications/feedback.html", context)
46             
47             msg = _('Thanks for the feedback!')
48             request.user.message_set.create(message=msg)
49             return HttpResponseRedirect(get_next_url(request))
50     else:
51         form = FeedbackForm(initial={'next':get_next_url(request)})
52
53     return render_to_response('feedback.html', {'form': form}, context_instance=RequestContext(request))
54 feedback.CANCEL_MESSAGE=_('We look forward to hearing your feedback! Please, give it next time :)')
55
56 def privacy(request):
57     return render_to_response('privacy.html', context_instance=RequestContext(request))
58
59 def logout(request):
60     return render_to_response('logout.html', {
61         'next' : get_next_url(request),
62     }, context_instance=RequestContext(request))
63
64 def badges(request):
65     badges = [b.ondb for b in sorted(BadgesMeta.by_id.values(), lambda b1, b2: cmp(b1.name, b2.name))]
66     
67     if request.user.is_authenticated():
68         my_badges = Award.objects.filter(user=request.user).values('badge_id').distinct()
69     else:
70         my_badges = []
71
72     return render_to_response('badges.html', {
73         'badges' : badges,
74         'mybadges' : my_badges,
75         'feedback_faq_url' : reverse('feedback'),
76     }, context_instance=RequestContext(request))
77
78 def badge(request, id, slug):
79     badge = Badge.objects.get(id=id)
80     awards = list(Award.objects.filter(badge=badge).order_by('user', 'awarded_at'))
81     award_count = len(awards)
82     
83     awards = sorted([dict(count=len(list(g)), user=k) for k, g in groupby(awards, lambda a: a.user)],
84                     lambda c1, c2: c2['count'] - c1['count'])
85
86     return render_to_response('badge.html', {
87         'award_count': award_count,
88         'awards' : awards,
89         'badge' : badge,
90     }, context_instance=RequestContext(request))
91