def process(annis_server): from texts.models import SearchField, SearchFieldValue from texts.models import Text search_fields = _fields(annis_server) current_order_and_splittable_by_title = {sf.title: { 'order': sf.order, 'splittable': sf.splittable } for sf in SearchField.objects.all() } logger.info("Rebuilding %d SearchFields, and SearchFieldValues" % len(search_fields)) SearchField.objects.all().delete() saved_SearchFieldValues = {} # (search_field ID, title) -> SearchFieldValue # Add all new search fields and mappings for search_field in search_fields: sf = SearchField() sf.title = search_field['name'] # Preserve any current order and splittable values current_order_and_splittable = current_order_and_splittable_by_title.get(sf.title) if current_order_and_splittable: sf.order = current_order_and_splittable['order'] sf.splittable = current_order_and_splittable['splittable'] else: sf.order = 10 sf.splittable = "" # Save the search field so that it has an id to be added to the # search field value search_field foreign key attribute sf.save() # Save value data for value in search_field['values']: value_value = value['value'] split_values = value_value.split(sf.splittable) if sf.splittable else [value_value] for split_value in split_values: title = split_value.strip() sfv = saved_SearchFieldValues.get((sf.id, title)) if not sfv: sfv = SearchFieldValue() sfv.search_field = sf sfv.title = title sfv.save() saved_SearchFieldValues[(sf.id, title)] = sfv text_ids = value['texts'] if text_ids: # Add the texts via the native add ManyToMany handling for sfv_text in Text.objects.filter(id__in=text_ids): sfv.texts.add(sfv_text)
def load_searchfields(): """Prepopulates the database with search fields that we care about in the web user interface. This is essential because two of the search fields need to have the splittable property properly set, or the data won't be ingested properly. """ corpus = SearchField() corpus.title = "corpus" corpus.order = 1 author = SearchField() author.title = "author" author.order = 2 ms_name = SearchField() ms_name.title = "msName" ms_name.order = 3 annotation = SearchField() annotation.title = "annotation" annotation.order = 4 annotation.splittable = "," translation = SearchField() translation.title = "translation" translation.order = 5 translation.splittable = "," for searchfield in [corpus, author, ms_name, annotation, translation]: try: SearchField.objects.get(title__exact=searchfield.title) except SearchField.DoesNotExist: searchfield.save()