diff --git a/CHANGELOG.txt b/CHANGELOG.txt index 0a375c2..362b2f7 100644 --- a/CHANGELOG.txt +++ b/CHANGELOG.txt @@ -3,6 +3,11 @@ - more tests: autopublisher, for example! +==== 0.1.20 (2021-06-27) === + +- management commands for easy cms/plugin auth group creation + + ==== 0.1.19 (2021-06-05) === - switched to github actions diff --git a/djangocms_misc/__init__.py b/djangocms_misc/__init__.py index 59a0d8c..5fd75cf 100644 --- a/djangocms_misc/__init__.py +++ b/djangocms_misc/__init__.py @@ -1,2 +1,2 @@ -__version__ = '0.1.19' +__version__ = '0.1.20' __author__ = 'benzkji' diff --git a/djangocms_misc/basic/management/commands/djangocms_misc_add_cms_group.py b/djangocms_misc/basic/management/commands/djangocms_misc_add_cms_group.py new file mode 100644 index 0000000..c634d98 --- /dev/null +++ b/djangocms_misc/basic/management/commands/djangocms_misc_add_cms_group.py @@ -0,0 +1,114 @@ +import logging +import os +import shutil + +from django.contrib.auth.models import Group, Permission +from django.core.management.base import BaseCommand +from django.db.models import Q + + +class Command(BaseCommand): + """ + """ + help = ( + "Add an auth.Group that gives basic CMS Permissions. User --plugin or --cms to add additional perms." + "") + + def add_arguments(self, parser): + parser.add_argument( + '--cms-base', + required=False, + action='store_const', + const=True, + default=False, + help="Basic CMS permissions (edit and publish pages, etc).") + parser.add_argument( + '--cms-publish', + required=False, + action='store_const', + const=True, + default=False, + help="adds exactly 'can publish page' permission)." + "") + parser.add_argument( + '--cms-superuser', + required=False, + action='store_const', + const=True, + default=False, + help="Superuser CMS permissions (add own users, page permissions, etc, etc)." + "") + parser.add_argument( + '--plugin', + required=False, + action='store_const', + const=True, + default=False, + help=""" +Searches for permissions with 'plugin' in their content type's app_label (ex.: cmsplugin_googlemaps), adds to group.""") + parser.add_argument( + '--cms', + action='store_const', + const=True, + default=False, + required=False, + help=""" +Searches for permissions with 'cms' in their content type's app_label (ex.:djangocms_text_ckeditor), adds to group.""") + parser.add_argument( + '--name', + required=False, + default='cms_base', + help=""" +Name of group. Defaults to cms_base.""") + parser.add_argument( + '--force', + action='store_const', + const=True, + required=False, + help=""" +Force overwrite of existing group and it's permissions.""") + + def handle(self, *args, **options): + group_name = options.get('name') + # copy + groups = Group.objects.filter(name=group_name) + if groups.count(): + if not options.get('force', None): + exit("use --force to overwrite existing groups!") + if groups.count() > 1: + exit("more than one group with name that name ({}) found! exiting.".format(group_name)) + group = groups.first() + else: + group = Group.objects.create(name=group_name) + + group.permissions.clear() + if options.get('cms_base'): + q_filter = Q(content_type__app_label='cms') + q_exclude = Q(name__icontains='user') + q_exclude |= Q(name__icontains='permission') + q_exclude |= Q(name__icontains='publish') + self.add_permission(group, q_filter, q_exclude) + if options.get('cms_publish'): + q_filter = Q(content_type__app_label='cms') + q_filter &= Q(name__icontains='publish') + self.add_permission(group, q_filter) + if options.get('cms_superuser'): + q_filter = Q(content_type__app_label='cms') + self.add_permission(group, q_filter) + if options.get('cms'): + q_filter = Q(content_type__app_label__icontains='cms') + q_exclude = Q(content_type__app_label='cms') + self.add_permission(group, q_filter, q_exclude) + if options.get('plugin'): + q_filter = Q(content_type__app_label__icontains='plugin') + q_exclude = Q(content_type__app_label='cms') + self.add_permission(group, q_filter, q_exclude) + + print(logging.INFO, 'Group created! ({})'.format(group_name)) + print(logging.INFO, 'Following permissions were granted:') + for perm in group.permissions.all(): + print(logging.INFO, '{}'.format(perm)) + + def add_permission(self, group, q_filter=Q(), q_exclude=Q()): + perms = Permission.objects.filter(q_filter).exclude(q_exclude) + group.permissions.add(*perms) diff --git a/djangocms_misc/basic/management/commands/djangocms_misc_add_groups.py b/djangocms_misc/basic/management/commands/djangocms_misc_add_groups.py new file mode 100644 index 0000000..f792df7 --- /dev/null +++ b/djangocms_misc/basic/management/commands/djangocms_misc_add_groups.py @@ -0,0 +1,34 @@ +import logging +import os +import shutil + +from django.contrib.auth.models import Group, Permission +from django.core.management import call_command +from django.core.management.base import BaseCommand +from django.db.models import Q + + +class Command(BaseCommand): + """ + """ + help = ( + "Add an basic set of auth.Groups for cms usage (cms-base, cms-publisher, cms-superuser, plugin-permissions) that gives basic CMS Permissions. User --plugin or --cms to add additional perms." + "") + + def add_arguments(self, parser): + parser.add_argument( + '--force', + action='store_const', + const=True, + required=False, + help=""" + Force overwrite of existing group and it's permissions.""") + + def handle(self, *args, **options): + args = [] + if options.get('force'): + args = ['--force', ] + call_command('djangocms_misc_add_cms_group', '--cms-base', *args) + call_command('djangocms_misc_add_cms_group', '--cms-publish', '--name=cms_publisher', *args) + call_command('djangocms_misc_add_cms_group', '--cms-superuser', '--name=cms_superuser', *args) + call_command('djangocms_misc_add_cms_group', '--cms', '--plugin', '--name=cms_plugin_permissions', *args) diff --git a/djangocms_misc/tests/settings.py b/djangocms_misc/tests/settings.py index b679391..4439cdd 100644 --- a/djangocms_misc/tests/settings.py +++ b/djangocms_misc/tests/settings.py @@ -117,6 +117,8 @@ ('base.html', 'Default'), ) +CMS_PERMISSION = True + COVERAGE_REPORT_HTML_OUTPUT_DIR = os.path.join( os.path.join(APP_ROOT, 'tests/coverage')) COVERAGE_MODULE_EXCLUDES = [ diff --git a/release.txt b/release.txt index 5c3d56c..2628abe 100644 --- a/release.txt +++ b/release.txt @@ -6,6 +6,8 @@ CHANGELOG.txt git flow release finish 0.xxx git push --all; git push --tags git push upstream --all; git push upstream --tags + +# no more, is in github action python setup.py sdist && python setup.py bdist_wheel --universal twine upload dist/* -r pypitest twine upload dist/*