class City(Intra): uuid = db.Column(db.String(36), index=True, unique=True, nullable=False) intra_code = db.Column(db.String(255), index=True, unique=True, nullable=False) slug = db.Column(db.String(255), index=True, unique=True, nullable=False) name = db.Column(db.String(255), nullable=False) country_id = db.Column(db.Integer, db.ForeignKey('country.id'), nullable=False) buildings = db.relationship('Building', lazy=True, backref=db.backref('city', lazy=True), cascade='all, delete-orphan') rooms = db.relationship('Room', lazy=True, backref=db.backref('city', lazy=True), cascade='all, delete-orphan') def __init__(self, key, name, country): self.uuid = uuid.uuid4() self.intra_code = key self.slug = slugify(key[3:]) self.name = name self.country = country def __repr__(self): return '<City %r>' % self.id
class Country(Intra): uuid = db.Column(db.String(36), index=True, unique=True, nullable=False) intra_code = db.Column(db.String(255), index=True, unique=True, nullable=False) slug = db.Column(db.String(255), index=True, unique=True, nullable=False) name = db.Column(db.String(255), nullable=False) cities = db.relationship('City', lazy=True, backref=db.backref('country', lazy=True), cascade='all, delete-orphan') buildings = db.relationship('Building', lazy=True, backref=db.backref('country', lazy=True), cascade='all, delete-orphan') rooms = db.relationship('Room', lazy=True, backref=db.backref('country', lazy=True), cascade='all, delete-orphan') def __init__(self, key, name): self.uuid = uuid.uuid4() self.intra_code = key self.slug = slugify(key) self.name = name def __repr__(self): return '<Country %r>' % self.id
class Dated(Base): __abstract__ = True # timestamps ctime = db.Column(db.DateTime, nullable=False, default=db.func.current_timestamp()) mtime = db.Column(db.DateTime, nullable=False, default=db.func.current_timestamp(), onupdate=db.func.current_timestamp())
class Intra(Base): __abstract__ = True # crawler timestamps # itime = first indexation time # utime = last update time itime = db.Column(db.DateTime, nullable=False, default=db.func.current_timestamp()) utime = db.Column(db.DateTime, nullable=False, default=db.func.current_timestamp(), onupdate=db.func.current_timestamp())
class RoomType(Base): uuid = db.Column(db.String(36), index=True, unique=True, nullable=False) intra_code = db.Column(db.String(255), index=True, unique=True, nullable=False) name = db.Column(db.String(255), nullable=False) def __init__(self, key, name): self.uuid = uuid.uuid4() self.intra_code = key self.name = name def __repr__(self): return '<RoomType %r>' % self.id
class Base(db.Model): __abstract__ = True __table_args__ = { 'mysql_row_format': 'DYNAMIC' } #id id = db.Column(db.Integer, primary_key=True)
class CrawlerLog(Base): type = db.Column(db.String(255), index=True, nullable=False) time = db.Column(db.DateTime, nullable=False, default=db.func.current_timestamp(), onupdate=db.func.current_timestamp()) status = db.Column(db.Integer, nullable=False) # -1=working, 0=failed, 1=succeeded msg = db.Column(db.Text) def __init__(self, type, msg=None): self.type = type self.status = -1 self.msg = msg def __repr__(self): return '<CrawlerLog %r>' % self.id
class School(Intra): # uuid uuid = db.Column(db.String(36), index=True, unique=True, nullable=False) intra_code = db.Column(db.String(255), index=True, nullable=False) # school info slug = db.Column(db.String(255), index=True, unique=True, nullable=False) name = db.Column(db.String(255), nullable=False) def __init__(code, name): self.uuid = uuid.uuid4() self.intra_code = code self.slug = slugify(code) self.name = name def __repr__(self): return '<School %r>' % self.id
class Room(Intra): uuid = db.Column(db.String(36), index=True, unique=True, nullable=False) intra_code = db.Column(db.String(255), index=True, unique=True, nullable=False) slug = db.Column(db.String(255), index=True, unique=True, nullable=False) name = db.Column(db.String(255), nullable=False) seats = db.Column(db.Integer, nullable=False) types = db.relationship('RoomType', secondary=room_types, lazy='subquery', backref=db.backref('rooms', lazy=True), cascade='all') country_id = db.Column(db.Integer, db.ForeignKey('country.id')) city_id = db.Column(db.Integer, db.ForeignKey('city.id')) building_id = db.Column(db.Integer, db.ForeignKey('building.id')) def __init__(self, key, name, seats, types, country=None, city=None, building=None): self.uuid = uuid.uuid4() self.intra_code = key self.slug = slugify(key[3:]) self.name = name self.seats = seats self.types = types self.country = country self.city = city self.building = building def __repr__(self): return '<Room %r>' % self.id
class Picture(Base): # uuid uuid = db.Column(db.String(36), index=True, unique=True, nullable=False) # picture metadata # origin can be: # - intra:<url> # - upload origin = db.Column(db.String(255), index=True, nullable=False) width = db.Column(db.Integer, nullable=False) height = db.Column(db.Integer, nullable=False) checksum = db.Column(db.String(32)) # available sizes _sizes = db.Column(db.Text, name='sizes', nullable=False) def __init__(self, image, origin='upload'): self.uuid = str(uuid.uuid4()) self.origin = origin self.width = image.width self.height = image.height self.sizes = SIZES save_picture(self.uuid, image) @property def sizes(self): return json.loads(self._sizes) @sizes.setter def sizes(self, value): self._sizes = json.dumps(value) def get_url(self, size=None): subdir = '' if size is not None and [size[0], size[1]] in self.sizes: subdir = str(size[0]) + 'x' + str(size[1]) + '/' return url_for('static', filename=PICTURE_STATIC_DIR + subdir + self.uuid + '.' + FORMAT) @property def url(self): return self.get_url() def __repr__(self): return '<Picture %r>' % self.id
class OauthToken(Base): # associated application client_id = db.Column(db.String(36), db.ForeignKey('oauth_app.client_id'), nullable=False) # token info token_type = db.Column(db.String(40), nullable=False) expires = db.Column(db.DateTime, nullable=False) _scopes = db.Column(db.Text, name='scopes', nullable=False) # token strings access_token = db.Column(db.String(255), unique=True, nullable=False) refresh_token = db.Column(db.String(255), unique=True, nullable=False) # associated user user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) def __init__(self, *args, **kwargs): self.access_token = kwargs.get('access_token') self.token_type = kwargs.get('token_type') self.refresh_token = kwargs.get('refresh_token') self.scopes = kwargs.get('scope').split() def delete(self): db.session.delete(self) db.session.commit() return self @property def scopes(self): return json.loads(self._scopes) @scopes.setter def scopes(self, scopes): self._scopes = json.dumps(scopes) def __repr__(self): return '<OauthToken %r>' % self.id
self.intra_code = key self.slug = slugify(key[3:]) self.name = name self.country = country self.city = city def __repr__(self): return '<Building %r>' % self.id room_types = db.Table( 'room_xref_room_type', db.Column('room_type_id', db.Integer, db.ForeignKey('room_type.id'), primary_key=True), db.Column('room_id', db.Integer, db.ForeignKey('room.id'), primary_key=True)) class Room(Intra): uuid = db.Column(db.String(36), index=True, unique=True, nullable=False) intra_code = db.Column(db.String(255), index=True, unique=True, nullable=False)
class OauthApp(Dated): # app credentials client_id = db.Column(db.String(36), index=True, unique=True, nullable=False) client_secret = db.Column(db.String(32), index=True, unique=True, nullable=False) # app settings is_confidential = db.Column(db.Boolean, nullable=False) _redirect_uris = db.Column(db.Text, name='redirect_uris', nullable=False) _default_scopes = db.Column(db.Text, name='default_scopes', nullable=False) # app info name = db.Column(db.String(255), index=True, nullable=False) description = db.Column(db.Text) website = db.Column(db.String(255), nullable=False) # app picture picture_id = db.Column(db.Integer, db.ForeignKey('picture.id')) picture = db.relationship('Picture', lazy=True, backref=db.backref('app', lazy=True), cascade='all, delete-orphan', single_parent=True) # app owner owner_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) # relations tokens = db.relationship('OauthToken', lazy=True, backref=db.backref('client', lazy=True), cascade='all, delete-orphan') # flask_oauth stuff @property def client_type(self): if self.is_confidential: return 'confidential' return 'public' @property def redirect_uris(self): return json.loads(self._redirect_uris) @redirect_uris.setter def redirect_uris(self, value): self._redirect_uris = json.dumps(value) @property def default_redirect_uri(self): return self.redirect_uris[0] @property def default_scopes(self): return json.loads(self._default_scopes) @default_scopes.setter def default_scopes(self, value): self._default_scopes = json.dumps(value) def __repr__(self): return '<OauthApp %r>' % self.id