Exemplo n.º 1
0
    def __init__(self, actor, action):

        if not isinstance(action, actions.Action):
            raise Exception("%s : %r is not an Action" % (actor, action))
        self.action = action
        self.actor = actor

        required = set(actor.required_roles)
        # required = set()
        # required |= actor.required_roles
        required |= action.required_roles

        debug_permissions = actor.debug_permissions and \
            action.debug_permissions

        if debug_permissions:
            if settings.DEBUG:
                logger.info("debug_permissions active for %r (required=%s)",
                            self, required)
            else:
                raise Exception(
                    "settings.DEBUG is False, but `debug_permissions` "
                    "for %r (required=%s) is active (settings=%s)." % (
                        self, required, os.environ['DJANGO_SETTINGS_MODULE']))

        self.allow_view = curry(make_view_permission_handler(
            self, action.readonly, debug_permissions, required), action)
        self._allow = curry(make_permission_handler(
            action, actor, action.readonly,
            debug_permissions, required,
            allowed_states=action.required_states), action)
Exemplo n.º 2
0
Arquivo: base.py Projeto: cuchac/lino
 def __init__(self, book, *args, **kw):
     self.book = book
     memo.Parser.__init__(self, *args, **kw)
     self.register_command('ref', self.cmd_ref)
     self.register_command('item', curry(self.cmd_item, '- '))
     self.register_command('oitem', curry(self.cmd_item, '#. '))
     self.register_command('ruleslist', self.cmd_ruleslist)
Exemplo n.º 3
0
Arquivo: base.py Projeto: DarioGT/lino
 def __init__(self, book, *args, **kw):
     self.book = book
     memo.Parser.__init__(self, *args, **kw)
     self.register_command('ref', self.cmd_ref)
     self.register_command('item', curry(self.cmd_item, '- '))
     self.register_command('oitem', curry(self.cmd_item, '#. '))
     self.register_command('ruleslist', self.cmd_ruleslist)
Exemplo n.º 4
0
    def generate_files(self):

        from lino.ui.extjs3 import UI

        # ~ UI = settings.SITE.get_ui_class
        ui = UI(make_messages=True)
        # ~ # install Lino urls under root location (`/`)
        # ~ ui = urlpatterns = ui.get_patterns()
        # ~ settings.SITE.setup()
        ui.make_linolib_messages()

        context = dict(
            header=rstgen.header,
            h1=curry(rstgen.header, 1),
            table=rstgen.table,
            doc=doc2rst,
            loading=loading,
            models=models,
            abstract=abstract,
            refto=refto,
            # ~ py2rst=rstgen.py2rst,
            full_model_name=full_model_name,
            model_overview=model_overview,
            model_referenced_from=model_referenced_from,
            model_ref=model_ref,
        )
        self.generate("index.rst.tmpl", "index.rst", **context)
        for a in loading.get_apps():
            app_label = a.__name__.split(".")[-2]
            app_models = models.get_models(a, include_auto_created=True)
            context.update(app=a, app_label=app_label, app_models=app_models)
            self.generate("app.rst.tmpl", "%s.rst" % app_label, **context)
            for model in app_models:
                context.update(model=model)
                self.generate("model.rst.tmpl", "%s.rst" % full_model_name(model), **context)
Exemplo n.º 5
0
    def generate_files(self):

        from lino.ui.extjs3 import UI
        #~ UI = settings.SITE.get_ui_class
        ui = UI(make_messages=True)
        # ~ # install Lino urls under root location (`/`)
        #~ ui = urlpatterns = ui.get_patterns()
        #~ settings.SITE.setup()
        ui.make_linolib_messages()

        context = dict(
            header=rstgen.header,
            h1=curry(rstgen.header, 1),
            table=rstgen.table,
            doc=doc2rst,
            loading=loading,
            models=models,
            abstract=abstract,
            refto=refto,
            #~ py2rst=rstgen.py2rst,
            full_model_name=full_model_name,
            model_overview=model_overview,
            model_referenced_from=model_referenced_from,
            model_ref=model_ref,
        )
        self.generate('index.rst.tmpl', 'index.rst', **context)
        for a in loading.get_apps():
            app_label = a.__name__.split('.')[-2]
            app_models = models.get_models(a, include_auto_created=True)
            context.update(app=a, app_label=app_label, app_models=app_models)
            self.generate('app.rst.tmpl', '%s.rst' % app_label, **context)
            for model in app_models:
                context.update(model=model, )
                self.generate('model.rst.tmpl',
                              '%s.rst' % full_model_name(model), **context)
