예제 #1
0
파일: models.py 프로젝트: Drunkar/redash
class ApiKey(ModelTimestampsMixin, BaseModel):
    org = peewee.ForeignKeyField(Organization)
    api_key = peewee.CharField(index=True, default=lambda: generate_token(40))
    active = peewee.BooleanField(default=True)
    object_type = peewee.CharField()
    object_id = peewee.IntegerField()
    object = GFKField('object_type', 'object_id')
    created_by = peewee.ForeignKeyField(User, null=True)

    class Meta:
        db_table = 'api_keys'
        indexes = (
            (('object_type', 'object_id'), False),
        )

    @classmethod
    def get_by_api_key(cls, api_key):
        return cls.get(cls.api_key==api_key, cls.active==True)

    @classmethod
    def get_by_object(cls, object):
        return cls.select().where(cls.object_type==object._meta.db_table, cls.object_id==object.id, cls.active==True).first()

    @classmethod
    def create_for_object(cls, object, user):
        return cls.create(org=user.org, object=object, created_by=user)
class Subscription(BaseModel):
    subscriber_type = CharField(null=True)
    subscriber_id = IntegerField(null=True)
    subscriber = GFKField('subscriber_type', 'subscriber_id')

    resource_type = CharField(null=True)
    resource_id = IntegerField(null=True)
    resource = GFKField('resource_type', 'resource_id')

    extra_type = CharField(null=True)
    extra_id = IntegerField(null=True)
    extra = GFKField('extra_type', 'extra_id')

    class Meta:
        indexes = (((
            'subscriber_type',
            'subscriber_id',
            'resource_type',
            'resource_id',
        ), True), )