from __future__ import annotations from dataclasses import KW_ONLY, field from typing import * # type: ignore import rio import datetime from mac_vendor_lookup import AsyncMacLookup from db_classes import * from .. import components as comps import asyncio class AddLocationPage(rio.Component): code: str = "" popup_message: str = "" popup_show: bool = False popup_color: str = 'warning' description: str = "" name: str = "" parent_code: str = "" parent: str = "" @rio.event.periodic(1) def set_office_init(self): self.office = self.session[comps.Settings].office #print("Populated:", self.office) async def _update_location(self, event: rio.TextInputChangeEvent): print("Checking " + self.parent) if get_location_id(self.parent) != False: self.parent_code = self.parent print("Found location " + get_location_id(self.parent).name) self.parent = get_location_id(self.parent).name async def add_part(self): if self.code == "": # FAIL self.popup_message = "\n Missing barcode! \n\n" self.popup_show = True self.popup_color = 'danger' await asyncio.sleep(1) self.popup_show = False else: # OK, add part if get_location_id(self.parent_code) != False: self.parent = get_location_id(self.parent_code) else: self.parent = None if create_location(name=self.name, barcode=self.code, parent=self.parent, description=self.description) == False: self.popup_message = "\n Duplicate barcode! \n\n" self.popup_show = True self.popup_color = 'warning' await asyncio.sleep(2) self.popup_show = False else: self.popup_message = "\n Part added! \n\n" self.popup_show = True self.popup_color = 'success' self.name: str = "" self.code: str = "" self.description: str = "" self.parent_code: str = "" self.parent: str = "" await asyncio.sleep(2) self.popup_show = False async def _add_part_enter(self, event: rio.TextInputConfirmEvent): await self.add_part() async def _add_part_button(self): await self.add_part() def build(self) -> rio.Component: return rio.Column( rio.Popup( anchor=rio.Text( text="Add a part below:", style='heading1', align_x = 0.5 ), color=self.bind().popup_color, is_open=self.bind().popup_show, content=rio.Text( text=self.bind().popup_message, ), ), rio.TextInput( label="Barcode", text=self.bind().code ), rio.TextInput( label="Parent Location (optional)", text=self.bind().parent, on_change=self._update_location ), rio.TextInput( label="Location Name", text=self.bind().name ), rio.MultiLineTextInput( label="Description (optional)", text=self.bind().description ), rio.Button( content="Add", on_press=self._add_part_button ), spacing=2, min_width=60, margin_bottom=4, align_x=0.5, align_y=0, )