Exemplo n.º 6
0
 def __init__(self, field, name, **kw):
     kw['type'] = 'boolean'
     StoreField.__init__(self, field, name, **kw)
     if not field.editable:
         def full_value_from_object(self, obj, ar):
             #~ return self.value2html(ar,self.field.value_from_object(obj))
             return self.format_value(ar, self.field.value_from_object(obj))
         self.full_value_from_object = curry(full_value_from_object, self)
Exemplo n.º 7
0
    def __init__(self, actor, action):
        # the following test would require us to import Action, which
        # would trigger a circular import Action -> BoundAction ->
        # BaseRequest -> InstanceAction -> Action
        # if not isinstance(action, Action):
        #     raise Exception("%s : %r is not an Action" % (actor, action))
        self.action = action
        self.actor = actor

        # take care of not accidentally modifying ther actor's
        # requirements!
        required = set(action.get_required_roles(actor))
        # required = set(actor.required_roles)
        # if action.defining_actor is None:
        #     required = set(actor.required_roles)
        # else:
        #     required = set(action.defining_actor.required_roles)
        # required = set()
        # required |= actor.required_roles
        required |= action.required_roles

        if True:  # 20170902 useful when debugging permission problems
            self.required = required

        debug_permissions = actor.debug_permissions and \
            action.debug_permissions

        if debug_permissions:
            if settings.DEBUG:
                logger.info("debug_permissions active for %r (required=%s)",
                            self, required)
            else:
                raise Exception(
                    "settings.DEBUG is False, but `debug_permissions` "
                    "for %r (required=%s) is active (settings=%s)." %
                    (self, required, os.environ['DJANGO_SETTINGS_MODULE']))

        self.allow_view = curry(
            make_view_permission_handler(self, action.readonly,
                                         debug_permissions, required), action)
        self._allow = curry(
            make_permission_handler(action, actor, action.readonly,
                                    debug_permissions, required), action)
Exemplo n.º 8
0
Arquivo: store.py Projeto: NewRGB/lino
    def __init__(self, field, name, **kw):
        kw['type'] = 'boolean'
        StoreField.__init__(self, field, name, **kw)
        if not field.editable:

            def full_value_from_object(self, obj, ar=None):
                # return self.value2html(ar,self.field.value_from_object(obj))
                return self.format_value(ar, self.field.value_from_object(obj))

            self.full_value_from_object = curry(full_value_from_object, self)
Exemplo n.º 9
0
    def install_permission_handler(self):
        """Define the `allow_read` handler used by
        :meth:`get_view_permission`.  This must be done only once, but
        after having configured :attr:`debug_permissions` and
        :attr:`required_roles`.

        """
        self.allow_read = curry(
            make_view_permission_handler(self, True, self.debug_permissions,
                                         self.required_roles), self)
Exemplo n.º 10
0
    def __init__(self, actor, action):
        # the following test would require us to import Action, which
        # would trigger a circular import Action -> BoundAction ->
        # BaseRequest -> InstanceAction -> Action
        # if not isinstance(action, Action):
        #     raise Exception("%s : %r is not an Action" % (actor, action))
        self.action = action
        self.actor = actor

        # take care of not accidentally modifying ther actor's
        # requirements!
        required = set(action.get_required_roles(actor))
        # required = set(actor.required_roles)
        # if action.defining_actor is None:
        #     required = set(actor.required_roles)
        # else:
        #     required = set(action.defining_actor.required_roles)
        # required = set()
        # required |= actor.required_roles
        required |= action.required_roles
        
        if True:  # 20170902 useful when debugging permission problems
            self.required = required  

        debug_permissions = actor.debug_permissions and \
            action.debug_permissions

        if debug_permissions:
            if settings.DEBUG:
                logger.info("debug_permissions active for %r (required=%s)",
                            self, required)
            else:
                raise Exception(
                    "settings.DEBUG is False, but `debug_permissions` "
                    "for %r (required=%s) is active (settings=%s)." % (
                        self, required, os.environ['DJANGO_SETTINGS_MODULE']))

        self.allow_view = curry(make_view_permission_handler(
            self, action.readonly, debug_permissions, required), action)
        self._allow = curry(make_permission_handler(
            action, actor, action.readonly,
            debug_permissions, required), action)
