Exemplo n.º 1
0
def set(section, option, value, session=None):
    """
    Set the given option to the specified value. If the option doesn't exist, it is created.

    :param section: The name of the section.
    :param option: The name of the option.
    :param value: The content of the value.
    :param session: The database session in use.
    """

    if not has_option(section=section, option=option, use_cache=False, session=session):
        section_existed = has_section(section=section)

        new_option = models.Config(section=section, opt=option, value=value)
        new_option.save(session=session)

        delete_from_cache(key=_value_cache_key(section, option))
        delete_from_cache(key=_has_option_cache_key(section, option))
        delete_from_cache(key=_items_cache_key(section))
        if not section_existed:
            delete_from_cache(key=SECTIONS_CACHE_KEY)
            delete_from_cache(key=_has_section_cache_key(section))
    else:
        old_value = session.query(models.Config.value).filter_by(section=section,
                                                                 opt=option).first()[0]
        if old_value != str(value):
            old_option = models.ConfigHistory(section=section,
                                              opt=option,
                                              value=old_value)
            old_option.save(session=session)
            session.query(models.Config).filter_by(section=section, opt=option).update({'value': str(value)})
            delete_from_cache(key=_value_cache_key(section, option))
            delete_from_cache(key=_items_cache_key(section))
Exemplo n.º 2
0
def set(section, option, value, session=None):
    """
    Set the given option to the specified value. If the option doesn't exist, it is created.

    :param section: The name of the section.
    :param option: The name of the option.
    :param value: The content of the value.
    :param session: The database session in use.
    """

    if not has_option(section=section, option=option, use_cache=False, session=session):
        new_option = models.Config(section=section, opt=option, value=value)
        new_option.save(session=session)
    else:
        old_option = models.Config.__history_mapper__.class_(section=section,
                                                             opt=option,
                                                             value=session.query(models.Config.value).filter_by(section=section,
                                                                                                                opt=option).first()[0])
        old_option.save(session=session)
        session.query(models.Config).filter_by(section=section, opt=option).update({'value': str(value)})