def test_model_registered_manually_without_privacy_meta__meta_class_instance_added(self): gdpr_assist.register(ModelWithoutPrivacyMeta) self.assertTrue(hasattr(ModelWithoutPrivacyMeta, '_privacy_meta')) self.assertIsInstance( ModelWithoutPrivacyMeta._privacy_meta, PrivacyMeta, )
def test_register_again__raises_exception(self): with self.assertRaises(ValueError) as cm: gdpr_assist.register(ModelWithPrivacyMeta, ModelWithPrivacyMeta._privacy_meta) self.assertEqual( "Model tests_app.ModelWithPrivacyMeta already registered", str(cm.exception), )
def field_model_factory(model_name, field_instance, field_name="field"): cls = type( str(model_name.format(field_instance.__class__.__name__)), (models.Model, ), { "__module__": TargetModel.__module__, field_name: field_instance }, ) class PrivacyMeta: fields = [field_name] gdpr_assist.register(cls, PrivacyMeta) return cls
def test_manager_gdpr_default_manager_name_not_set(self): self._deregister() class UserPrivacyMeta: fields = ["username", "email"] with self.assertRaises(RuntimeError) as ex: gdpr_assist.register(User, UserPrivacyMeta) self.assertEqual( str(ex.exception), "Registered gdpr_assist model Users manager specified 'use_in_migrations=True', with no name provided." ) self.assertIsInstance(User.objects, UserManager) self.assertNotIsInstance(User.objects, PrivacyManager)
def field_model_factory(model_name, field_instance, field_name='field'): Model = type( str(model_name.format(field_instance.__class__.__name__)), (models.Model, ), { '__module__': TargetModel.__module__, field_name: field_instance, }, ) class PrivacyMeta: fields = [field_name] gdpr_assist.register(Model, PrivacyMeta) return Model
def _register(self, default_manager_name): class UserPrivacyMeta: fields = ["username", "email"] gdpr_assist.register(User, UserPrivacyMeta, default_manager_name)
def test_model_registered_manually_without_privacy_meta__meta_class_instance_added( self): gdpr_assist.register(self.model) self.assertTrue(hasattr(self.model, "_privacy_meta")) self.assertIsInstance(self.model._privacy_meta, PrivacyMeta)
def register(self, gdpr_default_manager_name=None): gdpr_assist.register(self.model, self.PrivacyMeta, gdpr_default_manager_name)
""" ModelAdmin for testing """ from django.contrib import admin from django.contrib.auth.models import User import gdpr_assist from .models import ModelWithPrivacyMeta class UserPrivacyMeta: fields = ["username", "email"] gdpr_assist.register(User, UserPrivacyMeta, gdpr_default_manager_name="abc") class PrivacyMetaAdmin(gdpr_assist.admin.ModelAdmin): pass admin.site.unregister(User) admin.site.register(ModelWithPrivacyMeta, PrivacyMetaAdmin) admin.site.register(User, PrivacyMetaAdmin)
from django.contrib.auth.models import User from django.db import models from django.db.models.signals import pre_delete from django.dispatch import receiver from gdpr_assist import register from gdpr_assist.deletion import ANONYMISE from gdpr_assist.signals import pre_anonymise class UserPrivacyMeta: fields = ["username", "email"] register(User, UserPrivacyMeta, gdpr_default_manager_name="objects_anonymised") class Person(models.Model): """ A model with PII """ name = models.CharField(max_length=255) email = models.EmailField() class Meta: verbose_name_plural = "People" class PrivacyMeta: fields = ["name", "email"]
def register(self): gdpr_assist.register(ModelWithoutPrivacyMeta, self.PrivacyMeta)