Exemplo n.º 11
0
 def add_virtual_field(cls, name, vf):
     # disabled because UsersWithClients defines virtual fields on
     # connection_created
     if False:
         if name in cls.virtual_fields:
             raise Exception("Duplicate add_virtual_field() %s.%s" %
                             (cls, name))
     cls.virtual_fields[name] = vf
     #~ vf.lino_resolve_type(cls,name)
     vf.name = name
     vf.get = curry(vf.get, cls)
Exemplo n.º 12
0
 def add_virtual_field(cls, name, vf):
     # disabled because UsersWithClients defines virtual fields on
     # connection_created
     if False:
         if name in cls.virtual_fields:
             raise Exception("Duplicate add_virtual_field() %s.%s" %
                             (cls, name))
     cls.virtual_fields[name] = vf
     #~ vf.lino_resolve_type(cls,name)
     vf.name = name
     vf.get = curry(vf.get, cls)
Exemplo n.º 13
0
    def install_permission_handler(self):
        """Define the `allow_read` handler used by
        :meth:`get_view_permission`.  This must be done only once, but
        after having configured :attr:`debug_permissions` and
        :attr:`required_roles`.

        """
        self.allow_read = curry(make_view_permission_handler(
            self, True,
            self.debug_permissions,
            self.required_roles), self)
Exemplo n.º 14
0
    def install_permission_handler(self):

        from lino.core.perms import make_view_permission_handler

        # ~ if self.name == 'newcomers_left': # required.has_key('user_groups'):
            #~ logger.info("20121130 install_permission_handler() %s %s",self,self.required)
            #~ if self.required.get('user_groups') ==  'integ':
                #~ raise Exception("20121130")
        self.allow_read = curry(make_view_permission_handler(
            self, True,
            self.debug_permissions,
            **self.required), self)
Exemplo n.º 15
0
    def __init__(self, actor, action):

        if not isinstance(action, actions.Action):
            raise Exception("%s : %r is not an Action" % (actor, action))
        self.action = action
        self.actor = actor

        required = dict()
        if action.readonly:
            required.update(actor.required)
        #~ elif isinstance(action,InsertRow):
            #~ required.update(actor.create_required)
        elif isinstance(action, actions.DeleteSelected):
            required.update(actor.delete_required)
        else:
            required.update(actor.update_required)
        required.update(action.required)
        #~ print 20120628, str(a), required
        #~ def wrap(a,required,fn):
            #~ return fn

        debug_permissions = actor.debug_permissions and \
            action.debug_permissions

        if debug_permissions:
            if settings.DEBUG:
                logger.info("debug_permissions active for %r (required=%s)",
                            self, required)
            else:
                raise Exception(
                    "settings.DEBUG is False, but `debug_permissions` "
                    "for %r (required=%s) is active." % (self, required))

        from lino.core.perms import (
            make_permission_handler, make_view_permission_handler)
        self.allow_view = curry(make_view_permission_handler(
            self, action.readonly, debug_permissions, **required), action)
        self._allow = curry(make_permission_handler(
            action, actor, action.readonly,
            debug_permissions, **required), action)
Exemplo n.º 16
0
 def __getattr__(self,name):
     """
     Since there is only one EmptyTableRow class, we simulate a 
     getter here by manually creating an InstanceAction.
     """
     v = getattr(self._table,name)
     if isinstance(v,actions.Action):
         return actions.InstanceAction(v,self._table,self,None)
     # 20130525 dd.Report calls `get_story` on `self`, not on the `cls`
     if callable(v):
         return curry(v,self)
     #~ return v 
     #~ raise Exception("")
     raise AttributeError("EmptyTableRow on %s has no action and no callable '%s'" % (self._table,name))
