class Office(db.Model, Mixin): __tablename__ = "offices" id = db.Column(db.Integer, primary_key=True) name = db.Column(db.Integer, unique=True) timestamp = db.Column(db.DateTime(), index=True, default=datetime.utcnow) prefix = db.Column(db.String(2)) operators = db.relationship('Operators', backref='operators') tasks = db.relationship('Task', secondary=mtasks, lazy='subquery', backref=db.backref('offices', lazy=True)) def __init__(self, name, prefix): self.name = name self.prefix = prefix
class Office(db.Model, Mixin): __tablename__ = "offices" id = db.Column(db.Integer, primary_key=True) name = db.Column(db.Integer, unique=True) timestamp = db.Column(db.DateTime(), index=True, default=datetime.utcnow) prefix = db.Column(db.String(2)) operators = db.relationship('Operators', backref='operators') tasks = db.relationship('Task', secondary=mtasks, lazy='subquery', backref=db.backref('offices', lazy=True)) def __init__(self, name, prefix): self.name = name self.prefix = prefix @classmethod def get_first_available_prefix(cls): letters = list(map(lambda i: chr(i), range(97, 123))) prefix = None while letters and not prefix: match_letter = letters.pop() if not cls.query.filter_by(prefix=match_letter).first(): prefix = match_letter return prefix and prefix.upper() @property def tickets(self): return Serial.query.filter(Serial.office_id == self.id, Serial.number != 100) @property def display_text(self): show_prefix = Display_store.query.first()\ .prefix return f'{self.prefix if show_prefix else ""}{self.name}'
class Office(db.Model, Mixin): __tablename__ = "offices" id = db.Column(db.Integer, primary_key=True) name = db.Column(db.Integer, unique=True) timestamp = db.Column(db.DateTime(), index=True, default=datetime.utcnow) prefix = db.Column(db.String(2)) operators = db.relationship('Operators', backref='operators') tasks = db.relationship('Task', secondary=mtasks, lazy='subquery', backref=db.backref('offices', lazy=True)) def __init__(self, name=None, prefix=None): self.name = name or self.get_generic_available_name() self.prefix = prefix or self.get_first_available_prefix() @classmethod def get_first_available_prefix(cls): letters = list(reversed(list(map(lambda i: chr(i), range(97, 123))))) prefix = None while letters and not prefix: match_letter = letters.pop() if not cls.query.filter_by(prefix=match_letter).first(): prefix = match_letter return prefix and prefix.upper() @classmethod def get_generic_available_name(cls): name = 0 while not name: temp_name = randint(1, 1000) office = cls.query.filter_by(name=temp_name).first() if not office: name = temp_name return name @property def tickets(self): return Serial.query.filter(Serial.office_id == self.id, Serial.number != 100) @property def display_text(self): show_prefix = Display_store.query.first()\ .prefix return f'{self.prefix if show_prefix else ""}{self.name}' def delete_all(self): for ticket in self.tickets: db.session.delete(ticket) for task in self.tasks: if task.common: self.tasks.remove(task) else: db.session.delete(task) db.session.delete(self) db.session.commit()
class Office(db.Model, Mixin): __tablename__ = "offices" id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(300), unique=True) timestamp = db.Column(db.DateTime(), index=True, default=datetime.utcnow) prefix = db.Column(db.String(2)) operators = db.relationship('Operators', backref='operators') tasks = db.relationship('Task', secondary=mtasks, lazy='subquery', backref=db.backref('offices', lazy=True)) def __init__(self, name=None, prefix=None): self.name = name or self.get_generic_available_name() self.prefix = prefix or self.get_first_available_prefix() @classmethod def get_all_used_prefixes(cls): return [o.prefix for o in cls.query.all()] @classmethod def get_all_available_prefixes(cls): used_prefixes = cls.get_all_used_prefixes() return [p for p in PREFIXES if p not in used_prefixes] @classmethod def get_first_available_prefix(cls): available_prefixes = cls.get_all_available_prefixes() if not available_prefixes: raise AssertionError('No prefixes left!') return available_prefixes[0] @classmethod def get_generic_available_name(cls): name = 0 while not name: temp_name = str(randint(1, 1000)) office = cls.query.filter_by(name=temp_name).first() if not office: name = temp_name return name @property def tickets(self): return Serial.query.filter(Serial.office_id == self.id, Serial.number != 100) @property def display_text(self): show_prefix = Display_store.query.first()\ .prefix return f'{self.prefix if show_prefix else ""}{self.name}' def delete_all(self): for ticket in self.tickets: db.session.delete(ticket) for task in self.tasks: if task.common: self.tasks.remove(task) else: db.session.delete(task) db.session.delete(self) db.session.commit() def is_valid_new_name(self, name): return not self.query.filter(Office.name == name, Office.id != self.id).first()