예제 #1
0
    def update_obj(self,
                   obj: Model,
                   legal_reason: Optional[LegalReason] = None,
                   purpose: Optional["AbstractPurpose"] = None,
                   fields: Union[Fields, FieldMatrix] = '__ALL__',
                   base_encryption_key: Optional[str] = None,
                   anonymization: bool = True):
        parsed_fields: Fields = Fields(
            fields,
            obj.__class__) if not isinstance(fields, Fields) else fields

        if self.DELETE_FIELD_NAME in parsed_fields.local_fields and anonymization is True:
            self.update_related_fields(parsed_fields, obj, legal_reason,
                                       purpose, anonymization)

            obj.__class__.objects.filter(pk=obj.pk).delete()

            if self.anonymize_reversion(obj):
                from reversion.models import Version
                from gdpr.utils import get_reversion_versions
                get_reversion_versions(obj).delete()

        elif self.DELETE_FIELD_NAME in parsed_fields.local_fields:
            parsed_fields.local_fields = [
                i for i in parsed_fields.local_fields
                if i != self.DELETE_FIELD_NAME
            ]
            super().update_obj(obj, legal_reason, purpose, parsed_fields,
                               base_encryption_key, anonymization)
        else:
            super().update_obj(obj, legal_reason, purpose, parsed_fields,
                               base_encryption_key, anonymization)
예제 #2
0
    def test_local_and_related(self):
        fields = Fields(BASIC_FIELDS, Customer)

        self.assertListEqual(fields.local_fields, ['primary_email_address'])
        self.assertTrue('emails' in fields.related_fields)
        self.assertListEqual(fields.related_fields['emails'].local_fields,
                             ['email'])
예제 #3
0
    def update_obj(self, obj, legal_reason = None,
                   purpose = None,
                   fields = u'__ALL__',
                   base_encryption_key = None,
                   anonymization = True):
        parsed_fields = Fields(fields, obj.__class__) if not isinstance(fields, Fields) else fields

        if self.DELETE_FIELD_NAME in parsed_fields.local_fields and anonymization is True:
            self.update_related_fields(parsed_fields, obj, legal_reason, purpose, anonymization)

            obj.__class__.objects.filter(pk=obj.pk).delete()

            if self.anonymize_reversion(obj):
                from gdpr.utils import get_reversion_versions
                get_reversion_versions(obj).delete()

        elif self.DELETE_FIELD_NAME in parsed_fields.local_fields:
            parsed_fields.local_fields = [i for i in parsed_fields.local_fields if i != self.DELETE_FIELD_NAME]
            super(DeleteModelAnonymizer, self).update_obj(obj, legal_reason, purpose, parsed_fields, base_encryption_key, anonymization)
        else:
            super(DeleteModelAnonymizer, self).update_obj(obj, legal_reason, purpose, parsed_fields, base_encryption_key, anonymization)
예제 #4
0
    def test_multilevel_related(self):
        fields = Fields(MULTILEVEL_FIELDS, Customer)

        self.assertListEqual(fields.local_fields, [])
        self.assertTrue('accounts' in fields.related_fields)
        self.assertListEqual(fields.related_fields['accounts'].local_fields,
                             ['number', 'owner'])
        self.assertTrue(
            'payments' in fields.related_fields['accounts'].related_fields)
        self.assertListEqual(
            fields.related_fields['accounts'].related_fields['payments'].
            local_fields, ['value', 'date'])
