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