def add_equipment( category_id: int, name: str, owner: int = 1, description: str = "", qr_code_version: int = 2, ): control = "".join([choice(ascii_letters) for _ in range(6)]) try: eq = Equipment.create( name=name, holder=User.get(id=1), owner=User.get(id=owner), description=description, category=Category.get(id=category_id), control=control, ) except User.DoesNotExist: raise UserDoesNotExist(f'User with id {owner} does not exist') except Category.DoesNotExist: raise CategoryDoesNotExist( f'Category with id {category_id} does not exist') filename = f'{eq.id}_qr.png' new_qr_code(data_=f'{eq.id} {control}', ver=qr_code_version, size=qr_code_version, filename=filename) return filename
def change_equipment_category(id: int, new_category: int) -> bool: try: eq = Equipment.get(id=id) except Equipment.DoesNotExist: raise EquipmentDoesNotExist(f'Equipment with id {id} does not exist') eq.category = Category.get(id=new_category) eq.save() return True
def change_equipment_name(id: int, new_name: str) -> bool: try: eq = Equipment.get(id=id) except Equipment.DoesNotExist: raise EquipmentDoesNotExist(f'Equipment with id {id} does not exist') eq.name = new_name eq.save() return True
def change_equipment_description(id: int, new_description: str) -> bool: try: eq = Equipment.get(id=id) except Equipment.DoesNotExist: raise EquipmentDoesNotExist(f'Equipment with id {id} does not exist') eq.description = new_description eq.save() return True
def is_transfered(eq_row: dict, user_row: dict, date: datetime) -> list: """ Get history of specific equipment and with specific source after specific date, sorted by date descending """ return (row.get_as_dict() for row in History.select().where( (History.date > date) & (History.equipment == Equipment.get(id=eq_row['id'])) & (History.source == User.get(id=user_row['id']))))
def get_equipment_history(equipment_id: int, count: int = 20) -> list: try: eq = Equipment.get(id=equipment_id) return [ row.get_as_dict() for row in History.select().where(History.equipment == eq). order_by(History.id.desc()).limit(count) ] except Equipment.DoesNotExist: raise exceptions.EquipmentDoesNotExist( f'Equipment with id {equipment_id} does not exist')
def verify_transfer(id: int) -> bool: try: t = Transfer.get(id=id) except Transfer.DoesNotExist: raise TransferDoesNotExist(f'Transfer with id {id} does not exist') history.add_row(equipment_id=t.equipment.id, source_id=t.source.id, destination_id=t.destination.id) eq = Equipment.get(id=t.equipment.id) eq.holder = t.destination eq.save() t.delete_instance() return True
def get_equipment_history_by_date(equipment_id: int, start_day: int, start_month: int, start_year: int, end_day: int, end_month: int, end_year: int) -> list: try: eq = Equipment.get(id=equipment_id) return [ row.get_as_dict() for row in History.select().where((History.equipment == eq) & ( (date(day=start_day, month=start_month, year=start_year) <= History.date) & (History.date <= date( day=end_day, month=end_month, year=end_year)))) ] except Equipment.DoesNotExist: raise exceptions.EquipmentDoesNotExist( f'Equipment with id {equipment_id} does not exist')
def get_transfer_by_equipment_id(id: int) -> dict: try: eq = Equipment.get(id=id) except Equipment.DoesNotExist: raise EquipmentDoesNotExist(f'Equipment with id {id} does not exist') try: transfer = Transfer.get(equipment=eq) except Transfer.DoesNotExist: raise TransferDoesNotExist( f'Transfer with equipment with id {id} does not exist') try: transfer = Transfer.get(equipment=eq) except Transfer.DoesNotExist: raise TransferDoesNotExist( f'Transfer with equipment with id {id} does not exist') return transfer.get_as_dict()
def create_transfer(equipment_id: int, source_id: int, destination_id: int): try: source = User.get(id=source_id) except User.DoesNotExist: raise UserDoesNotExist(f'User with id {source_id} does not exist') try: destination = User.get(id=destination_id) except User.DoesNotExist: raise UserDoesNotExist(f'User with id {destination_id} does not exist') try: Transfer.create(equipment=Equipment.get(id=equipment_id), source=source, destination=destination) except Equipment.DoesNotExist: raise EquipmentDoesNotExist( f'Equipment with id {equipment_id} does not exist')
def validate_control_sum(id: int, sum: str): try: return sum == Equipment.get(id=id).control except Equipment.DoesNotExist: raise EquipmentDoesNotExist(f'Equipment with id {id} does not exist')
def delete_equipment(id: int): try: Equipment.delete().where(Equipment.id == id).execute() remove(f'./images/qr_codes/{id}_qr.png') except Equipment.DoesNotExist: raise EquipmentDoesNotExist(f'Equipment with id {id} does not exist')
def get_owner(id: int) -> dict: try: return Equipment.get(id=id).owner.get_as_dict() except Equipment.DoesNotExist: raise EquipmentDoesNotExist(f'Equipment with id {id} does not exist')
def get_equipment_by_name(name: str) -> dict: try: return Equipment.get(name=name).get_as_dict() except Equipment.DoesNotExist: raise EquipmentDoesNotExist( f'Equipment with name {name} does not exist')
def add_row(equipment_id: int, source_id: int, destination_id: int): row = History.create(source=User.get(id=source_id), destination=User.get(id=destination_id), equipment=Equipment.get(equipment_id), date=datetime.now())