예제 #1
0
파일: utils.py 프로젝트: rsyring/pypicalc2
    def testing_create(cls, **kwargs):
        # automatically sets most field types. Any fields passed into the method
        #   will override the automatic behavior
        # subclasses need to set any necessary key values before calling this method
        #   including primary and foreign keys
        insp = sainsp(cls)
        for column in insp.columns:
            # skip fields already in kwargs, foreign key references, and any
            #   field having a default or server_default configured
            if (column.key in kwargs or column.foreign_keys or column.server_default or
                    column.default or column.primary_key):
                continue

            # If the column is being used for polymorphic inheritance identification, then don't
            # set the value.
            if insp.mapper.polymorphic_on is column:
                continue

            if isinstance(column.type, sa.types.Enum):
                kwargs[column.key] = random.choice(column.type.enums)
            elif isinstance(column.type, sa.types.String):
                kwargs[column.key] = randchars(min(column.type.length or 25, 25))
            elif isinstance(column.type, (sa.types.Integer, sa.types.Numeric)):
                kwargs[column.key] = 0
            elif isinstance(column.type, sa.types.Date):
                kwargs[column.key] = dt.date.today()
            elif isinstance(column.type, sa.types.DateTime):
                kwargs[column.key] = dt.datetime.now()

        return cls.add(**kwargs)
예제 #2
0
파일: utils.py 프로젝트: rsyring/pypicalc2
 def ensure(self, key, _flush=False, _commit=True):
     cls_columns = sainsp(self).mapper.columns
     key_col = getattr(cls_columns, key)
     key_val = getattr(self, key)
     exiting_record = self.query.filter(key_col == key_val).first()
     if not exiting_record:
         db.session.add(self)
         if _flush:
             session_flush()
         elif _commit:
             session_commit()
         return self
     return exiting_record
예제 #3
0
파일: mixins.py 프로젝트: nZac/keg-elements
    def testing_create(cls, **kwargs):
        """Create an object for testing with default data appropriate for the field type

        * Will automatically set most field types ignoring those passed in via kwargs.
        * Subclasses that have foreign key relationships should setup those relationships before
          calling this method.

        Special kwargs:
        _numeric_defaults_range: a tuple of (HIGH, LOW) which controls the acceptable defaults of
                                 the two number types. Both integer and numeric (float) fields are
                                 controlled by this setting.
        """

        NUMERIC_HIGH, NUMERIC_LOW = kwargs.get('_numeric_defaults_range', (-100, 100))

        insp = sainsp(cls)

        skippable = lambda column: (column.key in kwargs      # skip fields already in kwargs
                                    or column.foreign_keys    # skip foreign keys
                                    or column.server_default  # skip fields with server defaults
                                    or column.default         # skip fields with defaults
                                    or column.primary_key     # skip any primary key
                                    )

        for column in (col for col in insp.columns if not skippable(col)):
            if isinstance(column.type, sa.types.Enum):
                kwargs[column.key] = random.choice(column.type.enums)
            elif isinstance(column.type, sa.types.Integer):
                kwargs[column.key] = random.randint(NUMERIC_HIGH, NUMERIC_LOW)
            elif isinstance(column.type, sa.types.Numeric):
                kwargs[column.key] = random.uniform(NUMERIC_HIGH, NUMERIC_LOW)
            elif isinstance(column.type, sa.types.Date):
                kwargs[column.key] = dt.date.today()
            elif isinstance(column.type, sa.types.DateTime):
                kwargs[column.key] = dt.datetime.now()
            elif isinstance(column.type, EmailType):
                kwargs[column.key] = randemail(min(column.type.length or 50, 50))
            elif isinstance(column.type, sa.types.String):
                kwargs[column.key] = randchars(min(column.type.length or 25, 25))

        return cls.add(**kwargs)
예제 #4
0
파일: mixins.py 프로젝트: nZac/keg-elements
 def to_dict(self, exclude=None):
     if exclude is None:
         exclude = []
     insp = sainsp(self)
     return dict((attr.key, attr.value)
                 for attr in insp.attrs if attr.key not in exclude)