Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

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

  • Loading branch information...
commit e2b3912d27769bf60f4af68d06abe4337f45bba7 1 parent 2d2ba22
@jacobian jacobian authored
View
11 django_website/accounts/profile_urls.py
@@ -0,0 +1,11 @@
+"""
+URLs for the profile pages (under ~)
+"""
+
+from __future__ import absolute_import
+from django.conf.urls.defaults import patterns, url, include
+from . import views as account_views
+
+urlpatterns = patterns('',
+ r'^(?P<username>[\w-]+)/$', account_views.user_profile, name='user_profile'),
+)
View
2  django_website/accounts/urls.py
@@ -2,6 +2,7 @@
from django.conf.urls.defaults import patterns, url, include
from registration.forms import RegistrationFormUniqueEmail
+from . import views as account_views
urlpatterns = patterns('',
url(
@@ -19,5 +20,6 @@
r'^reset/done/$',
'django.contrib.auth.views.password_reset_complete'
),
+ url(r'^_trac/userinfo/$', account_views.json_user_info),
(r'', include('registration.urls')),
)
View
50 django_website/accounts/views.py
@@ -1,6 +1,10 @@
import hashlib
+import json
from django.shortcuts import render, get_object_or_404
from django.contrib.auth.models import User
+from django.conf import settings
+from django.core import cache
+from django.http import HttpResponse
from ..cla.models import find_agreements
def user_profile(request, username):
@@ -12,3 +16,49 @@ def user_profile(request, username):
'clas': find_agreements(u),
}
return render(request, "accounts/user_profile.html", ctx)
+
+def json_user_info(request):
+ """
+ Return info about some users as a JSON object.
+
+ Part of a set of hacks that feed more info into Trac. This takes
+ a list of users as GET['user'] and returns a JSON object::
+
+ {
+ {USERNAME: {"core": false, "cla": true}},
+ {USERNAME: {"core": false, "cla": true}},
+ ...
+ }
+
+ De-duplication on GET['user'] is performed since I don't want to have to
+ think about how best to do it in JavaScript :)
+ """
+
+ userinfo = dict([
+ (name, get_user_info(name))
+ for name in set(request.GET.getlist('user'))])
+ return JSONResponse(userinfo)
+
+def get_user_info(username):
+ c = cache.get_cache('default')
+ key = 'trac_user_info:%s' % hashlib.md5(username).hexdigest()
+ info = c.get(key)
+ if info is None:
+ try:
+ u = User.objects.get(username=username)
+ except User.DoesNotExist:
+ info = {"core": False, "cla": False}
+ else:
+ info = {
+ "core": u.has_perm('auth.commit'),
+ "cla": bool(find_agreements(u))
+ }
+ c.set(key, info, 60*60)
+ return info
+
+class JSONResponse(HttpResponse):
+ def __init__(self, obj):
+ super(JSONResponse, self).__init__(
+ json.dumps(obj, indent=(2 if settings.DEBUG else None)),
+ content_type='application/json',
+ )
Please sign in to comment.
Something went wrong with that request. Please try again.