Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Added the first bits of some hacking to get more user info into Trac.

  • Loading branch information...
commit e2b3912d27769bf60f4af68d06abe4337f45bba7 1 parent 2d2ba22
Jacob Kaplan-Moss authored September 11, 2011
11  django_website/accounts/profile_urls.py
... ...
@@ -0,0 +1,11 @@
  1
+"""
  2
+URLs for the profile pages (under ~)
  3
+"""
  4
+
  5
+from __future__ import absolute_import
  6
+from django.conf.urls.defaults import patterns, url, include
  7
+from . import views as account_views
  8
+
  9
+urlpatterns = patterns('', 
  10
+    r'^(?P<username>[\w-]+)/$', account_views.user_profile, name='user_profile'),
  11
+)
2  django_website/accounts/urls.py
@@ -2,6 +2,7 @@
2 2
 
3 3
 from django.conf.urls.defaults import patterns, url, include
4 4
 from registration.forms import RegistrationFormUniqueEmail
  5
+from . import views as account_views
5 6
 
6 7
 urlpatterns = patterns('',
7 8
     url(
@@ -19,5 +20,6 @@
19 20
         r'^reset/done/$',
20 21
         'django.contrib.auth.views.password_reset_complete'
21 22
     ),
  23
+    url(r'^_trac/userinfo/$', account_views.json_user_info),
22 24
     (r'', include('registration.urls')),
23 25
 )
50  django_website/accounts/views.py
... ...
@@ -1,6 +1,10 @@
1 1
 import hashlib
  2
+import json
2 3
 from django.shortcuts import render, get_object_or_404
3 4
 from django.contrib.auth.models import User
  5
+from django.conf import settings
  6
+from django.core import cache
  7
+from django.http import HttpResponse
4 8
 from ..cla.models import find_agreements
5 9
 
6 10
 def user_profile(request, username):
@@ -12,3 +16,49 @@ def user_profile(request, username):
12 16
         'clas': find_agreements(u),
13 17
     }
14 18
     return render(request, "accounts/user_profile.html", ctx)
  19
+
  20
+def json_user_info(request):
  21
+    """
  22
+    Return info about some users as a JSON object.
  23
+
  24
+    Part of a set of hacks that feed more info into Trac. This takes
  25
+    a list of users as GET['user'] and returns a JSON object::
  26
+
  27
+        {
  28
+            {USERNAME: {"core": false, "cla": true}},
  29
+            {USERNAME: {"core": false, "cla": true}},
  30
+            ...
  31
+        }
  32
+
  33
+    De-duplication on GET['user'] is performed since I don't want to have to
  34
+    think about how best to do it in JavaScript :)
  35
+    """
  36
+
  37
+    userinfo = dict([
  38
+                (name, get_user_info(name))
  39
+                for name in set(request.GET.getlist('user'))])
  40
+    return JSONResponse(userinfo)
  41
+
  42
+def get_user_info(username):
  43
+    c = cache.get_cache('default')
  44
+    key = 'trac_user_info:%s' % hashlib.md5(username).hexdigest()
  45
+    info = c.get(key)
  46
+    if info is None:
  47
+        try:
  48
+            u = User.objects.get(username=username)
  49
+        except User.DoesNotExist:
  50
+            info = {"core": False, "cla": False}
  51
+        else:
  52
+            info = {
  53
+                "core": u.has_perm('auth.commit'),
  54
+                "cla": bool(find_agreements(u))
  55
+            }
  56
+        c.set(key, info, 60*60)
  57
+    return info
  58
+
  59
+class JSONResponse(HttpResponse):
  60
+    def __init__(self, obj):
  61
+        super(JSONResponse, self).__init__(
  62
+            json.dumps(obj, indent=(2 if settings.DEBUG else None)),
  63
+            content_type='application/json',
  64
+        )

0 notes on commit e2b3912

Please sign in to comment.
Something went wrong with that request. Please try again.