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)
Beispiel #2
0
def get_all_obj_and_parent_versions_queryset_list(
        obj: Model) -> List[QuerySet]:
    """Return list of object and its parent version querysets"""
    from gdpr.utils import get_reversion_versions

    return [get_reversion_versions(i) for i in get_all_parent_objects(obj)
            ] + [get_reversion_versions(obj)]
 def get_reversion_versions(self, obj: Model):
     from gdpr.utils import get_reversion_versions
     versions = [i for i in get_reversion_versions(obj)]  # QuerySet to list
     parent_obj_versions = [
         get_reversion_versions(i) for i in self.get_all_parent_objects(obj)
     ]
     versions += [
         item for sublist in parent_obj_versions for item in sublist
     ]
     return versions
Beispiel #4
0
    def test_reversion_delete(self):
        from reversion import revisions as reversion
        from reversion.models import Version
        from gdpr.utils import get_reversion_versions

        anon = ContactFormAnonymizer()
        anon.Meta.delete_reversion = True

        user = User(username='******')
        user.save()

        with reversion.create_revision():
            form = ContactForm()
            form.email = CUSTOMER__EMAIL
            form.full_name = CUSTOMER__LAST_NAME
            form.save()

            reversion.set_user(user)

        with reversion.create_revision():
            form.email = CUSTOMER__EMAIL2
            form.save()

            reversion.set_user(user)

        with reversion.create_revision():
            form.email = CUSTOMER__EMAIL3
            form.save()

            reversion.set_user(user)

        versions: List[Version] = get_reversion_versions(form).order_by('id')

        assert_equal(versions[0].field_dict['email'], CUSTOMER__EMAIL)
        assert_equal(versions[1].field_dict['email'], CUSTOMER__EMAIL2)
        assert_equal(versions[2].field_dict['email'], CUSTOMER__EMAIL3)

        anon.anonymize_obj(form, base_encryption_key=self.base_encryption_key)

        anon_versions: List[Version] = get_reversion_versions(form).order_by(
            'id')
        assert_equal(len(anon_versions), 0)
