Skip to content

Commit

Permalink
Merge 65c4cd0 into 8e45016
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesoutterside committed Mar 24, 2020
2 parents 8e45016 + 65c4cd0 commit e518914
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 24 deletions.
39 changes: 17 additions & 22 deletions gdpr_assist/models.py
Expand Up @@ -3,6 +3,7 @@
"""
from copy import copy
import six
import sys

from django.apps import apps
from django.db import models
Expand Down Expand Up @@ -47,9 +48,13 @@ def _cast_class(cls, queryset):
"""
# Make a subclass of PrivacyQuerySet and the original class
orig_cls = queryset.__class__
queryset.__class__ = type(
str('CastPrivacy{}'.format(orig_cls.__name__)), (cls, orig_cls), {},
)
new_cls_name = str('CastPrivacy{}'.format(orig_cls.__name__))
queryset.__class__ = type(new_cls_name, (cls, orig_cls), {},)

# add to current module
current_module = sys.modules[__name__]
setattr(current_module, new_cls_name, queryset.__class__)

return queryset


Expand Down Expand Up @@ -85,29 +90,19 @@ def _cast_class(cls, manager):
The new class is given the same name as the old class, but with the prefix
'CastPrivacy' to indicate the type of the object has changed, eg a normal
Manager will become CastPrivacyManager
Also add the new manager to the module, so it can be imported for migrations.
"""
# Make a subclass of PrivacyQuerySet and the original class
orig_cls = manager.__class__
manager.__class__ = type(
str('CastPrivacy{}'.format(orig_cls.__name__)), (cls, orig_cls), {},
)
return manager
new_cls_name = str('CastPrivacy{}'.format(orig_cls.__name__))
manager.__class__ = type(new_cls_name, (cls, orig_cls), {},)

def deconstruct(self):
"""
Deconstruct the original manager - it will be cast again next time.
"""
# Check bases are as expected from _cast_class
bases = self.__class__.__bases__
if len(bases) != 2: # pragma: no cover
raise ValueError('Unexpected base classes for CastPrivacyManager')

# Original is second - instatiate and deconstruct it
orig_cls = bases[1]
orig_args = self._constructor_args[0]
orig_kwargs = self._constructor_args[1]
orig_manager = orig_cls(*orig_args, **orig_kwargs)
return orig_manager.deconstruct()
# add to current module
current_module = sys.modules[__name__]
setattr(current_module, new_cls_name, manager.__class__)

return manager


class PrivacyMeta(object):
Expand Down
4 changes: 2 additions & 2 deletions tests/test_model_meta.py
Expand Up @@ -218,9 +218,9 @@ class TestRegisteredModelMigration(MigrationTestCase):
Check registered models can be migratated
"""
def test_manager_deconstruct__deconstructs(self):
# This should serialise to the original manager
# This should serialise to the privacy manager
string, imports = self.serialize(ModelWithPrivacyMeta.objects)
self.assertEqual(string, 'django.db.models.manager.Manager()')
self.assertEqual(string, 'gdpr_assist.models.CastPrivacyManager()')

# And check it serialises back
obj = self.serialize_round_trip(ModelWithPrivacyMeta.objects)
Expand Down

0 comments on commit e518914

Please sign in to comment.