예제 #1
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))
예제 #2
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))
예제 #3
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),
    )
예제 #4
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)
예제 #5
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),
    )
예제 #6
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
예제 #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
예제 #8
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))
예제 #9
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