Example #1
0
    def save_value(self,
                   document,
                   field,
                   location_start: int,
                   location_end: int,
                   location_text: str,
                   value=None,
                   user=None,
                   allow_overwriting_user_data=True):
        """
        Saves a new value to the field. Depending on the field type it should either
        rewrite existing DocumentFieldValues or add new ones.
        """
        field_values = list(
            models.DocumentFieldValue.objects.filter(document=document,
                                                     field=field))

        if self.multi_value:
            value = self.extraction_function(field, value, location_text)

            for field_value in field_values:
                if field_value.value == value \
                        and field_value.location_start == location_start \
                        and field_value.location_end == location_end:
                    return field_value
            field_value = models.DocumentFieldValue()
            return self.update(field_value, document, field, location_start,
                               location_end, location_text, value, user)
        else:
            # For single-value fields - either update the existing value or create a new one
            field_value = field_values[0] if len(
                field_values) > 0 else models.DocumentFieldValue()

            # Just ensure we don't have some mistakenly added multiple values
            for fv in field_values[1:]:
                fv.delete()

            # This will work only for existing field values having filled created_by or modified_by.
            if not allow_overwriting_user_data \
                    and (field_value.created_by is not None
                         or field_value.modified_by is not None):
                return field_value
            else:
                return self.update(field_value, document, field,
                                   location_start, location_end, location_text,
                                   value, user)
    def save_value(self,
                   document,
                   field,
                   location_start: int,
                   location_end: int,
                   location_text: str,
                   text_unit,
                   value=None,
                   user=None,
                   allow_overwriting_user_data=True,
                   extraction_hint=None):
        """
        Saves a new value to the field. Depending on the field type it should either
        rewrite existing DocumentFieldValues or add new ones.
        """
        field_value = self._get_value_to_save(
            document, field, location_start, location_end,
            value)  # type: 'DocumentFieldValue'
        if self.requires_value and value is None:
            if field_value and (allow_overwriting_user_data
                                or not field_value.is_user_value()):
                field_value.delete()
            return value

        if self.multi_value:
            if field_value:
                if field_value.removed_by_user:
                    if allow_overwriting_user_data:
                        field_value.removed_by_user = False
                        field_value.save()
                return field_value

            field_value = models.DocumentFieldValue()
            return self._update(field_value, document, field, location_start,
                                location_end, location_text, text_unit, value,
                                extraction_hint, user)
        else:
            if field_value:
                qr = models.DocumentFieldValue.objects.filter(
                    document=document, field=field).exclude(pk=field_value.pk)
                if not allow_overwriting_user_data:
                    qr = qr.exclude(Q(created_by__isnull=False) | Q(modified_by__isnull=False)) \

                qr.filter(location_start__isnull=True,
                          location_end__isnull=True).delete()
                qr.exclude(
                    location_start__isnull=True,
                    location_end__isnull=True).update(removed_by_user=True)

                if (field_value.location_start is not None or field_value.location_end is not None) \
                        and location_start is None and location_end is None:
                    if field_value.removed_by_user:
                        field_value = models.DocumentFieldValue()
                    elif allow_overwriting_user_data:
                        models.DocumentFieldValue.objects.filter(
                            pk=field_value.pk).update(removed_by_user=True)
                        field_value = models.DocumentFieldValue()
                    else:
                        return field_value
            else:
                field_value = models.DocumentFieldValue()

            # This will work only for existing field values having filled created_by or modified_by.
            if not allow_overwriting_user_data and field_value.is_user_value():
                return field_value
            else:
                return self._update(field_value, document, field,
                                    location_start, location_end,
                                    location_text, text_unit, value,
                                    extraction_hint, user)
    def save_value(self,
                   document,
                   field,
                   location_start: int,
                   location_end: int,
                   location_text: str,
                   sentence_text_unit,
                   value=None,
                   user=None,
                   removed_by_user=None,
                   allow_overwriting_user_data=True,
                   extraction_hint=None):
        """
        Saves a new value to the field. Depending on the field type it should either
        rewrite existing DocumentFieldValues or add new ones.
        """
        if field.is_calculated():
            return None

        if self.multi_value:
            value, hint = self.get_or_extract_value(document, field, value, extraction_hint,
                                                    location_text)

            q = models.DocumentFieldValue.objects.filter(document=document,
                                                         field=field,
                                                         location_start=location_start,
                                                         location_end=location_end)
            q = q.filter(value__isnull=True) if value is None else q.filter(value=value)

            field_value = q.first()

            if field_value:
                if field_value.removed_by_user:
                    field_value.removed_by_user = False
                    field_value.save()
                return field_value

            field_value = models.DocumentFieldValue()
            return self._update(field_value, document, field, location_start, location_end,
                                location_text, sentence_text_unit,
                                value,
                                hint,
                                user, removed_by_user)
        else:
            q = models.DocumentFieldValue.objects.filter(document=document,
                                                         field=field)
            field_value = q.first()

            if field_value:
                models.DocumentFieldValue.objects \
                    .filter(document=document, field=field) \
                    .exclude(pk=field_value.pk) \
                    .delete()
            else:
                field_value = models.DocumentFieldValue()

            # This will work only for existing field values having filled created_by or modified_by.
            if not allow_overwriting_user_data \
                    and (field_value.created_by is not None
                         or field_value.modified_by is not None):
                return field_value
            else:
                value, hint = self.get_or_extract_value(document, field, value, extraction_hint,
                                                        location_text)
                return self._update(field_value, document, field, location_start, location_end,
                                    location_text, sentence_text_unit,
                                    value, hint, user, removed_by_user)