Exemplo n.º 17
0
    def __init__(self, actor, action):

        if not isinstance(action, actions.Action):
            raise Exception("%s : %r is not an Action" % (actor, action))
        self.action = action
        self.actor = actor

        required = set(actor.required_roles)
        # required = set()
        # required |= actor.required_roles
        required |= action.required_roles

        debug_permissions = actor.debug_permissions and \
            action.debug_permissions

        if debug_permissions:
            if settings.DEBUG:
                logger.info("debug_permissions active for %r (required=%s)",
                            self, required)
            else:
                raise Exception(
                    "settings.DEBUG is False, but `debug_permissions` "
                    "for %r (required=%s) is active (settings=%s)." %
                    (self, required, os.environ['DJANGO_SETTINGS_MODULE']))

        self.allow_view = curry(
            make_view_permission_handler(self, action.readonly,
                                         debug_permissions, required), action)
        self._allow = curry(
            make_permission_handler(action,
                                    actor,
                                    action.readonly,
                                    debug_permissions,
                                    required,
                                    allowed_states=action.required_states),
            action)
Exemplo n.º 18
0
 def __getattr__(self, name):
     """
     Since there is only one EmptyTableRow class, we simulate a
     getter here by manually creating an InstanceAction.
     """
     v = getattr(self._table, name)
     if isinstance(v, actions.Action):
         return actions.InstanceAction(v, self._table, self, None)
     # 20130525 dd.Report calls `get_story` on `self`, not on the `cls`
     if callable(v):
         return curry(v, self)
     #~ return v
     #~ raise Exception("")
     raise AttributeError(
         "EmptyTableRow on %s has no action and no callable '%s'" % (
             self._table, name))
Exemplo n.º 19
0
def create_atomizer(fld, name):
    if isinstance(fld, fields.RemoteField):
        """
        Hack: we create a StoreField based on the remote field,
        then modify its behaviour.
        """
        sf = create_atomizer(fld.field, fld.name)

        def value_from_object(sf, obj, ar):
            #~ if fld.name == 'event__when_text':
                #~ logger.info("20130802 create_atomizer RemoteField value_from_object")
            m = fld.func
            return m(obj, ar)

        def full_value_from_object(sf, obj, ar):
            #~ logger.info("20120406 %s.full_value_from_object(%s)",sf.name,sf)
            m = fld.func
            v = m(obj, ar)
            #~ if fld.name == 'event__when_text':
                #~ logger.info("20130802 full_value_from_object %s",obj)
            return v

        sf.value_from_object = curry(value_from_object, sf)
        sf.full_value_from_object = curry(full_value_from_object, sf)
        #~ sf.field = fld.field
        #~ sf.value2list = curry(value2list,sf)
        return sf
    #~ if isinstance(fld,tables.ComputedColumn):
        #~ logger.info("20111230 Store.create_atomizer(%s)", fld)
        #~ return ComputedColumnField(fld)
    meth = getattr(fld, '_return_type_for_method', None)
    if meth is not None:
        # uh, this is tricky...
        return MethodStoreField(fld, name)
    #~ if isinstance(fld,fields.HtmlBox):
        #~ ...
    #~ if isinstance(fld,dd.LinkedForeignKey):
        #~ return LinkedForeignKeyField(fld,name)

    sf_class = getattr(fld, 'lino_atomizer_class', None)
    if sf_class is not None:
        return sf_class(fld, name)

    if isinstance(fld, dd.RequestField):
        delegate = create_atomizer(fld.return_type, fld.name)
        return RequestStoreField(fld, delegate, name)
    if isinstance(fld, dd.VirtualField):
        delegate = create_atomizer(fld.return_type, fld.name)
        return VirtStoreField(fld, delegate, name)
    if isinstance(fld, models.FileField):
        return FileFieldStoreField(fld, name)
    if isinstance(fld, models.ManyToManyField):
        return StoreField(fld, name)
    if isinstance(fld, dd.PasswordField):
        return PasswordStoreField(fld, name)
    if isinstance(fld, models.OneToOneField):
        return OneToOneStoreField(fld, name)
    if isinstance(fld, generic.GenericForeignKey):
        return GenericForeignKeyField(fld, name)
    if isinstance(fld, dd.GenericForeignKeyIdField):
        return ComboStoreField(fld, name)
    if isinstance(fld, models.ForeignKey):
        return ForeignKeyStoreField(fld, name)
    if isinstance(fld, models.TimeField):
        return TimeStoreField(fld, name)
    if isinstance(fld, models.DateTimeField):
        return DateTimeStoreField(fld, name)
    if isinstance(fld, dd.IncompleteDateField):
        return IncompleteDateStoreField(fld, name)
    if isinstance(fld, models.DateField):
        return DateStoreField(fld, name)
    if isinstance(fld, models.BooleanField):
        return BooleanStoreField(fld, name)
    if isinstance(fld, models.DecimalField):
        return DecimalStoreField(fld, name)
    if isinstance(fld, models.AutoField):
        return AutoStoreField(fld, name)
        #~ kw.update(type='int')
    if isinstance(fld, models.SmallIntegerField):
        return IntegerStoreField(fld, name)
    if isinstance(fld, dd.DisplayField):
        return DisplayStoreField(fld, name)
    if isinstance(fld, models.IntegerField):
        return IntegerStoreField(fld, name)
    kw = {}
    if choosers.uses_simple_values(fld):
        return StoreField(fld, name, **kw)
    else:
        return ComboStoreField(fld, name, **kw)
