summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLeif Johansson <leifj@sunet.se>2011-11-08 19:15:22 +0100
committerLeif Johansson <leifj@sunet.se>2011-11-08 19:15:22 +0100
commitfcfd994182d18c10199628427484b3da997fbc33 (patch)
treebcaa7246187b5c8ef317c6ba581c2bcf16a9f871
parent8c9274ab6185ca6e96377f50927712860a2868ab (diff)
enable new opensocial code
-rw-r--r--coip/apps/activitystreams/views.py18
-rw-r--r--coip/apps/opensocial/urls.py13
-rw-r--r--coip/apps/opensocial/views.py77
-rw-r--r--coip/urls.py6
4 files changed, 106 insertions, 8 deletions
diff --git a/coip/apps/activitystreams/views.py b/coip/apps/activitystreams/views.py
index 235ecd8..e0c76a7 100644
--- a/coip/apps/activitystreams/views.py
+++ b/coip/apps/activitystreams/views.py
@@ -5,23 +5,27 @@ Created on Nov 8, 2011
'''
from django.shortcuts import get_object_or_404
from coip.apps.name.models import Name
-from coip.multiresponse import json_response
+from coip.multiresponse import json_response, render403
from django_oauth2_lite.decorators import oauth2_required
from actstream.models import Action
from django.http import HttpResponseNotFound
from coip.extensions.templatetags.userdisplay import userdisplay
+import socket
+
+def domain():
+ return socket.getfqdn(socket.gethostname())
def user_to_json(o):
return {
'objectType': 'person',
- 'id': o.username,
+ 'id': "%s:%s" % (domain(),o.username),
'displayName': userdisplay(o)
}
def name_to_json(o):
return {
'objectType': 'group',
- 'id': o.id,
+ 'id': "%s:%d" % (domain(),o.id),
'url': o.url(),
'displayName': o.shortname()
}
@@ -46,6 +50,12 @@ def activity_to_json(activity):
}
return r
+def collection_to_json(lst):
+ return {
+ 'totalItems': len(lst),
+ 'items': lst
+ }
+
@oauth2_required(scope='memberships')
def name(request,id):
name = get_object_or_404(Name,pk=id)
@@ -54,6 +64,6 @@ def name(request,id):
# check ownership
stream = Action.objects.stream_for_object_as_target(name)
if stream:
- return json_response([activity_to_json(activity) for activity in stream])
+ return json_response(collection_to_json([activity_to_json(activity) for activity in stream]))
else:
return HttpResponseNotFound() \ No newline at end of file
diff --git a/coip/apps/opensocial/urls.py b/coip/apps/opensocial/urls.py
new file mode 100644
index 0000000..67b1177
--- /dev/null
+++ b/coip/apps/opensocial/urls.py
@@ -0,0 +1,13 @@
+'''
+Created on Nov 7, 2011
+
+@author: leifj
+'''
+from django.conf.urls.defaults import patterns, url, include
+
+urlpatterns = patterns('coip.apps.opensocial.views',
+ url(r'^rpc$',view='rpc'),
+ url(r'^people/(?P<uid>.+)$', view='person'),
+ url(r'^people/(?P<uid>.+)/(?P<gid>.+)$', view='person'),
+ url(r'^activitystreams/', include('coip.apps.activitystreams.urls'))
+) \ No newline at end of file
diff --git a/coip/apps/opensocial/views.py b/coip/apps/opensocial/views.py
new file mode 100644
index 0000000..ff04505
--- /dev/null
+++ b/coip/apps/opensocial/views.py
@@ -0,0 +1,77 @@
+'''
+Created on Nov 8, 2011
+
+@author: leifj
+'''
+import re
+from django.contrib.auth.models import User
+from coip.multiresponse import json_response
+from coip.apps.activitystreams.views import object_to_json
+from django_oauth2_lite.decorators import oauth2_required
+from django.http import HttpResponseNotFound, HttpResponseBadRequest
+from coip.apps.name.models import Name
+import json
+from django.utils import simplejson
+
+def _resolve_user(request,uid):
+ if uid == '@me':
+ return request.user
+
+ if re.match('^[0-9]+$',uid):
+ try:
+ return User.objects.get(id=uid)
+ except User.DoesNotExist:
+ return None
+
+ try:
+ return User.objects.get(username=uid)
+ except User.DoesNotExist:
+ return None
+
+def _resolve_group(request,user,gid):
+ if gid == '@self':
+ return user.get_profile().home
+
+ if re.match('^[0-9]+$',gid):
+ try:
+ return Name.objects.get(id=gid)
+ except User.DoesNotExist:
+ return None
+ try:
+ return Name.objects.get(username=gid)
+ except User.DoesNotExist:
+ return None
+
+
+def _opensocial_response(lst):
+ return {
+ "startIndex": 0,
+ "totalResults": len(lst),
+ "entry": [
+ object_to_json(o) for o in lst
+ ]
+ }
+
+def rpc(request):
+ if request.method == 'POST' and request.META['CONTENT_TYPE'] == 'application/json':
+ rpc_request = simplejson.load(request)
+ if rpc_request['method'] == 'system.listMethods':
+ return json_response(['people.get','groups.get','system.listMethods'])
+ else:
+ return HttpResponseBadRequest()
+
+@oauth2_required(scope='opensocial')
+def person(request,uid,gid='@self'):
+ user = _resolve_user(request,uid)
+
+ if not user:
+ return HttpResponseNotFound()
+
+ name = _resolve_group(request,user,gid)
+
+ if not name:
+ return HttpResponseNotFound()
+
+ ##TODO - implement listing people based on group memberships
+ return json_response(_opensocial_response([user]))
+
diff --git a/coip/urls.py b/coip/urls.py
index d74a332..c5246e2 100644
--- a/coip/urls.py
+++ b/coip/urls.py
@@ -72,10 +72,8 @@ urlpatterns = patterns('',
(r'^rtree/(?P<id>[0-9]+).json$', 'coip.apps.name.views.rtree'),
# APIs
(r'^api/activitystreams/', include('coip.apps.activitystreams.urls')),
- (r'^api/opensocial/1.0/rpc', 'coip.apps.opensocial.common.system'),
- #(r'^opensocial/2.0/activitystreams', include(opensocial_v2_as.urls)),
- (r'^api/opensocial/', include(opensocial_v1.urls)),
+ (r'^api/opensocial/', include('coip.apps.opensocial.urls')),
(r'^api/hello/?', 'coip.apps.name.views.hello'),
- (r'^api/', include(v1_api.urls)),
+ #(r'^api/', include(v1_api.urls)),
(r'^oauth2/', include('django_oauth2_lite.urls'))
)