diff --git a/django_cms_tools/permissions.py b/django_cms_tools/permissions.py new file mode 100644 index 0000000..9fa7885 --- /dev/null +++ b/django_cms_tools/permissions.py @@ -0,0 +1,49 @@ +# coding: utf-8 + +""" + permission helpers + ~~~~~~~~~~~~~~~~~~ + + create 06.Sep.2017 by Jens Diemer +""" + + +from __future__ import unicode_literals, absolute_import, print_function + +import logging + +from django_tools.permissions import ModelPermissionMixin as ModelPermissionBaseMixin + + +log = logging.getLogger(__name__) + + +class ModelPermissionMixin(ModelPermissionBaseMixin): + """ + Helper for easy model permission checks. + + e.g.: + from django_cms_tools.permissions import ModelPermissionMixin + + class FooModel(ModelPermissionMixin, models.Model): + ... + + def view(request): + if FooModel.edit_mode_and_change_permission(request): + ... + """ + @classmethod + def edit_mode_and_change_permission(cls, request): + edit_mode = hasattr(request, 'toolbar') and request.toolbar.edit_mode + # edit_mode = request.user.is_staff and request.session.get('cms_edit', False) + + if not edit_mode: + # log.debug("Not in edit mode.") + return False + + user = request.user + if not cls.has_change_permission(user, raise_exception=False): + # log.debug("User has no change permissions.") + return False + + return True