def clear_setting(setting_name): """ Delete setting from SETTINGS @param setting_name: e.g. 'current_user_id' @return: True or None """ try: Settings.get(Settings.parameter == setting_name).delete_instance() return True except Settings.DoesNotExist: return None
def db_sync_transaction_id(self, transaction_id): try: last_id = Settings.get(Settings.parameter == 'last_db_transaction') last_id.value = transaction_id last_id.save() except Settings.DoesNotExist: new_setting = Settings.create(parameter='last_db_transaction', value=transaction_id) new_setting.save()
def db_sync_transaction_id(self): # TODO Temporarily always get all transactions. For production, comment out line below. # return self.default_transaction_id try: last_id = Settings.get(Settings.parameter == 'last_db_transaction') return last_id.value except Settings.DoesNotExist: return self.default_transaction_id
def get_setting(setting_name, fallback_value=None): """ Load setting from SETTINGS @param setting_name: e.g. 'current_user_id' @param fallback_value: value to return if value not found @return: value or fallback_value or None """ try: return Settings.get(Settings.parameter == setting_name).value except Settings.DoesNotExist: return fallback_value
def lastBackupTime(self): """ Returns string representation of last DB backup. @return: string value """ try: last_sync = Settings.get(Settings.parameter == 'last_backup_time') last_time = arrow.get(last_sync.value) return last_time.humanize() except Settings.DoesNotExist: return 'Never'
def get_or_set_setting(setting_name, default_value): """ Load setting from SETTINGS. If not found, then set to default_value @param setting_name: e.g. 'current_user_id' @param default_value: value to set if setting not found @return: found value or default_value """ try: return Settings.get(Settings.parameter == setting_name).value except Settings.DoesNotExist: ObserverDBUtil.db_save_setting(setting_name, default_value) return default_value
def _update_backup_time(self): """ Set most recent backup time to now. @return: """ try: last_sync = Settings.get(Settings.parameter == 'last_backup_time') last_sync.value = arrow.now() last_sync.save() except Settings.DoesNotExist: new_setting = Settings.create(parameter='last_backup_time', value=arrow.now()) new_setting.save() self.lastBackupTimeChanged.emit()
def end_trip(self): """ End current trip - TODO: other DB lock function to lock trip(TODO) :return: """ self._logger.info("User ended trip # {}".format(self.tripId)) self._current_trip = None self._certs_model.clear( ) # FIELD-2084: prevent permit to carry to next trip self.tripsChanged.emit() self.tripIdChanged.emit('') self.currentVesselNameChanged.emit('') # remove current trip ID setting from DB try: tripnum = Settings.get(Settings.parameter == 'trip_number') tripnum.delete_instance() except Settings.DoesNotExist as e: self._logger.error( 'Could not delete trip_number setting: {}'.format(e))
def db_load_setting(parameter): try: return Settings.get(Settings.parameter == parameter).value except Settings.DoesNotExist as e: logging.info('DB Setting does not exist: ' + parameter) return None