59 lines
2.7 KiB
Python
59 lines
2.7 KiB
Python
from fasthtml.components import *
|
|
from pandas import DataFrame
|
|
|
|
from components.BaseComponent import BaseComponent
|
|
from utils.ComponentsInstancesHelper import ComponentsInstancesHelper
|
|
from components.admin.commands import ImportHolidaysCommandManager
|
|
from components.admin.constants import ADMIN_IMPORT_HOLIDAYS_INSTANCE_ID
|
|
from components.datagrid_new.components.DataGrid import DataGrid
|
|
from components.datagrid_new.settings import DataGridSettings
|
|
from components.hoildays.helpers.nibelisparser import NibelisParser
|
|
from components.repositories.constants import USERS_REPOSITORY_NAME, HOLIDAYS_TABLE_NAME
|
|
from components_helpers import mk_dialog_buttons, apply_boundaries
|
|
from core.instance_manager import InstanceManager
|
|
|
|
|
|
class ImportHolidays(BaseComponent):
|
|
def __init__(self, session, _id, settings_manager, boundaries=None):
|
|
super().__init__(session, _id)
|
|
self._settings_manager = settings_manager
|
|
self._boundaries = boundaries
|
|
self.commands = ImportHolidaysCommandManager(self)
|
|
self.datagrid = InstanceManager.get(session,
|
|
DataGrid.create_component_id(session),
|
|
DataGrid,
|
|
grid_settings=DataGridSettings(views_visible=False,
|
|
open_file_visible=False,
|
|
open_settings_visible=False))
|
|
|
|
def on_paste(self, content):
|
|
parser = NibelisParser(content)
|
|
holidays = parser.parse()
|
|
self.datagrid.init_from_dataframe(DataFrame(holidays))
|
|
return self.datagrid
|
|
|
|
def import_holidays(self):
|
|
repositories = ComponentsInstancesHelper.get_repositories(self._session)
|
|
key = repositories.db.ensure_exists(USERS_REPOSITORY_NAME, HOLIDAYS_TABLE_NAME)
|
|
datagrid = DataGrid(self._session, DataGrid.create_component_id(self._session), key, self._settings_manager)
|
|
datagrid.init_from_dataframe(self.datagrid.get_dataframe(), save_state=True)
|
|
return repositories.refresh()
|
|
|
|
def __ft__(self):
|
|
return Div(
|
|
Div("Import holidays...", cls="mb-2"),
|
|
Textarea(name="content",
|
|
cls="textarea",
|
|
placeholder="Paste the content.\nCtl+Enter when done.",
|
|
**self.commands.on_paste()),
|
|
Div(self.datagrid, cls="mt-2"),
|
|
mk_dialog_buttons(ok_title="Import", cls="mt-2", on_ok=self.commands.import_holidays()),
|
|
id=self._id,
|
|
cls="m-2",
|
|
**apply_boundaries(self._boundaries, other=26),
|
|
)
|
|
|
|
@staticmethod
|
|
def create_component_id(session, prefix=None, suffix=None):
|
|
return f"{ADMIN_IMPORT_HOLIDAYS_INSTANCE_ID}{session['user_id']}"
|