def config(): from labster.domain.models.config import Config DATA = get_initial_constants() config = Config() config.data = DATA return config
def save_constants(constants): from labster.domain.models.config import Config updated_constants = update_constants(constants) config = Config.query.first() if config is None: config = Config(data=updated_constants) else: config.data = updated_constants return config
def _get_constants() -> dict[str, Any]: """Get constants from config or local json with updating system. Pick new constants that are defined in TYPES but are not saved in the config yet from the json. Upgrades constants that already exist, given the version number. """ from labster.domain.models.config import Config config = Config.query.first() if config is None: config = Config() db.session.add(config) initial_constants = get_initial_constants() # upgrade _upgrade_if_needed(config, initial_constants) constants = config.data dotted_constants = DottedCollection.factory(constants) json_dotted_constants = DottedCollection.factory(initial_constants) if dotted_constants: for key in TYPES.keys(): # do not write "key not in .keys()", it doesn't return "dotted keys". if dotted_constants.get(key, _MARKER) is _MARKER: dotted_constants[key] = json_dotted_constants.get(key) constants = dotted_constants.to_python() return constants
def _upgrade_if_needed(config: Config, initial_constants: dict[str, Any]): """ "data migration" tool. - constants: the ones from the config, to update. - initial_constants: from the json file, that may have additions. """ needs_commit = False version = initial_constants.get("version") if not version: return constants = config.data or {} # Check the state we want rather than a version number. # if version == 0.2 and constants.get('version', 0) < 0.2: if "recrutement" not in constants: constants["recrutement"] = {} needs_commit = True if "grades" not in constants["recrutement"]: constants["recrutement"]["grades"] = [] needs_commit = True if ("recrutement" in constants and constants["recrutement"]["grades"] and constants["recrutement"]["grades"][0] == "IR"): constants["recrutement"]["grades"] = initial_constants["recrutement"][ "grades"] constants["version"] = version needs_commit = True if version == 0.3: # add salaire_brut_mensuel_indicatif.doctorat-plus-3 # Maybe a new DB doesn't have salaire_brut_mensuel_indicatif yet. if not constants["recrutement"].get("salaire_brut_mensuel_indicatif"): constants["recrutement"][ "salaire_brut_mensuel_indicatif"] = initial_constants[ "recrutement"]["salaire_brut_mensuel_indicatif"] else: post_doct_names = [ it[0] for it in constants["recrutement"] ["salaire_brut_mensuel_indicatif"]["Post-doctorant"] ] if "doctorat-plus-3" not in post_doct_names: constants["recrutement"]["salaire_brut_mensuel_indicatif"][ "Post-doctorant"].append(initial_constants["recrutement"] ["salaire_brut_mensuel_indicatif"] ["Post-doctorant"][-1]) # logger.info("--- constants updated for 0.3 - doctorat-plus-3") constants["version"] = 0.3 needs_commit = True if needs_commit: config.data = constants db.session.commit()