Exemplo n.º 20
0
 def install_permission_handler(self):
     self.allow_read = curry(make_view_permission_handler(
         self, True,
         self.debug_permissions,
         self.required_roles), self)
Exemplo n.º 21
0
Arquivo: store.py Projeto: NewRGB/lino
def create_atomizer(holder, fld, name):
    """
    The holder is where the (potential) choices come from. It can be
    a model, an actor or an action.
    """
    if name is None:
        return
        # raise Exception("20181023 create_atomizer() {}".format(fld))
    if isinstance(fld, fields.RemoteField):
        """
        Hack: we create a StoreField based on the remote field,
        then modify its behaviour.
        """
        sf = create_atomizer(holder, fld.field, fld.name)

        # print("20180712 create_atomizer {} from {}".format(sf, fld.field))

        def value_from_object(sf, obj, ar=None):
            m = fld.func
            return m(obj, ar)

        def full_value_from_object(sf, obj, ar=None):
            m = fld.func
            v = m(obj, ar)
            return v

        def set_value_in_object(sf, ar, instance, v):
            # print("20180712 {}.set_value_in_object({}, {})".format(
            #     sf, instance, v))
            old_value = sf.value_from_object(instance, ar.request)
            if old_value != v:
                return fld.setter(instance, v)

        sf.value_from_object = curry(value_from_object, sf)
        sf.full_value_from_object = curry(full_value_from_object, sf)
        sf.set_value_in_object = curry(set_value_in_object, sf)
        return sf

    meth = getattr(fld, '_return_type_for_method', None)
    if meth is not None:
        # uh, this is tricky...
        return MethodStoreField(fld, name)

    sf_class = getattr(fld, 'lino_atomizer_class', None)
    if sf_class is not None:
        return sf_class(fld, name)

    if isinstance(fld, fields.DummyField):
        return None
    if isinstance(fld, fields.RequestField):
        delegate = create_atomizer(holder, fld.return_type, fld.name)
        return RequestStoreField(fld, delegate, name)
    if isinstance(fld, fields.VirtualField):
        delegate = create_atomizer(holder, fld.return_type, fld.name)
        if delegate is None:
            # e.g. VirtualField with DummyField as return_type
            return None
            # raise Exception("No atomizer for %s %s %s" % (
            #     holder, fld.return_type, fld.name))
        return VirtStoreField(fld, delegate, name)
    if isinstance(fld, models.FileField):
        return FileFieldStoreField(fld, name)
    if isinstance(fld, models.ManyToManyField):
        return StoreField(fld, name)
    if isinstance(fld, fields.PasswordField):
        return PasswordStoreField(fld, name)
    if isinstance(fld, models.OneToOneField):
        return OneToOneStoreField(fld, name)
    if isinstance(fld, models.OneToOneRel):
        return OneToOneRelStoreField(fld, name)

    if settings.SITE.is_installed('contenttypes'):
        from lino.core.gfks import GenericForeignKey
        if isinstance(fld, GenericForeignKey):
            return GenericForeignKeyField(fld, name)
        from lino.modlib.gfks.fields import GenericForeignKeyIdField
        if isinstance(fld, GenericForeignKeyIdField):
            return ComboStoreField(fld, name)

    if isinstance(fld, models.ForeignKey):
        return ForeignKeyStoreField(fld, name)
    if isinstance(fld, models.TimeField):
        return TimeStoreField(fld, name)
    if isinstance(fld, models.DateTimeField):
        return DateTimeStoreField(fld, name)
    if isinstance(fld, fields.IncompleteDateField):
        return IncompleteDateStoreField(fld, name)
    if isinstance(fld, models.DateField):
        return DateStoreField(fld, name)
    if isinstance(fld, models.BooleanField):
        return BooleanStoreField(fld, name)
    if isinstance(fld, models.DecimalField):
        return DecimalStoreField(fld, name)
    if isinstance(fld, models.AutoField):
        return AutoStoreField(fld, name)
        # kw.update(type='int')
    if isinstance(fld, models.SmallIntegerField):
        return IntegerStoreField(fld, name)
    if isinstance(fld, fields.DisplayField):
        return DisplayStoreField(fld, name)
    if isinstance(fld, models.IntegerField):
        return IntegerStoreField(fld, name)
    if isinstance(fld, models.ManyToOneRel):
        # raise Exception("20190625 {} {} {}".format(holder, fld, name))
        return
    kw = {}
    if choosers.uses_simple_values(holder, fld):
        return StoreField(fld, name, **kw)
    else:
        return ComboStoreField(fld, name, **kw)
