Exemplo n.º 1
0
    def validate(self, model):
        applicable_model = get_field_tuple(model, self.applicable_field)[1]
        footnote_type = get_field_tuple(model, self.footnote_type_field)[1]

        if (
            footnote_type.application_code
            not in applicable_model.footnote_application_codes
        ):
            raise self.violation(model)
Exemplo n.º 2
0
def make_non_duplicate_record(factory, identifying_fields=None):
    """Creates two records using the passed factory that are not duplicates of
    each other and returns the record created last."""
    existing = factory.create()
    not_duplicate = factory.create()

    if identifying_fields is None:
        identifying_fields = list(factory._meta.model.identifying_fields)

    assert any(
        get_field_tuple(existing, f) != get_field_tuple(not_duplicate, f)
        for f in identifying_fields)

    return not_duplicate
Exemplo n.º 3
0
    def validate(self, model):
        _, container = get_field_tuple(model, self.container_field_name)
        contained = model
        if self.contained_field_name:
            _, contained = get_field_tuple(model, self.contained_field_name)

        if not container:
            # TODO should this raise an exception?
            log.debug(
                "Skipping %s: Container field %s not found.",
                self.__class__.__name__,
                self.container_field_name,
            )
            return

        self.query_contains_validity(container, contained, model)
Exemplo n.º 4
0
    def validate(self, model):
        identifying_fields = self.identifying_fields or model.identifying_fields
        query = dict(
            get_field_tuple(model, field) for field in identifying_fields)

        if (model.__class__.objects.filter(**query).approved_up_to_transaction(
                self.transaction).exclude(id=model.id).exists()):
            raise self.violation(model)
Exemplo n.º 5
0
def get_fields_dict(instance, field_names):
    """
    Retrieve dict of fields from django model instance, fetching data via the
    double underscore __ linked models.

    :param instance: Django model instance.
    :param field_names: List of fields to retrieve.
    :return: dict of {field_name: field_value}
    """
    return dict(get_field_tuple(instance, name) for name in field_names)
Exemplo n.º 6
0
def make_duplicate_record(factory, identifying_fields=None):
    """Creates two records using the passed factory that are duplicates of each
    other and returns the record created last."""
    existing = factory.create()

    # allow overriding identifying_fields
    if identifying_fields is None:
        identifying_fields = list(factory._meta.model.identifying_fields)

    return factory.create(**dict(
        get_field_tuple(existing, field) for field in identifying_fields))
Exemplo n.º 7
0
    def make_dupe(factory, identifying_fields=None):
        existing = factory.create()

        # allow overriding identifying_fields
        if identifying_fields is None:
            identifying_fields = list(factory._meta.model.identifying_fields)

            if hasattr(existing, "valid_between"):
                identifying_fields.append("valid_between")

        return factory.create(
            **dict(get_field_tuple(existing, field) for field in identifying_fields)
        )
Exemplo n.º 8
0
    def get_identifying_fields(
        self,
        identifying_fields: Optional[Iterable[str]] = None,
    ) -> Dict[str, Any]:
        """
        Get a name/value mapping of the fields that identify this model.

        :param identifying_fields Optional[Iterable[str]]: Optionally override
            the fields to retrieve
        :rtype dict[str, Any]: A dict of field names to values
        """

        identifying_fields = identifying_fields or self.identifying_fields
        fields = {}

        for field in identifying_fields:
            _, fields[field] = get_field_tuple(self, field)

        return fields
Exemplo n.º 9
0
    def validate(self, model):
        """
        Check other models with the same identifying fields do not have
        overlapping validity periods, except other versions of the same model.

        :param model TrackedModel: The model to compare with
        :raises self.violation: Rule violation
        """
        identifying_fields = self.identifying_fields or model.identifying_fields
        query = dict(get_field_tuple(model, field) for field in identifying_fields)
        query["valid_between__overlap"] = model.valid_between

        if (
            model.__class__.objects.filter(**query)
            .approved_up_to_transaction(self.transaction)
            .exclude(version_group=model.version_group)
            .exists()
        ):
            raise self.violation(model)
Exemplo n.º 10
0
    def validate(self, model):
        """
        Check no other model has the same identifying fields, except other
        versions of the same model.

        :param model TrackedModel: The model to compare with
        :raises self.violation: Rule violation
        """
        identifying_fields = self.identifying_fields or model.identifying_fields
        query = dict(get_field_tuple(model, field) for field in identifying_fields)

        if (
            type(model)
            .objects.filter(**query)
            .approved_up_to_transaction(self.transaction)
            .exclude(version_group=model.version_group)
            .exists()
        ):
            raise self.violation(model)
Exemplo n.º 11
0
def test_get_field_tuple(model_data, field, expected):
    model = factories.TestModel3Factory(**model_data)
    assert util.get_field_tuple(model, field) == (field, expected)