Exemplo n.º 1
0
    def _build_routes(cls, router):
        """
        Called in the main app router to get all of this controller's routes.
        Override to add custom/additional routes.
        """

        # Route the rest methods
        router.add(routing.build_scaffold_routes_for_controller(cls))
        for prefix in cls.Meta.prefixes:
            router.add(
                routing.build_scaffold_routes_for_controller(cls, prefix))

        # Auto route the remaining methods
        for route in routing.build_routes_for_controller(cls):
            vars = re.findall(r'\[(\w+)\]', route.template)
            if vars:
                action = route.handler_method
                split = action.split('_')
                prefixed = split[0] in cls.Meta.prefixes
                controller_data = {
                    'prefix': split[0] if prefixed else None,
                    'controller': inflector.underscore(cls.__name__),
                    'action': '_'.join(split[1:]) if prefixed else action,
                }

                for i in vars:
                    value = controller_data.get(i)
                    if not value:
                        continue
                    route.template = route.template.replace(
                        '[' + i + ']', value)
            router.add(route)
        events.fire('controller_build_routes', cls=cls, router=router)
Exemplo n.º 2
0
    def __init__(self, controller):
        display_properties, model_form_data, redirect_url = None, None, None
        if hasattr(controller.meta, "Model"):
            display_properties=sorted([name for name, property in controller.meta.Model._properties.items()])
            model_form_data=model_form(controller.meta.Model)
        try:
            redirect_url = controller.uri(action='list') if controller.uri_exists(action='list') else None
        except KeyError:
            pass

        defaults = dict(
            query_factory=default_query_factory,
            create_factory=default_create_factory,
            title=inflector.titleize(controller.proper_name),
            plural=inflector.underscore(controller.name),
            singular=inflector.underscore(inflector.singularize(controller.name)),
            ModelForm=model_form_data,
            display_properties=display_properties,
            display_properties_in_list=display_properties,
            redirect=redirect_url,
            form_action=None,
            form_encoding='application/x-www-form-urlencoded',
            flash_messages=True,
            layouts={
                None: 'layouts/default.html',
            },
            navigation={},
            field_name={
                "created": u"建立時間",
                "modified": u"修改時間",
                "sort": u"排序值",
                "is_enable": u"啟用"
            }
        )
        try:
            defaults["field_name"].update(controller.meta.Model.Meta.label_name)
        except:
            pass

        for k, v in defaults.iteritems():
            if not hasattr(self, k):
                setattr(self, k, v)
Exemplo n.º 3
0
def _load_model(controller):
    # Attempt to import the model automatically
    model_name = controller.__class__.__name__ + 'Model'
    try:
        import_form_base = str(controller.__module__)
        module = __import__(import_form_base, fromlist=['*'])
        setattr(controller.Meta, 'Model', getattr(module, model_name))
    except (ImportError, AttributeError):
        try:
            import_form_base = '.'.join(controller.__module__.split('.')[:-2])
            s = '%s.models.%s' % (import_form_base, inflector.underscore(model_name))
            module = __import__(s, fromlist=['*'])
            setattr(controller.Meta, 'Model', getattr(module, model_name))
        except (ImportError, AttributeError, ValueError):
            import logging
            logging.debug("Scaffold coudn't automatically determine a model class for controller %s, please assign it a Meta.Model class variable." % controller.__class__.__name__)
Exemplo n.º 4
0
 def _build_components(self):
     self.events.before_build_components(controller=self)
     if hasattr(self.Meta, 'components'):
         component_classes = self.Meta.components
         self.components = Bunch()
         for cls in component_classes:
             if hasattr(cls, 'name'):
                 name = cls.name
             else:
                 name = inflector.underscore(cls.__name__)
             self.components[name] = cls(weakref.proxy(self))
     else:
         if hasattr(self.Meta, 'model'):
             self.components = (scaffold.Scaffolding, )
         else:
             self.components = Bunch()
     self.events.after_build_components(controller=self)
Exemplo n.º 5
0
 def __init__(self, *args, **kwargs):
     self.request_start_time = time.time()
     super(Controller, self).__init__(*args, **kwargs)
     self.name = inflector.underscore(self.__class__.__name__)
     self.proper_name = self.__class__.__name__
     self.util = self.Util(weakref.proxy(self))
     self.route = None
     self.administrator = None
     self.administrator_level = 0
     self.prohibited_actions = []
     self.prohibited_controller = []
     self.params = ParamInfo(self.request)
     self.settings = settings
     self.logging = logging
     self.datastore = DataStore(self)
     self.server_name = os.environ["SERVER_NAME"]
     self.host_info = self.settings.get_host_item(self.server_name)
     self.namespace = self.host_info.namespace
     self.plugins = str(self.host_info.plugins).split(",")
     self.plugins_all = []
     self.theme = self.host_info.theme
     namespace_manager.set_namespace(self.namespace)