""" Project: PARTS Website Author: Edward Middleton-Smith Precision And Research Technology Systems Limited Technology: View Models Feature: Dog Dog Command Link View Model Description: Data model for dog dog command links view """ # internal from business_objects.dog.command import Command, Parameters_Command from business_objects.dog.dog import Dog, Parameters_Dog from business_objects.dog.dog_command_link import Dog_Command_Link, Parameters_Dog_Command_Link from business_objects.dog.obedience_level import Obedience_Level from datastores.datastore_dog import DataStore_Dog from models.model_view_dog_base import Model_View_Dog_Base from forms.dog.dog_command_link import Filters_Dog_Command_Link # from routes import bp_home from helpers.helper_app import Helper_App import lib.argument_validation as av # external from pydantic import BaseModel from typing import ClassVar from operator import attrgetter class Model_View_Dog_Dog_Command_Link(Model_View_Dog_Base): FLAG_HAND_SIGNAL_DESCRIPTION: ClassVar[str] = Dog_Command_Link.FLAG_HAND_SIGNAL_DESCRIPTION filter_dogs: list = None filter_command_categories: list = None filter_commands: list = None dog_command_links: list = None form_filters: Filters_Dog_Command_Link = None form_filters_old: Filters_Dog_Command_Link def __init__(self, form_filters_old, hash_page_current=Model_View_Dog_Base.HASH_PAGE_DOG_DOG_COMMAND_LINKS): _m = 'Model_View_Dog_Dog_Command_Link.__init__' Helper_App.console_log(f'{_m}\nstarting...') super().__init__(hash_page_current=hash_page_current, form_filters_old=form_filters_old) self._title = 'Dog Command Link' self.form_filters = form_filters_old datastore = DataStore_Dog() user_session = datastore.get_user_session() parameters_filter_dog = Parameters_Dog.get_default(user_session.id_user) self.filter_dogs, errors = datastore.get_many_dog(parameters_filter_dog) if len(self.filter_dogs) > 0: self.form_filters.id_dog.choices += [(str(dog.id_dog), dog.name) for dog in self.filter_dogs] parameters_filter_command = Parameters_Command.get_default(user_session.id_user) self.filter_command_categories, self.filter_commands, errors = datastore.get_many_command(parameters_filter_command) if len(self.filter_command_categories) > 0: self.form_filters.id_command_category.choices += [(str(command_category.id_command_category), command_category.name) for command_category in self.filter_command_categories] if len(self.filter_commands) > 0: Helper_App.console_log(f'filter commands: {self.filter_commands}') sorted_filter_commands = self.filter_commands sorted_filter_commands.sort(key = attrgetter('name')) Helper_App.console_log(f'sorted filter commands: {sorted_filter_commands}') self.form_filters.id_command.choices += [(str(command.id_command), command.name) for command in sorted_filter_commands] # .sort(key = lambda command: command[1]) Helper_App.console_log(f'Form filters: {self.form_filters}') parameters_filter_dog_command_link = Parameters_Dog_Command_Link.from_form_filters_dog_command_link(self.form_filters, user_session.id_user) Helper_App.console_log(f'Query args: {parameters_filter_dog_command_link}') self.dog_command_links, errors = datastore.get_many_dog_command_link(parameters_filter_dog_command_link)