Beispiel #5
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)
Beispiel #6
0
    def test_reversion_anonymization_parents(self):
        from reversion import revisions as reversion
        from reversion.models import Version

        anon = ChildEAnonymizer()

        user = User(username='******')
        user.save()

        with reversion.create_revision():
            e = ChildE()
            e.name = 'Lorem'
            e.first_name = 'Ipsum'
            e.last_name = 'Dolor'
            e.birth_date = CUSTOMER__BIRTH_DATE
            e.note = 'sit Amet'
            e.save()

            reversion.set_user(user)

        with reversion.create_revision():
            e.name = 'LOREM'
            e.first_name = 'IPSUM'
            e.last_name = 'DOLOR'
            e.birth_date = CUSTOMER__BIRTH_DATE + timedelta(days=2)
            e.note = 'SIT AMET'
            e.save()

            reversion.set_user(user)

        versions_a: List[Version] = get_reversion_versions(
            e.topparenta_ptr).order_by('id')
        versions_b: List[Version] = get_reversion_versions(
            e.parentb_ptr).order_by('id')
        versions_c: List[Version] = get_reversion_versions(
            e.parentc_ptr).order_by('id')
        versions_d: List[Version] = get_reversion_versions(
            e.extraparentd_ptr).order_by('id')
        versions_e: List[Version] = get_reversion_versions(e).order_by('id')

        assert_equal(
            get_reversion_local_field_dict(versions_a[0])['name'], 'Lorem')
        assert_equal(
            get_reversion_local_field_dict(versions_a[1])['name'], 'LOREM')

        assert_equal(
            get_reversion_local_field_dict(versions_b[0])['birth_date'],
            CUSTOMER__BIRTH_DATE)
        assert_equal(
            get_reversion_local_field_dict(versions_b[1])['birth_date'],
            CUSTOMER__BIRTH_DATE + timedelta(days=2))

        assert_equal(
            get_reversion_local_field_dict(versions_c[0])['first_name'],
            'Ipsum')
        assert_equal(
            get_reversion_local_field_dict(versions_c[1])['first_name'],
            'IPSUM')

        assert_equal(
            get_reversion_local_field_dict(versions_d[0])['note'], 'sit Amet')
        assert_equal(
            get_reversion_local_field_dict(versions_d[1])['note'], 'SIT AMET')

        assert_equal(
            get_reversion_local_field_dict(versions_e[0])['last_name'],
            'Dolor')
        assert_equal(
            get_reversion_local_field_dict(versions_e[1])['last_name'],
            'DOLOR')

        anon.anonymize_obj(e, base_encryption_key=self.base_encryption_key)

        anon_versions_a: List[Version] = get_reversion_versions(
            e.topparenta_ptr).order_by('id')
        anon_versions_b: List[Version] = get_reversion_versions(
            e.parentb_ptr).order_by('id')
        anon_versions_c: List[Version] = get_reversion_versions(
            e.parentc_ptr).order_by('id')
        anon_versions_d: List[Version] = get_reversion_versions(
            e.extraparentd_ptr).order_by('id')
        anon_versions_e: List[Version] = get_reversion_versions(e).order_by(
            'id')
        anon_e = ChildE.objects.get(pk=e.pk)

        assert_not_equal(
            get_reversion_local_field_dict(anon_versions_a[0])['name'],
            'Lorem')
        assert_not_equal(
            get_reversion_local_field_dict(anon_versions_a[1])['name'],
            'LOREM')

        assert_not_equal(
            get_reversion_local_field_dict(anon_versions_b[0])['birth_date'],
            CUSTOMER__BIRTH_DATE)
        assert_not_equal(
            get_reversion_local_field_dict(anon_versions_b[1])['birth_date'],
            CUSTOMER__BIRTH_DATE + timedelta(days=2))

        assert_not_equal(
            get_reversion_local_field_dict(anon_versions_c[0])['first_name'],
            'Ipsum')
        assert_not_equal(
            get_reversion_local_field_dict(anon_versions_c[1])['first_name'],
            'IPSUM')

        assert_not_equal(
            get_reversion_local_field_dict(anon_versions_d[0])['note'],
            'sit Amet')
        assert_not_equal(
            get_reversion_local_field_dict(anon_versions_d[1])['note'],
            'SIT AMET')

        assert_not_equal(
            get_reversion_local_field_dict(anon_versions_e[0])['last_name'],
            'Dolor')
        assert_not_equal(
            get_reversion_local_field_dict(anon_versions_e[1])['last_name'],
            'DOLOR')

        anon.deanonymize_obj(anon_e,
                             base_encryption_key=self.base_encryption_key)

        deanon_versions_a: List[Version] = get_reversion_versions(
            e.topparenta_ptr).order_by('id')
        deanon_versions_b: List[Version] = get_reversion_versions(
            e.parentb_ptr).order_by('id')
        deanon_versions_c: List[Version] = get_reversion_versions(
            e.parentc_ptr).order_by('id')
        deanon_versions_d: List[Version] = get_reversion_versions(
            e.extraparentd_ptr).order_by('id')
        deanon_versions_e: List[Version] = get_reversion_versions(e).order_by(
            'id')

        assert_equal(
            get_reversion_local_field_dict(deanon_versions_a[0])['name'],
            'Lorem')
        assert_equal(
            get_reversion_local_field_dict(deanon_versions_a[1])['name'],
            'LOREM')

        assert_equal(
            get_reversion_local_field_dict(deanon_versions_b[0])['birth_date'],
            CUSTOMER__BIRTH_DATE)
        assert_equal(
            get_reversion_local_field_dict(deanon_versions_b[1])['birth_date'],
            CUSTOMER__BIRTH_DATE + timedelta(days=2))

        assert_equal(
            get_reversion_local_field_dict(deanon_versions_c[0])['first_name'],
            'Ipsum')
        assert_equal(
            get_reversion_local_field_dict(deanon_versions_c[1])['first_name'],
            'IPSUM')

        assert_equal(
            get_reversion_local_field_dict(deanon_versions_d[0])['note'],
            'sit Amet')
        assert_equal(
            get_reversion_local_field_dict(deanon_versions_d[1])['note'],
            'SIT AMET')

        assert_equal(
            get_reversion_local_field_dict(deanon_versions_e[0])['last_name'],
            'Dolor')
        assert_equal(
            get_reversion_local_field_dict(deanon_versions_e[1])['last_name'],
            'DOLOR')
    def test_reversion_anonymization(self):
        from reversion import revisions as reversion
        from reversion.models import Version
        from gdpr.utils import get_reversion_versions

        anon = ContactFormAnonymizer()
        anon.Meta.anonymize_reversion = True
        anon.Meta.reversible_anonymization = True

        user = User(username='******')
        user.save()

        with reversion.create_revision():
            form = ContactForm()
            form.email = CUSTOMER__EMAIL
            form.full_name = CUSTOMER__LAST_NAME
            form.save()

            reversion.set_user(user)

        with reversion.create_revision():
            form.email = CUSTOMER__EMAIL2
            form.save()

            reversion.set_user(user)

        with reversion.create_revision():
            form.email = CUSTOMER__EMAIL3
            form.save()

            reversion.set_user(user)

        versions: List[Version] = get_reversion_versions(form).order_by('id')

        self.assertEqual(versions[0].field_dict['email'], CUSTOMER__EMAIL)
        self.assertEqual(versions[1].field_dict['email'], CUSTOMER__EMAIL2)
        self.assertEqual(versions[2].field_dict['email'], CUSTOMER__EMAIL3)

        anon.anonymize_obj(form, base_encryption_key=self.base_encryption_key)

        anon_versions: List[Version] = get_reversion_versions(form).order_by(
            'id')
        anon_form = ContactForm.objects.get(pk=form.pk)

        self.assertNotEqual(anon_versions[0].field_dict['email'],
                            CUSTOMER__EMAIL)
        self.assertNotEqual(anon_versions[1].field_dict['email'],
                            CUSTOMER__EMAIL2)
        self.assertNotEqual(anon_versions[2].field_dict['email'],
                            CUSTOMER__EMAIL3)
        self.assertNotEqual(anon_form.email, CUSTOMER__EMAIL3)

        anon.deanonymize_obj(anon_form,
                             base_encryption_key=self.base_encryption_key)

        deanon_versions: List[Version] = get_reversion_versions(form).order_by(
            'id')
        deanon_form = ContactForm.objects.get(pk=form.pk)

        self.assertEqual(deanon_versions[0].field_dict['email'],
                         CUSTOMER__EMAIL)
        self.assertEqual(deanon_versions[1].field_dict['email'],
                         CUSTOMER__EMAIL2)
        self.assertEqual(deanon_versions[2].field_dict['email'],
                         CUSTOMER__EMAIL3)
        self.assertEqual(deanon_form.email, CUSTOMER__EMAIL3)
        self.assertDictEqual(versions[0].field_dict,
                             deanon_versions[0].field_dict)
        self.assertDictEqual(versions[1].field_dict,
                             deanon_versions[1].field_dict)
        self.assertDictEqual(versions[2].field_dict,
                             deanon_versions[2].field_dict)