Exemplo n.º 22
0
def create_atomizer(model, fld, name):
    if isinstance(fld, fields.RemoteField):
        """
        Hack: we create a StoreField based on the remote field,
        then modify its behaviour.
        """
        sf = create_atomizer(model, fld.field, fld.name)

        def value_from_object(sf, obj, ar):
            m = fld.func
            return m(obj, ar)

        def full_value_from_object(sf, obj, ar):
            m = fld.func
            v = m(obj, ar)
            return v

        sf.value_from_object = curry(value_from_object, sf)
        sf.full_value_from_object = curry(full_value_from_object, sf)
        return sf
    meth = getattr(fld, '_return_type_for_method', None)
    if meth is not None:
        # uh, this is tricky...
        return MethodStoreField(fld, name)

    sf_class = getattr(fld, 'lino_atomizer_class', None)
    if sf_class is not None:
        return sf_class(fld, name)

    if isinstance(fld, fields.DummyField):
        return None
    if isinstance(fld, fields.RequestField):
        delegate = create_atomizer(model, fld.return_type, fld.name)
        return RequestStoreField(fld, delegate, name)
    if isinstance(fld, fields.VirtualField):
        delegate = create_atomizer(model, fld.return_type, fld.name)
        return VirtStoreField(fld, delegate, name)
    if isinstance(fld, models.FileField):
        return FileFieldStoreField(fld, name)
    if isinstance(fld, models.ManyToManyField):
        return StoreField(fld, name)
    if isinstance(fld, fields.PasswordField):
        return PasswordStoreField(fld, name)
    if isinstance(fld, models.OneToOneField):
        return OneToOneStoreField(fld, name)
    if isinstance(fld, GenericForeignKey):
        return GenericForeignKeyField(fld, name)
    if isinstance(fld, fields.GenericForeignKeyIdField):
        return ComboStoreField(fld, name)
    if isinstance(fld, models.ForeignKey):
        return ForeignKeyStoreField(fld, name)
    if isinstance(fld, models.TimeField):
        return TimeStoreField(fld, name)
    if isinstance(fld, models.DateTimeField):
        return DateTimeStoreField(fld, name)
    if isinstance(fld, fields.IncompleteDateField):
        return IncompleteDateStoreField(fld, name)
    if isinstance(fld, models.DateField):
        return DateStoreField(fld, name)
    if isinstance(fld, models.BooleanField):
        return BooleanStoreField(fld, name)
    if isinstance(fld, models.DecimalField):
        return DecimalStoreField(fld, name)
    if isinstance(fld, models.AutoField):
        return AutoStoreField(fld, name)
        #~ kw.update(type='int')
    if isinstance(fld, models.SmallIntegerField):
        return IntegerStoreField(fld, name)
    if isinstance(fld, fields.DisplayField):
        return DisplayStoreField(fld, name)
    if isinstance(fld, models.IntegerField):
        return IntegerStoreField(fld, name)
    kw = {}
    if choosers.uses_simple_values(model, fld):
        return StoreField(fld, name, **kw)
    else:
        return ComboStoreField(fld, name, **kw)
