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