Esempio n. 1
0
 class DatetimeSensitiveModel(Model):
     table = db.Table(
         'datetime_sensitive_model',
         metadata,
         db.Column('id', db.Integer, primary_key=True),
         db.Column('a_date_field', db.DateTime()),
     )
Esempio n. 2
0
class Task(Model):
    table = db.Table(
        'advanced_querying_task',
        metadata,
        db.Column('id', db.Integer, primary_key=True),
        db.Column('uuid',
                  db.String(36),
                  nullable=False,
                  index=True,
                  default=generate_uuid),
        db.Column('name', db.UnicodeText, nullable=False),
        db.Column('done_at', db.DateTime),
        db.Column('updated_at', db.DateTime, default=autonow),
    )

    @classmethod
    def list_pending(model, *expressions):
        table = model.table

        params = dict(order_by=(desc(table.c.updated_at), ))
        return model.objects().where_many(
            model.table.c.done_at == None,
            *expressions,
            **params,
        )

    @classmethod
    def get_by_uuid(model, uuid):
        table = model.table
        return model.objects().where_one(model.table.c.uuid == uuid)
Esempio n. 3
0
class User(Model):
    table = db.Table(
        'user', metadata, db.Column('id', db.Integer, primary_key=True),
        db.Column('email', db.String(100), nullable=False, unique=True),
        db.Column('password', db.String(100), nullable=False, unique=True),
        db.Column('created_at', db.DateTime, default=datetime.now),
        db.Column('updated_at', db.DateTime, default=datetime.now))
Esempio n. 4
0
 class DatetimeSensitiveModel(Model):
     table = db.Table(
         "datetime_sensitive_model",
         metadata,
         db.Column("id", db.Integer, primary_key=True),
         db.Column("a_date_field", db.DateTime()),
     )
Esempio n. 5
0
class ExquisiteModel(Model):
    table = db.Table(
        "dummy_exquisite",
        metadata,
        db.Column("id", db.Integer, primary_key=True),
        db.Column("score", db.Numeric(), default="10.3"),
        db.Column("created_at", db.DateTime(), default=now),
    )
Esempio n. 6
0
class DummyUserModel(Model):
    table = db.Table(
        "dummy_user_model",
        metadata,
        db.Column("id", db.Integer, primary_key=True),
        db.Column("name", db.String(80)),
        db.Column("age", db.Integer),
    )
Esempio n. 7
0
    class AnotherUserModel(Model):
        table = db.Table('another_user_model', metadata,
                         db.Column('id', db.Integer, primary_key=True),
                         db.Column('name', db.String(80)),
                         db.Column('age', db.Integer),
        )

        def preprocess(self, data):
            data['age'] = int(data.get('age', 0)) * 2
Esempio n. 8
0
class User(Model):
    table = db.Table('md_user', metadata,
                     db.Column('id', db.Integer, primary_key=True),
                     db.Column('github_id', db.Integer, nullable=False, unique=True),
                     db.Column('github_token', db.String(256), nullable=True),
                     db.Column('gravatar_id', db.String(40), nullable=False, unique=True),
                     db.Column('username', db.String(80), nullable=False, unique=True),
                     db.Column('email', db.String(100), nullable=False, unique=True),
                     db.Column('created_at', db.DateTime, default=datetime.now),
                     db.Column('updated_at', db.DateTime, default=datetime.now))
Esempio n. 9
0
    class AnotherUserModel(Model):
        table = db.Table(
            "another_user_model",
            metadata,
            db.Column("id", db.Integer, primary_key=True),
            db.Column("name", db.String(80)),
            db.Column("age", db.Integer),
        )

        def preprocess(self, data):
            data["age"] = int(data.get("age", 0)) * 2
Esempio n. 10
0
class User(Model):
    table = db.Table(
        "md_user",
        metadata,
        db.Column("id", db.Integer, primary_key=True),
        db.Column("github_id", db.Integer, nullable=False, unique=True),
        db.Column("github_token", db.String(256), nullable=True),
        db.Column("gravatar_id", db.String(40), nullable=False, unique=True),
        db.Column("username", db.String(80), nullable=False, unique=True),
        db.Column("email", db.String(100), nullable=False, unique=True),
        db.Column("created_at", db.DateTime, default=datetime.now),
        db.Column("updated_at", db.DateTime, default=datetime.now),
    )
Esempio n. 11
0
class Token(Model):
    table = db.Table(
        "auth_token",
        metadata,
        db.Column("id", db.Integer, primary_key=True, autoincrement=True),
        db.Column(
            "user_id",
            db.Integer,
            db.ForeignKey("auth_user.id", ondelete="CASCADE"),
            nullable=False,
        ),
        db.Column("data", db.UnicodeText, nullable=True),
        db.Column("source", db.Unicode(64), nullable=True),
        db.Column("created_at", db.DateTime, nullable=True),
        db.Column("expired_at", db.DateTime, nullable=True),
    )

    @property
    def user(self):
        return User.find_one_by(id=self.user_id)

    def is_valid(self):
        return pendulum.parse(token.expires_at) < pendulum.utcnow(tz='UTC')
Esempio n. 12
0
class ExquisiteModel(Model):
    table = db.Table('dummy_exquisite', metadata,
                     db.Column('id', db.Integer, primary_key=True),
                     db.Column('score', db.Numeric(), default='10.3'),
                     db.Column('created_at', db.DateTime(), default=now))
Esempio n. 13
0
class DummyUserModel(Model):
    table = db.Table('dummy_user_model', metadata,
                     db.Column('id', db.Integer, primary_key=True),
                     db.Column('name', db.String(80)),
                     db.Column('age', db.Integer))
Esempio n. 14
0
class User(Model):
    table = db.Table(
        'auth_user',
        metadata,
        db.Column('id', db.Integer, primary_key=True, autoincrement=True),
        db.Column('email', db.String(255), nullable=False, unique=True),
        db.Column('password', db.String(255), nullable=False),
        db.Column('auth_token', db.Text, nullable=True),
    )

    @classmethod
    def create(cls, email, password, **kw):
        email = email.lower()
        password = cls.secretify_password(password)
        now = autonow().isoformat()
        return super().create(email=email,
                              password=password,
                              created_at=now,
                              updated_at=now,
                              **kw).save()

    def to_dict(self):
        # prevent password to leak inside serialized payloads
        data = self.serialize()
        data.pop('password', None)
        return data

    @classmethod
    def secretify_password(cls, plain):
        return PasswordHasher().hash(plain.encode())

    def match_password(self, plain):
        try:
            PasswordHasher().verify(self.password, plain)
            return True
        except VerifyMismatchError:
            return False

    def change_password(self, old_password, new_password):
        right_password = self.match_password(old_password)
        if right_password:
            secret = self.secretify_password(new_password)
            return self.set(password=secret).save()

        return None

    def authenticate(self, token):
        table = Token.table
        found = Token.where_one(
            table.c.user_id == self.id,
            table.c.data == token,
        )
        if not found:
            logger.warning(f'no token found for user {self} found')
            raise AuthenticationFailed(
                f'token not found for email {self.email}')

        if not found.is_valid():
            raise UserTokenExpired(f'{self.email} "{uuid}:{value}"')

        return token