def edit_damper(self, new_number, new_d_type, new_check_date, new_location, new_is_released=False, new_notes=""): """Edit the damper.""" # Delete [u][/u] from the check_date. check_date_regex = re.compile(r"[0-9]{4}-[0-9]{2}-[0-9]{1,2}") mo = check_date_regex.search(new_check_date) if mo is not None: new_check_date = mo.group() self.is_the_number_exists = False self.is_the_location_exists = False if not self.d_types: # TODO: delete if doesn't need. toast(MDApp.get_running_app().tr._( "First add damper types into the DB")) else: if not new_number or not new_location: toast(MDApp.get_running_app().tr._("Fill all needed fields")) else: damper = Damper() if new_number != self.old_number: # if equal (the same damper) can be updated! try: self.is_the_number_exists = damper.is_the_number_exists( new_number) except sqlite3.DatabaseError: toast("IsNumberExistsError") else: if self.is_the_number_exists: toast(MDApp.get_running_app().tr._( "{} already exists").format(new_number)) # To avoid itersetion of toasts already exists for number and location # added (and not self.is_the_number_exists). if new_location != self.old_location and not self.is_the_number_exists: # if equal (the same damper) can be updated! try: self.is_the_location_exists = damper.is_the_location_exists( new_location) except sqlite3.DatabaseError: toast("IsLocationExistsError") else: if self.is_the_location_exists: toast(MDApp.get_running_app().tr._( "{} already exists").format(new_location)) if not self.is_the_number_exists and not self.is_the_location_exists: try: damper.edit_damper(self.old_number, new_number, new_d_type, new_check_date, new_location, new_is_released, new_notes) except sqlite3.DatabaseError: toast("EditDamperError") else: toast(MDApp.get_running_app().tr._("Edited"))
def add_damper(self, number, d_type, check_date, location, is_released=False, notes=""): """Add new damper into the MDList and DB.""" # Delete [u][/u] from the check_date. check_date_regex = re.compile(r"[0-9]{4}-[0-9]{2}-[0-9]{1,2}") mo = check_date_regex.search(check_date) if mo is not None: check_date = mo.group() if not self.d_types: toast(MDApp.get_running_app().tr._( "First add damper types into the DB")) else: if not number or not location: toast(MDApp.get_running_app().tr._("Fill all needed fields")) else: damper = Damper() try: is_exists = damper.is_the_number_exists(number) except sqlite3.DatabaseError: toast("IsNumberExistsError") else: if is_exists: toast(MDApp.get_running_app().tr._( "{} already exists").format(number)) else: try: is_exists = damper.is_the_location_exists(location) except sqlite3.DatabaseError: toast("IsLocationExistsError") else: if is_exists: toast(MDApp.get_running_app().tr._( "{} already exists").format(location)) else: try: damper.add_damper(number, d_type, check_date, location, is_released, notes) except sqlite3.DatabaseError: toast("AddDamperError") else: toast(MDApp.get_running_app().tr._( "Added new damper: {}").format(number))