def get_groups_users_map(friends): # creating a map from groups to users result = dict() print friends for friend_id in friends: friend = db_session.query(models.User).filter(models.User.fb_id == friend_id.left_id).first() print friend if friend: if friend.group_id in result.keys(): result[friend.group_id].append(utils.obj_to_json(None, friend, False)) else: result[friend.group_id] = [utils.obj_to_json(None, friend, False)] # now that we have a dictionary mapping from # groups -> users # we need the information about the groups group_array = [] for group_id in result.keys(): group = db_session.query(models.Group).filter(models.Group.id == group_id).first() group_array.append(group) print group_array return utils.list_to_json("groups", group_array)
def get_user_by_fbid(fb_id): # Execute query, result is a SQLAlchemy object, or nothing # TODO: Do error checking result = db_session.query(models.User).filter(models.User.fb_id==fb_id).first() if result is not None: return utils.obj_to_json('user', result, True) return None
def create_volume(req, db, context): name = get_required_input(req, 'name') size = get_required_input(req, 'size') data = {'display_name': name} ret = cinder_client().volumes.create(size, **data) volume = Volume( creator=context['user'].id, dept=context['user'].dept_id, # 部门设置成创建者所属的部门 name=name, volume_id=ret.id, status=ret.status, size=size, created_at=datetime.datetime.now()) db.add(volume) db.commit() write_operation_log(db, user_id=context['user'].id, resource_type='volume', resource_id=volume.id, resource_uuid=volume.volume_id, event='create_volume') db.commit() log.debug(volume) return obj_to_json(volume, 'volume')
def create_volume(req, db, context): name = get_required_input(req, 'name') size = get_required_input(req, 'size') data = {'display_name': name} ret = cinder_client().volumes.create(size, **data) volume = Volume(creator = context['user'].id, dept = context['user'].dept_id, # 部门设置成创建者所属的部门 name = name, volume_id = ret.id, status = ret.status, size = size, created_at = datetime.datetime.now()) db.add(volume) db.commit() write_operation_log(db, user_id = context['user'].id, resource_type = 'volume', resource_id = volume.id, resource_uuid = volume.volume_id, event = 'create_volume') db.commit() log.debug(volume) return obj_to_json(volume, 'volume')
def write_exception_log(obj_log): '''写入日志到本地文件exception_log.txt ''' try: file = open(os.path.join(os.getcwd(),'exception_log.txt'),'a') file.writelines(obj_to_json(obj_log) + '\n') finally: if file: file.close()
def update_dorm_status(fb_id, status): result = db_session.query(models.User).filter(models.User.fb_id==fb_id).first() if result: if int(result.is_near_dorm) == int(status): return utils.to_app_json({"data":"cannot change user to same status"}) # TODO: add code that will send APN to users when status changes result.is_near_dorm = status result.last_broadcast = datetime.datetime.utcnow() temp = result db_session.commit() return utils.obj_to_json(USER, temp, True) return utils.error_json_message("User does not exist")
def change_group_id(fb_id, new_group, passcode): result = db_session.query(models.User).filter(models.User.fb_id == fb_id).first() new_grp = db_session.query(models.Group).filter(models.Group.id == new_group).first() if result and new_grp: print new_grp.passcode print passcode if str(new_grp.passcode) == str(passcode): # Changing a group_id to the same group_id will cause a server error if int(result.group_id) == int(new_group): return utils.obj_to_json('user', result, True) # Modifying the user's color_id result.group_id = new_group new_group = db_session.query(models.Group).filter(models.Group.id == new_group).first() new_group.users.append(result) # changing the color_id result.color_id = get_new_color(result.group_id) temp = result db_session.commit() return utils.obj_to_json('user',temp, True) elif result and passcode == "newgroup": print "entered correct code" # create new group new_g = models.Group(offset = 0, passcode=0, latitude=0.0, longitude=0.0) # reflect that in the user info result.group_id = new_g.id new_g.users.append(result) result.color_id = 0 temp = result db_session.add(new_g) db_session.commit() groups.assign_passcode(result.fb_id) return utils.obj_to_json('user',temp, True) return Response('Wrong access code', 401) return utils.json_message("Wrong group access token")
def get(self, req, *args, **kwargs): pid = kwargs.get('id') if pid: try: item = Restaurant.objects.get(id=int(pid)) p = obj_to_json(item, False) p['address'] = self.getAddress(item) return JsonResponse({'data':p}) except Exception as e: print(e.message); return JsonResponse({'data':''}) else: # get list return self.getList(req)#JsonResponse({'data':''})
def take_snapshot(req, db, context, server): name = get_required_input(req, 'snapshot_name') snapshot_id = nova_client().servers.create_image(server.instance_id, name) snapshot = Snapshot(creator=context['user'].id, dept=server.dept, owner=server.owner, name=name, status='creating', snapshot_id=snapshot_id, created_at=datetime.datetime.now()) db.add(snapshot) db.commit() log.debug(snapshot) return obj_to_json(snapshot, 'snapshot')
def take_snapshot(req, db, context, server): name = get_required_input(req, 'snapshot_name'); snapshot_id = nova_client().servers.create_image(server.instance_id, name) snapshot = Snapshot(creator = context['user'].id, dept = server.dept, owner = server.owner, name = name, status = 'creating', snapshot_id = snapshot_id, created_at = datetime.datetime.now()) db.add(snapshot) db.commit() log.debug(snapshot) return obj_to_json(snapshot, 'snapshot')
def create_dept(req, db, context): name = get_required_input(req, 'name') desc = get_required_input(req, 'desc') parent_id = int(get_required_input(req, 'parent_id')) if db.query(Dept).filter(Dept.name==name, Dept.deleted==0).count() > 0: raise DeptAlreadyExistError(name) if db.query(Dept).filter(Dept.id==parent_id, Dept.deleted==0).count() == 0: raise ParentDeptNotFoundError(parent_id) dept = Dept(name=name, desc=desc, parent_id=parent_id) db.add(dept) db.commit() log.debug(dept) return obj_to_json(dept, 'dept')
def create_dept(req, db, context): name = get_required_input(req, 'name') desc = get_required_input(req, 'desc') parent_id = int(get_required_input(req, 'parent_id')) if db.query(Dept).filter(Dept.name == name, Dept.deleted == 0).count() > 0: raise DeptAlreadyExistError(name) if db.query(Dept).filter(Dept.id == parent_id, Dept.deleted == 0).count() == 0: raise ParentDeptNotFoundError(parent_id) dept = Dept(name=name, desc=desc, parent_id=parent_id) db.add(dept) db.commit() log.debug(dept) return obj_to_json(dept, 'dept')
def add_user(access_token, device_id): graph = facebook.GraphAPI(access_token) profile = graph.get_object("me") request_picture_url = "http://graph.facebook.com/" + profile['id'] + "/picture" picture_url = urllib2.urlopen(request_picture_url).geturl() new_group = models.Group(offset = 0, passcode=0, latitude=0.0, longitude=0.0) new_user = models.User( fb_id = profile["id"], color_id = 0, is_near_dorm = False, first_name = profile["first_name"], last_name = profile["last_name"], image_url = picture_url, email = profile["email"], device_id = device_id, last_broadcast=datetime.datetime.utcnow() ) # Adds each friend to the friend table in the db params = dict() params['access_token'] = access_token params['id'] = profile['id'] taskqueue.add(url='/tasks/add_friends', method='POST', params=params) # Assign the user to the newly created group new_group.users = [new_user] db_session.add(new_group) db_session.commit() groups.assign_passcode(profile['id']) return utils.obj_to_json('user', new_group.users[0], True)
def create_user(req, db, context): name = get_required_input(req, 'username') password = get_required_input(req, 'password') email = get_input(req, 'email') dept_id = int(get_required_input(req, 'dept_id')) role_id = get_input(req, 'role_id') if db.query(User).filter(User.name==name, User.deleted==0).count() > 0: raise UsernameAlreadyExistError(name) if db.query(User).filter(User.email==email, User.deleted==0).count() > 0: raise EmailAlreadyExistError(email) if is_dept_admin_of(context, dept_id) == False: raise NotDeptAdminError(dept_id) if role_id == None: user_role = db.query(Role).filter(Role.name=='普通用户').first() role_id = user_role.id else: role_id = int(role_id) operator_role_id = int(context['membership'].role_id) if role_id < operator_role_id: raise RolePermissionDenyError(role_id) try: user = User(name=name, password=password, email=email, dept_id=dept_id) db.add(user) db.flush() membership = UserRoleMembership(user_id=user.id, role_id=role_id) db.add(membership) db.commit() log.debug(user) return obj_to_json(user, 'user') except Exception, e: handle_db_error(db, e)
vcpus=flavor.get('vcpus', 0), ip='', created_at=datetime.datetime.now()) db.add(server) db.commit() write_operation_log(db, user_id=context['user'].id, resource_type='server', resource_id=server.id, resource_uuid=instance['id'], event='create server') db.commit() log.debug(server) return obj_to_json(server, 'server') @pre_check @openstack_call def delete_server(req, db, context, server_id): server = find_server(db, context, server_id) server.stae = 'deleting' db.add(server) db.commit() try: nova_client().servers.delete(server.instance_id) except nv_ex.NotFound, e: # server在openstack中已被删除 server.deleted = 1
def show_volume(req, db, context, volume_id): volume = find_volume(db, context, volume_id); return obj_to_json(volume, 'volume')
def show_snapshot(req, db, context, snapshot_id): snapshot = find_snapshot(db, context, snapshot_id); return obj_to_json(snapshot, 'snapshot')
def show_server(req, db, context, server_id): server = find_server(db, context, server_id) return obj_to_json(server, 'server')
def show_image(req, db, context, image_id): image = find_image(db, context, image_id) return obj_to_json(image, 'image')
def show_dept(req, db, context, dept_id): dept_id = int(dept_id) for d in context['depts']: if dept_id == d.id: return obj_to_json(d, 'dept') raise DeptNotFoundError(dept_id)
def show_volume(req, db, context, volume_id): volume = find_volume(db, context, volume_id) return obj_to_json(volume, 'volume')
def show_server(req, db, context, server_id): server = find_server(db, context, server_id); return obj_to_json(server, 'server')
vcpus = flavor.get('vcpus', 0), ip = '', created_at = datetime.datetime.now()) db.add(server) db.commit() write_operation_log(db, user_id = context['user'].id, resource_type = 'server', resource_id = server.id, resource_uuid = instance['id'], event = 'create server') db.commit() log.debug(server) return obj_to_json(server, 'server') @pre_check @openstack_call def delete_server(req, db, context, server_id): server = find_server(db, context, server_id); server.stae = 'deleting' db.add(server) db.commit() try: nova_client().servers.delete(server.instance_id) except nv_ex.NotFound, e: # server在openstack中已被删除 server.deleted = 1
def __repr__(self): return obj_to_json(self.layers)
def get_group_by_id(group_id): if group_id: group = db_session.query(models.Group).filter(models.Group.id == group_id).first() if group: return utils.obj_to_json('group', group, True) return utils.error_json_message('Group does not exist')
def show_snapshot(req, db, context, snapshot_id): snapshot = find_snapshot(db, context, snapshot_id) return obj_to_json(snapshot, 'snapshot')