def create(cls, data): foreign_id = data.get('foreign_id') src = Source.by_foreign_id(foreign_id) if src is None: src = cls() src.foreign_id = foreign_id or make_token() src.update(data) db.session.flush() return src
def to_token(self): if self.token_id is None: self.token_id = "%s.%s" % (self.id, make_token()) key = cache.key(self.TOKENS, self.token_id) state = { "id": self.id, "roles": list(self.roles), "is_admin": self.is_admin, } cache.set_complex(key, state, expires=self.expire) return self.token_id
def create(cls, data, role): collection = cls() collection.update(data) collection.foreign_id = make_token() collection.creator = role db.session.add(collection) db.session.flush() if role is not None: Permission.grant_resource(Permission.COLLECTION, collection.id, role, True, True) return collection
def create(cls, data, role=None): foreign_id = data.get('foreign_id') or make_token() collection = cls.by_foreign_id(foreign_id) if collection is None: collection = cls() collection.foreign_id = foreign_id collection.creator = role collection.update(data) db.session.add(collection) db.session.flush() if role is not None: Permission.grant_collection(collection.id, role, True, True) return collection
def load_or_create(cls, foreign_id, type_, name, email=None, is_admin=None): role = cls.by_foreign_id(foreign_id) if role is None: role = cls() role.foreign_id = foreign_id role.name = name or email role.type = type_ role.is_admin = False role.is_muted = False role.is_tester = False role.is_blocked = False role.notified_at = datetime.utcnow() if role.api_key is None: role.api_key = make_token() if email is not None: role.email = email if is_admin is not None: role.is_admin = is_admin # see: https://github.com/alephdata/aleph/issues/111 auto_admins = [a.lower() for a in settings.ADMINS] if email is not None and email.lower() in auto_admins: role.is_admin = True db.session.add(role) db.session.flush() return role