class Ticket(db.Model): __tablename__ = "ticket" id = db.Column(db.Integer, primary_key=True, autoincrement=True) registered_on = db.Column(db.TIMESTAMP, nullable=False, default=datetime.datetime.utcnow) changed_on = db.Column(db.TIMESTAMP, nullable=False, default=datetime.datetime.utcnow) text = db.Column(db.String(200)) theme = db.Column(db.String(50)) email = db.Column(db.String(255), nullable=False) status_id = db.Column(db.Integer, db.ForeignKey(TicketStatus.id)) status = db.relationship('TicketStatus', foreign_keys='Ticket.status_id') comments = db.relationship('Comment', back_populates="ticket", lazy=True) def __init__(self, theme, email, text=''): self.email = email self.text = text self.theme = theme self.status_id = 1 def __repr__(self): return str({"id": str(self.id), "text": self.text, "theme": self.theme}) def json(self): """ JSONify object :return: """ return json.dumps( { "id": self.id, "registered_on": str(self.registered_on), "changed_on": str(self.changed_on), "text": self.text, "theme": self.theme, "email": self.email, "status": TicketStatus.query.filter_by(id=self.status_id).first().json(), "comments": [json.loads(c.json()) for c in self.comments] }) def change_status_check(self, new_status_id): """ Тикет создается в статусе “открыт”, может перейти в “отвечен” или “закрыт”, из отвечен в “ожидает ответа” или TODO(? - как из "отвечен" в "ожидает ответа"). На что может поменяться “ожидает ответа” “закрыт”, статус “закрыт” финальный (нельзя изменить статус или добавить комментарий) :param new_status_id: :return: """ new_status = TicketStatus.query.filter_by(id=new_status_id).first() if self.status.name == "opened" and new_status.name in ["answered", "closed"]: return True elif self.status.name == "answered" and new_status.name in ["waiting", "closed"]: return True return False
class RawMaterialStock(db.Model): id = db.Column(db.Integer, primary_key=True) location_id = db.Column(db.Integer, db.ForeignKey(Location.id)) raw_material_id = db.Column(db.Integer, db.ForeignKey(RawMaterial.id)) available_stock = db.Column(db.Integer, db.CheckConstraint("available_stock>=0"), nullable=False) location = db.relationship(Location, foreign_keys=[location_id]) raw_material = db.relationship(RawMaterial, foreign_keys=[raw_material_id]) time_created = db.Column(db.TIMESTAMP, server_default=db.func.now()) time_updated = db.Column(db.TIMESTAMP, onupdate=db.func.now(), server_default=db.func.now()) db.UniqueConstraint( "location_id", "raw_material_id", name="raw_material_stock_location_id_raw_material_id_uindex", )
class ProductManufacturing(db.Model): id = db.Column(db.Integer, primary_key=True) to_location_id = db.Column(db.Integer(), db.ForeignKey(Location.id), nullable=False) product_id = db.Column(db.Integer(), db.ForeignKey(Product.id), nullable=False) description = db.Column(db.TEXT) to_location = db.relationship(Location, foreign_keys=[to_location_id]) product = db.relationship(Product, foreign_keys=[product_id]) batch_size = db.Column(db.Integer(), db.CheckConstraint("batch_size >= 0"), nullable=False) time_created = db.Column(db.TIMESTAMP, server_default=db.func.now()) time_updated = db.Column(db.TIMESTAMP, onupdate=db.func.now(), server_default=db.func.now()) def __str__(self): return "{}".format(self.id)
class RawMaterialMovement(db.Model): id = db.Column(db.Integer, primary_key=True) movement_date = db.Column(db.Date, server_default=db.func.now()) from_location_id = db.Column(db.Integer(), db.ForeignKey(Location.id)) to_location_id = db.Column(db.Integer(), db.ForeignKey(Location.id)) raw_material_id = db.Column(db.Integer(), db.ForeignKey(RawMaterial.id), nullable=False) description = db.Column(db.TEXT) from_location = db.relationship(Location, foreign_keys=[from_location_id]) to_location = db.relationship(Location, foreign_keys=[to_location_id]) raw_material = db.relationship(RawMaterial, foreign_keys=[raw_material_id]) qty = db.Column(db.Integer(), db.CheckConstraint("qty >= 0"), nullable=False) time_created = db.Column(db.TIMESTAMP, server_default=db.func.now()) time_updated = db.Column(db.TIMESTAMP, onupdate=db.func.now(), server_default=db.func.now()) def __str__(self): return "{}".format(self.id)
class ProductRawMaterial(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(100), unique=True, nullable=False) raw_material_id = db.Column(db.Integer(), db.ForeignKey(RawMaterial.id), nullable=False) raw_material = db.relationship(RawMaterial, foreign_keys=[raw_material_id]) product_id = db.Column(db.Integer(), db.ForeignKey(Product.id), nullable=False) product = db.relationship(Product, foreign_keys=[product_id]) raw_material_quantity = db.Column(db.Integer(), nullable=False) description = db.Column(db.TEXT) time_created = db.Column(db.TIMESTAMP, server_default=db.func.now()) time_updated = db.Column(db.TIMESTAMP, onupdate=db.func.now(), server_default=db.func.now()) def __str__(self): return "{}".format(self.name) def __repr__(self): return "{}: {}".format(self.id, self.__str__())
class TicketStatus(db.Model): __tablename__ = "ticket_status" id = db.Column(db.Integer, primary_key=True, autoincrement=True) name = db.Column(db.Enum(TicketStatusEnum)) ticket = db.relationship('Ticket') def __repr__(self): return str({"id": str(self.id), "name": self.name}) def json(self): """ JSONify object :return: """ return {"id": self.id, "name": TicketStatusEnum(self.id).name}
class Comment(db.Model): __tablename__ = "comment" id = db.Column(db.Integer, primary_key=True, autoincrement=True) registered_on = db.Column(db.TIMESTAMP, nullable=False, default=datetime.datetime.utcnow) text = db.Column(db.String(200)) email = db.Column(db.String(255), nullable=False) ticket_id = db.Column(db.Integer, db.ForeignKey(Ticket.id)) ticket = db.relationship('Ticket', foreign_keys='Comment.ticket_id') def json(self): """ JSONify object :return: """ return json.dumps({ "id": self.id, "registered_on": str(self.registered_on), "text": self.text, "email": self.email })