#!/usr/bin/python # -*- coding: utf-8 -*- from api.models.model_location_group import LocationGroup from api.utils.database import db from marshmallow_sqlalchemy import ModelSchema from marshmallow import fields from api.models import Base # Location is linked with LocationGroup via many to many relationship: location_groups = db.Table( 'location_groups', db.Column('location_id', db.Integer, db.ForeignKey('location.id'), primary_key=True), db.Column('location_group_id', db.Integer, db.ForeignKey('location_group.id'), primary_key=True)) class Location(Base): __tablename__ = 'location' name = db.Column(db.String(12), unique=True, nullable=False) external_id = db.Column(db.String(12), unique=True, nullable=False) secret_code = db.Column(db.String(12), unique=True, nullable=False) location_groups = db.relationship('LocationGroup', secondary=location_groups,
from api.utils.database import db from marshmallow_sqlalchemy import ModelSchema from marshmallow import fields from .ref import Ref, RefSchema from .category import Category, CategorySchema from .crud import CRUD category_infos = db.Table( 'category_infos', db.metadata, db.Column('category_id', db.Integer, db.ForeignKey('categories.id')), db.Column('info_id', db.Integer, db.ForeignKey('infos.id'))) class Info(db.Model, CRUD): __tablename__ = 'infos' id = db.Column(db.Integer, primary_key=True, autoincrement=True) c_name = db.Column(db.String(255), nullable=True) e_name = db.Column(db.String(255), nullable=True) # TODO 类型信息,暂时还不确定是否和category有区别 desc = db.Column(db.String(255), nullable=True) ref_min = db.Column(db.Float, nullable=True) ref_max = db.Column(db.Float, nullable=True) alias = db.Column(db.String(50), nullable=True) refs = db.relationship('Ref', backref='info', lazy='dynamic') categories = db.relationship('Category', secondary=category_infos, backref='infos') def __init__(self, c_name, e_name,
from datetime import datetime from typing import List from marshmallow import pre_dump, fields from api.models.tag import Tag, TagSchema from api.models.user import UserSchema from api.models.comment import CommentSchema, Comment from api.utils.database import db, ma post_tag = db.Table( "post_tag", db.Column("post_id", db.Integer, db.ForeignKey("posts.id"), primary_key=True), db.Column("tag_id", db.Integer, db.ForeignKey("tags.id"), primary_key=True), ) class Post(db.Model): __tablename__ = "posts" __table_args__ = {"extend_existing": True} id = db.Column(db.Integer, primary_key=True, autoincrement=True) body = db.Column(db.Text) title = db.Column(db.String(120)) timestamp = db.Column(db.DateTime, default=datetime.utcnow) user_id = db.Column(db.Integer, db.ForeignKey("users.id")) # Relationships tags = db.relationship(Tag, secondary=post_tag, backref=db.backref("posts_")) author = db.relationship("User", backref="posts") comments = db.relationship('Comment', backref='post', lazy='dynamic')
from api.utils.database import db from flask_sqlalchemy import SQLAlchemy from marshmallow import Schema, fields, ValidationError, pre_load ##### MODELS ##### associations = db.Table( 'associations', db.Column('category_id', db.Integer, db.ForeignKey('category.id'), primary_key=True), db.Column('product_id', db.Integer, db.ForeignKey('product.id'), primary_key=True)) class Category(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(20), nullable=False, unique=True) products = db.relationship("Product", secondary=associations, back_populates="categories", lazy="dynamic") def __init__(self, name, **kwargs): self.name = name products = kwargs.get('products', None) if products: for p in products: product = Product(name=p)
from api.utils.database import db from marshmallow_sqlalchemy import ModelSchema from marshmallow import fields from .crud import CRUD from flask_bcrypt import Bcrypt from datetime import datetime, timedelta import jwt import os from .role import Role, RoleSchema role_users = db.Table( 'role_users', db.metadata, db.Column('role_id', db.Integer, db.ForeignKey('roles.id')), db.Column('user_id', db.Integer, db.ForeignKey('users.id'))) class User(db.Model, CRUD): __tablename__ = 'users' id = db.Column(db.Integer, primary_key=True, autoincrement=True) email = db.Column(db.String(256)) password = db.Column(db.String(256), nullable=False, unique=True) roles = db.relationship('Role', secondary=role_users, backref='users') def __init__(self, email, password, roles=[]): self.email = email self.password = Bcrypt().generate_password_hash(password).decode() for item in roles: role = Role.query.filter_by(slug=item).one() self.roles.append(role) def password_is_valid(self, password):