def set(self, value): """ set Set the value into the configuration table for this current user. :param value: Value to be set :returns: nothing. """ # We can't store the values in the given format, we need to convert # them in string first. We also need to validate the value type. parser_map = { 'integer': int, 'numeric': float, 'date': dateutil_parser.parse, 'datetime': dateutil_parser.parse, 'keyboardshortcut': json.dumps } error_map = {'keyboardshortcut': 'keyboard shortcut'} try: if self._type in ('boolean', 'switch', 'node'): assert isinstance(value, bool) elif self._type == 'options': has_value = next((True for opt in self.options if 'value' in opt and opt['value'] == value), False) assert (has_value or (self.select2 and self.select2['tags'])) elif self._type == 'date': value = parser_map[self._type](value).date() else: value = parser_map.get(self._type, lambda v: v)(value) if self._type == 'integer': value = self.normalize_range(value) assert isinstance(value, int) if self._type == 'numeric': value = self.normalize_range(value) assert (isinstance(value, int) or isinstance(value, float) or isinstance(value, decimal.Decimal)) except Exception as e: current_app.logger.exception(e) return False, gettext("Invalid value for {0} option.".format( error_map.get(self._type, self._type))) pref = UserPrefTable.query.filter_by(pid=self.pid).filter_by( uid=current_user.id).first() value = "{}".format(value) if pref is None: pref = UserPrefTable(uid=current_user.id, pid=self.pid, value=value) db.session.add(pref) else: pref.value = value db.session.commit() return True, None
def set(self, value): """ set Set the value into the configuration table for this current user. :param value: Value to be set :returns: nothing. """ # We can't store the values in the given format, we need to convert # them in string first. We also need to validate the value type. if self._type == 'boolean' or self._type == 'switch' or \ self._type == 'node': if type(value) != bool: return False, gettext("Invalid value for a boolean option.") elif self._type == 'integer': value = int(value) if type(value) != int: return False, gettext("Invalid value for an integer option.") elif self._type == 'numeric': value = float(value) t = type(value) if t != float and t != int and t != decimal.Decimal: return False, gettext("Invalid value for a numeric option.") elif self._type == 'date': try: value = dateutil_parser.parse(value).date() except Exception as e: current_app.logger.exeception(e) return False, gettext("Invalid value for a date option.") elif self._type == 'datetime': try: value = dateutil_parser.parse(value) except Exception as e: current_app.logger.exeception(e) return False, gettext("Invalid value for a datetime option.") elif self._type == 'options': if value not in self.options: return False, gettext("Invalid value for an options option.") pref = UserPrefTable.query.filter_by(pid=self.pid).filter_by( uid=current_user.id).first() if pref is None: pref = UserPrefTable(uid=current_user.id, pid=self.pid, value=str(value)) db.session.add(pref) else: pref.value = str(value) db.session.commit() return True, None
def on_user_logged_in(sender, user): try: from pgadmin.model import UserPreference, Preferences bin_pref = Preferences.query.filter_by( name="pg_bin_dir").order_by("id").first() check_pref = UserPreference.query.filter_by( pid=bin_pref.id, uid=user.id).order_by("pid") if check_pref.count() > 0: pass else: path = None for p in ["pg10", "pg96", "pg95", "pg94"]: bin_path = os.path.join(PGC_HOME, p, "bin") if os.path.exists(bin_path): path = bin_path break if path: pref = UserPreference(pid=bin_pref.id, uid=user.id, value=path) db.session.add(pref) db.session.commit() except Exception as e: pass
def set(self, value): """ set Set the value into the configuration table for this current user. :param value: Value to be set :returns: nothing. """ # We can't store the values in the given format, we need to convert # them in string first. We also need to validate the value type. if self._type == 'boolean' or self._type == 'switch' or \ self._type == 'node': if type(value) != bool: return False, gettext("Invalid value for a boolean option.") elif self._type == 'integer': value = int(value) if self.min_val is not None and value < self.min_val: value = self.min_val if self.max_val is not None and value > self.max_val: value = self.max_val if type(value) != int: return False, gettext("Invalid value for an integer option.") elif self._type == 'numeric': value = float(value) if self.min_val is not None and value < self.min_val: value = self.min_val if self.max_val is not None and value > self.max_val: value = self.max_val t = type(value) if t != float and t != int and t != decimal.Decimal: return False, gettext("Invalid value for a numeric option.") elif self._type == 'date': try: value = dateutil_parser.parse(value).date() except Exception as e: current_app.logger.exception(e) return False, gettext("Invalid value for a date option.") elif self._type == 'datetime': try: value = dateutil_parser.parse(value) except Exception as e: current_app.logger.exception(e) return False, gettext("Invalid value for a datetime option.") elif self._type == 'options': has_value = False for opt in self.options: if 'value' in opt and opt['value'] == value: has_value = True if not has_value and self.select2 and not self.select2['tags']: return False, gettext("Invalid value for an options option.") elif self._type == 'keyboardshortcut': try: value = json.dumps(value) except Exception as e: current_app.logger.exception(e) return False, gettext( "Invalid value for a keyboard shortcut option." ) pref = UserPrefTable.query.filter_by( pid=self.pid ).filter_by(uid=current_user.id).first() value = u"{}".format(value) if pref is None: pref = UserPrefTable( uid=current_user.id, pid=self.pid, value=value ) db.session.add(pref) else: pref.value = value db.session.commit() return True, None
def set(self, value): """ set Set the value into the configuration table for this current user. :param value: Value to be set :returns: nothing. """ # We can't store the values in the given format, we need to convert # them in string first. We also need to validate the value type. if self._type == 'boolean' or self._type == 'switch' or \ self._type == 'node': if type(value) != bool: return False, gettext("Invalid value for a boolean option.") elif self._type == 'integer': value = int(value) if self.min_val is not None and value < self.min_val: value = self.min_val if self.max_val is not None and value > self.max_val: value = self.max_val if type(value) != int: return False, gettext("Invalid value for an integer option.") elif self._type == 'numeric': value = float(value) if self.min_val is not None and value < self.min_val: value = self.min_val if self.max_val is not None and value > self.max_val: value = self.max_val t = type(value) if t != float and t != int and t != decimal.Decimal: return False, gettext("Invalid value for a numeric option.") elif self._type == 'date': try: value = dateutil_parser.parse(value).date() except Exception as e: current_app.logger.exeception(e) return False, gettext("Invalid value for a date option.") elif self._type == 'datetime': try: value = dateutil_parser.parse(value) except Exception as e: current_app.logger.exeception(e) return False, gettext("Invalid value for a datetime option.") elif self._type == 'options': has_value = False for opt in self.options: if 'value' in opt and opt['value'] == value: has_value = True if not has_value and self.select2 and not self.select2['tags']: return False, gettext("Invalid value for an options option.") elif self._type == 'keyboardshortcut': try: value = json.dumps(value) except Exception as e: current_app.logger.exeception(e) return False, gettext( "Invalid value for a keyboard shortcut option." ) pref = UserPrefTable.query.filter_by( pid=self.pid ).filter_by(uid=current_user.id).first() value = u"{}".format(value) if pref is None: pref = UserPrefTable( uid=current_user.id, pid=self.pid, value=value ) db.session.add(pref) else: pref.value = value db.session.commit() return True, None