def chat_log_query(session, querydict): limit = 0 skip = 0 filter_str = '' from_id, to_id = None, None if querydict.has_key('from') and (isinstance(querydict['from'], str) or isinstance(querydict['from'], unicode)) and len(querydict['from'])>0: from_id = querydict['from'] if querydict.has_key('to') and (isinstance(querydict['to'], str) or isinstance(querydict['to'], unicode)) and len(querydict['to'])>0: to_id = querydict['to'] if from_id is None or to_id is None: return json.dumps({'result':u'chat_log_query_from_and_to_required'}, ensure_ascii=True, indent=4) if querydict.has_key('limit'): try: limit = int(querydict['limit']) except: pass del querydict['limit'] if querydict.has_key('skip'): try: skip = int(querydict['skip']) except: pass del querydict['skip'] if querydict.has_key('filter'): filter_str = querydict['filter'] del querydict['filter'] db, collection1 = get_collection(app, 'main', 'collection_chat_log') ret = list(collection1.find({'$or':[{'from':add_mongo_id(from_id), 'to':add_mongo_id(to_id)}, {'to':add_mongo_id(from_id), 'from':add_mongo_id(to_id)}]}).limit(limit).skip(skip).sort('timestamp', pymongo.DESCENDING)) ret = json.dumps(remove_mongo_id(ret), ensure_ascii=True, indent=4) return ret
def user_group_get(session, querydict): ret = [] app = aConfig['gConfig']['wsgi']['application'] db, collection = get_collection(app, 'main', 'collection_groups') q = {} if querydict.has_key('username'): if isinstance(querydict['username'], str) or isinstance(querydict['username'], unicode) or isinstance(querydict['username'], dict): q['username'] = querydict['username'] if querydict.has_key('_id'): if isinstance(querydict['_id'], str) or isinstance(querydict['_id'], unicode): q['_id'] = add_mongo_id(querydict['_id']) if len(list(q.keys()))>0: users = user_query(session, querydict) if len(users)>0: user0 = users[0] _id = user0['_id'] grps = group_query(session) for i in grps: if i.has_key('members') and _id in i['members']: ret.append(i) ret = json.dumps(remove_mongo_id(ret), ensure_ascii=True, indent=4) else: ret = json.dumps({'result':u'user_group_get_user_not_exist'}, ensure_ascii=True, indent=4) else: ret = json.dumps({'result':u'user_group_get_one_user_required'}, ensure_ascii=True, indent=4) return ret
def chat_offline_save_log(aConfig, obj): id = None app = aConfig['gConfig']['wsgi']['application'] if obj['op'] not in ['chat/online', 'chat/offline', 'chat/info/online', 'chat/info/offline', 'chat/request/contact/remove', 'chat/request/group/quit'] and obj.has_key('to'): db, collection = get_collection(app, 'main', 'collection_chat_log_offline') id = collection.save(add_mongo_id(obj)) return id
def handle_query_feature(session, aConfig, querydict, user_id): statuscode, mimetype, body = 200, 'text/json', '{}' app = aConfig['gConfig']['wsgi']['application'] db, collection = get_collection(aConfig, app, 'main', 'collection_features') limit = 100 if 'limit' in querydict and isinstance(querydict['limit'], int): limit = querydict['limit'] cond = {} if 'name' in querydict: cond['$or'] = [ { 'properties.name': {'$regex': '^.*' + querydict['name'] + '.*$'} }, { 'properties.py': {'$regex': '^.*' + querydict['name'] + '.*$'} }] if 'webgis_type' in querydict: if isinstance(querydict['webgis_type'], str) : cond['properties.webgis_type'] = add_mongo_id(querydict['webgis_type']) elif isinstance(querydict['webgis_type'], list): cond['properties.webgis_type'] = {'$in': add_mongo_id(querydict['webgis_type'])} if 'tower_id' in querydict: if isinstance(querydict['tower_id'], str) : cond['_id'] = add_mongo_id(querydict['tower_id']) elif isinstance(querydict['tower_id'], list): cond['_id'] = {'$in':add_mongo_id(querydict['tower_id'])} arr = remove_mongo_id(list(collection.find(cond).limit(limit))) return statuscode, mimetype, json.dumps(arr, ensure_ascii=True, indent=4)
def resend_offline_msg(session, to_id, limit=10): app = aConfig['gConfig']['wsgi']['application'] db, collection = get_collection(app, 'main', 'collection_chat_log_offline') arr = list(collection.find({'to':add_mongo_id(to_id)}).limit(limit).sort('timestamp', pymongo.DESCENDING)) ids = [i['_id'] for i in arr] collection.remove({'_id':{'$in': ids}}) for i in arr: gJoinableQueue.put(remove_mongo_id(i))
def get_user(querydict): ret = None if 'username' in querydict and 'password' in querydict: app = aConfig['gConfig']['wsgi']['application'] db, collection = get_collection(aConfig, app, 'main', 'collection_users') if check_is_mobile_phone_number(querydict['username']): ret = collection.find_one({'password':querydict['password'],'mobile_phone':querydict['username']}) elif check_is_mail_addr(querydict['username']): ret = collection.find_one({'password':querydict['password'],'mail_addr':querydict['username']}) else: ret = collection.find_one({'password':querydict['password'],'username':querydict['username']}) return ret
def get_device_from_tower(session, aConfig, tower_id): ret = None app = aConfig['gConfig']['wsgi']['application'] db, collection = get_collection(aConfig, app, 'main', 'collection_tower_device_bind') if isinstance(tower_id, str) : one = collection.find_one({'tower_id':add_mongo_id(tower_id)}) if one: ret = remove_mongo_id(one['device_id']) elif isinstance(tower_id, list): arr = _.pluck(list(collection.find({'tower_id':{'$in':add_mongo_id(tower_id)}})), 'device_id') ret = remove_mongo_id(arr) return ret
def handle_geo_near(session, aConfig, querydict, user_id): def calc_dist(p1, p2): po1 = tuple((p1[1], p1[0])) po2 = tuple((p2[1], p2[0])) return int(great_circle(po1, po2).m) statuscode, mimetype, body = 200, 'text/json', '{}' app = aConfig['gConfig']['wsgi']['application'] db, collection = get_collection(aConfig, app, 'main', 'collection_features') limit = 100 if 'limit' in querydict and isinstance(querydict['limit'], int): limit = querydict['limit'] if 'geometry' in querydict: geojsonobj = querydict['geometry'] if isinstance(geojsonobj, str) : try: geojsonobj = json.loads(geojsonobj) except: geojsonobj = {} if 'type' in geojsonobj: cond = {'geometry2d': {'$nearSphere': {'$geometry': geojsonobj}}} if 'webgis_type' in querydict: if isinstance(querydict['webgis_type'], str) : cond['properties.webgis_type'] = querydict['webgis_type'] if isinstance(querydict['webgis_type'], list): cond['properties.webgis_type'] = {'$in': querydict['webgis_type']} minDistance = 0 maxDistance = 0 if 'min_distance' in querydict and (isinstance(querydict['min_distance'], float) or isinstance(querydict['min_distance'], int)): minDistance = querydict['min_distance'] if 'max_distance' in querydict and (isinstance(querydict['max_distance'], float) or isinstance(querydict['max_distance'], int)): maxDistance = querydict['max_distance'] cond['geometry2d']['$nearSphere']['$minDistance'] = minDistance cond['geometry2d']['$nearSphere']['$maxDistance'] = maxDistance arr = remove_mongo_id(list(collection.find(cond).limit(limit))) if 'is_calc_dist' in querydict and (querydict['is_calc_dist'] is True or querydict['is_calc_dist'].lower() == 'true'): if geojsonobj['type'] == 'Point': def get_dist(x = {}): x['properties']['calc_dist'] = calc_dist(geojsonobj['coordinates'], x['geometry']['coordinates']) return x arr = _.map_(arr, get_dist) body = json.dumps(arr, ensure_ascii=True, indent=4) else: body = json.dumps({'result': 'geometry_geojson_required'}, ensure_ascii=True, indent=4) else: body = json.dumps({'result': 'geometry_required'}, ensure_ascii=True, indent=4) return statuscode, mimetype, body
def handle_query_network_data(session, aConfig, querydict, user_id): statuscode, mimetype, body = 200, 'text/json', '{}' app = aConfig['gConfig']['wsgi']['application'] db, collection = get_collection(aConfig, app, 'main', 'collection_network') limit = 0 if 'limit' in querydict and isinstance(querydict['limit'], int): limit = querydict['limit'] if 'name' in querydict: arr = [] arr = list(collection.find({'$or':[{'properties.name': {'$regex':'^.*' + querydict['name'] + '.*$'}},{'properties.py': {'$regex':'^.*' + querydict['name'] + '.*$'}}]}).limit(limit)) if len(arr): arr = remove_mongo_id(arr) body = json.dumps(arr, ensure_ascii=True, indent=4) return statuscode, mimetype, body
def chat_save_log(aConfig, obj): id = None app = aConfig['gConfig']['wsgi']['application'] if obj.has_key('op') and obj['op'] in ['chat/chat', 'chat/online', 'chat/offline']: db, collection = get_collection(app, 'main', 'collection_chat_log') # if obj.has_key('timestamp'): # obj['timestamp'] = datetime.datetime.fromtimestamp(obj['timestamp']/1000).strftime('%Y-%m-%d %H:%M:%S') if obj['op'] in ['chat/online', 'chat/offline']: obj1 = copy.deepcopy(obj) for k in list(obj1.keys()): if not k in ['from', 'timestamp', 'op', 'to']: del obj1[k] if obj1.has_key('_id'): del obj1['_id'] id = collection.save(add_mongo_id(obj1)) else: id = collection.save(add_mongo_id(obj)) return id
def group_query(session, querydict={}): ret = [] app = aConfig['gConfig']['wsgi']['application'] db, collection = get_collection(app, 'main', 'collection_groups') q = {} limit = 0 skip = 0 if querydict.has_key('limit'): limit = int(querydict['limit']) del querydict['limit'] if querydict.has_key('skip'): skip = int(querydict['skip']) del querydict['skip'] if querydict.has_key('group_name'): if isinstance(querydict['group_name'], str) or isinstance(querydict['group_name'], unicode): q['group_name'] = querydict['group_name'] if isinstance(querydict['group_name'], list): q['group_name'] = {'$in': querydict['group_name']} if isinstance(querydict['group_name'], dict): q['group_name'] = querydict['group_name'] if querydict.has_key('_id'): if isinstance(querydict['_id'], str) or isinstance(querydict['_id'], unicode): q['_id'] = querydict['_id'] if isinstance(querydict['_id'], list): q['_id'] = {'$in': querydict['_id']} ret = list(collection.find(add_mongo_id(q)).limit(limit).skip(skip)) if querydict.has_key('user_detail') and querydict['user_detail'] is True: keys = list(gWebSocketsMap.keys()) for i in ret: idx = ret.index(i) detail = [] userlist = user_query(session, {'_id':i['members']}) for j in userlist: if j.has_key('contacts'): del j['contacts'] if j.has_key('password'): del j['password'] if str(j['_id']) in keys: j['online_status'] = 'online' else: j['online_status'] = 'offline' detail.append(j) ret[idx]['members'] = detail return ret
def group_remove(session, querydict): ret = '' app = aConfig['gConfig']['wsgi']['application'] if querydict.has_key('_id') and len(querydict['_id']) > 0: try: db, collection = get_collection(app, 'main', 'collection_groups') existone = collection.find_one({'_id':add_mongo_id(querydict['_id'])}) if existone: collection.remove({'_id':existone['_id']}) ret = json.dumps(remove_mongo_id(existone), ensure_ascii=True, indent=4) else: ret = json.dumps({'result':u'group_remove_fail_group_not_exist'}, ensure_ascii=True, indent=4) except: if hasattr(sys.exc_info()[1], 'message'): ret = json.dumps({'result':u'group_remove_fail:%s' % sys.exc_info()[1].message}, ensure_ascii=True, indent=4) else: ret = json.dumps({'result':u'group_remove_fail' }, ensure_ascii=True, indent=4) else: ret = json.dumps({'result':u'group_remove_fail_found_group_id_required'}, ensure_ascii=True, indent=4) return ret
def user_query(session, querydict): ret = [] app = aConfig['gConfig']['wsgi']['application'] db, collection = get_collection(app, 'main', 'collection_users') q = {} limit = 0 skip = 0 user_detail = False if querydict.has_key('user_detail') and querydict['user_detail'] is True: user_detail = True del querydict['user_detail'] if querydict.has_key('limit'): limit = int(querydict['limit']) del querydict['limit'] if querydict.has_key('skip'): skip = int(querydict['skip']) del querydict['skip'] if querydict.has_key('username'): if isinstance(querydict['username'], str) or isinstance(querydict['username'], unicode): q['username'] = querydict['username'] if isinstance(querydict['username'], list): q['username'] = {'$in': querydict['username']} if isinstance(querydict['username'], dict): q['username'] = querydict['username'] if querydict.has_key('_id'): if isinstance(querydict['_id'], str) or isinstance(querydict['_id'], unicode): q['_id'] = add_mongo_id(querydict['_id']) if isinstance(querydict['_id'], list): q['_id'] = {'$in': [add_mongo_id(i) for i in querydict['_id']]} if isinstance(querydict['_id'], dict): q['_id'] = querydict['_id'] rec = list(collection.find(q).limit(limit).skip(skip)) keys = list(gWebSocketsMap.keys()) for i in rec: if user_detail: if str(i['_id']) in keys: i['online_status'] = 'online' else: i['online_status'] = 'offline' ret.append(i) return ret
def group_add(session, querydict): ret = '' app = aConfig['gConfig']['wsgi']['application'] if querydict.has_key('owner_id')\ and len(querydict['owner_id']) > 0\ and querydict.has_key('group_name')\ and len(querydict['group_name']) > 0: try: db, collection = get_collection(app, 'main', 'collection_groups') existone = collection.find_one({'group_name':querydict['group_name']}) if existone: ret = json.dumps({'result':u'group_add_fail_group_name_already_exist'}, ensure_ascii=True, indent=4) else: obj = {} obj['owner_id'] = querydict['owner_id'] obj['group_name'] = querydict['group_name'] ts = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) obj['found_date'] = ts obj['update_date'] = ts obj['members'] = [add_mongo_id(obj['owner_id']), ] if querydict.has_key('avatar') and len(querydict['avatar']) > 0: obj['avatar'] = querydict['avatar'] if querydict.has_key('description') and len(querydict['description']) > 0: obj['description'] = querydict['description'] _id = collection.save(add_mongo_id(obj)) rec = collection.find_one({'_id':_id}) ret = json.dumps(remove_mongo_id(rec), ensure_ascii=True, indent=4) except: if hasattr(sys.exc_info()[1], 'message'): ret = json.dumps({'result':u'group_add_fail:%s' % sys.exc_info()[1].message}, ensure_ascii=True, indent=4) else: ret = json.dumps({'result':u'group_add_fail' }, ensure_ascii=True, indent=4) else: if not querydict.has_key('owner_id') or len(querydict['owner_id']) == 0: ret = json.dumps({'result':u'group_add_fail_owner_id_required'}, ensure_ascii=True, indent=4) if not querydict.has_key('group_name') or len(querydict['group_name']) == 0: ret = json.dumps({'result':u'group_add_fail_group_name_required'}, ensure_ascii=True, indent=4) return ret
def user_add(session, querydict): ret = '' app = aConfig['gConfig']['wsgi']['application'] if querydict.has_key('username') and querydict.has_key('password') and len(querydict['username'])>0 and len(querydict['password'])>0: try: db, collection = get_collection(app, 'main', 'collection_users') existone = collection.find_one({'username':querydict['username']}) if existone: ret = json.dumps({'result':u'user_add_fail_username_already_exist'}, ensure_ascii=True, indent=4) else: obj = {} obj['username'] = querydict['username'] obj['display_name'] = querydict['username'] obj['password'] = querydict['password'] ts = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) obj['register_date'] = ts obj['update_date'] = ts obj['description'] = '' obj['person_info'] = {} obj['contacts'] = [] obj['avatar'] = None if querydict.has_key('person_info') : obj['person_info'] = querydict['person_info'] if querydict.has_key('contacts') and isinstance(querydict['contacts'], list): obj['contacts'] = querydict['contacts'] if querydict.has_key('avatar') and len(querydict['avatar']) > 0: obj['avatar'] = querydict['avatar'] _id = collection.save(add_mongo_id(obj)) rec = collection.find_one({'_id':_id}) ret = json.dumps(remove_mongo_id(rec), ensure_ascii=True, indent=4) except: if hasattr(sys.exc_info()[1], 'message'): ret = json.dumps({'result':u'user_add_fail:%s' % sys.exc_info()[1].message}, ensure_ascii=True, indent=4) else: ret = json.dumps({'result':u'user_add_fail' }, ensure_ascii=True, indent=4) else: ret = json.dumps({'result':u'user_add_fail_username_password_required'}, ensure_ascii=True, indent=4) return ret
def group_update(session, querydict): ret = '' app = aConfig['gConfig']['wsgi']['application'] if querydict.has_key('_id') and len(querydict['_id'])>0: try: _id = add_mongo_id(querydict['_id']) db, collection = get_collection(app, 'main', 'collection_groups') existone = collection.find_one({'_id':_id}) if existone: del querydict['_id'] querydict['update_date'] = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) collection.update({'_id':existone['_id']}, {'$set': add_mongo_id(querydict)}, multi=False, upsert=False) one = collection.find_one({'_id':_id}) ret = json.dumps(remove_mongo_id(one), ensure_ascii=True, indent=4) else: ret = json.dumps({'result':u'group_update_group_not_exist'}, ensure_ascii=True, indent=4) except: if hasattr(sys.exc_info()[1], 'message'): ret = json.dumps({'result':u'group_update_fail:%s' % sys.exc_info()[1].message}, ensure_ascii=True, indent=4) else: ret = json.dumps({'result':u'group_update_fail' }, ensure_ascii=True, indent=4) else: ret = json.dumps({'result':u'group_update_fail_group_id_required'}, ensure_ascii=True, indent=4) return ret
def handle_geo_within(session, aConfig, querydict, user_id): statuscode, mimetype, body = 200, 'text/json', '{}' app = aConfig['gConfig']['wsgi']['application'] db, collection = get_collection(aConfig, app, 'main', 'collection_features') limit = 0 if 'limit' in querydict and isinstance(querydict['limit'], int): limit = querydict['limit'] if 'geometry' in querydict: geojsonobj = querydict['geometry'] if isinstance(geojsonobj, str) : try: geojsonobj = json.loads(geojsonobj) except: geojsonobj = {} if 'type'in geojsonobj: cond = {'geometry2d':{'$geoWithin':{'$geometry':geojsonobj}}} if 'webgis_type' in querydict: if isinstance(querydict['webgis_type'], str) : cond['properties.webgis_type'] = querydict['webgis_type'] if isinstance(querydict['webgis_type'], list): cond['properties.webgis_type'] = {'$in': querydict['webgis_type']} arr = list(collection.find(cond).limit(limit)) body = json.dumps(remove_mongo_id(arr), ensure_ascii=True, indent=4) else: body = json.dumps({'result': 'geometry_geojson_required'}, ensure_ascii=True, indent=4) else: body = json.dumps({'result': 'geometry_required'}, ensure_ascii=True, indent=4) return statuscode, mimetype, body statuscode, mimetype, body = 200, 'text/json', '{}' if check_permission(user_id, 'enable_sensor'): device_id = None db, collection = get_collection(aConfig, app, 'main', 'collection_device') if 'tower_id' in querydict: device_id = get_device_from_tower(session, aConfig, querydict['tower_id']) if 'device_id' in querydict: device_id = querydict['device_id'] #add for db device = None if device_id: cond = {'_id': device_id} device = collection.find_one(add_mongo_id(cond)) if 'sensor_id' in querydict and len(querydict['sensor_id']): sensor_id = querydict['sensor_id'] gevent.spawn(savedb, collection, querydict, device, device_id, sensor_id, enable) else: gevent.spawn(savedb, collection, querydict, device, device_id, None, enable) #add for gSensorControlList device = None if device_id: device = _.find(gSensorControlList, {'_id': device_id}) device_index = _.find_index(gSensorControlList, {'_id': device_id}) if device and 'sensors' in device: pass else: device = {} device['_id'] = device_id device['sensors'] = [] if 'sensor_id' in querydict and len(querydict['sensor_id']): sensor_id = querydict['sensor_id'] sensor = _.find(device['sensors'], {'_id': sensor_id}) if sensor: index = _.find_index(device['sensors'], {'_id': sensor_id}) sensor['enable'] = enable if 'interval' in querydict and (isinstance(querydict['interval'], int) or isinstance(querydict['interval'], float)): sensor['interval'] = querydict['interval'] device['sensors'][index] = sensor else: sensor = {} sensor['_id'] = sensor_id sensor['enable'] = enable if 'interval' in querydict and (isinstance(querydict['interval'], int) or isinstance(querydict['interval'], float)): sensor['interval'] = querydict['interval'] device['sensors'].append(sensor) else: for i in range(len(device['sensors'])): device['sensors'][i]['enable'] = enable if 'interval' in querydict and (isinstance(querydict['interval'], int) or isinstance(querydict['interval'], float)): device['sensors'][i]['interval'] = querydict['interval'] if device_index < 0: gSensorControlList.append(device) else: gSensorControlList[device_index] = device else: body = json.dumps({'result': 'cannot_find_device_id'}, ensure_ascii=True, indent=4) else: body = json.dumps({'result':'permission_deny'}, ensure_ascii=True, indent=4) return statuscode, mimetype, body
def request_response(session, websocket, obj={}): #'chat/request/contact/add', #'chat/request/contact/remove', #'chat/response/contact/add/accept', #'chat/response/contact/add/reject' #'chat/request/group/join' #'chat/request/group/quit' #'chat/response/group/join/accept', #'chat/response/group/join/reject', tolist = [] app = aConfig['gConfig']['wsgi']['application'] try: if obj['op'] == 'chat/response/contact/add/accept': if obj.has_key('from') and len(obj['from'])>0 and obj.has_key('to') and len(obj['to'])>0: db, collection = get_collection(app, 'main','collection_users') userlist = user_query(session, {'_id':[obj['from'], obj['to']]}) for user in userlist: if str(user['_id']) == obj['from'] and not add_mongo_id(obj['to']) in user['contacts']: user['contacts'].append(add_mongo_id(obj['to'])) if str(user['_id']) == obj['to'] and not add_mongo_id(obj['from']) in user['contacts']: user['contacts'].append(add_mongo_id(obj['from'])) user['update_date'] = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) collection.save(user) fromuser = {} fromuser['op'] = obj['op'] fromuser['_id'] = obj['from'] fromuser['from'] = obj['to'] fromuser['to'] = obj['from'] fromuser['contacts'] = json.loads(user_contact_get(session, {'_id':obj['from'],'user_detail':True})) gJoinableQueue.put(remove_mongo_id(fromuser)) touser = {} touser['op'] = obj['op'] touser['_id'] = obj['to'] touser['from'] = obj['from'] touser['to'] = obj['to'] touser['contacts'] = json.loads(user_contact_get(session, {'_id':obj['to'],'user_detail':True})) gJoinableQueue.put(remove_mongo_id(touser)) elif obj['op'] == 'chat/response/contact/add/reject': if obj.has_key('from') and len(obj['from'])>0 and obj.has_key('to') and len(obj['to'])>0: userlist = user_query(session, {'_id':obj['from']}) if len(userlist)>0: user0 = userlist[0] user0['op'] = obj['op'] user0['from'] = obj['from'] user0['to'] = obj['to'] if user0.has_key('password'): del user0['password'] if user0.has_key('contacts'): del user0['contacts'] if obj.has_key('reject_reason') and len(obj['reject_reason'])>0: user0['reject_reason'] = obj['reject_reason'] gJoinableQueue.put(remove_mongo_id(user0)) elif obj['op'] == 'chat/request/contact/add': if obj.has_key('from') and len(obj['from'])>0 and obj.has_key('to') and len(obj['to'])>0: userlist = user_query(session, {'_id':obj['from']}) if len(userlist)>0: user0 = userlist[0] user0['op'] = obj['op'] user0['from'] = obj['from'] user0['to'] = obj['to'] if user0.has_key('password'): del user0['password'] if user0.has_key('contacts'): del user0['contacts'] gJoinableQueue.put(remove_mongo_id(user0)) elif obj['op'] == 'chat/request/contact/remove': if obj.has_key('from') and len(obj['from'])>0 and obj.has_key('to') and len(obj['to'])>0: db, collection = get_collection(app, 'main', 'collection_users') userlist = user_query(session, {'_id':[obj['from'], obj['to']]}) remover, removee = None, None for user in userlist: if str(user['_id']) == obj['from'] and add_mongo_id(obj['to']) in user['contacts']: user['contacts'].remove(add_mongo_id(obj['to'])) remover = user['display_name'] if str(user['_id']) == obj['to'] and add_mongo_id(obj['from']) in user['contacts']: user['contacts'].remove(add_mongo_id(obj['from'])) removee = user['display_name'] user['update_date'] = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) collection.save(user) fromuser = {} fromuser['op'] = obj['op'] fromuser['_id'] = obj['from'] fromuser['from'] = obj['to'] fromuser['to'] = obj['from'] fromuser['remover'] = remover fromuser['removee'] = removee fromuser['remove_type'] = 'remover' fromuser['contacts'] = json.loads(user_contact_get(session, {'_id':obj['from'], 'user_detail':True})) gJoinableQueue.put(remove_mongo_id(fromuser)) touser = {} touser['op'] = obj['op'] touser['_id'] = obj['to'] touser['from'] = obj['from'] touser['to'] = obj['to'] touser['remover'] = remover touser['removee'] = removee touser['remove_type'] = 'removee' touser['contacts'] = json.loads(user_contact_get(session, {'_id':obj['to'], 'user_detail':True})) gJoinableQueue.put(remove_mongo_id(touser)) elif obj['op'] == 'chat/request/group/join': if obj.has_key('from') and len(obj['from'])>0 and obj.has_key('to_group') and len(obj['to_group'])>0: grps = group_query(session, {'_id':obj['to_group']}) if len(grps)>0: grp0 = grps[0] userlist = user_query(session, {'_id':obj['from']}) if len(userlist)>0: user0 = userlist[0] user0['op'] = obj['op'] user0['from'] = obj['from'] user0['request_src'] = obj['from'] user0['to_group'] = obj['to_group'] user0['to'] = grp0['owner_id'] if user0.has_key('password'): del user0['password'] if user0.has_key('contacts'): del user0['contacts'] gJoinableQueue.put(remove_mongo_id(user0)) elif obj['op'] == 'chat/request/group/quit': if obj.has_key('from') and len(obj['from'])>0 and obj.has_key('to_group') and len(obj['to_group'])>0: grps = group_query(session, {'_id':obj['to_group']}) if len(grps)>0: grp0 = grps[0] members = [] if add_mongo_id(obj['from']) in grp0['members']: grp0['members'].remove(add_mongo_id(obj['from'])) members = [str(i) for i in grp0['members']] db, collection = get_collection(app, 'main', 'collection_groups') collection.save(grp0) broadcast(session, websocket, members, {'op':obj['op'], 'from':obj['from'], 'to_group':obj['to_group']} ) elif obj['op'] == 'chat/response/group/join/accept': if obj.has_key('to_group') and len(obj['to_group'])>0 and obj.has_key('request_src') and len(obj['request_src'])>0: grps = group_query(session, {'_id': obj['to_group']}) if len(grps)>0: grp0 = grps[0] if not add_mongo_id(obj['request_src']) in grp0['members']: grp0['members'].append(add_mongo_id(obj['request_src'])) db, collection = get_collection(app, 'main', 'collection_groups') collection.save(grp0) members = [str(i) for i in grp0['members']] broadcast(session, websocket, members, obj) elif obj['op'] == 'chat/response/group/join/reject': if obj.has_key('from') and len(obj['from'])>0 and obj.has_key('to') and len(obj['to'])>0 and obj.has_key('to_group') and len(obj['to_group'])>0: userlist = user_query(session, {'_id':obj['from']}) if len(userlist)>0: user0 = userlist[0] user0['op'] = obj['op'] user0['from'] = obj['from'] user0['to'] = obj['to'] user0['to_group'] = obj['to_group'] if user0.has_key('password'): del user0['password'] if user0.has_key('contacts'): del user0['contacts'] if obj.has_key('reject_reason') and len(obj['reject_reason'])>0: user0['reject_reason'] = obj['reject_reason'] gJoinableQueue.put(remove_mongo_id(user0)) except gevent.queue.Full: print('chat queue is full')
def user_contact_get(session, querydict): ret = [] app = aConfig['gConfig']['wsgi']['application'] db, collection = get_collection(app, 'main', 'collection_users') q = {} if querydict.has_key('username'): if isinstance(querydict['username'], str) or isinstance(querydict['username'], unicode): q['username'] = querydict['username'] del querydict['username'] if querydict.has_key('_id'): if isinstance(querydict['_id'], str) or isinstance(querydict['_id'], unicode): q['_id'] = add_mongo_id(querydict['_id']) del querydict['_id'] if len(list(q.keys()))>0: contacts = [] selfid = None rec = collection.find_one(q) if rec and rec.has_key('contacts'): # contacts = rec['contacts'] contacts = [add_mongo_id(i) for i in rec['contacts']] ret = contacts selfid = rec['_id'] limit = 0 skip = 0 user_detail = False if querydict.has_key('user_detail') and querydict['user_detail'] is True: user_detail = True del querydict['user_detail'] if querydict.has_key('limit'): try: limit = int(querydict['limit']) except: pass del querydict['limit'] if querydict.has_key('skip'): try: skip = int(querydict['skip']) except: pass del querydict['skip'] if user_detail: if querydict.has_key('filter'): contactlist = user_query(session, {'username':{'$regex': '^.*' + querydict['filter'] + '.*$'}, '_id': {'$in':contacts, '$ne':selfid}, 'limit':limit, 'skip':skip}) del querydict['filter'] else: contactlist = user_query(session, {'_id':contacts, 'limit':limit, 'skip':skip}) ret = [] keys = list(gWebSocketsMap.keys()) for i in contactlist: if i.has_key('contacts'): del i['contacts'] if i.has_key('password'): del i['password'] if str(i['_id']) in keys: i['online_status'] = 'online' else: i['online_status'] = 'offline' ret.append(i) ret = json.dumps(remove_mongo_id(ret), ensure_ascii=True, indent=4) else: ret = json.dumps({'result':u'user_contact_query_one_user_required'}, ensure_ascii=True, indent=4) return ret