Ejemplo n.º 1
0
def db_update_options(store, field_id, options, language):
    """
    Update options

    :param store: the store on which perform queries.
    """
    field = models.Field.get(store, field_id)
    if field is None:
        raise errors.FieldIdNotFound

    old_options = store.find(models.FieldOption,
                             models.FieldOption.field_id == field_id)

    indexed_old_options = {}
    for o in old_options:
        indexed_old_options[o.id] = o

    new_options = []
    n = 1
    for option in options:
        opt_dict = {}
        opt_dict['field_id'] = field_id
        opt_dict['number'] = n

        keys = get_field_option_localized_keys(field.type)
        fill_localized_keys(option['attrs'], keys, language)
        opt_dict['attrs'] = option['attrs']

        # check for reuse (needed to keep translations)
        if 'id' in option and option['id'] in indexed_old_options:
            o = indexed_old_options[option['id']]
            o.update(opt_dict, keys)

            new_options.append(indexed_old_options[option['id']])
            del indexed_old_options[option['id']]
        else:
            new_options.append(models.FieldOption(opt_dict))

        n += 1

    # remove all the not reused old options
    for o_id in indexed_old_options:
        store.remove(indexed_old_options[o_id])

    for n in new_options:
        store.add(n)
Ejemplo n.º 2
0
def db_update_fieldoption(store, fieldoption_id, option, language):
    fill_localized_keys(option, models.FieldOption.localized_keys, language)

    if fieldoption_id is not None:
        o = store.find(models.FieldOption, models.FieldOption.id == fieldoption_id).one()
    else:
        o = None

    if o is None:
        o = models.FieldOption()
        store.add(o)

    o.trigger_field = option['trigger_field'] if option['trigger_field'] != '' else None
    o.trigger_step = option['trigger_step'] if option['trigger_step'] != '' else None

    o.update(option)

    return o.id
Ejemplo n.º 3
0
def db_update_fieldoption(store, fieldoption_id, option, language):
    fill_localized_keys(option, models.FieldOption.localized_strings, language)

    if fieldoption_id is not None:
        o = store.find(models.FieldOption,
                       models.FieldOption.id == fieldoption_id).one()
    else:
        o = None

    if o is None:
        o = models.FieldOption()
        store.add(o)

    o.update(option)

    for activated_field in option['activated_fields']:
        o.activated_fields.add(
            store.find(models.Field, models.Field.id == activated_field))

    return o.id