class Log(db.Model): __tablename__ = 'logs' id = db.Column(db.Integer, primary_key=True) time = db.Column(db.DateTime, nullable=False) user_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False) user = db.relationship('User', backref=backref('logs', cascade="all,delete,delete-orphan")) action = db.Column(db.String(24), nullable=False) geometry = db.Column(Geometry('MULTIPOLYGON', srid=4326)) format = db.Column(db.String) srs = db.Column(db.String) mapping = db.Column(db.String) source = db.Column(db.String) layer = db.Column(db.String) zoom_level_start = db.Column(db.Integer) zoom_level_end = db.Column(db.Integer) refreshed = db.Column(db.Boolean) @property def geometry_as_geojson(self): if self.geometry is not None: geom = json.dumps(shapely.geometry.mapping(to_shape( self.geometry))) return geom return False
class SearchLog(db.Model): __tablename__ = 'search_logs' id = db.Column(db.Integer, primary_key=True) time = db.Column(db.DateTime, default=datetime.datetime.utcnow, nullable=False) user_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False) user = db.relationship('User', backref=backref('search_logs', cascade="all,delete,delete-orphan"))
class SearchLogGeometry(db.Model): __tablename__ = 'search_log_geometries' id = db.Column(db.Integer, primary_key=True) search_log_id = db.Column(db.Integer, db.ForeignKey('search_logs.id'), nullable=False) search_log = db.relationship('SearchLog', backref=backref( 'geometries', cascade="all,delete,delete-orphan")) geometry = db.Column(Geometry('POLYGON', srid=3857)) identifier = db.Column(db.String)
class WFSSession(db.Model): __tablename__ = 'wfs_sessions' id = db.Column(db.Integer, primary_key=True) active = db.Column(db.Boolean, default=True) layer = db.Column(db.String, nullable=False) url = db.Column(db.String, nullable=False) start_edit = db.Column(db.DateTime, default=datetime.utcnow) last_update = db.Column(db.DateTime, default=None) user_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False) user = db.relationship('User', backref=backref('wfs_sessions', cascade="all,delete,delete-orphan")) @classmethod def by_active_user_layer(cls, layer, user): q = WFSSession.query.filter_by(user=user, active=True, layer=layer) return q.first() def update(self): self.last_update = datetime.utcnow()
class EmailVerification(db.Model): __tablename__ = 'password_recovery' id = db.Column(db.Integer, primary_key=True) hash = db.Column(db.String(36), unique=True, nullable=False) type = db.Column(db.String(10), nullable=False) user_id = db.Column(db.Integer, db.ForeignKey('users.id')) valid_till = db.Column( db.DateTime, default=lambda: datetime.datetime.utcnow() + RECOVER_VALID_FOR) @classmethod def by_hash(cls, hash): q = EmailVerification.query.filter(EmailVerification.hash == hash) recover = q.first() if recover: if datetime.datetime.utcnow() < recover.valid_till: return recover db.session.remove(recover) db.session.commit() @classmethod def recover(cls, user): return EmailVerification(user=user, hash=str(uuid.uuid4()), type='recover') @classmethod def verify(cls, user): return EmailVerification(user=user, hash=str(uuid.uuid4()), type='verify') @classmethod def verify_import(cls, user): return EmailVerification(user=user, hash=str(uuid.uuid4()), type='import') @property def is_recover(self): return self.type == 'recover' @property def is_verify(self): return self.type == 'verify' @property def is_import(self): return self.type == 'import' @property def url(self): if self.type == 'recover': return url_for('user.recover_password', uuid=self.hash, _external=True) if self.type == 'verify': return url_for('user.verify', uuid=self.hash, _external=True) if self.type == 'import': return url_for('user.new_password', uuid=self.hash, _external=True) raise AssertionError('unknown verification type: %s', self.type)