Ejemplo n.º 1
0
class Button(db.Model):
    __tablename__ = 'buttons'

    id = db.Column(db.Integer, primary_key=True)
    node_id = db.Column(db.String(50), nullable=False, unique=True)
    user_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False)
    customer_id = db.Column(db.Integer, db.ForeignKey('customers.id'))
    service_id = db.Column(db.Integer, db.ForeignKey('services.id'))

    orders = db.relationship('Order',
                             primaryjoin="Order.button_id == Button.id")

    def __repr__(self):
        """Set to a unique key specific to the object in the database.
        Required for cache.memoize() to work across requests.
        """
        return "<{} {}>".format(self.__class__.__name__, self.id)

    def save(self, user=None, service=None, customer=None):
        """
        Saves an button infomation
        :param user:
        :param service:
        :param customer:
        :return:
        """
        if user and user.id:
            self.user_id = user.id
        if service and service.id:
            self.service_id = service.id
        if customer and customer.id:
            self.customer_id = customer.id
        db.session.add(self)

        try:
            db.session.commit()
        except SQLAlchemyError, e:
            db.session.rollback()
            raise e
        return self
Ejemplo n.º 2
0
class Service(db.Model):
    __tablename__ = 'services'
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(120), nullable=False)
    wechat_template_id = db.Column(db.String(60))
    wechat_template = db.Column(db.String(500))  # 微信模板内容
    count = db.Column(db.Integer, default=1)  # 数量
    unit = db.Column(db.String(6), default=u'桶')  # 单位
    user_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False)

    # One-to-Many
    buttons = db.relationship('Button',
                              backref='service',
                              primaryjoin='Button.service_id == Service.id')

    # One-to-Many
    orders = db.relationship('Order',
                             backref='service',
                             primaryjoin='Order.service_id == Service.id')

    def __init__(self, name):
        self.name = name

    def __repr__(self):
        """Set to a unique key specific to the object in the database.
        Required for cache.memoize() to work across requests.
        """
        return "<{} {}>".format(self.__class__.__name__, self.id)

    def save(self, user=None):
        """

        :param user:
        :return:
        """
        if user:
            self.user_id = user.id
        db.session.add(self)
        db.session.commit()
        return self

    def delete(self):
        """

        :return:
        """
        db.session.delete(self)
        db.session.commit()
Ejemplo n.º 3
0
class Order(db.Model):
    __tablename__ = 'orders'
    id = db.Column(db.Integer, primary_key=True)
    button_id = db.Column(db.Integer, db.ForeignKey('buttons.id'), nullable=False)
    user_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False)
    service_id = db.Column(db.Integer, db.ForeignKey('services.id'), nullable=False)
    service_name = db.Column(db.VARCHAR(255))
    price = db.Column(db.Integer)
    customer_name = db.Column(db.VARCHAR(64))
    customer_addr = db.Column(db.VARCHAR(512))
    phone = db.Column(db.VARCHAR(15))
    create_time = db.Column(db.DateTime, default=datetime.utcnow())
    solved = db.Column(db.Boolean, default=False)
    status = db.Column(db.SmallInteger, default=0)              # 订单状态:0-正在处理, 1-已处理,2-取消

    def __repr__(self):
        """Set to a unique key specific to the object in the database.
        Required for cache.memoize() to work across requests.
        """
        return "<{} {}>".format(self.__class__.__name__, self.id)

    def save(self, button=None, create_time=None):
        """
        Saves an order and return an order object
        :param button:
        :param user:
        :return: Order object
        """
        if self.id:
            db.session.add(self)
            db.session.commit()
        elif button:
            self.button_id = button.id
            self.user_id = button.user.id
            service = button.service
            self.service_id = service.id
            self.service_name = service.name
            self.price = service.price
            customer = button.customer
            self.customer_name = customer.name
            self.phone = customer.telephone
            self.customer_addr = customer.addr
            if create_time:
                self.create_time = create_time
            self.status = 0
            db.session.add(self)
            try:
                db.session.commit()
            except SQLAlchemyError:
                db.session.rollback()
        return self

    def delete(self):
        """
        :return:
        """
        db.session.delete(self)
        try:
            db.session.commit()
        except SQLAlchemyError:
            db.session.rollback()

    @classmethod
    def get_orders(cls, user, page=1, per_page=15):
        if user.is_authenticated():
            return user.orders.order_by(desc(cls.create_time), cls.button_id).paginate(page, per_page, True).items

    @classmethod
    def orders_count(cls):
        if current_user.is_authenticated():
            return current_user.orders.count()
Ejemplo n.º 4
0
class Customer(db.Model):
    __tablename__ = 'customers'
    id = db.Column(db.Integer, primary_key=True)
    user_id = db.Column(db.Integer, db.ForeignKey('users.id'))
    name = db.Column(db.String(30))
    addr = db.Column(db.String(120), nullable=False)
    telephone = db.Column(db.String(20))
    mobile_phone = db.Column(db.String(15))
    wechat_id = db.Column(db.String(50))

    # One-to-many
    buttons = db.relationship('Button',
                              backref='customer',
                              primaryjoin='Button.customer_id == Customer.id')

    def __init__(self, addr=None):
        self.addr = addr

    def __repr__(self):
        """Set to a unique key specific to the object in the database.
        Required for cache.memoize() to work across requests.
        """
        return "<{} {}>".format(self.__class__.__name__, self.id)

    @classmethod
    def get(cls, id):
        id = int(id)
        return cls.query.filter_by(id=id).first()

    def save(self, user=None):
        """

        :param user:
        :return:
        """
        if self.id:

            db.session.add(self)
        else:
            if user:
                self.user_id = user.id
            db.session.add(self)
        try:
            db.session.commit()
        except SQLAlchemyError:
            db.session.rollback()
        return self

    def delete(self):
        """

        :return:
        """
        if Button.query.filter_by(customer_id=self.id).first():
            raise IntegrityError

        db.session.delete(self)
        try:
            db.session.commit()
        except SQLAlchemyError as e:
            db.session.rollback()
            raise e
Ejemplo n.º 5
0
class Service(db.Model):
    __tablename__ = 'services'
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(120), nullable=False)
    wechat_template_id = db.Column(db.String(60))
    wechat_template = db.Column(db.String(500))  # 微信模板内容
    count = db.Column(db.Integer, default=1)  # 数量
    unit = db.Column(db.String(6), default=u'桶')  # 单位
    user_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False)
    price = db.Column(db.DECIMAL)
    # One-to-Many
    buttons = db.relationship('Button',
                              backref='service',
                              primaryjoin='Button.service_id == Service.id')

    # One-to-Many
    orders = db.relationship('Order',
                             backref='service',
                             primaryjoin='Order.service_id == Service.id')

    def __repr__(self):
        """Set to a unique key specific to the object in the database.
        Required for cache.memoize() to work across requests.
        """
        return "<{} {}>".format(self.__class__.__name__, self.id)

    @classmethod
    def get(cls, id):
        id = int(id)
        return cls.query.filter_by(id=id).first()

    def save(self, user=None):
        """

        :param user:
        :return:
        """
        if user:
            self.user_id = user.id
        db.session.add(self)
        try:
            db.session.commit()
        except SQLAlchemyError:
            db.session.rollback()

        return self

    def delete(self):
        """

        :return:
        """
        db.session.delete(self)
        try:
            db.session.commit()
        except SQLAlchemyError as e:
            db.session.rollback()
            raise

    def get_services(self, user):
        return user.services