Example #1
0
class User(SurrogatePK, Model):
    username = db.Column(db.String(40))
    password_hash = db.Column(db.String(40))

    @property
    def password(self):
        raise AttributeError("当前属性不可读")

    @password.setter
    def password(self, value):
        self.password_hash = generate_password_hash(value)

    def check_password(self, password):
        password_byte = str(password).encode('utf')  # 然后传入这个变量
        return check_password_hash(self.password_hash, password_byte)

    def to_dict(self):
        return {
            "id":
            self.id,
            "username":
            self.username,
            "created_at":
            self.created_at.strftime("%Y-%m-%d %H:%M:%S")
            if self.created_at else None,
            "updated_at":
            self.updated_at.strftime("%Y-%m-%d %H:%M:%S")
            if self.updated_at else None
        }

    def set_password(self, password):
        self.password_hash = generate_password_hash(password)

    def validate_password(self, password):
        return check_password_hash(self.password_hash, password)
Example #2
0
class Course(SurrogatePK, Model):
    name = db.Column(db.String(30))  # course name
    start_time = db.Column(db.DateTime)  # course start time
    end_time = db.Column(db.DateTime)  # course end time

    def to_dict(self):
        return {
            "name": self.name,
            "start_time": self.start_time,
            "end_time": self.end_time
        }
Example #3
0
def reference_col(tablename, nullable=False, pk_name='id', **kwargs):
    """Column that adds primary key foreign key reference.

    Usage: ::

        category_id = reference_col('category')
        category = relationship('Category', backref='categories')
    """
    return db.Column(db.ForeignKey('{0}.{1}'.format(tablename, pk_name)),
                     nullable=nullable,
                     **kwargs)
Example #4
0
class SurrogatePK(object):
    """A mixin that adds a surrogate integer 'primary key' column named
    ``id`` to any declarative-mapped class.
    """
    __table_args__ = {'extend_existing': True}

    id = db.Column(db.BigInteger, primary_key=True)
    created_at = db.Column(db.DateTime, default=datetime.now)
    updated_at = db.Column(db.DateTime,
                           default=datetime.now,
                           onupdate=datetime.now)

    @classmethod
    def get_by_id(cls, id):
        id = int(id)
        if id <= 0:
            raise ValueError('ID must not be negative or zero!')
        if any((isinstance(id, (str, bytes))
                and id.isdigit(), isinstance(id, (int, float))), ):
            return cls.query.get(int(id))
        return None
Example #5
0
class SurrogatePK(object):
    """A mixin that adds a surrogate integer 'primary key' column named ``id`` to any declarative-mapped class."""

    __table_args__ = {'extend_existing': True}

    id = db.Column(db.Integer, primary_key=True)

    @classmethod
    def get_by_id(cls, record_id):
        """Get record by ID."""
        if any((isinstance(record_id, basestring)
                and record_id.isdigit(), isinstance(record_id,
                                                    (int, float))), ):
            return cls.query.get(int(record_id))
        return None
Example #6
0
class User(DBMixin, db.Model):
    id = db.Column(db.String(64), primary_key=True, nullable=False)
    name = db.Column(db.String(64), nullable=False)