# Pollutants.py -- Pamhyr # Copyright (C) 2023-2024 INRAE # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see <https://www.gnu.org/licenses/>. # -*- coding: utf-8 -*- import logging from tools import ( trace, timer, old_pamhyr_date_to_timestamp, date_iso_to_timestamp, date_dmy_to_timestamp, ) from Model.Tools.PamhyrDB import SQLSubModel from Model.Except import NotImplementedMethodeError logger = logging.getLogger() class Pollutants(SQLSubModel): _sub_classes = [] _id_cnt = 0 def __init__(self, id: int = -1, name: str = "", status=None): super(Pollutants, self).__init__() self._status = status if id == -1: self.id = Pollutants._id_cnt else: self.id = id self._name = str(name) self._enabled = True Pollutants._id_cnt = max( Pollutants._id_cnt + 1, self.id) @property def name(self): return self._name @name.setter def name(self, name): self._name = name self._status.modified() @classmethod def _db_create(cls, execute): execute(""" CREATE TABLE Pollutants( id INTEGER NOT NULL PRIMARY KEY, name TEXT NOT NULL UNIQUE ) """) return cls._create_submodel(execute) @classmethod def _db_update(cls, execute, version): return True @classmethod def _db_load(cls, execute, data=None): new = [] status = data["status"] table = execute( "SELECT id, name " + f"FROM Pollutants" ) if table is not None: for row in table: id = row[0] name = row[1] new_pollutant = cls( id=id, name=name, status=status ) new.append(new_pollutant) return new def _db_save(self, execute, data=None): sql = ( "INSERT INTO " + "Pollutants(id, name " + "VALUES (" + f"{self.id}, " + f"'{self._db_format(self._name)}'" + ")" ) execute(sql) return True @property def enabled(self): return self._enabled @enabled.setter def enabled(self, enabled): self._enabled = enabled self._status.modified()