Пример #1
0
    def getFlatObject(self, schema=None):
        ''' return flat object with static properties and last existing value of dyn props '''
        data = {}
        hybrid_properties = list(get_hybrid_properties(self.__class__).keys())
        # if self.ID is not None:
        max_iter = max(len(self.__table__.columns), len(
            self.__properties__), len(hybrid_properties))
        for i in range(max_iter):
            # Get static Properties
            try:
                curStatProp = list(self.__table__.columns)[i]
                data[curStatProp.key] = self.getProperty(
                    curStatProp.key)
            except:
                pass
            # Get dynamic Properties
            try:
                curDynPropName = list(self.__properties__)[i]
                data[curDynPropName] = self.getProperty(curDynPropName)
            except Exception as e:
                pass
            try:
                PropName = hybrid_properties[i]
                data[PropName] = self.getProperty(PropName)
            except Exception as e:
                pass

        # if not schema and hasattr(self, 'getForm'):
        #     schema = self.getForm()['schema']
        if schema:
            data = formatValue(data, schema)

        return data
Пример #2
0
def to_json(inst, cls):
    """
    Jsonify the sql alchemy query result.
    """
    convert = dict()
    # add your coversions for things like datetime's
    # and what-not that aren't serializable.
    d = dict()
    for c in cls.__table__.columns:
        v = getattr(inst, c.name)
        if c.type in convert.keys() and v is not None:
            try:
                d[c.name] = convert[c.type](v)
            except:
                d[c.name] = "Error:  Failed to covert using ", str(
                    convert[c.type])
        elif v is None:
            d[c.name] = str()
        else:
            d[c.name] = v

    for c in get_hybrid_properties(cls):
        v = getattr(inst, c)
        d[c] = v

    return d
Пример #3
0
def info(ctx, table, filter_):
    """ Print table summary information
    """
    import sqlalchemy_utils as sau
    from ..db import construct_filter

    db = _db_from_ctx(ctx)
    echoer = cliutils.Echoer(logging.getLogger("tilez"))

    echoer.info("Information about: {}".format(table))
    query = construct_filter(db.session.query(table), filter_)

    echoer.process("Number of entries: {}".format(query.count()))

    # Diagnostic info about table
    template = "{idx: <10} {name: <20}{type: <15}"

    echoer.process("Enumerating columns in table: {}".format(table))
    echoer.item(
        "{idx: <10} {name: <20}{type: <30}".format(idx="COLUMN #", name="NAME", type="TYPE"), bold=True, underline=True
    )
    for idx, col in enumerate(sau.get_columns(table).values()):
        echoer.item(
            template.format(
                **{
                    "idx": "Col {0:02d}".format(idx),
                    "name": '"{}"'.format(col.name),
                    "type": "{type} {pk}".format(type=col.type, pk="(PRIMARY KEY)" if col.primary_key else ""),
                }
            )
        )

    for idx, (name, col) in enumerate(six.iteritems(sau.get_hybrid_properties(table))):
        echoer.item(template.format(**{"idx": "HYBRID {0:02d}".format(idx), "name": '"{}"'.format(name), "type": "?"}))
Пример #4
0
 def is_hybrid(self):
     if self._in_form():
         model = self.parent._marvinform._param_form_lookup[
             self.full].Meta.model
         hybrids = get_hybrid_properties(model).keys()
         return self.db_column in hybrids
     return None
Пример #5
0
    def getFlatObject(self, schema=None):
        ''' return flat object with static properties and last existing value of dyn props '''
        data = {}
        hybrid_properties = list(get_hybrid_properties(self.__class__).keys())
        if self.ID is not None:
            max_iter = max(len(self.__table__.columns), len(
                self.__properties__), len(hybrid_properties))
            for i in range(max_iter):
                # Get static Properties
                try:
                    curStatProp = list(self.__table__.columns)[i]
                    data[curStatProp.key] = self.getProperty(
                        curStatProp.key)
                except:
                    pass
                # Get dynamic Properties
                try:
                    curDynPropName = list(self.__properties__)[i]
                    data[curDynPropName] = self.getProperty(curDynPropName)
                except Exception as e:
                    pass
                try:
                    PropName = hybrid_properties[i]
                    data[PropName] = self.getProperty(PropName)
                except Exception as e:
                    pass

        if not schema and hasattr(self, 'getForm'):
            schema = self.getForm()['schema']
        if schema:
            data = formatValue(data, schema)

        return data
Пример #6
0
 def adapt_attribute(self, attr_name):
     cols = get_attrs(self.from_obj)
     hybrids = get_hybrid_properties(self.model).keys()
     if attr_name in hybrids:
         return ClauseAdapter(self.from_obj).traverse(
             getattr(self.model, attr_name))
     else:
         return getattr(cols, attr_name)
Пример #7
0
    def as_dict(self):
        hybrid_properties = list(get_hybrid_properties(self.__class__).keys())
        values = {c.key: getattr(self, c.key)
                  for c in inspect(self).mapper.column_attrs}

        for propName in hybrid_properties:
            values[propName] = getattr(self, propName)
        return values
Пример #8
0
 def adapt_attribute(self, attr_name):
     cols = get_attrs(self.from_obj)
     hybrids = get_hybrid_properties(self.model).keys()
     if attr_name in hybrids:
         return ClauseAdapter(self.from_obj).traverse(
             getattr(self.model, attr_name)
         )
     else:
         return getattr(cols, attr_name)
