import flet as ft from flet.core.types import OptionalControlEventCallable import json import requests color = [ "#f0ece2", "#dfd3c3", "#c7b198", "#596e79", "#7E93A0FF" ] class DataPicer(ft.DatePicker): def __init__(self,creation_time, handle_dismissal: OptionalControlEventCallable = None,handle_change: OptionalControlEventCallable = None): import datetime super().__init__( current_date=creation_time, first_date=datetime.datetime(year=2023, month=10, day=1), last_date=datetime.datetime(year=2027, month=10, day=1), on_change=handle_change, on_dismiss=handle_dismissal, ) class JsonJober: @classmethod def read_json(cls, path_file: str = ""): """Чтение json файла""" with open(path_file, 'r', encoding='utf-8') as file: data = json.load(file) return data if isinstance(data, list) else [data] class Text(ft.Text): def __init__(self, value: str = ""): super().__init__( value=value, size=24, color=color[3] ) class Loger(ft.SnackBar): def __init__(self, text): super().__init__( content=ft.Text(value=text, color=color[3], size=24) ) class Field(ft.TextField): def __init__(self, label: str = "", password: bool = False,width:int=0): super().__init__( max_lines=1, border_color=color[3], color=color[3], text_size=24, label=label, password=password, can_reveal_password=password, label_style=ft.TextStyle( color=color[3], size=24 ), ) if width == 0: self.expand=True, else: self.expand=False self.width=width class Button(ft.ElevatedButton): def __init__(self, text: str = "", on_click: OptionalControlEventCallable = None): super().__init__( color=color[3], bgcolor=color[1], text=text, on_click=on_click, style=ft.ButtonStyle( overlay_color=color[4], text_style=ft.TextStyle( size=24 ) ) ) class TextCellFilter(ft.Container): def __init__(self,id:int=0, value: str = "", on_click: OptionalControlEventCallable = None): self.id = id super().__init__( border=ft.border.all(1, color[3]), border_radius=ft.border_radius.all(5), content=ft.Text( value=value, no_wrap=False, size=24, color=color[3], ), on_click=on_click ) class DownList(ft.Container): def __init__(self, page: ft.Page, column=[],columns={}, filter=None, load_first: bool = False): self.columns = columns if columns: column = columns["name"] self.dialog = DropdownDialog(load_first=load_first, data_set=column, on_click=self.on_click_text) self.filter = filter self.view_text = Text() self._id_ = -1 super().__init__( expand=True, width=300, border=ft.border.all(1, color[3]), border_radius=ft.border_radius.all(5), content=self.view_text, on_click=self.open_dialog ) def load_professions(self): self.filter.dialog = DropdownDialog( load_first=False, data_set=Observer.Professions, on_click=self.filter.on_click_text ) def load_positions(self): self.filter.dialog = DropdownDialog( load_first=False, data_set=Observer.Positions, on_click=self.filter.on_click_text ) def load_other_positions(self): self.filter.dialog = DropdownDialog( load_first=False, data_set=[], dismiss=self.filter.click_field ) def update_filter_list(self): if self.filter is None: return self.filter.view_text.value = "" if self.view_text.value == "Профессии рабочих": self.load_professions() elif self.view_text.value == "Должности служащих, руководителей": self.load_positions() elif self.view_text.value == "Иные должности, не указанные в ОКПДТР": self.load_other_positions() self.page.update() self.filter = self.filter def click_field(self, e): self.view_text.value = self.dialog.filter_text.value self.page.close(self.dialog) self.page.update() def on_click_text(self, e): if self.columns: self._id_ = e.control.id self.view_text.value = e.control.content.value self.page.close(self.dialog) self.update_filter_list() self.page.update() def open_dialog(self, e): self.page.open(self.dialog) def change_filter_text(self, e): self.view_text.value = e.control.content.value self.hidden_text.value = e.control.content.value self.update() class ObserverMethod: @classmethod def load_dropdown(cls, name_list: str = "", column: str = "", name_lists=[], columns=[]): from user_data import User_data as ud if ud.suggestions is None: ud.suggestions = JsonJober.read_json(path_file=ud.path_json) if columns: return cls._load_columns(suggestions=ud.suggestions[0], columns=columns, name_list=name_list) if name_list: return cls._load_name_list(ud.suggestions[0], name_list, column) return cls._load_name_lists(ud.suggestions[0], name_lists) @staticmethod def _load_columns(suggestions, columns, name_list=""): lists = {} if name_list in suggestions: for col in columns: lists[col] = [item for item in suggestions[name_list][col]] return lists @staticmethod def _load_name_list(suggestions, name_list, column): temp_data_list = [] if column: if column in suggestions[name_list]: temp_data_list = [item for item in suggestions[name_list][column]] else: temp_data_list = [item for item in suggestions[name_list]] return temp_data_list @staticmethod def _load_name_lists(suggestions, name_lists): temp_data_list = [] for col in name_lists: if col in suggestions: temp_data_list.extend([item for item in suggestions[col]]) return temp_data_list class Observer: inn_manufacturing_enterprises = ObserverMethod.load_dropdown(name_list="Manufacturing_enterprises", columns=["name", "inn"]) Manufacturing_enterprises = ObserverMethod.load_dropdown(name_list="Manufacturing_enterprises", column="name") Classification_professions = ObserverMethod.load_dropdown(name_list="Classification_professions") Professions = ObserverMethod.load_dropdown(name_list="Professions") Positions = ObserverMethod.load_dropdown(name_list="Positions") Specialization = ObserverMethod.load_dropdown(name_list="Specialization") class DropdownDialog(ft.AlertDialog): def __init__(self, data_set, dismiss: OptionalControlEventCallable = None, on_click: OptionalControlEventCallable = None, load_first: bool = False): self.data_set = data_set self.load_first = load_first self.filter_text = ft.TextField(on_change=self.filter_data) self.result = ft.Column(height=230, width=400, controls=[], scroll=True) self.on_click = on_click self.init_dialog() super().__init__( content=ft.Column( height=300, width=500, controls=[ ft.Row(controls=[self.filter_text]), self.result, ft.Button(text="Закрыть", on_click=dismiss), ] ), on_dismiss=dismiss ) def init_dialog(self): temp = [] if not self.load_first: for itemx, item in enumerate(self.data_set): if itemx >= 100: self.result.controls = temp return temp.append(TextCellFilter(value=item, on_click=self.on_click,id=itemx)) else: for iitem, item in enumerate(self.data_set): temp.append(TextCellFilter(value=item, on_click=self.on_click,id=iitem)) self.result.controls = temp def filter_data(self, e): if len(e.control.value) < 3: return filter_text = e.control.value.lower() filtered_data = [] for iitem,item in enumerate(self.data_set): if filter_text in item.lower(): filtered_data.append(TextCellFilter(value=item, on_click=self.on_click,id=iitem)) self.result.controls = filtered_data self.result.update() class DataGrid(ft.DataTable): def __init__(self, page: ft.Page): self.page = page self.column_name = None self.load_column_names() super().__init__(columns=self.add_list_column_names()) def return_data(self): return self.rows def load_column_names(self): if not self.column_name: url = "http://localhost:8001/api-data-base/get_name_column?table_name=%D0%A1ollection_form_opk" try: response = requests.post(url) if response.status_code == 200: self.column_name = response.json() else: print(f"Error: Received status code {response.status_code}") except requests.exceptions.RequestException as e: print(f"Error: {e}") def add_list_column_names(self): temp_data = [] if self.column_name: for col in self.column_name: if col not in ["id", "ПОО", "Субъект_рф", "ИНН_ОПК", "время_создания", "указаное_время"]: temp_data.append(ft.DataColumn(label=Text(value=col))) return temp_data def load_new_row(self): temp_data = [] temp = DownList(page=self.page, column=Observer.Professions) for col in self.column_name: if col in ["id", "ПОО", "Субъект_рф", "ИНН_ОПК", "время_создания", "указаное_время"]: continue if col == "ОПК": temp_data.append(ft.DataCell(content=DownList(load_first=True, page=self.page, columns=Observer.inn_manufacturing_enterprises))) elif col == "Группа_профессий_должностей": temp_data.append(ft.DataCell(content=DownList(filter=temp, load_first=True, page=self.page, column=Observer.Classification_professions))) elif col == "Наименование_профессии": temp_data.append(ft.DataCell(content=temp)) elif col == "Профессии_специальности_СПО": temp_data.append(ft.DataCell(content=DownList(page=self.page, column=Observer.Specialization))) else: temp_data.append(ft.DataCell(content=Field())) return temp_data def add_new_row(self, e): self.rows.append(ft.DataRow(cells=self.load_new_row())) self.update()