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