def create_field(store, field_dict, language): """ Transaction that perform db_create_field """ field = db_create_field(store, field_dict, language) return anon_serialize_field(store, field, language)
def create_field(store, field, language): """ Transaction that perform db_create_field """ f = db_create_field(store, field, language) return anon_serialize_field(store, f, language)
def db_create_field(store, request, language): """ Add a new field to the store, then return the new serialized object. :param: store: the store reference :param: request: the field definition dict :param: language: the language of the field definition dict :return: a serialization of the object """ is_template, step_id, fieldgroup_id = field_integrity_check(request) # XXX you probably want to split this if else statement into two functions # that are called create_field_from_template and create_field if not 'template_id' in request: fill_localized_keys(request, models.Field.localized_strings, language) field = models.Field.new(store, request) db_update_options(store, field.id, request['options'], language) else: template = store.find(models.Field, models.Field.id == request['template_id']).one() if not template: raise errors.InvalidInputFormat("The specified template id %s does not exist" % request.get('template_id')) field = template.copy(store, is_template) associate_field(store, field, step_id, fieldgroup_id) return anon_serialize_field(store, field, language)
def update_field(store, field_id, request, language): """ Update the specified field with the details. raises :class:`globaleaks.errors.FieldIdNotFound` if the field does not exist. :param store: the store on which perform queries. :param: field_id: the field_id of the field to update :param: request: the field definition dict :param: language: the language of the field definition dict :return: a serialization of the object """ errmsg = 'Invalid or not existent field ids in request.' is_template, step_id, fieldgroup_id = field_integrity_check(request) field = models.Field.get(store, field_id) try: if not field: raise errors.InvalidInputFormat(errmsg) fill_localized_keys(request, models.Field.localized_strings, language) field.update(request) # children handling: # - old children are cleared # - new provided childrens are evaluated and added children = request['children'] if children and field.type != 'fieldgroup': raise errors.InvalidInputFormat( "children can be associated only to fields of type fieldgroup") ancestors = set(fieldtree_ancestors(store, field.id)) field.children.clear() for c in children: child = models.Field.get(store, c['id']) # check child do exists and graph is not recursive if not child or child.id == field.id or child.id in ancestors: raise errors.InvalidInputFormat(errmsg) # remove current step/field fieldgroup/field association disassociate_field(store, child.id) field.children.add(child) db_update_options(store, field.id, request['options'], language) # remove current step/field fieldgroup/field association disassociate_field(store, field_id) associate_field(store, field, step_id, fieldgroup_id) except Exception as dberror: log.err('Unable to update field: {e}'.format(e=dberror)) raise errors.InvalidInputFormat(dberror) return anon_serialize_field(store, field, language)
def update_field(store, field_id, request, language): """ Update the specified field with the details. raises :class:`globaleaks.errors.FieldIdNotFound` if the field does not exist. :param store: the store on which perform queries. :param: field_id: the field_id of the field to update :param: request: the field definition dict :param: language: the language of the field definition dict :return: a serialization of the object """ errmsg = 'Invalid or not existent field ids in request.' is_template, step_id, fieldgroup_id = field_integrity_check(request) field = models.Field.get(store, field_id) try: if not field: raise errors.InvalidInputFormat(errmsg) fill_localized_keys(request, models.Field.localized_strings, language) field.update(request) # children handling: # - old children are cleared # - new provided childrens are evaluated and added children = request['children'] if children and field.type != 'fieldgroup': raise errors.InvalidInputFormat("children can be associated only to fields of type fieldgroup") ancestors = set(fieldtree_ancestors(store, field.id)) field.children.clear() for c in children: child = models.Field.get(store, c['id']) # check child do exists and graph is not recursive if not child or child.id == field.id or child.id in ancestors: raise errors.InvalidInputFormat(errmsg) # remove current step/field fieldgroup/field association disassociate_field(store, child.id) field.children.add(child) db_update_options(store, field.id, request['options'], language) # remove current step/field fieldgroup/field association disassociate_field(store, field_id) associate_field(store, field, step_id, fieldgroup_id) except Exception as dberror: log.err('Unable to update field: {e}'.format(e=dberror)) raise errors.InvalidInputFormat(dberror) return anon_serialize_field(store, field, language)
def db_get_fields_recursively(store, field, language): ret = [] for children in field.children: s = anon_serialize_field(store, children, language) ret.append(s) ret += db_get_fields_recursively(store, children, language) a = [ field['label'] for field in ret] return ret
def get_field(store, field_id, is_template, language): """ Serialize a speficied field, localizing its content depending on the language. :param field_id: the id corresponding to the field. :param language: the language in which to localize data :return: the currently configured field. :rtype: dict """ field = store.find(models.Field, And(models.Field.id == field_id, models.Field.is_template == is_template)).one() if not field: log.err('Invalid field requested') raise errors.FieldIdNotFound return anon_serialize_field(store, field, language)
def update_field(store, field_id, field, language): """ Update the specified field with the details. raises :class:`globaleaks.errors.FieldIdNotFound` if the field does not exist. :param store: the store on which perform queries. :param field_id: the field_id of the field to update :param field: the field definition dict :param language: the language of the field definition dict :return: a serialization of the object """ field = db_update_field(store, field_id, field, language) return anon_serialize_field(store, field, language)
def get_field(store, field_id, language): """ Serialize a specified field :param store: the store on which perform queries. :param field_id: the id corresponding to the field. :param language: the language in which to localize data :return: the currently configured field. :rtype: dict """ field = store.find(models.Field, models.Field.id == field_id).one() if not field: raise errors.FieldIdNotFound return anon_serialize_field(store, field, language)
def get_field(store, field_id, is_template, language): """ Serialize a specified field :param store: the store on which perform queries. :param field_id: the id corresponding to the field. :param is_template: a boolean specifying if the requested field needs to be a template :param language: the language in which to localize data :return: the currently configured field. :rtype: dict """ field = store.find(models.Field, And(models.Field.id == field_id, models.Field.is_template == is_template)).one() if not field: log.err('Invalid field requested') raise errors.FieldIdNotFound return anon_serialize_field(store, field, language)
def get_field_list(store, is_template, language): """ Serialize all the root fields (templates or not templates) localizing their content depending on the language. :return: the current field list serialized. :param language: the language of the field definition dict :rtype: list of dict """ ret = [] for f in store.find(models.Field, models.Field.is_template == is_template): if not store.find(models.FieldField, models.FieldField.child_id == f.id).one(): ret.append(anon_serialize_field(store, f, language)) return ret
def get_fieldtemplates_list(store, language): """ Serialize all the field templates localizing their content depending on the language. :param store: the store on which perform queries. :param language: the language of the field definition dict :return: the current field list serialized. :rtype: list of dict """ ret = [] for f in store.find(models.Field, models.Field.instance == u'template'): if not store.find(models.FieldField, models.FieldField.child_id == f.id).one(): ret.append(anon_serialize_field(store, f, language)) return ret
def db_create_field(store, request, language): """ Create and add a new field to the store, then return the new serialized object. :param store: the store on which perform queries. :param: request: the field definition dict :param: language: the language of the field definition dict :return: a serialization of the object """ _, step_id, fieldgroup_id = field_integrity_check(request) fill_localized_keys(request, models.Field.localized_strings, language) field = models.Field.new(store, request) db_update_options(store, field.id, request['options'], language) associate_field(store, field, step_id, fieldgroup_id) return anon_serialize_field(store, field, language)
def get_field(store, field_id, is_template, language): """ Serialize a specified field :param store: the store on which perform queries. :param field_id: the id corresponding to the field. :param is_template: a boolean specifying if the requested field needs to be a template :param language: the language in which to localize data :return: the currently configured field. :rtype: dict """ field = store.find( models.Field, And(models.Field.id == field_id, models.Field.is_template == is_template)).one() if not field: log.err('Invalid field requested') raise errors.FieldIdNotFound return anon_serialize_field(store, field, language)
def db_create_field_from_template(store, request, language): """ Create and add a new field to the store starting from a template, then return the new serialized object. :param store: the store on which perform queries. :param: request: the field definition dict :param: language: the language of the field definition dict :return: a serialization of the object """ _, step_id, fieldgroup_id = field_integrity_check(request) template = store.find(models.Field, models.Field.id == request['template_id']).one() if not template: raise errors.InvalidInputFormat("The specified template id %s does not exist" % request.get('template_id')) field = template.copy(store, False) associate_field(store, field, step_id, fieldgroup_id) return anon_serialize_field(store, field, language)
def db_get_context_fields(store, context_id, language=GLSetting.memory_copy.default_language): """ Returns: (dict) the fields associated with the context with the specified id. """ context = store.find(models.Context, models.Context.id == context_id).one() if not context: log.err("Requested invalid context") raise errors.ContextIdNotFound steps_list = context.steps fields = [] for ss in steps_list: for children in ss.children: f = anon_serialize_field(store, children, 'en') fields.append(f) return fields
def db_create_field_from_template(store, request, language): """ Create and add a new field to the store starting from a template, then return the new serialized object. :param store: the store on which perform queries. :param: request: the field definition dict :param: language: the language of the field definition dict :return: a serialization of the object """ _, step_id, fieldgroup_id = field_integrity_check(request) template = store.find(models.Field, models.Field.id == request['template_id']).one() if not template: raise errors.InvalidInputFormat( "The specified template id %s does not exist" % request.get('template_id')) field = template.copy(store, False) associate_field(store, field, step_id, fieldgroup_id) return anon_serialize_field(store, field, language)
def _get_field(self, store, field_id): field = models.Field.get(store, field_id) return anon_serialize_field(store, field, 'en')