Exemplo n.º 23
0
def create_atomizer(holder, fld, name):
    """
    The holder is where the (potential) choices come from. It can be
    a model, an actor or an action.
    """
    if name is None:
        return
        # raise Exception("20181023 create_atomizer() {}".format(fld))
    if isinstance(fld, fields.RemoteField):
        """
        Hack: we create a StoreField based on the remote field,
        then modify its behaviour.
        """
        sf = create_atomizer(holder, fld.field, fld.name)
        # print("20180712 create_atomizer {} from {}".format(sf, fld.field))

        def value_from_object(sf, obj, ar=None):
            m = fld.func
            return m(obj, ar)

        def full_value_from_object(sf, obj, ar=None):
            m = fld.func
            v = m(obj, ar)
            return v

        def set_value_in_object(sf, ar, instance, v):
            # print("20180712 {}.set_value_in_object({}, {})".format(
            #     sf, instance, v))
            old_value = sf.value_from_object(instance, ar.request)
            if old_value != v:
                return fld.setter(instance, v)

        sf.value_from_object = curry(value_from_object, sf)
        sf.full_value_from_object = curry(full_value_from_object, sf)
        sf.set_value_in_object = curry(set_value_in_object, sf)
        return sf
    
    meth = getattr(fld, '_return_type_for_method', None)
    if meth is not None:
        # uh, this is tricky...
        return MethodStoreField(fld, name)

    sf_class = getattr(fld, 'lino_atomizer_class', None)
    if sf_class is not None:
        return sf_class(fld, name)

    if isinstance(fld, fields.DummyField):
        return None
    if isinstance(fld, fields.RequestField):
        delegate = create_atomizer(holder, fld.return_type, fld.name)
        return RequestStoreField(fld, delegate, name)
    if isinstance(fld, fields.VirtualField):
        delegate = create_atomizer(holder, fld.return_type, fld.name)
        if delegate is None:
            # e.g. VirtualField with DummyField as return_type
            return None
            # raise Exception("No atomizer for %s %s %s" % (
            #     holder, fld.return_type, fld.name))
        return VirtStoreField(fld, delegate, name)
    if isinstance(fld, models.FileField):
        return FileFieldStoreField(fld, name)
    if isinstance(fld, models.ManyToManyField):
        return StoreField(fld, name)
    if isinstance(fld, fields.PasswordField):
        return PasswordStoreField(fld, name)
    if isinstance(fld, models.OneToOneField):
        return OneToOneStoreField(fld, name)
    if isinstance(fld, models.OneToOneRel):
        return OneToOneRelStoreField(fld, name)

    if settings.SITE.is_installed('contenttypes'):
        from lino.core.gfks import GenericForeignKey
        if isinstance(fld, GenericForeignKey):
            return GenericForeignKeyField(fld, name)
        from lino.modlib.gfks.fields import GenericForeignKeyIdField
        if isinstance(fld, GenericForeignKeyIdField):
            return ComboStoreField(fld, name)

    if isinstance(fld, models.ForeignKey):
        return ForeignKeyStoreField(fld, name)
    if isinstance(fld, models.TimeField):
        return TimeStoreField(fld, name)
    if isinstance(fld, models.DateTimeField):
        return DateTimeStoreField(fld, name)
    if isinstance(fld, fields.IncompleteDateField):
        return IncompleteDateStoreField(fld, name)
    if isinstance(fld, models.DateField):
        return DateStoreField(fld, name)
    if isinstance(fld, models.BooleanField):
        return BooleanStoreField(fld, name)
    if isinstance(fld, models.DecimalField):
        return DecimalStoreField(fld, name)
    if isinstance(fld, models.AutoField):
        return AutoStoreField(fld, name)
        # kw.update(type='int')
    if isinstance(fld, models.SmallIntegerField):
        return IntegerStoreField(fld, name)
    if isinstance(fld, fields.DisplayField):
        return DisplayStoreField(fld, name)
    if isinstance(fld, models.IntegerField):
        return IntegerStoreField(fld, name)
    kw = {}
    if choosers.uses_simple_values(holder, fld):
        return StoreField(fld, name, **kw)
    else:
        return ComboStoreField(fld, name, **kw)
