class City(db.Model): __tablename__ = "cities" code_insee = db.Column(db.String(10), primary_key=True, unique=True) code_postal = db.Column( db.String(50), primary_key=True ) # Long string because some examples seem to have chained post codes name = db.Column(db.String(100), primary_key=True) population = db.Column(db.Float(asdecimal=True)) area = db.Column(db.Float(asdecimal=True)) # Parents county_code = db.Column(db.ForeignKey('counties.code')) county = db.relationship('County', foreign_keys=county_code)
class Log(db.Model): __tablename__ = 'log' id = db.Column(db.Integer, primary_key=True) amount = db.Column(db.Float(precision='8', decimal_return_scale='2')) currency = db.Column(ChoiceType(CURRENCY_CHOICE)) description = db.Column(db.String(4096)) created = db.Column(db.DateTime, default=datetime.datetime.now) def __str__(self): return self.id
class Station(db.Model): __tablename__ = 'station' code = db.Column(db.Integer, primary_key=True) nom = db.Column(db.String(100), unique=True) zone = db.Column(db.String(100), unique=True) departement = db.Column(db.String(100), unique=False) typologie = db.Column(db.String(100)) x_reglementaire = db.Column(db.Float(50)) y_reglementaire = db.Column(db.Float(50)) def __init__(self, code, nom, zone, departement, typologie, x_reglementaire, y_reglementaire): self.code = code self.nom = nom self.zone = zone self.departement = departement self.typologie = typologie self.x_reglementaire = x_reglementaire self.y_reglementaire = y_reglementaire def __str__(self): return "%s" % self.nom
class Query(db.Model): __tablename__ = 'queries' query_id = db.Column(db.Integer, autoincrement=True, primary_key=True) query_time = db.Column(db.DateTime, nullable=False) answer_time = db.Column(db.Float(4), nullable=False) url = db.Column(db.String(200), index=True, nullable=False) method = db.Column(db.String(200), index=True, nullable=False) status_code = db.Column(db.Integer, nullable=False) def make_json_serializable(self): return { 'query_id': self.query_id, 'query_time': self.query_time, 'answer_time': self.answer_time, 'url': self.url, 'method': self.method, 'status_code': self.status_code }
class Order(db.Model): __tablename__ = 'orders' order_id = db.Column(db.String(50), nullable=False, primary_key=True) user_id = db.Column(db.Integer, db.ForeignKey('users.user_id')) currency = db.Column(db.String(10), nullable=False) price_paid = db.Column(db.Float(20), nullable=False) email = db.Column(db.String(255), nullable=False) timestamp = db.Column(db.String(50), default=str(datetime.now())[:-7]) checkout_id = db.Column(db.String(100), nullable=False) status = db.Column(db.String(50), default='Paid', nullable=False) shipping = db.relationship('Shipping', backref='/shipping', lazy=True) def as_dict(self): ''' return as dict // this is needed for when I do a fetch request with json ''' return {c.name: str(getattr(self, c.name)) for c in self.__table__.columns} def create_order(self): db.session.add(self) db.session.commit() def custom_query(self, query, value): ''' custom user query. Pass through query, and value . example username:Ian ''' return self.query.filter_by(**{query:value}) def update_status(self, order_id): result = self.custom_query('order_id', order_id).first() if not result: return False result.status = 'Fulfilled' db.session.commit() return True @staticmethod def generate_order(length=6): ''' generate order ID until an ID is generated that doesn't already exist''' generate = string.ascii_uppercase + string.ascii_uppercase + string.digits returned_id = ''.join(choice(generate) for i in range(length)) while(Order().custom_query('order_id', returned_id).first()): generate = string.ascii_uppercase + string.ascii_uppercase + string.digits return returned_id