]> git.openstreetmap.org Git - osqa.git/blobdiff - forum/views/meta.py
We update "karma" to "reputation", as in the translation file.
[osqa.git] / forum / views / meta.py
index 6417e8f51cd110fc6c6b00a50bdb2cb7ee9c7ec2..2177301d48b3bf723a254815aef85106a08484d0 100644 (file)
+import os
+from itertools import groupby
 from django.shortcuts import render_to_response, get_object_or_404
 from django.core.urlresolvers import reverse
-from django.template import RequestContext
-from django.http import HttpResponseRedirect, HttpResponse
+from django.template import RequestContext, loader
+from django.http import HttpResponseRedirect, HttpResponse, Http404
+from django.views.static import serve
+from forum import settings
+from forum.modules import decorate
+from forum.views.decorators import login_required
 from forum.forms import FeedbackForm
 from django.core.urlresolvers import reverse
-from django.core.mail import mail_admins
 from django.utils.translation import ugettext as _
-from forum.utils.forms import get_next_url
-from forum.models import Badge, Award
+from django.db.models import Count
+from forum.forms import get_next_url
+from forum.models import Badge, Award, User, Page
+from forum.badges.base import BadgesMeta
+from forum import settings
+from forum.utils.mail import send_template_email
+from django.utils.safestring import mark_safe
+from forum.templatetags.extra_filters import or_preview
+import decorators
+import re
 
-def about(request):
-    return render_to_response('about.html', context_instance=RequestContext(request))
+def favicon(request):
+    return HttpResponseRedirect(str(settings.APP_FAVICON))
+
+def custom_css(request):
+    return HttpResponse(or_preview(settings.CUSTOM_CSS, request), mimetype="text/css")
+
+def static(request, title, content):
+    return render_to_response('static.html', {'content' : content, 'title': title},
+                              context_instance=RequestContext(request))
+
+def media(request, skin, path):
+    return serve(request, "%s/media/%s" % (skin, path),
+                 document_root=os.path.join(os.path.dirname(os.path.dirname(__file__)), 'skins').replace('\\', '/'))
+
+def markdown_help(request):
+    return render_to_response('markdown_help.html', context_instance=RequestContext(request))
+
+
+def opensearch(request):
+    return render_to_response('opensearch.html', {'settings' : settings}, context_instance=RequestContext(request))
 
-def faq(request):
-    data = {
-        'gravatar_faq_url': reverse('faq') + '#gravatar',
-        #'send_email_key_url': reverse('send_email_key'),
-        'ask_question_url': reverse('ask'),
-    }
-    return render_to_response('faq.html', data, context_instance=RequestContext(request))
 
 def feedback(request):
-    data = {}
-    form = None
     if request.method == "POST":
-        form = FeedbackForm(request.POST)
+        form = FeedbackForm(request.user, data=request.POST)
         if form.is_valid():
-            if not request.user.is_authenticated:
-                data['email'] = form.cleaned_data.get('email',None)
-            data['message'] = form.cleaned_data['message']
-            data['name'] = form.cleaned_data.get('name',None)
-            message = render_to_response('feedback_email.txt',data,context_instance=RequestContext(request))
-            mail_admins(_('Q&A forum feedback'), message)
+            context = {
+                 'user': request.user,
+                 'email': request.user.is_authenticated() and request.user.email or form.cleaned_data.get('email', None),
+                 'message': form.cleaned_data['message'],
+                 'name': request.user.is_authenticated() and request.user.username or form.cleaned_data.get('name', None),
+                 'ip': request.META['REMOTE_ADDR'],
+            }
+
+            recipients = User.objects.filter(is_superuser=True)
+            send_template_email(recipients, "notifications/feedback.html", context)
+
             msg = _('Thanks for the feedback!')
             request.user.message_set.create(message=msg)
             return HttpResponseRedirect(get_next_url(request))
     else:
-        form = FeedbackForm(initial={'next':get_next_url(request)})
+        form = FeedbackForm(request.user, initial={'next':get_next_url(request)})
+
+    return render_to_response('feedback.html', {'form': form}, context_instance=RequestContext(request))
 
