def __new__(mcs, name, bases, clsdict): if (ABSTRACT_ATTR in clsdict or getattr(clsdict.get('Meta', object), 'abstract', False)): return super().__new__(mcs, name, bases, clsdict) cls = super().__new__(mcs, name, bases, clsdict) routes = {} include_methods = set(cls.Meta.include_methods) exclude_methods = set(cls.Meta.exclude_methods) for method_name in ALL_METHODS: if (method_name in exclude_methods or method_name not in include_methods): continue route = getattr(clsdict.get(method_name), FN_ROUTES_ATTR, [None])[0] if not route: route = Route(None, deep_getattr(clsdict, bases, method_name)) if method_name in INDEX_METHODS: rule = '/' else: rule = cls.Meta.member_param route.rule = rule routes[method_name] = [route] setattr(cls, CONTROLLER_ROUTES_ATTR, routes) return cls
def __new__(mcs, name, bases, clsdict): mcs_args = McsArgs(mcs, name, bases, clsdict) if mcs_args.is_abstract: return super().__new__(*mcs_args) cls = super().__new__(*mcs_args) routes = {} include_methods = set(cls.Meta.include_methods) exclude_methods = set(cls.Meta.exclude_methods) for method_name in ALL_METHODS: if (method_name in exclude_methods or method_name not in include_methods): continue route = getattr(clsdict.get(method_name), FN_ROUTES_ATTR, [None])[0] if not route: route = Route(None, mcs_args.getattr(method_name)) if method_name in INDEX_METHODS: rule = '/' else: rule = cls.Meta.member_param route.rule = rule routes[method_name] = [route] setattr(cls, CONTROLLER_ROUTES_ATTR, routes) return cls
def __new__(mcs, name, bases, clsdict): if ABSTRACT_ATTR in clsdict: return super().__new__(mcs, name, bases, clsdict) routes = {} include_methods = set(deep_getattr(clsdict, bases, 'include_methods')) exclude_methods = set(deep_getattr(clsdict, bases, 'exclude_methods')) for method_name in ALL_METHODS: if (method_name in exclude_methods or method_name not in include_methods): continue route = getattr(clsdict.get(method_name), FN_ROUTES_ATTR, [None])[0] if not route: route = Route(None, deep_getattr(clsdict, bases, method_name)) route._controller_name = name if method_name in INDEX_METHODS: rule = '/' else: rule = deep_getattr(clsdict, bases, 'member_param') route.rule = rule routes[method_name] = [route] cls = super().__new__(mcs, name, bases, clsdict) if cls.model is None: raise AttributeError(f'{name} is missing the model attribute') setattr(cls, CONTROLLER_ROUTES_ATTR, routes) return cls
def test_should_register_defaults_to_true(self): route = Route('/path', lambda: 'view_func') assert route.should_register(None) is True
def test_full_name_with_func(self): def a_view(): pass route = Route('/foo', a_view) assert route.full_name == 'tests.bundles.controller.test_route.a_view'
def test_should_register_with_callable(self): route = Route('/path', lambda: 'view_func', only_if=lambda x: x) assert route.should_register(True) is True assert route.should_register(False) is False
def test_should_register_with_boolean(self): route = Route('/path', lambda: 'view_func', only_if=True) assert route.should_register(None) is True