def url_for(self, controller = None, action = None, named_url = None, url_params = None, url_args=None, url_kwargs=None): from django_url_framework.controller import get_actions, get_controller_name if controller: controller_name = controller else: controller_name = self.controller._controller_name Controller = self.controller._site.controllers.get(controller_name, None) if Controller is None: raise InvalidControllerError(controller_name) if not named_url: if action: try: action_func = get_actions(Controller,with_prefix=False)[action] except KeyError: import traceback traceback.print_exc() raise InvalidActionError(action) named_url = getattr(action_func,'named_url',None) if named_url is None: controller_name = get_controller_name(Controller, with_prefix=False) named_url = '%s_%s' % (controller_name, action) else: named_url = controller_name url = reverse(named_url, args=url_args, kwargs=url_kwargs) if url_params is not None: return u'%s?%s' % (url, urlencode(url_params)) return url
def load_controllers(self, app_path, controllers): found_controller, found_helper = (None, None) for controller_file in controllers: """Load controller""" try: found_controller = imp.find_module( '%s_controller' % controller_file, app_path) except ImportError, e: self.logger.warning("Failed to find proper controller in %s" % controller_file, exc_info=True) continue else: controller_module = imp.load_module( '%s_controller' % controller_file, *found_controller) self.logger.debug("Loaded controller from %s" % controller_file) for controller_class_name in dir(controller_module): # test_name = '%sController' % ''.join([i.title() for i in controller_file.split('_')]) if not controller_class_name.endswith('Controller'): continue controller_class = getattr(controller_module, controller_class_name) if controller_class != ActionController and issubclass( controller_class, ActionController): controller_name = get_controller_name(controller_class) if controller_name in self.controllers: continue self.controllers[controller_name] = controller_class """Load helper""" try: found_helper = imp.find_module( '%s_helper' % controller_file, app_path) except ImportError, e: self.logger.debug("No helper found for %s" % controller_name) continue else: helper_module = imp.load_module( '%s_helper' % controller_file, *found_helper) self.logger.debug("Loaded helper for %s" % controller_name) helper_class = getattr( helper_module, '%sHelper' % controller_file.title(), None) if helper_class and issubclass( helper_class, ApplicationHelper): self.helpers[controller_name] = helper_class finally: if found_helper: found_helper[0].close()
def load_controllers(self, app_path, controllers): found_controller, found_helper = (None, None) for controller_file in controllers: """Load controller""" try: found_controller = imp.find_module('%s_controller' % controller_file, [app_path]) except ImportError: self.logger.warning("Failed to find proper controller in %s" % controller_file, exc_info=True) continue else: controller_module = imp.load_module('%s_controller' % controller_file, *found_controller) self.logger.debug("Loaded controller from %s" % controller_file) for controller_class_name in dir(controller_module): if not controller_class_name.endswith('Controller'): continue controller_class = getattr(controller_module, controller_class_name) if controller_class != ActionController and issubclass(controller_class, ActionController): controller_name = get_controller_name(controller_class) if controller_name in self.controllers: continue self.controllers[controller_name] = controller_class """Load helper""" try: found_helper = imp.find_module('%s_helper' % controller_file, [app_path]) except ImportError: continue else: helper_module = imp.load_module('%s_helper' % controller_file, *found_helper) self.logger.debug("Loaded helper for %s" % controller_name) helper_class = getattr(helper_module ,'%sHelper' % controller_file.title(), None) if helper_class and issubclass(helper_class, ApplicationHelper): self.helpers[controller_name] = helper_class finally: if found_helper: found_helper[0].close() finally: if found_controller: found_controller[0].close()
def url_for(self, controller=None, action=None, named_url=None, url_params=None, url_args=None, url_kwargs=None): from django_url_framework.controller import get_actions, get_controller_name if controller: controller_name = controller else: controller_name = self.controller._controller_name Controller = self.controller._site.controllers.get( controller_name, None) if Controller is None: raise InvalidControllerError(controller_name) if not named_url: if action: try: action = action.strip('"\'') action_func = get_actions(Controller, with_prefix=False)[action] except KeyError: import traceback traceback.print_exc() raise InvalidActionError(action) named_url = getattr(action_func, 'named_url', None) if named_url is None: controller_name = get_controller_name(Controller, with_prefix=False) named_url = '%s_%s' % (controller_name, action) else: named_url = controller_name url = reverse(named_url, args=url_args, kwargs=url_kwargs) if url_params is not None: return u'%s?%s' % (url, urlencode(url_params)) return url
def test_without_inflection_TestController(self): class TestController(object): pass self.assertEqual(get_controller_name(TestController), "test")
def test_with_inflection_HTTPResponseCodeController(self): class HTTPResponseCodeController(object): use_inflection_library = True self.assertEqual(get_controller_name(HTTPResponseCodeController), "http_response_code")
def test_with_inflection_IDBarController(self): class IDBarController(object): use_inflection_library = True self.assertEqual(get_controller_name(IDBarController), "id_bar")
def test_without_inflection_IDBarController(self): class IDBarController(object): pass self.assertEqual(get_controller_name(IDBarController), "idbar")
def test_without_inflection_HTTPResponseCodeController(self): class HTTPResponseCodeController(object): pass self.assertEqual(get_controller_name(HTTPResponseCodeController), "httpresponse_code")