# -*- coding: utf-8 -*- import logging from copy import copy from tools import trace, timer from Model.DB import SQLSubModel from Model.Friction.Friction import Friction logger = logging.getLogger() class FrictionList(SQLSubModel): _sub_classes = [ Friction ] def __init__(self, status = None): super(FrictionList, self).__init__() self._status = status self._frictions = [] @classmethod def _sql_create(cls, execute): return cls._create_submodel(execute) @classmethod def _sql_update_0_0_1(cls, execute, version): execute("ALTER TABLE `section` RENAME TO `friction`") @classmethod def _sql_update(cls, execute, version): if version == "0.0.0": logger.info(f"Update friction TABLE from {version}") cls._sql_update_0_0_1(execute, version) return True @classmethod def _sql_load(cls, execute, data = None): new = cls(status = data['status']) new._frictions = Friction._sql_load( execute, data ) return new def _sql_save(self, execute, data = None): reach = data["reach"] execute(f"DELETE FROM friction WHERE reach = {reach.id}") ok = True ind = 0 for friction in self._frictions: data["ind"] = ind ok &= friction._sql_save(execute, data = data) ind += 1 return ok def __len__(self): return len(self._frictions) @property def frictions(self): return self._frictions.copy() def get(self, row): return self._frictions[row] def set(self, row, new): self._frictions[row] = new self._status.modified() def new(self, index): n = Friction(status = self._status) self._frictions.insert(index, n) self._status.modified() return n def insert(self, index, new): self._frictions.insert(index, new) self._status.modified() def delete(self, frictions): for friction in frictions: self._frictions.remove(friction) self._status.modified() def delete_i(self, indexes): frictions = list( map( lambda x: x[1], filter( lambda x: x[0] in indexes, enumerate(self._frictions) ) ) ) self.delete(frictions) def sort(self, reverse=False, key=None): self._frictions.sort(reverse=reverse, key=key) self._status.modified() def move_up(self, index): if index < len(self._frictions): next = index - 1 l = self._frictions l[index], l[next] = l[next], l[index] self._status.modified() def move_down(self, index): if index >= 0: prev = index + 1 l = self._frictions l[index], l[prev] = l[prev], l[index] self._status.modified()