def join(self, username): """Lets a user join a room on a specific Namespace.""" user = User() user.request = self.request user.mdb_collection = self.request.registry.max_store.users user.fromDatabase(username) subscribed_conversations = [a['id'] for a in user.talkingIn['items'] if a['objectType'] == 'conversation'] self.socket.rooms = set(subscribed_conversations) print 'User %s listening to conversations: %s' % (username, ', '.join(subscribed_conversations))
def test_deepcopy(self): """ """ from max.utils.dicts import deepcopy from max.models import User actor = User.from_object(None, {'username': '******', 'displayName': 'Sheldon'}) old_dict = { 'level1_key': { 'level2_key': { 'level3_key': {}, 'level3_value': 54 }, 'level2_value': [ {'inner': 'item'} ] }, 'actor': actor } dict_copy = deepcopy(old_dict) # Dicts and lists get copied self.assertNotEqual(id(old_dict), id(dict_copy)) self.assertNotEqual(id(old_dict['level1_key']), id(dict_copy['level1_key'])) self.assertNotEqual(id(old_dict['level1_key']['level2_key']), id(dict_copy['level1_key']['level2_key'])) self.assertNotEqual(id(old_dict['level1_key']['level2_value']), id(dict_copy['level1_key']['level2_value'])) self.assertNotEqual(id(old_dict['level1_key']['level2_key']['level3_key']), id(dict_copy['level1_key']['level2_key']['level3_key'])) self.assertNotEqual(id(old_dict['level1_key']['level2_value'][0]), id(dict_copy['level1_key']['level2_value'][0])) # Primitives and objects remain referenced self.assertEqual(id(old_dict['actor']), id(dict_copy['actor'])) self.assertEqual(id(old_dict['level1_key']['level2_key']['level3_value']), id(dict_copy['level1_key']['level2_key']['level3_value'])) self.assertEqual(id(old_dict['level1_key']['level2_value'][0]['inner']), id(dict_copy['level1_key']['level2_value'][0]['inner']))
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 test_recursive_update_dict(self): """ """ from max.utils.dicts import RUDict from max.models import User actor = User.from_object(None, {'username': '******', 'displayName': 'Sheldon'}) old_dict = { 'level1_key': { 'level2_key': { 'level3_key': {}, 'level3_value': 54 }, 'level2_value': [] }, 'actor': {} } new_dict = { 'level1_key': { 'level2_key': { 'level3_key': { 'new_value': 'new' }, }, 'level2_key2': {'value': 3} }, 'actor': actor } rdict = RUDict(old_dict) rdict.update(new_dict) self.assertIsInstance(rdict['actor'], User) self.assertEqual(rdict['level1_key']['level2_value'], []) self.assertEqual(rdict['level1_key']['level2_key']['level3_value'], 54) self.assertEqual(rdict['level1_key']['level2_key']['level3_key']['new_value'], 'new') self.assertEqual(rdict['level1_key']['level2_key2']['value'], 3) self.assertNotEqual(id(rdict['level1_key']), id(new_dict['level1_key']['level2_key'])) self.assertNotEqual(id(rdict['level1_key']['level2_key']), id(new_dict['level1_key']['level2_key'])) self.assertNotEqual(id(rdict['level1_key']['level2_key2']), id(new_dict['level1_key']['level2_key2'])) self.assertEqual(id(rdict['level1_key']['level2_key']['level3_key']['new_value']), id(new_dict['level1_key']['level2_key']['level3_key']['new_value'])) self.assertEqual(id(rdict['actor']), id(new_dict['actor']))
def addUser(users, request): """ Add a user Creates a new user in the system, with all the attributes provided in the posted user object. - `username` - Used to identify and login the user. This is the only required parameter and cannot be modified. - `displayname` - The full name of the user. - `twitterUsername` - A valid Twitter® username (without @ prefix), used on the twitter integration service. This operation is idempotent, which means that a request to create a user that already exists, will not recreate or overwrite that user. Instead, the existing user object will be returned. You can tell when this happens by looking at the HTTP response status code. A new user insertion will return a **201 CREATED** code, and a **200 OK** for an existing user. + Request { "username": "******", "displayName": "user2", "twitterUsername": "******" } """ payload = request.decoded_payload if not isinstance(payload, dict): raise ValidationError('Unexpected data type in request') username = payload.get('username', None) if username is None: raise ValidationError('Missing username in request') rest_params = {'username': username.lower()} # Initialize a User object from the request newuser = User.from_request(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 # Determine if we have to recreate exchanges for an existing user # Defaults NOT to recreate them if not specified create_exchanges = asbool(request.params.get('notifications', False)) if create_exchanges: notifier = RabbitNotifications(request) notifier.add_user(username) else: # New User code = 201 # Determine if we have to recreate exchanges for a new user # Defaults to Create them if not specified create_exchanges = asbool(request.params.get('notifications', True)) userid = newuser.insert(notifications=create_exchanges) newuser['_id'] = userid handler = JSONResourceEntity(request, newuser.getInfo(), status_code=code) return handler.buildResponse()