from __future__ import annotations from dataclasses import KW_ONLY, field from typing import * # type: ignore import rio from .. import components as comps from .add_page import AddPage from db_classes import * import functools import asyncio class BrowsePage(rio.Component): searchtext: str = "" items: list = [] office: str = "" filters: dict = {} popup_message: str = "" popup_show: bool = False popup_color: str = 'success' elocation: str = "" elocation_code: str = "" epartnum: str = "" emac: str = "" eserial: str = "" efwver: str = "" ecode: str = "" emacvendor: str = "" emanu: str = "" emanufield: str = "" eoffice: str = "" edescription: str = "" info_show = False edit_show = False iname: str ="" imanu: str ="" iserial: str ="" imac: str ="" ifw: str ="" iloc: str ="" icheckouts: str ="" icheckout_times: str ="" ibarcode: str ="" idesc: str ="" @rio.event.on_populate async def _search(self, query=searchtext): self.office = self.session[comps.Settings].office self.filters['office'] = self.office self.items = search_item(query, self.filters) await self.force_refresh() @rio.event.periodic(1) async def set_office_init(self): if self.office != self.session[comps.Settings].office: self.office = self.session[comps.Settings].office #print(self.office) await self._search() async def _search_trigger(self, event: rio.TextInputChangeEvent): await self._search(event.text) async def copy_info(self, text: str): self.popup_message = "\n Copied! \n\n" self.popup_color = 'success' await self.session.set_clipboard(text) self.popup_show = True await asyncio.sleep(1.5) self.popup_show = False async def _open_info_popup(self, code: str, skip_display=False) -> str | None: itm: dict = find_item(code) try: self.iloc = itm["location"] except: self.iloc = "" if itm["checkout"]: checkout = itm["checkout_user"] + " - " + self.iloc else: checkout = self.iloc self.iname=str(itm["fullname"]) self.imanu=str(itm["manufacturer"]) self.iserial=str(itm["serial"]) self.imac=str(itm["mac"]) self.ifw=str(itm["fwver"]) self.iloc=str(checkout) self.icheckouts=str(itm["checkout"]) self.icheckout_times=str(itm["checkout_start"]) + " to " + str(itm["checkout_end"]) #office=str(itm["office"]) self.ibarcode=str(itm["barcode"]) self.idesc=str(itm["description"]) if not skip_display: self.edit_show = False self.info_show = True async def _close_info_popup(self): self.edit_show = False self.info_show = False await self.force_refresh() async def _close_edit_popup(self): await self._open_info_popup(self.ibarcode) await self.force_refresh() async def _save_edit_popup(self): self.edit_show = False self.info_show = True await self.force_refresh() await self._add_part_button() await asyncio.sleep(1) await self._open_info_popup(self.ibarcode, skip_display=True) await self._search() await asyncio.sleep(1) self.popup_show = False await self._open_info_popup(self.ibarcode, skip_display=True) await self._search() async def _delete_edit_popup(self): self.edit_show = False self.info_show = False # TODO: delete here await self.force_refresh() async def _open_edit_popup(self, code: str) -> str | None: itm: dict = find_item(code) try: self.elocation: str = itm["location"] self.elocation_code: str = find_item_location(code).locationid except: self.elocation: str = "" self.elocation_code: str = "" self.epartnum: str = itm["fullname"] self.emac: str = itm["mac"] self.eserial: str = itm["serial"] self.efwver: str = itm["fwver"] self.ecode: str = code self.popup_message: str = "" self.popup_show: bool = False self.popup_color: str = 'warning' self.emacvendor: str = "" self.emanu: str = itm["manufacturer"] self.emanufield: str = itm["manufacturer"] self.eoffice: str = itm["office"] self.edescription: str = itm["description"] self.info_show = False self.edit_show = True async def add_part(self): if self.ecode == "": # FAIL self.popup_message = "\n Missing barcode! \n\n" self.popup_show = True self.popup_color = 'danger' else: # OK, add part if self.elocation == "": self.elocation_code = "" if update_item(self.epartnum, self.eserial, self.office, self.ecode, locationid=self.elocation_code, description=self.edescription, manufacturer=self.emanu, mac=self.emac, fwver=self.efwver) == False: self.popup_message = "\n Unable to update! \n\n" self.popup_show = True self.popup_color = 'warning' else: self.popup_message = "\n Part updated! \n\n" self.popup_show = True self.popup_color = 'success' #self.ename: str = "" self.epartnum = "" self.emac = "" self.eserial = "" self.efwver = "" self.ecode = "" self.emacvendor = "" self.emanu = "" self.emanufield = "" self.edescription = "" self.elocation = "" self.elocation_code = "" async def _add_part_button(self): await self.add_part() def click_item_page(self, code): self.session[comps.Settings].selected_item = code self.session.attach(self.session[comps.Settings]) self.session.navigate_to("/item") async def click_item_dialog(self, code): self.session[comps.Settings].selected_item = code await self._open_info_popup(code) async def _update_elocation(self, event: rio.TextInputChangeEvent): print("Checking " + self.elocation) if get_location_id(self.elocation) != False: self.elocation_code = self.elocation print("Found location " + get_location_id(self.elocation).name) self.elocation = get_location_id(self.elocation).name def build(self) -> rio.Component: searchview: rio.ListView = rio.ListView(grow_y=True) for item in self.items: try: loc = item["location"] except: loc = "" if item["checkout"]: checkout = item["checkout_user"] + " - " + loc else: checkout = loc searchview.add(rio.SimpleListItem(text=item["fullname"],secondary_text=(item["manufacturer"] + " - Serial: " + item["serial"] + "\n" + checkout), on_press=functools.partial( self.click_item_dialog, code=item["barcode"]))) details = rio.ListView(grow_y=True, min_width=40) details.add(rio.SimpleListItem(text=self.iname, on_press=functools.partial(self.copy_info, text=self.iname))) details.add(rio.SimpleListItem(text=self.imanu, on_press=functools.partial(self.copy_info, text=self.imanu))) details.add(rio.SimpleListItem(text="Serial",secondary_text=self.iserial, on_press=functools.partial(self.copy_info, text=self.iserial))) details.add(rio.SimpleListItem(text="MAC",secondary_text=self.imac, on_press=functools.partial(self.copy_info, text=self.imac))) details.add(rio.SimpleListItem(text="FW Version",secondary_text=self.ifw, on_press=functools.partial(self.copy_info, text=self.ifw))) details.add(rio.SimpleListItem(text="Location",secondary_text=self.iloc, on_press=functools.partial(self.copy_info, text=self.iloc))) details.add(rio.SimpleListItem(text="Checked out?",secondary_text=self.icheckouts, on_press=functools.partial(self.copy_info, text=self.icheckouts))) details.add(rio.SimpleListItem(text="Checkout start/end",secondary_text=self.icheckout_times, on_press=functools.partial(self.copy_info, text=self.icheckout_times))) #details.add(rio.SimpleListItem(text="Office",secondary_text=office, on_press=functools.partial(copy_info, text=office))) details.add(rio.SimpleListItem(text="Barcode",secondary_text=self.ibarcode, on_press=functools.partial(self.copy_info, text=self.ibarcode))) details.add(rio.SimpleListItem(text="Description",secondary_text=self.idesc, on_press=functools.partial(self.copy_info, text=self.idesc))) return rio.Column( rio.Row( rio.TextInput( text=self.bind().searchtext, on_change=self._search_trigger, label="Search" ) ), searchview, rio.Popup( anchor=rio.Text( text="", style='heading1', align_x = 0.5 ), color=self.popup_color, is_open=self.popup_show, content=rio.Text( text=self.popup_message, ), ), rio.Popup( anchor=rio.Text( text="", style='heading1', align_x = 0.5 ), #color=self.popup_color, is_open=self.info_show, min_width=40, position='fullscreen', color=rio.Color.TRANSPARENT, content=rio.Card( rio.Column( details, rio.Row( rio.Button( content="Close", on_press=self._close_info_popup ), rio.Button( content="Edit", on_press=functools.partial(self._open_edit_popup, code=self.ibarcode) ), spacing=2, margin=2 ), spacing=1, margin=2, ), align_x=0.5, align_y=0.5, ) ), rio.Popup( anchor=rio.Text( text="", style='heading1', align_x = 0.5 ), #color=self.popup_color, is_open=self.edit_show, min_width=40, position='fullscreen', color=rio.Color.TRANSPARENT, content=rio.Card( rio.Column( rio.TextInput( label="Barcode", text=self.bind().ecode, is_sensitive=False ), rio.TextInput( label="Full part number", text=self.bind().epartnum, ), rio.TextInput( label="Serial", text=self.bind().eserial, ), rio.TextInput( label="MAC", text=self.bind().emac, ), rio.TextInput( label="Location", text=self.bind().elocation, on_change=self._update_elocation, ), rio.TextInput( label="Manufacturer", text=self.bind().emanu, ), rio.TextInput( label="FW Ver", text=self.bind().efwver ), rio.MultiLineTextInput( label="Description", text=self.bind().edescription ), rio.Row( rio.Button( content="Cancel", on_press=self._close_edit_popup, color='warning' ), rio.Button( content="Delete", on_press=self._delete_edit_popup, color='danger' ), rio.Button( content="Save", on_press=self._save_edit_popup ), spacing=2, margin=2 ), spacing=1, margin=2 ), align_x=0.5, align_y=0.5 ) ), spacing=2, min_width=60, align_x=0.5, align_y=0, )