]> git.openstreetmap.org Git - osqa.git/blob - forum/views/meta.py
8195e44f896b1478c4d8a5585c3048c9b93448a2
[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, 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
20 import decorators
21 import re
22
23 def favicon(request):
24     return HttpResponseRedirect(str(settings.APP_FAVICON))
25
26 def custom_css(request):
27     return HttpResponse(or_preview(settings.CUSTOM_CSS, request), mimetype="text/css")
28
29 def static(request, title, content):
30     return render_to_response('static.html', {'content' : content, 'title': title},
31                               context_instance=RequestContext(request))
32
33 def media(request, skin, path):
34     return serve(request, "%s/media/%s" % (skin, path),
35                  document_root=os.path.join(os.path.dirname(os.path.dirname(__file__)), 'skins').replace('\\', '/'))
36
37 def markdown_help(request):
38     return render_to_response('markdown_help.html', context_instance=RequestContext(request))
39
40
41 def opensearch(request):
42     return render_to_response('opensearch.html', {'settings' : settings}, context_instance=RequestContext(request))
43
44
45 def feedback(request):
46     if request.method == "POST":
47         form = FeedbackForm(request.POST)
48         if form.is_valid():
49             context = {'user': request.user}
50
51             if not request.user.is_authenticated:
52                 context['email'] = form.cleaned_data.get('email', None)
53             context['message'] = form.cleaned_data['message']
54             context['name'] = form.cleaned_data.get('name', None)
55             context['ip'] = request.META['REMOTE_ADDR']
56
57             recipients = User.objects.filter(is_superuser=True)
58             send_template_email(recipients, "notifications/feedback.html", context)
59
60             msg = _('Thanks for the feedback!')
61             request.user.message_set.create(message=msg)
62             return HttpResponseRedirect(get_next_url(request))
63     else:
64         form = FeedbackForm(initial={'next':get_next_url(request)})
65
66     return render_to_response('feedback.html', {'form': form}, context_instance=RequestContext(request))
67
68 feedback.CANCEL_MESSAGE=_('We look forward to hearing your feedback! Please, give it next time :)')
69
70 def privacy(request):
71     return render_to_response('privacy.html', context_instance=RequestContext(request))
72
73 def logout(request):
74     return render_to_response('logout.html', {
75     'next' : get_next_url(request),
76     }, context_instance=RequestContext(request))
77
78 @decorators.render('badges.html', 'badges', _('badges'), weight=300)
79 def badges(request):
80     badges = [b.ondb for b in sorted(BadgesMeta.by_id.values(), lambda b1, b2: cmp(b1.name, b2.name))]
81
82     if request.user.is_authenticated():
83         my_badges = Award.objects.filter(user=request.user).values('badge_id').distinct()
84     else:
85         my_badges = []
86
87     return {
88         'badges' : badges,
89         'mybadges' : my_badges,
90     }
91
92 def badge(request, id, slug):
93     badge = Badge.objects.get(id=id)
94     awards = list(Award.objects.filter(badge=badge).order_by('user', 'awarded_at'))
95     award_count = len(awards)
96
97     awards = sorted([dict(count=len(list(g)), user=k) for k, g in groupby(awards, lambda a: a.user)],
98                     lambda c1, c2: c2['count'] - c1['count'])
99
100     return render_to_response('badge.html', {
101     'award_count': award_count,
102     'awards' : awards,
103     'badge' : badge,
104     }, context_instance=RequestContext(request))
105
106 def page(request, path):
107     if path in settings.STATIC_PAGE_REGISTRY:
108         try:
109             page = Page.objects.get(id=settings.STATIC_PAGE_REGISTRY[path])
110
111             if (not page.published) and (not request.user.is_superuser):
112                 raise Http404
113         except:
114             raise Http404
115     else:
116         raise Http404
117
118     template = page.extra.get('template', 'default')
119     sidebar = page.extra.get('sidebar', '')
120
121     if template == 'default':
122         base = 'base_content.html'
123     elif template == 'sidebar':
124         base = 'base.html'
125
126         sidebar_render = page.extra.get('render', 'markdown')
127
128         if sidebar_render == 'markdown':
129             sidebar = page._as_markdown(sidebar)
130         elif sidebar_render == 'html':
131             sidebar = mark_safe(sidebar)
132
133     else:
134         return HttpResponse(page.body, mimetype=page.extra.get('mimetype', 'text/html'))
135
136     render = page.extra.get('render', 'markdown')
137
138     if render == 'markdown':
139         body = page.as_markdown()
140     elif render == 'html':
141         body = mark_safe(page.body)
142     else:
143         body = page.body
144
145     return render_to_response('page.html', {
146     'page' : page,
147     'body' : body,
148     'sidebar': sidebar,
149     'base': base,
150     }, context_instance=RequestContext(request))
151
152