<commit_before># -*- coding: UTF-8
"""
Implementation of the code executed when an HTTP client reach /admin/fields URI.
"""
from __future__ import unicode_literals

from storm.exceptions import DatabaseError
from twisted.internet.defer import inlineCallbacks

from globaleaks import models
from globaleaks.handlers.base import BaseHandler
from globaleaks.handlers.authentication import authenticated, transport_security_check
from globaleaks.handlers.node import anon_serialize_option, get_field_option_localized_keys
from globaleaks.models import Field, Step
from globaleaks.rest import errors, requests
from globaleaks.settings import transact, transact_ro
from globaleaks.utils.structures import fill_localized_keys, get_localized_values
from globaleaks.utils.utility import log

def admin_serialize_field(field, language):
    """
    Serialize a field, localizing its content depending on the language.

    :param field: the field object to be serialized
    :param language: the language in which to localize data
    :return: a serialization of the object
    """

    # naif likes if we add reference links
    # this code is inspired by:
    #  - https://www.youtube.com/watch?v=KtNsUgKgj9g

    options = [ anon_serialize_option(o, field.type, language) for o in field.options ]

    ret_dict = {
        'id': field.id,
        'multi_entry': field.multi_entry,
        'required': field.required,
        'preview': False,
        'stats_enabled': field.stats_enabled,
        'type': field.type,
        'x': field.x,
        'y': field.y,
        'options': options,
        'children': [f.id for f in field.children],
    }

    return get_localized_values(ret_dict, field, field.localized_strings, language)

def db_update_options(store, field_id, options, language):
    """
    Update options
    """
    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

    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)

           # remove key from old steps to be removed
           del indexed_old_options[option['id']]
        else:
           o = models.FieldOption.new(store, opt_dict, keys)

        n += 1

    # remove all the not reused old options
    for o in indexed_old_options:
        store.remove(o)
<commit_msg>fixed wrong operation for option removal<commit_after># -*- coding: UTF-8
"""
Implementation of the code executed when an HTTP client reach /admin/fields URI.
"""
from __future__ import unicode_literals

from storm.exceptions import DatabaseError
from twisted.internet.defer import inlineCallbacks

from globaleaks import models
from globaleaks.handlers.base import BaseHandler
from globaleaks.handlers.authentication import authenticated, transport_security_check
from globaleaks.handlers.node import anon_serialize_option, get_field_option_localized_keys
from globaleaks.models import Field, Step
from globaleaks.rest import errors, requests
from globaleaks.settings import transact, transact_ro
from globaleaks.utils.structures import fill_localized_keys, get_localized_values
from globaleaks.utils.utility import log

def admin_serialize_field(field, language):
    """
    Serialize a field, localizing its content depending on the language.

    :param field: the field object to be serialized
    :param language: the language in which to localize data
    :return: a serialization of the object
    """

    # naif likes if we add reference links
    # this code is inspired by:
    #  - https://www.youtube.com/watch?v=KtNsUgKgj9g

    options = [ anon_serialize_option(o, field.type, language) for o in field.options ]

    ret_dict = {
        'id': field.id,
        'multi_entry': field.multi_entry,
        'required': field.required,
        'preview': False,
        'stats_enabled': field.stats_enabled,
        'type': field.type,
        'x': field.x,
        'y': field.y,
        'options': options,
        'children': [f.id for f in field.children],
    }

    return get_localized_values(ret_dict, field, field.localized_strings, language)

def db_update_options(store, field_id, options, language):
    """
    Update options
    """
    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

    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)

           # remove key from old steps to be removed
           del indexed_old_options[option['id']]
        else:
           o = models.FieldOption.new(store, opt_dict, keys)

        n += 1

    # remove all the not reused old options
    for opt_id in indexed_old_options:
        o = store.find(models.FieldOption, models.FieldOption.id == opt_id).one()
        store.remove(o)

{'fraction_non_alphanumeric': 0.08839315665883932, 'fraction_numerical': 0.0013418316001341832, 'mean_word_length': 3.2081863091037404, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '108326', 'n_tokens_mistral': 1784, 'n_tokens_neox': 1735, 'n_words': 507}