-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #135 from ryuusenshi/moderatedmodelimproved
Enhanced ModeratedModel: embedded Moderator support
- Loading branch information
Showing
4 changed files
with
135 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,88 @@ | ||
import six | ||
""" | ||
This module enables automatic Model registration with custom Moderators | ||
from . import moderation | ||
usage example: | ||
class MyModel(ModeratedModel): | ||
desc = models.TextField() | ||
class Moderator: | ||
notify_user = False | ||
""" | ||
import inspect | ||
|
||
from django.utils.six import with_metaclass | ||
from django.db.models import base | ||
|
||
from moderation.register import ModerationManager | ||
from moderation.moderator import GenericModerator | ||
from moderation.utils import clear_builtins | ||
from moderation.utils import django_14 | ||
|
||
moderation = ModerationManager() | ||
|
||
|
||
class ModeratedModelBase(type): | ||
""" | ||
Metaclass for the ``ModeratedModel`` type | ||
-- automatically registers ``ModeratedModel's`` | ||
-- resolves subclass ``Moderator`` into | ||
a instance of ``GenericModerator`` | ||
""" | ||
def _resolve_moderator(cls): | ||
""" | ||
``ModeratedModel`` that defines the class Moderator | ||
will have that class resolved into | ||
a class derived from ``GenericModerator`` | ||
usage example: | ||
class MyModel(ModeratedModel): | ||
desc = models.TextField() | ||
# ``Moderator`` below will extend ``GenericModerator`` | ||
# and will be used when the ``Model`` is registered | ||
class Moderator: | ||
notify_user = False | ||
""" | ||
if hasattr(cls, 'Moderator') and inspect.isclass(cls.Moderator): | ||
Moderator = cls.Moderator | ||
# in python3 __dict__ is dictproxy | ||
attrs = dict(Moderator.__dict__) | ||
attrs = clear_builtins(attrs) | ||
|
||
return type( | ||
'%sModerator' % cls.__name__, | ||
(GenericModerator,), | ||
attrs, | ||
) | ||
else: | ||
return None | ||
|
||
def __init__(cls, name, bases, clsdict): | ||
""" | ||
Registers ``ModeratedModel`` | ||
""" | ||
super(ModeratedModelBase, cls).__init__(name, bases, clsdict) | ||
|
||
if (any(x.__name__ == 'ModeratedModel' for x in cls.mro()[1:])): | ||
moderation.register(cls) | ||
moderation.register(cls, cls._resolve_moderator()) | ||
|
||
|
||
class ModelBase(ModeratedModelBase, base.ModelBase): | ||
pass | ||
|
||
""" | ||
Common metaclass for ``ModeratedModel`` enabling it to inherit | ||
the behavior of django ``Model`` objects | ||
class ModeratedModel(six.with_metaclass(ModelBase, base.Model)): | ||
""" | ||
|
||
class Meta: | ||
abstract = True | ||
if not django_14(): | ||
# django.utils.six.with_metaclass is broken in django < 1.5 | ||
class ModeratedModel(with_metaclass(ModelBase, base.Model)): | ||
class Meta: | ||
abstract = True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters