def grantPermissionOnContext(context, request): """ """ permission = request.matchdict.get('permission', None) if permission not in ['read', 'write', 'join', 'invite']: raise InvalidPermission, "There's not any permission named '%s'" % permission urlhash = request.matchdict.get('urlHash', None) subscription = None pointer = 0 while subscription == None and pointer < len(request.actor.subscribedTo['items']): if request.actor.subscribedTo['items'][pointer]['urlHash'] == urlhash: subscription = request.actor.subscribedTo['items'][pointer] pointer += 1 if not subscription: raise Unauthorized, "You can't set permissions on a context where you are not subscribed" #If we reach here, we are subscribed to a context and ready to set the permission permissions = subscription['permissions'] if permission in permissions: #Already have the permission code = 200 else: #Assign the permission code = 201 request.actor.grantPermission(subscription, permission) permissions.append(permission) handler = JSONResourceEntity(subscription, status_code=code) return handler.buildResponse()
def revokePermissionOnContext(context, request): """ """ permission = request.matchdict.get('permission', None) if permission not in ['read', 'write', 'join', 'invite']: raise InvalidPermission, "There's not any permission named '%s'" % permission urlhash = request.matchdict.get('urlHash', None) subscription = None pointer = 0 while subscription == None and pointer < len(request.actor.subscribedTo['items']): if request.actor.subscribedTo['items'][pointer]['urlHash'] == urlhash: subscription = request.actor.subscribedTo['items'][pointer] pointer += 1 if not subscription: raise Unauthorized, "You can't remove permissions on a context where you are not subscribed" #If we reach here, we are subscribed to a context and ready to remove the permission code = 200 permissions = subscription['permissions'] if permission in permissions: #We have the permission, let's delete it request.actor.revokePermission(subscription, permission) subscription['permissions'] = [a for a in permissions if permission != a] handler = JSONResourceEntity(subscription, status_code=code) return handler.buildResponse()
def addActivityComment(context, request): """ POST /activities/{activity}/comments """ #XXX TODO ara només es tracta la primera activitat, # s'ha de iterar si es vol que el comentari sigui de N activitats activityid = request.matchdict['activity'] mmdb = MADMaxDB(context.db) refering_activity = mmdb.activity[activityid] # Prepare rest parameters to be merged with post data rest_params = {'verb': 'comment', 'object': {'inReplyTo': [{'_id':ObjectId(activityid), 'objectType':refering_activity.object['objectType']}]}} # Initialize a Activity object from the request newactivity = Activity() newactivity.fromRequest(request, rest_params=rest_params) code = 201 newactivity_oid = newactivity.insert() newactivity['_id'] = newactivity_oid comment = dict(newactivity.object) comment['published'] = newactivity.published comment['author'] = request.actor comment['id'] = newactivity._id del comment['inReplyTo'] refering_activity.addComment(comment) handler = JSONResourceEntity(newactivity.flatten(), status_code=code) return handler.buildResponse()
def grantPermissionOnContext(context, request): """ """ permission = request.matchdict.get('permission', None) if permission not in ['read', 'write', 'join', 'invite']: raise InvalidPermission, "There's not any permission named '%s'" % permission urlhash = request.matchdict.get('urlHash', None) subscription = None pointer = 0 while subscription == None and pointer < len( request.actor.subscribedTo['items']): if request.actor.subscribedTo['items'][pointer]['urlHash'] == urlhash: subscription = request.actor.subscribedTo['items'][pointer] pointer += 1 if not subscription: raise Unauthorized, "You can't set permissions on a context where you are not subscribed" #If we reach here, we are subscribed to a context and ready to set the permission permissions = subscription['permissions'] if permission in permissions: #Already have the permission code = 200 else: #Assign the permission code = 201 request.actor.grantPermission(subscription, permission) permissions.append(permission) handler = JSONResourceEntity(subscription, status_code=code) return handler.buildResponse()
def addUser(context, request): """ """ username = request.matchdict['username'] rest_params = {'username': username} # Initialize a User object from the request newuser = User() newuser.fromRequest(request, rest_params=rest_params) # If we have the _id setted, then the object already existed in the DB, # otherwise, proceed to insert it into the DB # In both cases, respond with the JSON of the object and the appropiate # HTTP Status Code if newuser.get('_id'): # Already Exists code = 200 else: # New User code = 201 userid = newuser.insert() newuser['_id'] = userid handler = JSONResourceEntity(newuser.flatten(), status_code=code) return handler.buildResponse()
def addUserActivity(context, request): """ /users/{username}/activities Afegeix una activitat """ rest_params = {'actor': request.actor, 'verb': 'post'} # Initialize a Activity object from the request newactivity = Activity() newactivity.fromRequest(request, rest_params=rest_params) # If we have the _id setted, then the object already existed in the DB, # otherwise, proceed to insert it into the DB # In both cases, respond with the JSON of the object and the appropiate # HTTP Status Code if newactivity.get('_id'): # Already Exists code = 200 else: # New User code = 201 activity_oid = newactivity.insert() newactivity['_id'] = activity_oid handler = JSONResourceEntity(newactivity.flatten(), status_code=code) return handler.buildResponse()
def revokePermissionOnContext(context, request): """ """ permission = request.matchdict.get('permission', None) if permission not in ['read', 'write', 'join', 'invite']: raise InvalidPermission, "There's not any permission named '%s'" % permission urlhash = request.matchdict.get('urlHash', None) subscription = None pointer = 0 while subscription == None and pointer < len( request.actor.subscribedTo['items']): if request.actor.subscribedTo['items'][pointer]['urlHash'] == urlhash: subscription = request.actor.subscribedTo['items'][pointer] pointer += 1 if not subscription: raise Unauthorized, "You can't remove permissions on a context where you are not subscribed" #If we reach here, we are subscribed to a context and ready to remove the permission code = 200 permissions = subscription['permissions'] if permission in permissions: #We have the permission, let's delete it request.actor.revokePermission(subscription, permission) subscription['permissions'] = [ a for a in permissions if permission != a ] handler = JSONResourceEntity(subscription, status_code=code) return handler.buildResponse()
def addAdminUserActivity(context, request): """ /admin/people|contexts/{username|urlHash}/activities Add activity impersonated as a valid MAX user or context """ rest_params = {'actor': request.actor, 'verb': 'post'} # Initialize a Activity object from the request newactivity = Activity() newactivity.fromRequest(request, rest_params=rest_params) # If we have the _id setted, then the object already existed in the DB, # otherwise, proceed to insert it into the DB # In both cases, respond with the JSON of the object and the appropiate # HTTP Status Code if newactivity.get('_id'): # Already Exists code = 200 else: # New User code = 201 activity_oid = newactivity.insert() newactivity['_id'] = activity_oid handler = JSONResourceEntity(newactivity.flatten(), status_code=code) return handler.buildResponse()
def ModifyUser(context, request): """ """ actor = request.actor properties = actor.getMutablePropertiesFromRequest(request, mutable_permission="user_mutable") actor.modifyUser(properties) handler = JSONResourceEntity(actor.flatten()) return handler.buildResponse()
def ModifyUser(context, request): """ """ actor = request.actor properties = actor.getMutablePropertiesFromRequest( request, mutable_permission="user_mutable") actor.modifyUser(properties) handler = JSONResourceEntity(actor.flatten()) return handler.buildResponse()
def getContext(context, request): """ """ mmdb = MADMaxDB(context.db) urlhash = request.matchdict.get('urlHash', None) found_context = mmdb.contexts.getItemsByurlHash(urlhash) if not found_context: raise ObjectNotFound, "There's no context matching this url hash: %s" % urlhash handler = JSONResourceEntity(found_context[0].flatten()) return handler.buildResponse()
def getActivity(context, request): """ /activities/{activity} Mostra una activitat """ mmdb = MADMaxDB(context.db) activity_oid = request.matchdict['activity'] activity = mmdb.activity[activity_oid].flatten() handler = JSONResourceEntity(activity) return handler.buildResponse()
def ModifyContext(context, request): """ """ urlHash = request.matchdict['urlHash'] contexts = MADMaxCollection(context.db.contexts) maxcontext = contexts.getItemsByurlHash(urlHash) if maxcontext: maxcontext = maxcontext[0] else: raise ObjectNotFound, 'Unknown context: %s' % urlHash properties = maxcontext.getMutablePropertiesFromRequest(request) maxcontext.modifyContext(properties) maxcontext.updateUsersSubscriptions() handler = JSONResourceEntity(maxcontext.flatten()) return handler.buildResponse()
def follow(context, request): """ /people/{username}/follows/{followedDN}' """ #XXX TODO ara nomes es tracta un sol follow # s'ha de iterar si es vol que el comentari sigui de N follows actor = request.actor rest_params = {'actor': request.actor} # Initialize a Activity object from the request newactivity = Activity(request, rest_params=rest_params) code = 201 newactivity_oid = newactivity.insert() newactivity['_id'] = newactivity_oid actor.addFollower(newactivity['object']) handler = JSONResourceEntity(newactivity.flatten(), status_code=code) return handler.buildResponse()
def addActivityComment(context, request): """ POST /activities/{activity}/comments """ #XXX TODO ara només es tracta la primera activitat, # s'ha de iterar si es vol que el comentari sigui de N activitats activityid = request.matchdict['activity'] mmdb = MADMaxDB(context.db) refering_activity = mmdb.activity[activityid] # Prepare rest parameters to be merged with post data rest_params = { 'verb': 'comment', 'object': { 'inReplyTo': [{ '_id': ObjectId(activityid), 'objectType': refering_activity.object['objectType'] }] } } # Initialize a Activity object from the request newactivity = Activity() newactivity.fromRequest(request, rest_params=rest_params) code = 201 newactivity_oid = newactivity.insert() newactivity['_id'] = newactivity_oid comment = dict(newactivity.object) comment['published'] = newactivity.published comment['author'] = request.actor comment['id'] = newactivity._id del comment['inReplyTo'] refering_activity.addComment(comment) handler = JSONResourceEntity(newactivity.flatten(), status_code=code) return handler.buildResponse()
def addContext(context, request): """ """ # Initialize a Context object from the request newcontext = Context() newcontext.fromRequest(request) # If we have the _id setted, then the object already existed in the DB, # otherwise, proceed to insert it into the DB # In both cases, respond with the JSON of the object and the appropiate # HTTP Status Code if newcontext.get('_id'): # Already Exists code = 200 else: # New User code = 201 contextid = newcontext.insert() newcontext['_id'] = contextid handler = JSONResourceEntity(newcontext.flatten(), status_code=code) return handler.buildResponse()
def subscribe(context, request): """ /people/{username}/subscriptions """ # XXX For now only one context can be subscribed at a time actor = request.actor rest_params = {'actor': actor, 'verb': 'subscribe'} # Initialize a Activity object from the request newactivity = Activity() newactivity.fromRequest(request, rest_params=rest_params) #Check if user is already subscribed subscribed_contexts_hashes = [a['urlHash'] for a in actor.subscribedTo['items']] if sha1(newactivity.object['url']).hexdigest() in subscribed_contexts_hashes: # If user already subscribed, send a 200 code and retrieve the original subscribe activity # post when user was susbcribed. This way in th return data we'll have the date of subscription code = 200 activities = MADMaxCollection(context.db.activity) query = {'verb': 'subscribe', 'object.url': newactivity.object['url'], 'actor.username': actor.username} newactivity = activities.search(query)[-1] # Pick the last one, so we get the last time user subscribed (in case a unsbuscription occured sometime...) else: # If user wasn't created, 201 indicates that the subscription has just been added code = 201 newactivity_oid = newactivity.insert() # Insert a subscribe activity newactivity['_id'] = newactivity_oid #Register subscription to the actor contexts = MADMaxCollection(context.db.contexts, query_key='urlHash') scontext = contexts[sha1(newactivity['object']['url']).hexdigest()] actor.addSubscription(scontext) handler = JSONResourceEntity(newactivity.flatten(), status_code=code) return handler.buildResponse()
def getUser(context, request): """ """ handler = JSONResourceEntity(request.actor.flatten()) return handler.buildResponse()