def add_route(gid, routes): with db.auto_commit(): Group.get_or_404(id=gid, msg='无指定权限组') for route_id in routes: Route.get_or_404(id=route_id, msg='无指定路由节点') Menu.abort_repeat(group_id=gid, route_id=route_id) Menu.create(group_id=gid, route_id=route_id)
def test_delete_route(self): route = Route.create(self.points) route_id = route.id self.assertIsNotNone(route) route.delete() db_route = Route.get(route_id) self.assertIsNone(db_route)
def test_create_route(self): route = Route.create(self.points) db_route = Route.get(route.id) self.assertIsNotNone(db_route.path) self.assertEqual(type(db_route.path), WKBElement) self.assertIsNotNone(db_route.created_ts) self.assertIsNone(db_route.modified_ts) self.assertEqual(db_route.robots, [])
def test_get_all_docking_stations_in_radius_of_route(self): route = Route.create([[-75.4980, 12.1897], [-75.4990, 12.1900]]) json_data = json.dumps({'radius': 500, 'route_id': route.id}) response = self.client.get(BASE_URL, query_string=json_data) data = json.loads(response.get_data()) self.assertEqual(len(data), 2) self.assertEqual(response.status_code, 200)
def _create_robot_routes(self): robot = Robot.create(self.route.id, None) robot.robot_routes[-1].delete() route_1 = Route.create([[-13, 15], [-13, 14]]) route_2 = Route.create([[-11, 15], [-12, 14]]) route_3 = Route.create([[-10, 15], [-11, 14]]) route_4 = Route.create([[-10, 14], [-11, 14]]) t1 = datetime(2018, 1, 1) t2 = datetime(2018, 3, 3) t3 = datetime(2018, 5, 27) t4 = datetime(2018, 10, 11) t5 = datetime(2018, 10, 12) t6 = datetime(2019, 1, 3) t7 = datetime(2019, 2, 2) t8 = datetime(2019, 5, 9) t9 = datetime(2019, 6, 6) robot_route_1 = RobotRoute.create(robot.id, self.route.id, t1) robot_route_1.update(t2) robot_route_2 = RobotRoute.create(robot.id, route_1.id, t3) robot_route_2.update(t4) robot_route_3 = RobotRoute.create(robot.id, route_2.id, t5) robot_route_3.update(t6) robot_route_4 = RobotRoute.create(robot.id, route_3.id, t7) robot_route_4.update(t8) robot_route_5 = RobotRoute.create(robot.id, route_4.id, t9) return { 'robot': robot, 'robot_route_1_route_id': robot_route_1.route_id, 'robot_route_2_route_id': robot_route_2.route_id, 'robot_route_3_route_id': robot_route_3.route_id, 'robot_route_4_route_id': robot_route_4.route_id, 'robot_route_5_route_id': robot_route_5.route_id }
def tearDown(self): for robot in Robot.get_all(): robot.delete() for docking_station in DockingStation.get_all(): docking_station.delete() for route in Route.get_all(): route.delete() for robot_route in RobotRoute.get_all(): robot_route.delete()
def delete(id: int): route = Route.get_or_404(id=id, msg='路由不存在, 删除失败') # 禁止任意删除 if Menu.get(route_id=id): raise Forbidden(msg='存在权限组的菜单绑定路由,不可删除') with db.auto_commit(): route.delete(commit=False) Route.query.filter_by(parent_id=id).delete( synchronize_session=False)
def add_parent_node(cur_route_node): if cur_route_node['parent_id'] == cur_route_node['id'] == 0: pass else: if not (cur_route_node['parent_id'] in id2route_node.keys() or cur_route_node['parent_id'] == 0): parent_route = Route.get_or_404( id=cur_route_node['parent_id']) id2route_node_clone[parent_route.id] = dict(parent_route) add_parent_node(dict(parent_route))
def test_update_route(self): route = Route.create(self.points) original_path = route.path original_points = route.points self.assertIsNone(route.modified_ts) new_points = [[-1.3223, -14.3221], [-1.0531, -15.3221]] route.update(new_points) self.assertNotEqual(route.path, original_path) self.assertNotEqual(route.points, original_points) self.assertIsNotNone(route.modified_ts)
def find_routes_by_ts_range(self, start_ts=None, end_ts=None): start_ts = start_ts or datetime(1970, 1, 1) end_ts = end_ts or datetime.now(tz=timezone.utc) routes = [] for robot_route in self.robot_routes: if self._does_ts_range_overlap_robot_route(robot_route, start_ts, end_ts): routes.append(Route.get(robot_route.route_id)) return routes
def get_all(cls, query_string=None): query_string = json.loads(query_string) if query_string else {} if not query_string.get('radius'): return cls.query.all() if query_string.get('route_id'): route = Route.get(query_string['route_id']) return cls.get_all_in_route_radius(route, query_string['radius']) return cls.get_all_in_radius(query_string['target_longitude'], query_string['target_latitude'], query_string['radius'])
def change_route(route_list: list): cur_t = RouteTree() cur_t.generate_by_dir({'id': 0, 'children': route_list}) cur_list = cur_t.deserialize() with db.auto_commit(): for cur_route in cur_list: old_route = Route.get(id=cur_route['id']) if old_route and \ (old_route.parent_id != cur_route['parent_id'] or old_route.order != cur_route['order']): old_route.update(id=cur_route['id'], parent_id=cur_route['parent_id'], order=cur_route['order'], commit=False)
def test_update_robot_on_route_to_new_route(self): robot = Robot.create(self.route.id, None) old_robot_route = robot.current_robot_route() new_route = Route.create([[-11, -11], [9, 19], [10, 15]]) robot.update(route_id=new_route.id) updated_robot_route = robot.current_robot_route() self.assertEqual(robot.route_id, new_route.id) self.assertNotEqual(old_robot_route, updated_robot_route) self.assertIsNotNone(old_robot_route.end_ts) self.assertIsNone(updated_robot_route.end_ts) self.assertTrue(robot in new_route.robots) self.assertIsNone(robot.docking_station_id)
def add(title, father, text): assert not isinstance(current_user, AnonymousUserMixin), 'user has not logged in' assert isinstance(title, basestring), 'name is not string' assert len(title) >= 5, 'route name too short!' assert father is None or isinstance(father, ObjectId), 'father is not not valid category id' new_route = Route() new_route.title = title new_route.author = current_user.id new_route.create_ts = dt_to_ts(datetime.utcnow()) if father: new_route.father = father new_route.content.new_file() new_route.content.write(text.encode('utf-8')) new_route.content.close() new_route.save() # add id of new route to father's routes list from app.helpers.category import CategoryHelper CategoryHelper.add_route(father, new_route.id) return new_route
def get_route(route_unid): route = RouteModel.get_route_by_unid(route_unid) if not route: abort(404, message='No route found', status='false') return route
def search(keywords): """Search routes by title""" assert isinstance(keywords, basestring) return list(Route.objects(title__contains=keywords))
def get_user_create_routes(): return list(Route.objects(author=current_user.id))
def get_all(): return Route.objects()
def get(route_id): return Route.objects(id=route_id).first()
def update(id: int, **kwargs): route = Route.get_or_404(id=id, msg='该节点不存在') route.update(**kwargs)
def setUp(self): self.route = Route.create([[-79, 14], [-78, 15]]) self.docking_station = DockingStation.create(-80, 14)
def get(self): routes = RouteModel.get_routes() return { 'routes': [route.serialize() for route in routes] }
def find_route_by_ts(self, ts): for robot_route in self.robot_routes: if self._is_ts_within_robot_route_range(robot_route, ts): return [Route.get(robot_route.route_id)] return []
def setUp(self): self.route = Route.create([[-74.0478, 12.4687], [-80.4199, 10.9196]]) self.route2 = Route.create([[-75.0478, 12.4687], [-81.4199, 10.9196]]) self.route_schema = RouteSchema()
def create_element(form): Route.get_or_404(id=form['route_id']) Element.create(**form)
def test_get_route(self): route = Route.create(self.points) route_id = route.id db_route = Route.get(route_id) self.assertEqual(db_route.id, route_id)
def get_route_node(id): """按ID获取路由节点""" route_node = Route.get_or_404(id=id, msg='路由节点不存在') return Success(route_node)
def create(**kwargs): Route.abort_repeat(msg='唯一键重复', name=kwargs['name']) Route.create(**kwargs)
def test_get_nonexistent_route(self): route = Route.get(100) self.assertIsNone(route)
def _create_route(self): points = [[-79.6289, -10.7901], [-77.0800, -16.5098], [-73.7402, -22.4719], [-75.4980, -30.6757], [-81.9140, -36.9147]] return Route.create(points)
def setUp(self): self.docking_station = DockingStation.create(-74.0478, 12.4687) self.route = Route.create([[-75.10, 12.50], [-81.42, 10.92]]) self.robot_schema = RobotSchema() self.robot = Robot.create(self.route.id, None)
def get_route_node(id) -> dir: return Route.filte(id=id).first()