Пример #9
0
    def validate_fields(self, fields):
        descriptors = get_all_descriptors(self.from_obj)
        hybrids = get_hybrid_properties(self.model)
        expressions = self.column_property_expressions

        for field in fields:
            if field in hybrids or field in expressions:
                continue
            self.validate_field(field, descriptors)
Пример #10
0
    def validate_fields(self, fields):
        descriptors = get_all_descriptors(self.from_obj)
        hybrids = get_hybrid_properties(self.model)
        expressions = self.column_property_expressions

        for field in fields:
            if field in hybrids or field in expressions:
                continue
            self.validate_field(field, descriptors)
Пример #11
0
 def adapt_attribute(self, attr_name):
     cols = get_attrs(self.from_obj)
     hybrids = get_hybrid_properties(self.model).keys()
     if (attr_name in hybrids
             or attr_name in self.column_property_expressions):
         column = adapt(self.from_obj, getattr(self.model, attr_name))
     else:
         column = getattr(cols, attr_name)
     return self.format_column(column)
Пример #12
0
 def adapted_descriptors(self):
     return (
         get_all_descriptors(self.from_obj).items() +
         [
             (
                 key,
                 ClauseAdapter(self.from_obj).traverse(
                     getattr(self.model, key)
                 )
             )
             for key in get_hybrid_properties(self.model).keys()
         ]
     )
Пример #13
0
 def adapted_descriptors(self):
     return (
         get_all_descriptors(self.from_obj).items() +
         [
             (
                 key,
                 ClauseAdapter(self.from_obj).traverse(
                     getattr(self.model, key)
                 )
             )
             for key in get_hybrid_properties(self.model).keys()
         ]
     )
Пример #14
0
 def adapt_attribute(self, attr_name):
     cols = get_attrs(self.from_obj)
     hybrids = get_hybrid_properties(self.model).keys()
     if (
         attr_name in hybrids or
         attr_name in self.column_property_expressions
     ):
         column = ClauseAdapter(self.from_obj).traverse(
             getattr(self.model, attr_name)
         )
     else:
         column = getattr(cols, attr_name)
     return self.format_column(column)
Пример #15
0
    def GetFlatObject(self, schema=None):
        ''' return flat object with static properties and last existing value of dyn props '''
        resultat = {}
        hybrid_properties = list(get_hybrid_properties(self.__class__).keys())
        if self.ID is not None:
            max_iter = max(len(self.__table__.columns),
                           len(self.PropDynValuesOfNow),
                           len(hybrid_properties))
            for i in range(max_iter):
                #### Get static Properties ####
                try:
                    curStatProp = list(self.__table__.columns)[i]
                    resultat[curStatProp.key] = self.GetProperty(
                        curStatProp.key)
                except:
                    pass
                #### Get dynamic Properties ####
                try:
                    curDynPropName = list(self.PropDynValuesOfNow)[i]
                    resultat[curDynPropName] = self.GetProperty(curDynPropName)
                except Exception as e:
                    pass
                try:
                    PropName = hybrid_properties[i]
                    resultat[PropName] = self.GetProperty(PropName)
                except Exception as e:
                    pass

        else:
            max_iter = len(self.__table__.columns)
            for i in range(max_iter):
                #### Get static Properties ####
                try:
                    curStatProp = list(self.__table__.columns)[i]
                    curVal = self.GetProperty(curStatProp.key)
                    if curVal is not None:
                        resultat[curStatProp.key] = self.GetProperty(
                            curStatProp.key)
                except:
                    pass
        # Add TypeName in JSON
        # resultat['TypeName'] = self.GetType().Name
        return resultat
 def test_declarative_model(self):
     assert (list(get_hybrid_properties(
         self.Category).keys()) == ['lowercase_name'])
 def test_aliased_class(self):
     assert (list(
         get_hybrid_properties(sa.orm.aliased(
             self.Category)).keys()) == ['lowercase_name'])
Пример #18
0
 def test_aliased_class(self, Category):
     props = get_hybrid_properties(sa.orm.aliased(Category))
     assert list(props.keys()) == ['lowercase_name']
 def test_declarative_model(self, Category):
     assert (
         list(get_hybrid_properties(Category).keys()) ==
         ['lowercase_name']
     )
Пример #20
0
 def as_dict(self):
    d = {c.name: getattr(self, c.name) for c in self.__table__.columns}
    d.update({col: getattr(self, col) for col in
             get_hybrid_properties(Indicator).keys()})
    return d
Пример #21
0
 def validate_fields(self, fields):
     descriptors = get_all_descriptors(self.from_obj)
     for field in fields:
         if field in get_hybrid_properties(self.model):
             continue
         self.validate_field(field, descriptors)
 def test_aliased_class(self):
     assert (
         list(get_hybrid_properties(sa.orm.aliased(self.Category)).keys())
         ==
         ['lowercase_name']
     )
 def test_mapper(self):
     assert (list(get_hybrid_properties(sa.inspect(
         self.Category)).keys()) == ['lowercase_name'])
Пример #24
0
 def validate_fields(self, fields):
     descriptors = get_all_descriptors(self.from_obj)
     for field in fields:
         if field in get_hybrid_properties(self.model):
             continue
         self.validate_field(field, descriptors)
 def test_mapper(self, Category):
     assert (
         list(get_hybrid_properties(sa.inspect(Category)).keys()) ==
         ['lowercase_name']
     )