예제 #5
0
    def update_obj(self, obj, legal_reason = None,
                   purpose = None,
                   fields = u'__ALL__',
                   base_encryption_key = None,
                   anonymization = True):
        if not anonymization and not self.is_reversible(obj):
            raise self.IrreversibleAnonymizerException(u'{} for obj "{}" is not reversible.'.format((self.__class__.__name__), (obj)))

        if base_encryption_key:
            self._base_encryption_key = base_encryption_key

        parsed_fields = Fields(fields, obj.__class__) if not isinstance(fields, Fields) else fields

        if anonymization:
            raw_local_fields = [i for i in parsed_fields.local_fields if not self.is_field_anonymized(obj, i)]
        else:
            raw_local_fields = [i for i in parsed_fields.local_fields if
                                self.is_field_anonymized(obj, i) and self[i].get_is_reversible(obj)]

        if raw_local_fields:
            update_dict = dict((
                name, self.get_value_from_obj(self[name], obj, name, anonymization)) for name in raw_local_fields)
            if self.anonymize_reversion(obj):
                from gdpr.utils import get_reversion_local_field_dict
                versions = self.get_reversion_versions(obj)
                versions_update_dict = [
                    (
                        version,
                        dict((
                            name, self.get_value_from_version(self[name], obj, version, name,
                                                              anonymization=anonymization))
                            for name in raw_local_fields
                            if name in get_reversion_local_field_dict(version))
                    )
                    for version in versions
                ]
                self.perform_update_with_version(
                    obj, update_dict, versions_update_dict, legal_reason,
                    anonymization=anonymization
                )
            else:
                self.perform_update(obj, update_dict, legal_reason, anonymization=anonymization)

        self.update_related_fields(parsed_fields, obj, legal_reason, purpose, anonymization)
예제 #6
0
    def update_obj(self,
                   obj: Model,
                   legal_reason: Optional[LegalReason] = None,
                   purpose: Optional["AbstractPurpose"] = None,
                   fields: Union[Fields, FieldMatrix] = '__ALL__',
                   base_encryption_key: Optional[str] = None,
                   anonymization: bool = True):
        if not anonymization and not self.is_reversible(obj):
            raise self.IrreversibleAnonymizerException(
                f'{self.__class__.__name__} for obj "{obj}" is not reversible.'
            )

        if base_encryption_key:
            self._base_encryption_key = base_encryption_key

        parsed_fields: Fields = Fields(
            fields,
            obj.__class__) if not isinstance(fields, Fields) else fields

        if anonymization:
            raw_local_fields = [
                i for i in parsed_fields.local_fields
                if not self.is_field_anonymized(obj, i)
            ]
        else:
            raw_local_fields = [
                i for i in parsed_fields.local_fields
                if self.is_field_anonymized(obj, i)
                and self[i].get_is_reversible(obj)
            ]

        if raw_local_fields:
            update_dict = {
                name: self.get_value_from_obj(self[name], obj, name,
                                              anonymization)
                for name in raw_local_fields
            }
            if self.anonymize_reversion(obj):
                from reversion.models import Version
                from gdpr.utils import get_reversion_local_field_dict
                versions: List[Version] = self.get_reversion_versions(obj)
                versions_update_dict = [(version, {
                    name:
                    self.get_value_from_version(self[name],
                                                obj,
                                                version,
                                                name,
                                                anonymization=anonymization)
                    for name in raw_local_fields
                    if name in get_reversion_local_field_dict(version)
                }) for version in versions]
                self.perform_update_with_version(obj,
                                                 update_dict,
                                                 versions_update_dict,
                                                 legal_reason,
                                                 anonymization=anonymization)
            else:
                self.perform_update(obj,
                                    update_dict,
                                    legal_reason,
                                    anonymization=anonymization)

        self.update_related_fields(parsed_fields, obj, legal_reason, purpose,
                                   anonymization)
예제 #7
0
 def test_local(self):
     fields = Fields(LOCAL_FIELDS, Customer)
     self.assertListEqual(fields.local_fields, list(LOCAL_FIELDS))
예제 #8
0
 def test_local_all(self):
     fields = Fields('__ALL__', Customer)
     self.assertListEqual(fields.local_fields,
                          list(CustomerAnonymizer().fields.keys()))
예제 #9
0
 def get_parsed_fields(self, model: Type[Model]) -> Fields:
     return Fields(self.fields or (), model)
예제 #10
0
 def get_parsed_fields(self, model):
     return Fields(self.fields or (), model)