class Schema(db.Model): """Represent a JSON schema.""" id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(100)) description = db.Column(db.String(500)) last_updated = db.Column(db.DateTime(), default=datetime.utcnow) json_schema = db.Column(JSONB, default={}) # relationship objects = db.relationship("JsonObject", backref="schema", lazy="dynamic", cascade="all,delete-orphan") licenses = db.relationship("License", secondary=lambda: association_table_license, backref="schemas") # foreign keys org_id = db.Column(db.Integer(), db.ForeignKey("organization.id"), default=None) creator_id = db.Column(db.Integer(), db.ForeignKey("user.id"), default=None)
class Organization(db.Model): """Represent an organization.""" id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(100), unique=True) description = db.Column(db.String(500), default="") organization_type = db.Column(db.String(100), default="") website = db.Column(db.String(100), default="") last_updated = db.Column(db.DateTime(), default=datetime.utcnow) is_membership_restricted = db.Column(db.Boolean(), default=True) # relationship objects = db.relationship( "JsonObject", backref="organization", lazy="dynamic", cascade="all,delete-orphan", ) schemas = db.relationship("Schema", backref="organization", lazy="dynamic", cascade="all,delete-orphan") def __str__(self): return self.name def __eq__(self, obj): return self.id == obj.id
class User(db.Model, UserMixin): """ Represent a user. """ id = db.Column(db.Integer, primary_key=True) login = db.Column(db.String(30), unique=True, nullable=False) pwdhash = db.Column(db.String(), nullable=False) created_at = db.Column(db.DateTime(), default=datetime.utcnow) last_seen = db.Column(db.DateTime(), default=datetime.utcnow) apikey = db.Column(db.String(), default=secrets.token_urlsafe(50)) public_profile = db.Column(db.Boolean(), default=True) # user rights is_active = db.Column(db.Boolean(), default=False) is_admin = db.Column(db.Boolean(), default=False) is_api = db.Column(db.Boolean(), default=False) # relationships organizations = db.relationship( "Organization", secondary=lambda: association_table_organization, backref="users", ) objects = db.relationship("JsonObject", backref="creator", lazy="dynamic", cascade="all,delete-orphan") schemas = db.relationship("Schema", backref="creator", lazy="dynamic", cascade="all,delete-orphan") def get_id(self): """ Return the id of the user. """ return self.id def check_password(self, password): """ Check the password of the user. """ return check_password_hash(self.pwdhash, password) def generate_apikey(self): self.apikey = secrets.token_urlsafe(50) def __str__(self): return self.login @validates("login") def validates_login(self, key, value): assert 3 <= len(value) <= 30, AssertionError( "maximum length for login: 30") return re.sub("[^a-zA-Z0-9_.]", "", value.strip())
class License(db.Model): """Represent a license. https://opensource.org/licenses/alphabetical """ id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(), default="", nullable=False, unique=True) license_id = db.Column(db.String(), default="", nullable=False, unique=True) created_at = db.Column(db.DateTime(), default=datetime.utcnow) def __str__(self): return self.name