Exemplo n.º 24
0
def create_atomizer(model, fld, name):
    if isinstance(fld, fields.RemoteField):
        """
        Hack: we create a StoreField based on the remote field,
        then modify its behaviour.
        """
        sf = create_atomizer(model, fld.field, fld.name)

        def value_from_object(sf, obj, ar):
            m = fld.func
            return m(obj, ar)

        def full_value_from_object(sf, obj, ar):
            m = fld.func
            v = m(obj, ar)
            return v

        sf.value_from_object = curry(value_from_object, sf)
        sf.full_value_from_object = curry(full_value_from_object, sf)
        return sf
    meth = getattr(fld, '_return_type_for_method', None)
    if meth is not None:
        # uh, this is tricky...
        return MethodStoreField(fld, name)

    sf_class = getattr(fld, 'lino_atomizer_class', None)
    if sf_class is not None:
        return sf_class(fld, name)

    if isinstance(fld, fields.DummyField):
        return None
    if isinstance(fld, fields.RequestField):
        delegate = create_atomizer(model, fld.return_type, fld.name)
        return RequestStoreField(fld, delegate, name)
    if isinstance(fld, fields.VirtualField):
        delegate = create_atomizer(model, fld.return_type, fld.name)
        return VirtStoreField(fld, delegate, name)
    if isinstance(fld, models.FileField):
        return FileFieldStoreField(fld, name)
    if isinstance(fld, models.ManyToManyField):
        return StoreField(fld, name)
    if isinstance(fld, fields.PasswordField):
        return PasswordStoreField(fld, name)
    if isinstance(fld, models.OneToOneField):
        return OneToOneStoreField(fld, name)
    if isinstance(fld, GenericForeignKey):
        return GenericForeignKeyField(fld, name)
    if isinstance(fld, fields.GenericForeignKeyIdField):
        return ComboStoreField(fld, name)
    if isinstance(fld, models.ForeignKey):
        return ForeignKeyStoreField(fld, name)
    if isinstance(fld, models.TimeField):
        return TimeStoreField(fld, name)
    if isinstance(fld, models.DateTimeField):
        return DateTimeStoreField(fld, name)
    if isinstance(fld, fields.IncompleteDateField):
        return IncompleteDateStoreField(fld, name)
    if isinstance(fld, models.DateField):
        return DateStoreField(fld, name)
    if isinstance(fld, models.BooleanField):
        return BooleanStoreField(fld, name)
    if isinstance(fld, models.DecimalField):
        return DecimalStoreField(fld, name)
    if isinstance(fld, models.AutoField):
        return AutoStoreField(fld, name)
        #~ kw.update(type='int')
    if isinstance(fld, models.SmallIntegerField):
        return IntegerStoreField(fld, name)
    if isinstance(fld, fields.DisplayField):
        return DisplayStoreField(fld, name)
    if isinstance(fld, models.IntegerField):
        return IntegerStoreField(fld, name)
    kw = {}
    if choosers.uses_simple_values(model, fld):
        return StoreField(fld, name, **kw)
    else:
        return ComboStoreField(fld, name, **kw)