|
| 1 | +from app import db, login_manager |
| 2 | +from flask_login import UserMixin |
| 3 | +from werkzeug.security import generate_password_hash, check_password_hash |
| 4 | + |
| 5 | +"""Association table, storing which users are working on which groups of products""" |
| 6 | +association_table = db.Table('users_batches', |
| 7 | + db.Column('user_id', db.Integer, db.ForeignKey('user.id')), |
| 8 | + db.Column('batch_id', db.Integer, db.ForeignKey('batch.id'))) |
| 9 | + |
| 10 | + |
| 11 | +class User(db.Model, UserMixin): |
| 12 | + id = db.Column(db.Integer, primary_key=True) |
| 13 | + username = db.Column(db.String(64), index=True, unique=True) |
| 14 | + email = db.Column(db.String(120), index=True, unique=True) |
| 15 | + password_hash = db.Column(db.String(128)) |
| 16 | + admin = db.Column(db.Boolean) |
| 17 | + |
| 18 | + recorded_data = db.relation('ProdData') |
| 19 | + |
| 20 | + def set_password(self, password): |
| 21 | + self.password_hash = generate_password_hash(password) |
| 22 | + |
| 23 | + def check_password(self, password): |
| 24 | + return check_password_hash(self.password_hash, password) |
| 25 | + |
| 26 | + def __repr__(self): |
| 27 | + return '<User {}>'.format(self.username) |
| 28 | + |
| 29 | + |
| 30 | +class PartType(db.Model): |
| 31 | + """Used to store different types of products""" |
| 32 | + id = db.Column(db.Integer, primary_key=True) |
| 33 | + part_name = db.Column(db.String) |
| 34 | + |
| 35 | + steps = db.relationship('Step') |
| 36 | + |
| 37 | + def clone_part_type(self): |
| 38 | + # Get next ID |
| 39 | + new_part_name = self.part_name + "(copy)" |
| 40 | + new_part_type = PartType(part_name=new_part_name) |
| 41 | + db.session.add(new_part_type) |
| 42 | + db.session.commit() |
| 43 | + for step in self.steps: |
| 44 | + step.clone_step(part_type_id=new_part_type.id) |
| 45 | + return new_part_type |
| 46 | + |
| 47 | + |
| 48 | +class Batch(db.Model): |
| 49 | + """Used to store batches of parts""" |
| 50 | + # Set sqlite autoincrement to true for sqlite to stop it reusing IDs after deletion |
| 51 | + __table_args__ = {'sqlite_autoincrement': True} |
| 52 | + id = db.Column(db.Integer, primary_key=True) |
| 53 | + batch_number = db.Column(db.String, nullable=False) |
| 54 | + part_type_id = db.Column(db.Integer, db.ForeignKey('part_type.id'), nullable=False) |
| 55 | + completed = db.Column(db.Boolean, default=False) |
| 56 | + created_timestamp = db.Column(db.Integer) |
| 57 | + last_edit_timestamp = db.Column(db.Integer) |
| 58 | + completed_timestamp = db.Column(db.Integer) |
| 59 | + |
| 60 | + users = db.relationship('User', secondary=association_table, lazy='dynamic', backref=db.backref('batches')) |
| 61 | + parts = db.relationship('Part') |
| 62 | + csvs = db.relationship('Csv') |
| 63 | + part_type = db.relationship('PartType', backref=db.backref('batches')) |
| 64 | + |
| 65 | + |
| 66 | +class ProdData(db.Model): |
| 67 | + """ Holds the data recorded for each part/step during production""" |
| 68 | + id = db.Column(db.Integer, primary_key=True, autoincrement=True) |
| 69 | + part_id = db.Column(db.Integer, db.ForeignKey('part.id')) |
| 70 | + field_id = db.Column(db.Integer, db.ForeignKey('field.id')) |
| 71 | + user_id = db.Column(db.Integer, db.ForeignKey('user.id')) |
| 72 | + |
| 73 | + field_data = db.Column(db.String) |
| 74 | + |
| 75 | + |
| 76 | +class Part(db.Model): |
| 77 | + """Used to store basic information about a single product""" |
| 78 | + id = db.Column(db.Integer, primary_key=True) |
| 79 | + relative_id = db.Column(db.Integer) # The id to quickly identify the part within a batch, usually starting at 1 |
| 80 | + batch_id = db.Column(db.Integer, db.ForeignKey('batch.id')) |
| 81 | + last_edit_timestamp = db.Column(db.Integer) |
| 82 | + |
| 83 | + data = db.relationship('ProdData') |
| 84 | + |
| 85 | + |
| 86 | +class Field(db.Model): |
| 87 | + """ Specifies which fields of data should appear for each step""" |
| 88 | + id = db.Column(db.Integer, primary_key=True, autoincrement=True) |
| 89 | + step_id = db.Column(db.Integer, db.ForeignKey('step.id')) |
| 90 | + field_name = db.Column(db.String) |
| 91 | + batch_wide = db.Column(db.Boolean) # True if the data field counts for all the parts in the batch |
| 92 | + |
| 93 | + data = db.relationship('ProdData') |
| 94 | + |
| 95 | + def clone_field(self, new_step_id): |
| 96 | + new_field = Field(step_id=new_step_id, |
| 97 | + field_name=self.field_name, |
| 98 | + batch_wide=self.batch_wide) |
| 99 | + db.session.add(new_field) |
| 100 | + db.session.commit() |
| 101 | + |
| 102 | + |
| 103 | +class Csv(db.Model): |
| 104 | + """Data saved in csv format""" |
| 105 | + id = db.Column(db.Integer, primary_key=True, autoincrement=True) |
| 106 | + batch_id = db.Column(db.Integer, db.ForeignKey('batch.id')) |
| 107 | + step_id = db.Column(db.Integer, db.ForeignKey('step.id')) |
| 108 | + body = db.Column(db.String) |
| 109 | + uploaded_timestamp = db.Column(db.Integer) |
| 110 | + |
| 111 | + |
| 112 | +class Step(db.Model): |
| 113 | + """Holds the information for each step for each type of part""" |
| 114 | + id = db.Column(db.Integer, primary_key=True, autoincrement=True) |
| 115 | + part_type_id = db.Column(db.Integer, db.ForeignKey('part_type.id')) |
| 116 | + step_number = db.Column(db.Integer) |
| 117 | + step_title = db.Column(db.String) |
| 118 | + step_instructions = db.Column(db.String) |
| 119 | + step_image = db.Column(db.String) |
| 120 | + csv_upload = db.Column(db.Boolean) |
| 121 | + csv_id = db.Column(db.Integer, db.ForeignKey('csv.id')) |
| 122 | + |
| 123 | + fields = db.relationship('Field') |
| 124 | + |
| 125 | + def clone_step(self, part_type_id): |
| 126 | + new_step = Step(part_type_id=part_type_id, |
| 127 | + step_number=self.step_number, |
| 128 | + step_title=self.step_title, |
| 129 | + step_instructions=self.step_instructions, |
| 130 | + step_image=self.step_image) |
| 131 | + db.session.add(new_step) |
| 132 | + db.session.commit() |
| 133 | + for field in self.fields: |
| 134 | + field.clone_field(new_step.id) |
| 135 | + |
| 136 | + |
| 137 | +@login_manager.user_loader |
| 138 | +def load_user(id): |
| 139 | + return User.query.get(int(id)) |
| 140 | + |
| 141 | + |
| 142 | +def copy_row(model, row, ignored_columns=[]): |
| 143 | + copy = model() |
| 144 | + |
| 145 | + for col in row.__table__.columns: |
| 146 | + if col.name not in ignored_columns: |
| 147 | + try: |
| 148 | + copy.__setattr__(col.name, getattr(row, col.name)) |
| 149 | + except Exception as e: |
| 150 | + print(e) |
| 151 | + continue |
| 152 | + return copy |
0 commit comments