-    data['form'] = form
-    return render_to_response('feedback.html', data, context_instance=RequestContext(request))
 feedback.CANCEL_MESSAGE=_('We look forward to hearing your feedback! Please, give it next time :)')
 
 def privacy(request):
     return render_to_response('privacy.html', context_instance=RequestContext(request))
 
-def logout(request):#refactor/change behavior?
-#currently you click logout and you get
-#to this view which actually asks you again - do you really want to log out?
-#I guess rationale was to tell the user that s/he may be still logged in
-#through their external login sytem and we'd want to remind them about it
-#however it might be a little annoying
-#why not just show a message: you are logged out of osqa, but
-#if you really want to log out -> go to your openid provider
+@decorate.withfn(login_required)
+def logout(request):
     return render_to_response('logout.html', {
-        'next' : get_next_url(request),
+    'next' : get_next_url(request),
     }, context_instance=RequestContext(request))
 
-def badges(request):#user status/reputation system
-    badges = Badge.objects.all().order_by('name')
-    my_badges = []
+@decorators.render('badges.html', 'badges', _('badges'), weight=300)
+def badges(request):
+    badges = [b.ondb for b in sorted(BadgesMeta.by_id.values(), lambda b1, b2: cmp(b1.name, b2.name))]
+
     if request.user.is_authenticated():
-        my_badges = Award.objects.filter(user=request.user).values('badge_id')
-        #my_badges.query.group_by = ['badge_id']
+        my_badges = Award.objects.filter(user=request.user).values('badge_id').distinct()
+    else:
+        my_badges = []
 
-    return render_to_response('badges.html', {
+    return {
         'badges' : badges,
         'mybadges' : my_badges,
-        'feedback_faq_url' : reverse('feedback'),
-    }, context_instance=RequestContext(request))
+    }
+
+def badge(request, id, slug):
+    badge = Badge.objects.get(id=id)
+    awards = list(Award.objects.filter(badge=badge).order_by('user', 'awarded_at'))
+    award_count = len(awards)
 
-def badge(request, id):
-    badge = get_object_or_404(Badge, id=id)
-    awards = Award.objects.extra(
-        select={'id': 'auth_user.id', 
-                'name': 'auth_user.username', 
-                'rep':'auth_user.reputation', 
-                'gold': 'auth_user.gold', 
-                'silver': 'auth_user.silver', 
-                'bronze': 'auth_user.bronze'},
-        tables=['award', 'auth_user'],
-        where=['badge_id=%s AND user_id=auth_user.id'],
-        params=[id]
-    ).distinct('id')
+    awards = sorted([dict(count=len(list(g)), user=k) for k, g in groupby(awards, lambda a: a.user)],
+                    lambda c1, c2: c2['count'] - c1['count'])
 
     return render_to_response('badge.html', {
-        'awards' : awards,
-        'badge' : badge,
+    'award_count': award_count,
+    'awards' : awards,
+    'badge' : badge,
     }, context_instance=RequestContext(request))
 
+def page(request, path):
+    if path in settings.STATIC_PAGE_REGISTRY:
+        try:
+            page = Page.objects.get(id=settings.STATIC_PAGE_REGISTRY[path])
+
+            if (not page.published) and (not request.user.is_superuser):
+                raise Http404
+        except:
+            raise Http404
+    else:
+        raise Http404
+
+    template = page.extra.get('template', 'default')
+    sidebar = page.extra.get('sidebar', '')
+
+    if template == 'default':
+        base = 'base_content.html'
+    elif template == 'sidebar':
+        base = 'base.html'
+
+        sidebar_render = page.extra.get('render', 'markdown')
+
+        if sidebar_render == 'markdown':
+            sidebar = page._as_markdown(sidebar)
+        elif sidebar_render == 'html':
+            sidebar = mark_safe(sidebar)
+
+    else:
+        return HttpResponse(page.body, mimetype=page.extra.get('mimetype', 'text/html'))
+
+    render = page.extra.get('render', 'markdown')
+
+    if render == 'markdown':
+        body = page.as_markdown()
+    elif render == 'html':
+        body = mark_safe(page.body)
+    else:
+        body = page.body
+
+    return render_to_response('page.html', {
+    'page' : page,
+    'body' : body,
+    'sidebar': sidebar,
+    'base': base,
+    }, context_instance=RequestContext(request))
+
+