diff --git a/djangocms_alias/__init__.py b/djangocms_alias/__init__.py index b9c36b38..f84c53b0 100644 --- a/djangocms_alias/__init__.py +++ b/djangocms_alias/__init__.py @@ -1,3 +1 @@ -__version__ = '1.11.0' - -default_app_config = 'djangocms_alias.apps.AliasConfig' +__version__ = "1.11.0" diff --git a/djangocms_alias/admin.py b/djangocms_alias/admin.py index 1b9ceaa9..79df6898 100644 --- a/djangocms_alias/admin.py +++ b/djangocms_alias/admin.py @@ -22,28 +22,44 @@ __all__ = [ - 'AliasAdmin', - 'CategoryAdmin', - 'AliasContentAdmin', + "AliasAdmin", + "CategoryAdmin", + "AliasContentAdmin", ] alias_content_admin_classes = [admin.ModelAdmin] -alias_content_admin_list_display = ('name', 'get_category',) -alias_content_admin_list_filter = (SiteFilter, CategoryFilter, LanguageFilter,) +alias_content_admin_list_display = ( + "name", + "get_category", +) +alias_content_admin_list_filter = ( + SiteFilter, + CategoryFilter, + LanguageFilter, +) djangocms_versioning_enabled = AliasCMSConfig.djangocms_versioning_enabled if djangocms_versioning_enabled: from djangocms_versioning.admin import ExtendedVersionAdminMixin from .filters import UnpublishedFilter + alias_content_admin_classes.insert(0, ExtendedVersionAdminMixin) - alias_content_admin_list_display = ('name', 'get_category',) - alias_content_admin_list_filter = (SiteFilter, CategoryFilter, LanguageFilter, UnpublishedFilter) + alias_content_admin_list_display = ( + "name", + "get_category", + ) + alias_content_admin_list_filter = ( + SiteFilter, + CategoryFilter, + LanguageFilter, + UnpublishedFilter, + ) @admin.register(Category) class CategoryAdmin(TranslatableAdmin): - list_display = ['name'] + list_display = ["name"] def save_model(self, request, obj, form, change): change = not obj._state.adding @@ -59,10 +75,10 @@ def save_model(self, request, obj, form, change): @admin.register(Alias) class AliasAdmin(admin.ModelAdmin): - list_display = ['name', 'category'] - list_filter = ['site', 'category'] - fields = ('category', 'site') - readonly_fields = ('static_code', ) + list_display = ["name", "category"] + list_filter = ["site", "category"] + fields = ("category", "site") + readonly_fields = ("static_code",) def get_urls(self): return urlpatterns + super().get_urls() @@ -76,16 +92,21 @@ def has_delete_permission(self, request, obj=None): if obj: if not obj.is_in_use: return request.user.has_perm( - get_model_permission_codename(self.model, 'add'), + get_model_permission_codename(self.model, "add"), ) return request.user.is_superuser return False def get_deleted_objects(self, objs, request): - deleted_objects, model_count, perms_needed, protected = super().get_deleted_objects(objs, request) + ( + deleted_objects, + model_count, + perms_needed, + protected, + ) = super().get_deleted_objects(objs, request) # This is bad and I should feel bad. - if 'placeholder' in perms_needed: - perms_needed.remove('placeholder') + if "placeholder" in perms_needed: + perms_needed.remove("placeholder") return deleted_objects, model_count, perms_needed, protected def save_model(self, request, obj, form, change): @@ -118,12 +139,16 @@ class AliasContentAdmin(*alias_content_admin_classes): def get_queryset(self, request): queryset = super().get_queryset(request) # Force the category set to Lower, to be able to sort the category in ascending/descending order - queryset = queryset.annotate(alias_category_translations_ordered=Lower("alias__category__translations__name")) + queryset = queryset.annotate( + alias_category_translations_ordered=Lower( + "alias__category__translations__name" + ) + ) return queryset # Add Alias category in the admin manager list and order field @admin.display( - description=_('category'), + description=_("category"), ordering="alias_category_translations_ordered", ) def get_category(self, obj): @@ -172,8 +197,9 @@ def get_list_display_links(self, request, list_display): return super().get_list_display_links(request, list_display) def _get_rename_alias_link(self, obj, request, disabled=False): - url = admin_reverse('{}_{}_change'.format( - obj._meta.app_label, obj._meta.model_name), args=(obj.pk,) + url = admin_reverse( + "{}_{}_change".format(obj._meta.app_label, obj._meta.model_name), + args=(obj.pk,), ) return render_to_string( "admin/djangocms_alias/icons/rename_alias.html", @@ -188,8 +214,9 @@ def _get_alias_usage_link(self, obj, request, disabled=False): ) def _get_change_alias_settings_link(self, obj, request, disabled=False): - url = admin_reverse('{}_{}_change'.format( - obj._meta.app_label, obj.alias._meta.model_name), args=(obj.alias.pk,) + url = admin_reverse( + "{}_{}_change".format(obj._meta.app_label, obj.alias._meta.model_name), + args=(obj.alias.pk,), ) return render_to_string( "admin/djangocms_alias/icons/change_alias_settings.html", @@ -213,10 +240,13 @@ def _get_preview_link(self, obj, request, disabled=False): {"url": preview_url, "disabled": disabled, "keepsideframe": False}, ) - def change_view(self, request, object_id, form_url='', extra_context=None): + def change_view(self, request, object_id, form_url="", extra_context=None): extra_context = extra_context or {} # Provide additional context to the changeform - extra_context['is_versioning_enabled'] = is_versioning_enabled() + extra_context["is_versioning_enabled"] = is_versioning_enabled() return super().change_view( - request, object_id, form_url, extra_context=extra_context, + request, + object_id, + form_url, + extra_context=extra_context, ) diff --git a/djangocms_alias/apps.py b/djangocms_alias/apps.py index 9b7374ad..bdb05b64 100644 --- a/djangocms_alias/apps.py +++ b/djangocms_alias/apps.py @@ -3,5 +3,5 @@ class AliasConfig(AppConfig): - name = 'djangocms_alias' - verbose_name = _('django CMS Alias') + name = "djangocms_alias" + verbose_name = _("django CMS Alias") diff --git a/djangocms_alias/cms_config.py b/djangocms_alias/cms_config.py index 6a5629cc..fcd5806a 100644 --- a/djangocms_alias/cms_config.py +++ b/djangocms_alias/cms_config.py @@ -8,7 +8,7 @@ try: - apps.get_app_config('djangocms_internalsearch') + apps.get_app_config("djangocms_internalsearch") from .internal_search import AliasContentConfig except (ImportError, LookupError): AliasContentConfig = None @@ -27,12 +27,13 @@ class AliasCMSConfig(CMSAppConfig): moderated_models = [AliasContent] djangocms_moderation_enabled = getattr( - settings, 'MODERATING_ALIAS_MODELS_ENABLED', True) + settings, "MODERATING_ALIAS_MODELS_ENABLED", True + ) djangocms_versioning_enabled = getattr( - settings, 'VERSIONING_ALIAS_MODELS_ENABLED', True) + settings, "VERSIONING_ALIAS_MODELS_ENABLED", True + ) if djangocms_versioning_enabled and djangocms_versioning_installed: - from cms.utils.i18n import get_language_tuple from djangocms_versioning.datastructures import VersionableItem @@ -40,7 +41,7 @@ class AliasCMSConfig(CMSAppConfig): versioning = [ VersionableItem( content_model=AliasContent, - grouper_field_name='alias', + grouper_field_name="alias", extra_grouping_fields=["language"], version_list_filter_lookups={"language": get_language_tuple}, copy_function=copy_alias_content, @@ -49,9 +50,10 @@ class AliasCMSConfig(CMSAppConfig): ] djangocms_references_enabled = getattr( - settings, 'REFERENCES_ALIAS_MODELS_ENABLED', True) + settings, "REFERENCES_ALIAS_MODELS_ENABLED", True + ) reference_fields = [ - (AliasPlugin, 'alias'), + (AliasPlugin, "alias"), ] # Internalsearch configuration diff --git a/djangocms_alias/cms_menus.py b/djangocms_alias/cms_menus.py index 96c72124..bfad7a62 100644 --- a/djangocms_alias/cms_menus.py +++ b/djangocms_alias/cms_menus.py @@ -9,9 +9,8 @@ class AliasDisableMenu(Modifier): """Disable menu rendering on alias pages""" def modify(self, request, nodes, namespace, root_id, post_cut, breadcrumb): - if ( - request.toolbar.app_name == PLUGIN_URL_NAME_PREFIX - or isinstance(request.toolbar.obj, AliasContent) + if request.toolbar.app_name == PLUGIN_URL_NAME_PREFIX or isinstance( + request.toolbar.obj, AliasContent ): return [] return nodes diff --git a/djangocms_alias/cms_plugins.py b/djangocms_alias/cms_plugins.py index b808da9e..d4091856 100644 --- a/djangocms_alias/cms_plugins.py +++ b/djangocms_alias/cms_plugins.py @@ -20,13 +20,13 @@ __all__ = [ - 'Alias', + "Alias", ] @plugin_pool.register_plugin class Alias(CMSPluginBase): - name = _('Alias') + name = _("Alias") model = AliasPlugin form = AliasPluginForm @@ -35,8 +35,8 @@ def get_render_template(self, context, instance, placeholder): isinstance(instance.placeholder.source, AliasContent) and instance.is_recursive() ): - return 'djangocms_alias/alias_recursive.html' - return 'djangocms_alias/{}/alias.html'.format(instance.template) + return "djangocms_alias/alias_recursive.html" + return f"djangocms_alias/{instance.template}/alias.html" @classmethod def get_extra_plugin_menu_items(cls, request, plugin): @@ -49,10 +49,10 @@ def get_extra_plugin_menu_items(cls, request, plugin): plugin_menu_items = [ PluginMenuItem( - _('Edit Alias'), + _("Edit Alias"), edit_endpoint, - action='sideframe', - attributes={'icon': 'alias'}, + action="sideframe", + attributes={"icon": "alias"}, ), ] @@ -63,42 +63,42 @@ def get_extra_plugin_menu_items(cls, request, plugin): ): plugin_menu_items.append( PluginMenuItem( - _('Detach Alias'), + _("Detach Alias"), detach_endpoint, - action='modal', - attributes={'icon': 'alias'}, + action="modal", + attributes={"icon": "alias"}, ) ) return plugin_menu_items data = { - 'plugin': plugin.pk, - 'language': get_language_from_request(request, check_path=True), + "plugin": plugin.pk, + "language": get_language_from_request(request, check_path=True), } endpoint = add_url_parameters(admin_reverse(CREATE_ALIAS_URL_NAME), **data) return [ PluginMenuItem( - _('Create Alias'), + _("Create Alias"), endpoint, - action='modal', - attributes={'icon': 'alias'}, + action="modal", + attributes={"icon": "alias"}, ), ] @classmethod def get_extra_placeholder_menu_items(cls, request, placeholder): data = { - 'placeholder': placeholder.pk, - 'language': get_language_from_request(request, check_path=True), + "placeholder": placeholder.pk, + "language": get_language_from_request(request, check_path=True), } endpoint = add_url_parameters(admin_reverse(CREATE_ALIAS_URL_NAME), **data) menu_items = [ PluginMenuItem( - _('Create Alias'), + _("Create Alias"), endpoint, - action='modal', - attributes={'icon': 'alias'}, + action="modal", + attributes={"icon": "alias"}, ), ] return menu_items @@ -106,7 +106,7 @@ def get_extra_placeholder_menu_items(cls, request, placeholder): @classmethod def can_create_alias(cls, user, plugins=None, replace=False): if not user.has_perm( - get_model_permission_codename(AliasModel, 'add'), + get_model_permission_codename(AliasModel, "add"), ): return False @@ -114,9 +114,8 @@ def can_create_alias(cls, user, plugins=None, replace=False): return True elif replace: target_placeholder = plugins[0].placeholder - if ( - not target_placeholder.check_source(user) - or not has_plugin_permission(user, Alias.__name__, 'add') + if not target_placeholder.check_source(user) or not has_plugin_permission( + user, Alias.__name__, "add" ): return False @@ -124,8 +123,9 @@ def can_create_alias(cls, user, plugins=None, replace=False): has_plugin_permission( user, plugin.plugin_type, - 'add', - ) for plugin in plugins + "add", + ) + for plugin in plugins ) @classmethod @@ -134,8 +134,9 @@ def can_detach(cls, user, target_placeholder, plugins): has_plugin_permission( user, plugin.plugin_type, - 'add', - ) for plugin in plugins + "add", + ) + for plugin in plugins ) and target_placeholder.check_source(user) @classmethod diff --git a/djangocms_alias/cms_toolbars.py b/djangocms_alias/cms_toolbars.py index 9ba34c32..100f72c1 100644 --- a/djangocms_alias/cms_toolbars.py +++ b/djangocms_alias/cms_toolbars.py @@ -32,19 +32,19 @@ __all__ = [ - 'AliasToolbar', + "AliasToolbar", ] -ALIAS_MENU_IDENTIFIER = 'alias' -ALIAS_MENU_CREATE_IDENTIFIER = 'alias-add' -ALIAS_LANGUAGE_BREAK = 'alias-language' +ALIAS_MENU_IDENTIFIER = "alias" +ALIAS_MENU_CREATE_IDENTIFIER = "alias-add" +ALIAS_LANGUAGE_BREAK = "alias-language" @toolbar_pool.register class AliasToolbar(CMSToolbar): - name = _('Alias') - plural_name = _('Aliases') + name = _("Alias") + plural_name = _("Aliases") def populate(self): self.add_aliases_link_to_admin_menu() @@ -59,7 +59,7 @@ def post_template_populate(self): self.enable_create_wizard_button() def add_aliases_link_to_admin_menu(self): - if not self.request.user.has_perm('djangocms_alias.change_category'): + if not self.request.user.has_perm("djangocms_alias.change_category"): return admin_menu = self.toolbar.get_or_create_menu(ADMIN_MENU_IDENTIFIER) @@ -79,27 +79,27 @@ def add_alias_menu(self): ) can_change = self.request.user.has_perm( - get_model_permission_codename(Alias, 'change'), + get_model_permission_codename(Alias, "change"), ) disabled = not can_change or not self.toolbar.edit_mode_active alias_menu.add_modal_item( - _('Change alias settings'), + _("Change alias settings"), url=admin_reverse( - 'djangocms_alias_alias_change', + "djangocms_alias_alias_change", args=[self.toolbar.obj.alias_id], ), disabled=disabled, ) alias_menu.add_modal_item( - _('Rename alias'), + _("Rename alias"), url=admin_reverse( - 'djangocms_alias_aliascontent_change', + "djangocms_alias_aliascontent_change", args=[self.toolbar.obj.pk], ), disabled=disabled, ) alias_menu.add_modal_item( - _('View usage'), + _("View usage"), url=admin_reverse( USAGE_ALIAS_URL_NAME, args=[self.toolbar.obj.alias_id], @@ -109,10 +109,10 @@ def add_alias_menu(self): # Only show deletion if versioning is not enabled if not is_versioning_enabled(): alias_menu.add_modal_item( - _('Delete alias'), + _("Delete alias"), url=admin_reverse( DELETE_ALIAS_URL_NAME, - args=(self.toolbar.obj.alias_id, ), + args=(self.toolbar.obj.alias_id,), ), on_close=admin_reverse( LIST_ALIASCONTENT_URL_NAME, @@ -135,7 +135,7 @@ def get_insert_position(cls, admin_menu, item_name): start = admin_menu.find_first(Break, identifier=SHORTCUTS_BREAK) end = admin_menu.find_first(Break, identifier=ADMINISTRATION_BREAK) - items = admin_menu.get_items()[start.index + 1: end.index] + items = admin_menu.get_items()[start.index + 1 : end.index] for idx, item in enumerate(items): try: if force_str(item_name.lower()) < force_str(item.name.lower()): # noqa: E501 @@ -147,21 +147,17 @@ def get_insert_position(cls, admin_menu, item_name): def enable_create_wizard_button(self): button_lists = [ - result.item - for result in self.toolbar.find_items(item_type=ButtonList) + result.item for result in self.toolbar.find_items(item_type=ButtonList) ] buttons = list( # flatten the list - itertools.chain.from_iterable([ - item.buttons - for item in button_lists - ]) + itertools.chain.from_iterable([item.buttons for item in button_lists]) ) # There will always be this button, because we are in the context of # alias app views create_wizard_button = [ - button for button in buttons if button.name == gettext('Create') + button for button in buttons if button.name == gettext("Create") ][0] from cms.wizards.wizard_pool import entry_choices @@ -175,7 +171,7 @@ def enable_create_wizard_button(self): create_wizard_button.disabled = not enable_create_wizard_button def override_language_switcher(self): - language_menu = self.toolbar.get_menu(LANGUAGE_MENU_IDENTIFIER, _('Language')) + language_menu = self.toolbar.get_menu(LANGUAGE_MENU_IDENTIFIER, _("Language")) # Remove all existing language links # remove_item uses `items` attribute so we have to copy object for _item in copy(language_menu.items): @@ -191,12 +187,14 @@ def override_language_switcher(self): if alias_content: with force_language(code): url = alias_content.get_absolute_url() - language_menu.add_link_item(name, url=url, active=self.current_lang == code) + language_menu.add_link_item( + name, url=url, active=self.current_lang == code + ) def change_language_menu(self): if self.toolbar.edit_mode_active and isinstance(self.toolbar.obj, AliasContent): can_change = self.request.user.has_perm( - get_model_permission_codename(Alias, 'change'), + get_model_permission_codename(Alias, "change"), ) else: can_change = False @@ -219,7 +217,9 @@ def change_language_menu(self): copy = [ (code, name) for code, name in languages.items() - if code != self.current_lang and (code, name) in remove and current_placeholder + if code != self.current_lang + and (code, name) in remove + and current_placeholder ] if add or remove or copy: @@ -227,19 +227,21 @@ def change_language_menu(self): if add: add_plugins_menu = language_menu.get_or_create_menu( - '{0}-add'.format(LANGUAGE_MENU_IDENTIFIER), - _('Add Translation'), + f"{LANGUAGE_MENU_IDENTIFIER}-add", + _("Add Translation"), ) - add_url = admin_reverse('djangocms_alias_aliascontent_add') + add_url = admin_reverse("djangocms_alias_aliascontent_add") for code, name in add: - url = add_url_parameters(add_url, language=code, alias=alias_content.alias_id) + url = add_url_parameters( + add_url, language=code, alias=alias_content.alias_id + ) add_plugins_menu.add_modal_item(name, url=url) if remove: remove_plugins_menu = language_menu.get_or_create_menu( - '{0}-del'.format(LANGUAGE_MENU_IDENTIFIER), - _('Delete Translation'), + f"{LANGUAGE_MENU_IDENTIFIER}-del", + _("Delete Translation"), ) disabled = len(remove) == 1 for code, name in remove: @@ -248,7 +250,7 @@ def change_language_menu(self): show_draft_content=True, ) translation_delete_url = admin_reverse( - 'djangocms_alias_aliascontent_delete', + "djangocms_alias_aliascontent_delete", args=(alias_content.pk,), ) url = add_url_parameters(translation_delete_url, language=code) @@ -256,16 +258,15 @@ def change_language_menu(self): if copy: copy_plugins_menu = language_menu.get_or_create_menu( - '{0}-copy'.format(LANGUAGE_MENU_IDENTIFIER), - _('Copy all plugins') + f"{LANGUAGE_MENU_IDENTIFIER}-copy", _("Copy all plugins") ) - title = _('from %s') - question = _('Are you sure you want to copy all plugins from %s?') + title = _("from %s") + question = _("Are you sure you want to copy all plugins from %s?") try: - copy_url = admin_reverse('cms_placeholder_copy_plugins') + copy_url = admin_reverse("cms_placeholder_copy_plugins") except NoReverseMatch: - copy_url = admin_reverse('djangocms_alias_alias_copy_plugins') + copy_url = admin_reverse("djangocms_alias_alias_copy_plugins") for code, name in copy: source_placeholder = alias_content.alias.get_placeholder( @@ -276,11 +277,11 @@ def change_language_menu(self): title % name, action=copy_url, data={ - 'source_language': code, - 'source_placeholder_id': source_placeholder.pk, - 'target_language': self.current_lang, - 'target_placeholder_id': current_placeholder.pk, + "source_language": code, + "source_placeholder_id": source_placeholder.pk, + "target_language": self.current_lang, + "target_placeholder_id": current_placeholder.pk, }, question=question % name, - on_success=self.toolbar.REFRESH_PAGE + on_success=self.toolbar.REFRESH_PAGE, ) diff --git a/djangocms_alias/cms_wizards.py b/djangocms_alias/cms_wizards.py index 55620c88..45573b6e 100644 --- a/djangocms_alias/cms_wizards.py +++ b/djangocms_alias/cms_wizards.py @@ -10,32 +10,30 @@ class CreateAliasWizard(Wizard): - def user_has_add_permission(self, user, **kwargs): return Alias.can_create_alias(user) class CreateAliasCategoryWizard(Wizard): - def user_has_add_permission(self, user, **kwargs): return user.has_perm( - get_model_permission_codename(Category, 'add'), + get_model_permission_codename(Category, "add"), ) create_alias_wizard = CreateAliasWizard( - title=_('New alias'), + title=_("New alias"), weight=200, form=CreateAliasWizardForm, model=AliasModel, - description=_('Create a new alias.'), + description=_("Create a new alias."), ) create_alias_category_wizard = CreateAliasCategoryWizard( - title=_('New alias category'), + title=_("New alias category"), weight=200, form=CreateCategoryWizardForm, model=Category, - description=_('Create a new alias category.'), + description=_("Create a new alias category."), ) wizard_pool.register(create_alias_wizard) diff --git a/djangocms_alias/compat.py b/djangocms_alias/compat.py index c1a77c60..103cff6d 100644 --- a/djangocms_alias/compat.py +++ b/djangocms_alias/compat.py @@ -4,4 +4,4 @@ DJANGO_VERSION = get_version() -DJANGO_4_0 = LooseVersion(DJANGO_VERSION) < LooseVersion('4.1') +DJANGO_4_0 = LooseVersion(DJANGO_VERSION) < LooseVersion("4.1") diff --git a/djangocms_alias/constants.py b/djangocms_alias/constants.py index e3bb5a13..650e109f 100644 --- a/djangocms_alias/constants.py +++ b/djangocms_alias/constants.py @@ -1,16 +1,16 @@ -PLUGIN_URL_NAME_PREFIX = 'djangocms_alias' +PLUGIN_URL_NAME_PREFIX = "djangocms_alias" # Alias Url endpoint names -CREATE_ALIAS_URL_NAME = f'{PLUGIN_URL_NAME_PREFIX}_create' -DELETE_ALIAS_URL_NAME = f'{PLUGIN_URL_NAME_PREFIX}_delete' -DETACH_ALIAS_PLUGIN_URL_NAME = f'{PLUGIN_URL_NAME_PREFIX}_detach_plugin' -CATEGORY_LIST_URL_NAME = f'{PLUGIN_URL_NAME_PREFIX}_category_changelist' -CHANGE_CATEGORY_URL_NAME = f'{PLUGIN_URL_NAME_PREFIX}_category_change' -SELECT2_ALIAS_URL_NAME = f'{PLUGIN_URL_NAME_PREFIX}_select2' -USAGE_ALIAS_URL_NAME = f'{PLUGIN_URL_NAME_PREFIX}_alias_usage' -LIST_ALIASCONTENT_URL_NAME = f'{PLUGIN_URL_NAME_PREFIX}_aliascontent_changelist' -CHANGE_ALIASCONTENT_URL_NAME = f'{PLUGIN_URL_NAME_PREFIX}_aliascontent_change' -CATEGORY_SELECT2_URL_NAME = f'{PLUGIN_URL_NAME_PREFIX}_category_list_select2' +CREATE_ALIAS_URL_NAME = f"{PLUGIN_URL_NAME_PREFIX}_create" +DELETE_ALIAS_URL_NAME = f"{PLUGIN_URL_NAME_PREFIX}_delete" +DETACH_ALIAS_PLUGIN_URL_NAME = f"{PLUGIN_URL_NAME_PREFIX}_detach_plugin" +CATEGORY_LIST_URL_NAME = f"{PLUGIN_URL_NAME_PREFIX}_category_changelist" +CHANGE_CATEGORY_URL_NAME = f"{PLUGIN_URL_NAME_PREFIX}_category_change" +SELECT2_ALIAS_URL_NAME = f"{PLUGIN_URL_NAME_PREFIX}_select2" +USAGE_ALIAS_URL_NAME = f"{PLUGIN_URL_NAME_PREFIX}_alias_usage" +LIST_ALIASCONTENT_URL_NAME = f"{PLUGIN_URL_NAME_PREFIX}_aliascontent_changelist" +CHANGE_ALIASCONTENT_URL_NAME = f"{PLUGIN_URL_NAME_PREFIX}_aliascontent_change" +CATEGORY_SELECT2_URL_NAME = f"{PLUGIN_URL_NAME_PREFIX}_category_list_select2" # Static Alias DEFAULT_STATIC_ALIAS_CATEGORY_NAME = "Static Alias" diff --git a/djangocms_alias/filters.py b/djangocms_alias/filters.py index 4d399d4d..df9f1cd7 100644 --- a/djangocms_alias/filters.py +++ b/djangocms_alias/filters.py @@ -91,9 +91,9 @@ class CategoryFilter(admin.SimpleListFilter): def lookups(self, request, model_admin): qs = model_admin.get_queryset(request) - cat_id = qs.values_list('alias__category', flat=True).distinct() + cat_id = qs.values_list("alias__category", flat=True).distinct() # Ensure the category is ordered by the name alphabetically by default - cat = Category.objects.filter(pk__in=cat_id).order_by('translations__name') + cat = Category.objects.filter(pk__in=cat_id).order_by("translations__name") for obj in cat: yield str(obj.pk), smart_str(obj) @@ -138,7 +138,9 @@ def queryset(self, request, queryset): def choices(self, changelist): yield { "selected": self.value() is None, - "query_string": changelist.get_query_string(remove=[self.parameter_name]), + "query_string": changelist.get_query_string( + remove=[self.parameter_name] + ), "display": _("Hide"), } for lookup, title in self.lookup_choices: diff --git a/djangocms_alias/forms.py b/djangocms_alias/forms.py index ab1fa61a..25422fc0 100644 --- a/djangocms_alias/forms.py +++ b/djangocms_alias/forms.py @@ -24,35 +24,35 @@ __all__ = [ - 'AliasPluginForm', - 'BaseCreateAliasForm', - 'CreateAliasForm', - 'CreateAliasWizardForm', - 'CreateCategoryWizardForm', + "AliasPluginForm", + "BaseCreateAliasForm", + "CreateAliasForm", + "CreateAliasWizardForm", + "CreateCategoryWizardForm", ] def get_category_widget(formfield, user): - dbfield = AliasModel._meta.get_field('category') + dbfield = AliasModel._meta.get_field("category") return RelatedFieldWidgetWrapper( formfield.widget, dbfield.remote_field, admin_site=admin.site, can_add_related=user.has_perm( - get_model_permission_codename(Category, 'add'), + get_model_permission_codename(Category, "add"), ), can_change_related=user.has_perm( - get_model_permission_codename(Category, 'change'), + get_model_permission_codename(Category, "change"), ), can_delete_related=user.has_perm( - get_model_permission_codename(Category, 'delete'), + get_model_permission_codename(Category, "delete"), ), ) class BaseCreateAliasForm(forms.Form): plugin = forms.ModelChoiceField( - queryset=CMSPlugin.objects.exclude(plugin_type='Alias'), + queryset=CMSPlugin.objects.exclude(plugin_type="Alias"), required=False, widget=forms.HiddenInput(), ) @@ -66,19 +66,19 @@ class BaseCreateAliasForm(forms.Form): def clean(self): cleaned_data = super().clean() - plugin = cleaned_data.get('plugin') - placeholder = cleaned_data.get('placeholder') + plugin = cleaned_data.get("plugin") + placeholder = cleaned_data.get("placeholder") if not plugin and not placeholder: raise forms.ValidationError( - _('A plugin or placeholder is required to create an alias.') + _("A plugin or placeholder is required to create an alias.") ) if plugin and placeholder: raise forms.ValidationError( _( - 'An alias can only be created from a plugin or placeholder, ' # noqa: E501 - 'not both.' + "An alias can only be created from a plugin or placeholder, " # noqa: E501 + "not both." ) ) @@ -96,18 +96,18 @@ class CreateAliasForm(BaseCreateAliasForm): required=True, ) replace = forms.BooleanField( - label=_('Replace current plugin'), - help_text=_('Replace current plugin with alias'), + label=_("Replace current plugin"), + help_text=_("Replace current plugin with alias"), required=False, ) def __init__(self, *args, **kwargs): - self.user = kwargs.pop('user') + self.user = kwargs.pop("user") super().__init__(*args, **kwargs) - if not has_plugin_permission(self.user, 'Alias', 'add'): - self.fields['replace'].widget = forms.HiddenInput() + if not has_plugin_permission(self.user, "Alias", "add"): + self.fields["replace"].widget = forms.HiddenInput() self.set_category_widget(self.user) self.fields["site"].initial = get_current_site() @@ -116,27 +116,27 @@ def clean(self): cleaned_data = super().clean() if AliasContent.objects.filter( - name=cleaned_data.get('name'), - language=cleaned_data.get('language'), - alias__category=cleaned_data.get('category'), + name=cleaned_data.get("name"), + language=cleaned_data.get("language"), + alias__category=cleaned_data.get("category"), ).exists(): raise forms.ValidationError( - _('Alias with this Name and Category already exists.') + _("Alias with this Name and Category already exists.") ) return cleaned_data def set_category_widget(self, user): - formfield = self.fields['category'] + formfield = self.fields["category"] formfield.widget = get_category_widget(formfield, user) def get_plugins(self): - plugin = self.cleaned_data.get('plugin') - placeholder = self.cleaned_data.get('placeholder') + plugin = self.cleaned_data.get("plugin") + placeholder = self.cleaned_data.get("placeholder") if placeholder: plugins = placeholder.get_plugins( - self.cleaned_data.get('language'), + self.cleaned_data.get("language"), ) else: plugins = [plugin] + list(plugin.get_descendants()) @@ -144,20 +144,21 @@ def get_plugins(self): def save(self): alias = AliasModel.objects.create( - category=self.cleaned_data.get('category'), - site=self.cleaned_data.get('site'), + category=self.cleaned_data.get("category"), + site=self.cleaned_data.get("site"), ) alias_content = AliasContent.objects.create( alias=alias, - name=self.cleaned_data.get('name'), - language=self.cleaned_data.get('language'), + name=self.cleaned_data.get("name"), + language=self.cleaned_data.get("language"), ) if is_versioning_enabled(): from djangocms_versioning.models import Version + Version.objects.create(content=alias_content, created_by=self.user) - if self.cleaned_data.get('replace'): - placeholder = self.cleaned_data.get('placeholder') - plugin = self.cleaned_data.get('plugin') + if self.cleaned_data.get("replace"): + placeholder = self.cleaned_data.get("placeholder") + plugin = self.cleaned_data.get("plugin") source_plugins = None else: placeholder, plugin = None, None @@ -172,9 +173,7 @@ def save(self): class CreateAliasWizardForm(forms.Form): name = forms.CharField( - label=_('Name'), - required=True, - widget=AdminTextInputWidget() + label=_("Name"), required=True, widget=AdminTextInputWidget() ) site = forms.ModelChoiceField( queryset=Site.objects.all(), @@ -187,29 +186,30 @@ class CreateAliasWizardForm(forms.Form): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) - if not getattr(self, 'user', None): + if not getattr(self, "user", None): self.user = self._request.user self.set_category_widget(self.user) self.fields["site"].initial = get_current_site() def set_category_widget(self, user): - formfield = self.fields['category'] + formfield = self.fields["category"] formfield.widget = get_category_widget(formfield, user) @transaction.atomic def save(self): alias = AliasModel.objects.create( - category=self.cleaned_data.get('category'), - site=self.cleaned_data.get('site'), + category=self.cleaned_data.get("category"), + site=self.cleaned_data.get("site"), ) alias_content = AliasContent.objects.create( alias=alias, - name=self.cleaned_data.get('name'), + name=self.cleaned_data.get("name"), language=self.language_code, ) if is_versioning_enabled(): from djangocms_versioning.models import Version + Version.objects.create(content=alias_content, created_by=self._request.user) emit_content_change([alias_content]) @@ -217,25 +217,23 @@ def save(self): class CreateCategoryWizardForm(TranslatableModelForm): - class Meta: model = Category fields = [ - 'name', + "name", ] class Select2Mixin: - class Media: css = { - 'all': ('cms/js/select2/select2.css', ), + "all": ("cms/js/select2/select2.css",), } js = ( - 'admin/js/jquery.init.js', - 'cms/js/select2/select2.js', - 'djangocms_alias/js/dist/bundle.alias.create.min.js', - 'djangocms_alias/js/alias_plugin.js', + "admin/js/jquery.init.js", + "cms/js/select2/select2.js", + "djangocms_alias/js/dist/bundle.alias.create.min.js", + "djangocms_alias/js/alias_plugin.js", ) @@ -249,18 +247,17 @@ def get_url(self): def build_attrs(self, *args, **kwargs): attrs = super().build_attrs(*args, **kwargs) - attrs.setdefault('data-select2-url', self.get_url()) + attrs.setdefault("data-select2-url", self.get_url()) return attrs class AliasSelectWidget(Select2Mixin, forms.TextInput): - def get_url(self): return admin_reverse(SELECT2_ALIAS_URL_NAME) def build_attrs(self, *args, **kwargs): attrs = super().build_attrs(*args, **kwargs) - attrs.setdefault('data-select2-url', self.get_url()) + attrs.setdefault("data-select2-url", self.get_url()) return attrs @@ -270,29 +267,33 @@ class AliasPluginForm(forms.ModelForm): queryset=Site.objects.all(), widget=SiteSelectWidget( attrs={ - "data-placeholder": _("Select site to restrict the list of aliases below"), + "data-placeholder": _( + "Select site to restrict the list of aliases below" + ), } ), required=False, ) category = forms.ModelChoiceField( - label=_('Category'), + label=_("Category"), queryset=Category.objects.all(), widget=CategorySelectWidget( attrs={ - 'data-placeholder': _('Select category to restrict the list of aliases below'), # noqa: E501 + "data-placeholder": _( + "Select category to restrict the list of aliases below" + ), # noqa: E501 }, ), - empty_label='', + empty_label="", required=False, ) alias = forms.ModelChoiceField( - label=_('Alias'), + label=_("Alias"), queryset=AliasModel.objects.all(), widget=AliasSelectWidget( attrs={ - 'data-placeholder': _('Select an alias'), + "data-placeholder": _("Select an alias"), }, ), ) @@ -308,25 +309,24 @@ def _set_category_widget_value(self): # If the form is changing an existing Alias # Be sure to show the values for an Alias if self.instance and self.instance.pk: - self.fields['site'].initial = self.instance.alias.site - self.fields['category'].initial = self.instance.alias.category + self.fields["site"].initial = self.instance.alias.site + self.fields["category"].initial = self.instance.alias.category # Otherwise this is creation # Set the site to the current site by default else: - self.fields['site'].initial = get_current_site() + self.fields["site"].initial = get_current_site() class Meta: model = AliasPlugin fields = ( - 'site', - 'category', - 'alias', - 'template', + "site", + "category", + "alias", + "template", ) class AliasContentForm(forms.ModelForm): - alias = forms.ModelChoiceField( queryset=AliasModel.objects.all(), required=True, @@ -336,22 +336,22 @@ class AliasContentForm(forms.ModelForm): class Meta: model = AliasContent - fields = ('name',) + fields = ("name",) def clean(self): cleaned_data = super().clean() - alias = cleaned_data.get('alias') + alias = cleaned_data.get("alias") if not alias: return cleaned_data if AliasContent.objects.filter( - name=cleaned_data.get('name'), - language=cleaned_data.get('language'), + name=cleaned_data.get("name"), + language=cleaned_data.get("language"), alias__category=alias.category, ).exists(): raise forms.ValidationError( - _('Alias with this Name and Category already exists.') + _("Alias with this Name and Category already exists.") ) return cleaned_data diff --git a/djangocms_alias/internal_search.py b/djangocms_alias/internal_search.py index acb0e5e3..f7bfdced 100644 --- a/djangocms_alias/internal_search.py +++ b/djangocms_alias/internal_search.py @@ -14,14 +14,14 @@ def get_title(obj): return obj.result.title -get_title.short_description = _('Title') +get_title.short_description = _("Title") def get_category(obj): return obj.result.category -get_category.short_description = _('Category') +get_category.short_description = _("Category") def get_language(obj): @@ -63,8 +63,8 @@ class AliasContentConfig(BaseSearchConfig): def prepare_text(self, obj): request = get_request(obj.language) context = RequestContext(request) - if 'request' not in context: - context['request'] = request + if "request" not in context: + context["request"] = request toolbar = get_toolbar_from_request(request) renderer = toolbar.get_content_renderer() diff --git a/djangocms_alias/migrations/0001_initial.py b/djangocms_alias/migrations/0001_initial.py index f8b02372..a8b484d9 100644 --- a/djangocms_alias/migrations/0001_initial.py +++ b/djangocms_alias/migrations/0001_initial.py @@ -10,86 +10,183 @@ class Migration(migrations.Migration): - initial = True dependencies = [ - ('cms', '0034_remove_pagecontent_placeholders'), + ("cms", "0034_remove_pagecontent_placeholders"), ] operations = [ migrations.CreateModel( - name='Alias', + name="Alias", fields=[ - ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), - ('position', models.PositiveIntegerField(default=0, verbose_name='position')), + ( + "id", + models.AutoField( + auto_created=True, + primary_key=True, + serialize=False, + verbose_name="ID", + ), + ), + ( + "position", + models.PositiveIntegerField(default=0, verbose_name="position"), + ), ], options={ - 'verbose_name': 'alias', - 'verbose_name_plural': 'aliases', - 'ordering': ['position'], + "verbose_name": "alias", + "verbose_name_plural": "aliases", + "ordering": ["position"], }, ), migrations.CreateModel( - name='AliasContent', + name="AliasContent", fields=[ - ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), - ('name', models.CharField(max_length=120, verbose_name='name')), - ('language', models.CharField(choices=settings.LANGUAGES, default=cms.utils.i18n.get_current_language, max_length=10)), - ('alias', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='contents', to='djangocms_alias.Alias')), + ( + "id", + models.AutoField( + auto_created=True, + primary_key=True, + serialize=False, + verbose_name="ID", + ), + ), + ("name", models.CharField(max_length=120, verbose_name="name")), + ( + "language", + models.CharField( + choices=settings.LANGUAGES, + default=cms.utils.i18n.get_current_language, + max_length=10, + ), + ), + ( + "alias", + models.ForeignKey( + on_delete=django.db.models.deletion.CASCADE, + related_name="contents", + to="djangocms_alias.Alias", + ), + ), ], options={ - 'verbose_name': 'alias content', - 'verbose_name_plural': 'alias contents', + "verbose_name": "alias content", + "verbose_name_plural": "alias contents", }, ), migrations.CreateModel( - name='AliasPlugin', + name="AliasPlugin", fields=[ - ('cmsplugin_ptr', models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, related_name='djangocms_alias_aliasplugin', serialize=False, to='cms.CMSPlugin')), - ('template', models.CharField(choices=get_templates(), default=TEMPLATE_DEFAULT, max_length=255, verbose_name='template')), - ('alias', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='cms_plugins', to='djangocms_alias.Alias', verbose_name='alias')), + ( + "cmsplugin_ptr", + models.OneToOneField( + auto_created=True, + on_delete=django.db.models.deletion.CASCADE, + parent_link=True, + primary_key=True, + related_name="djangocms_alias_aliasplugin", + serialize=False, + to="cms.CMSPlugin", + ), + ), + ( + "template", + models.CharField( + choices=get_templates(), + default=TEMPLATE_DEFAULT, + max_length=255, + verbose_name="template", + ), + ), + ( + "alias", + models.ForeignKey( + on_delete=django.db.models.deletion.CASCADE, + related_name="cms_plugins", + to="djangocms_alias.Alias", + verbose_name="alias", + ), + ), ], options={ - 'verbose_name': 'alias plugin model', - 'verbose_name_plural': 'alias plugin models', + "verbose_name": "alias plugin model", + "verbose_name_plural": "alias plugin models", }, - bases=('cms.cmsplugin',), + bases=("cms.cmsplugin",), ), migrations.CreateModel( - name='Category', + name="Category", fields=[ - ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ( + "id", + models.AutoField( + auto_created=True, + primary_key=True, + serialize=False, + verbose_name="ID", + ), + ), ], options={ - 'verbose_name': 'category', - 'verbose_name_plural': 'categories', + "verbose_name": "category", + "verbose_name_plural": "categories", }, bases=(parler.models.TranslatableModelMixin, models.Model), ), migrations.CreateModel( - name='CategoryTranslation', + name="CategoryTranslation", fields=[ - ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), - ('language_code', models.CharField(db_index=True, max_length=15, verbose_name='Language')), - ('name', models.CharField(max_length=120, unique=True, verbose_name='name')), - ('master', models.ForeignKey(editable=False, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='translations', to='djangocms_alias.Category')), + ( + "id", + models.AutoField( + auto_created=True, + primary_key=True, + serialize=False, + verbose_name="ID", + ), + ), + ( + "language_code", + models.CharField( + db_index=True, max_length=15, verbose_name="Language" + ), + ), + ( + "name", + models.CharField(max_length=120, unique=True, verbose_name="name"), + ), + ( + "master", + models.ForeignKey( + editable=False, + null=True, + on_delete=django.db.models.deletion.CASCADE, + related_name="translations", + to="djangocms_alias.Category", + ), + ), ], options={ - 'verbose_name': 'category Translation', - 'db_table': 'djangocms_alias_category_translation', - 'db_tablespace': '', - 'managed': True, - 'default_permissions': (), + "verbose_name": "category Translation", + "db_table": "djangocms_alias_category_translation", + "db_tablespace": "", + "managed": True, + "default_permissions": (), }, ), migrations.AddField( - model_name='alias', - name='category', - field=models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='aliases', to='djangocms_alias.Category', verbose_name='category'), + model_name="alias", + name="category", + field=models.ForeignKey( + on_delete=django.db.models.deletion.PROTECT, + related_name="aliases", + to="djangocms_alias.Category", + verbose_name="category", + ), ), migrations.AlterUniqueTogether( - name='categorytranslation', - unique_together={('language_code', 'master')}, + name="categorytranslation", + unique_together={("language_code", "master")}, ), ] diff --git a/djangocms_alias/migrations/0002_auto_20200723_1424.py b/djangocms_alias/migrations/0002_auto_20200723_1424.py index ab296d63..dbc91d23 100644 --- a/djangocms_alias/migrations/0002_auto_20200723_1424.py +++ b/djangocms_alias/migrations/0002_auto_20200723_1424.py @@ -6,35 +6,57 @@ class Migration(migrations.Migration): - dependencies = [ - ('sites', '0002_alter_domain_unique'), - ('djangocms_alias', '0001_initial'), + ("sites", "0002_alter_domain_unique"), + ("djangocms_alias", "0001_initial"), ] operations = [ migrations.AddField( - model_name='alias', - name='creation_method', - field=models.CharField(blank=True, choices=[('template', 'by template'), ('code', 'by code')], default='code', max_length=20, verbose_name='creation_method'), + model_name="alias", + name="creation_method", + field=models.CharField( + blank=True, + choices=[("template", "by template"), ("code", "by code")], + default="code", + max_length=20, + verbose_name="creation_method", + ), ), migrations.AddField( - model_name='alias', - name='site', - field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to='sites.Site'), + model_name="alias", + name="site", + field=models.ForeignKey( + blank=True, + null=True, + on_delete=django.db.models.deletion.CASCADE, + to="sites.Site", + ), ), migrations.AddField( - model_name='alias', - name='static_code', - field=models.CharField(blank=True, help_text='To render the alias in templates.', max_length=255, null=True, verbose_name='static code'), + model_name="alias", + name="static_code", + field=models.CharField( + blank=True, + help_text="To render the alias in templates.", + max_length=255, + null=True, + verbose_name="static code", + ), ), migrations.AlterField( - model_name='categorytranslation', - name='master', - field=parler.fields.TranslationsForeignKey(editable=False, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='translations', to='djangocms_alias.Category'), + model_name="categorytranslation", + name="master", + field=parler.fields.TranslationsForeignKey( + editable=False, + null=True, + on_delete=django.db.models.deletion.CASCADE, + related_name="translations", + to="djangocms_alias.Category", + ), ), migrations.AlterUniqueTogether( - name='alias', - unique_together={('static_code', 'site')}, + name="alias", + unique_together={("static_code", "site")}, ), ] diff --git a/djangocms_alias/models.py b/djangocms_alias/models.py index 438757a6..cf1da479 100644 --- a/djangocms_alias/models.py +++ b/djangocms_alias/models.py @@ -24,24 +24,24 @@ __all__ = [ - 'Category', - 'Alias', - 'AliasContent', - 'AliasPlugin', + "Category", + "Alias", + "AliasContent", + "AliasPlugin", ] # Add additional choices through the ``settings.py``. -TEMPLATE_DEFAULT = 'default' +TEMPLATE_DEFAULT = "default" def get_templates(): choices = [ - (TEMPLATE_DEFAULT, _('Default')), + (TEMPLATE_DEFAULT, _("Default")), ] choices += getattr( settings, - 'DJANGOCMS_ALIAS_TEMPLATES', + "DJANGOCMS_ALIAS_TEMPLATES", [], ) return choices @@ -50,15 +50,15 @@ def get_templates(): class Category(TranslatableModel): translations = TranslatedFields( name=models.CharField( - verbose_name=_('name'), + verbose_name=_("name"), max_length=120, unique=True, ), ) class Meta: - verbose_name = _('category') - verbose_name_plural = _('categories') + verbose_name = _("category") + verbose_name_plural = _("categories") def __str__(self): # Be sure to be able to see the category name even if it's not in the current language @@ -70,40 +70,45 @@ def get_absolute_url(self): class Alias(models.Model): - CREATION_BY_TEMPLATE = 'template' - CREATION_BY_CODE = 'code' + CREATION_BY_TEMPLATE = "template" + CREATION_BY_CODE = "code" CREATION_METHODS = ( - (CREATION_BY_TEMPLATE, _('by template')), - (CREATION_BY_CODE, _('by code')), + (CREATION_BY_TEMPLATE, _("by template")), + (CREATION_BY_CODE, _("by code")), ) creation_method = models.CharField( - verbose_name=_('creation_method'), choices=CREATION_METHODS, - default=CREATION_BY_CODE, max_length=20, blank=True, + verbose_name=_("creation_method"), + choices=CREATION_METHODS, + default=CREATION_BY_CODE, + max_length=20, + blank=True, ) category = models.ForeignKey( Category, - verbose_name=_('category'), - related_name='aliases', + verbose_name=_("category"), + related_name="aliases", on_delete=models.PROTECT, ) position = models.PositiveIntegerField( - verbose_name=_('position'), + verbose_name=_("position"), default=0, ) static_code = models.CharField( - verbose_name=_('static code'), + verbose_name=_("static code"), max_length=255, blank=True, null=True, - help_text=_('To render the alias in templates.') + help_text=_("To render the alias in templates."), ) site = models.ForeignKey(Site, on_delete=models.CASCADE, null=True, blank=True) class Meta: - verbose_name = _('alias') - verbose_name_plural = _('aliases') - ordering = ['position'] - unique_together = (('static_code', 'site'),) # Only restrict instances that have a site specified + verbose_name = _("alias") + verbose_name_plural = _("aliases") + ordering = ["position"] + unique_together = ( + ("static_code", "site"), + ) # Only restrict instances that have a site specified def __init__(self, *args, **kwargs): self._plugins_cache = {} @@ -117,7 +122,7 @@ def __str__(self): @cached_property def name(self): """Show alias name for current language""" - return self.get_name() or '' + return self.get_name() or "" @cached_property def is_in_use(self): @@ -127,36 +132,41 @@ def is_in_use(self): def objects_using(self): objects = set() object_ids = defaultdict(set) - plugins = self.cms_plugins.select_related('placeholder').prefetch_related('placeholder__source') + plugins = self.cms_plugins.select_related("placeholder").prefetch_related( + "placeholder__source" + ) for plugin in plugins: obj = plugin.placeholder.source obj_class_name = obj.__class__.__name__ - if obj_class_name.endswith('Content'): - attr_name = obj_class_name.replace('Content', '').lower() + if obj_class_name.endswith("Content"): + attr_name = obj_class_name.replace("Content", "").lower() attr_related_model = obj._meta.get_field(attr_name).related_model - id_attr = getattr(obj, '{}_id'.format(attr_name)) + id_attr = getattr(obj, f"{attr_name}_id") if id_attr: object_ids[attr_related_model].update([id_attr]) else: objects.update([obj]) else: objects.update([obj]) - objects.update([ - obj - for model_class, ids in object_ids.items() - for obj in model_class.objects.filter(pk__in=ids) - ]) + objects.update( + [ + obj + for model_class, ids in object_ids.items() + for obj in model_class.objects.filter(pk__in=ids) + ] + ) return list(objects) def get_name(self, language=None): content = self.get_content(language, show_draft_content=True) - name = getattr(content, 'name', 'Alias {} (No content)'.format(self.pk)) + name = getattr(content, "name", f"Alias {self.pk} (No content)") if is_versioning_enabled() and content: from djangocms_versioning.constants import DRAFT + version = content.versions.first() if version.state == DRAFT: - return '{} (Not published)'.format(name) + return f"{name} (Not published)" return name @@ -178,11 +188,13 @@ def get_content(self, language=None, show_draft_content=False): try: return self._content_cache[language] except KeyError: - qs = self.contents.select_related( - 'alias__category', - ).prefetch_related( - 'placeholders' - ).filter(language=language) + qs = ( + self.contents.select_related( + "alias__category", + ) + .prefetch_related("placeholders") + .filter(language=language) + ) if show_draft_content and is_versioning_enabled(): from djangocms_versioning.constants import DRAFT, PUBLISHED @@ -191,14 +203,18 @@ def get_content(self, language=None, show_draft_content=False): # Ensure that we are getting the latest valid content, the top most version can become # archived with a previous version re-published qs = remove_published_where(qs) - qs = qs.filter(Q(versions__state=DRAFT) | Q(versions__state=PUBLISHED)).order_by('-versions__created') + qs = qs.filter( + Q(versions__state=DRAFT) | Q(versions__state=PUBLISHED) + ).order_by("-versions__created") self._content_cache[language] = qs.first() return self._content_cache[language] def get_placeholder(self, language=None, show_draft_content=False): - content = self.get_content(language=language, show_draft_content=show_draft_content) - return getattr(content, 'placeholder', None) + content = self.get_content( + language=language, show_draft_content=show_draft_content + ) + return getattr(content, "placeholder", None) def get_plugins(self, language=None): if not language: @@ -217,9 +233,10 @@ def get_languages(self): if is_versioning_enabled(): from djangocms_versioning.helpers import remove_published_where + queryset = remove_published_where(queryset) - self._content_languages_cache = queryset.values_list('language', flat=True) + self._content_languages_cache = queryset.values_list("language", flat=True) return self._content_languages_cache def clear_cache(self): @@ -231,7 +248,7 @@ def clear_cache(self): def delete(self, *args, **kwargs): super().delete(*args, **kwargs) self.category.aliases.filter(position__gt=self.position).update( - position=F('position') - 1, + position=F("position") - 1, ) def save(self, *args, **kwargs): @@ -256,21 +273,21 @@ def _set_position(self, position): self.position = position self.save() - self.category.aliases.filter(*filters).update(position=op(F('position'), 1)) # noqa: E501 + self.category.aliases.filter(*filters).update(position=op(F("position"), 1)) # noqa: E501 class AliasContent(models.Model): alias = models.ForeignKey( Alias, on_delete=models.CASCADE, - related_name='contents', + related_name="contents", ) name = models.CharField( - verbose_name=_('name'), + verbose_name=_("name"), max_length=120, ) placeholders = PlaceholderRelationField() - placeholder_slotname = 'content' + placeholder_slotname = "content" language = models.CharField( max_length=10, choices=settings.LANGUAGES, @@ -278,11 +295,11 @@ class AliasContent(models.Model): ) class Meta: - verbose_name = _('alias content') - verbose_name_plural = _('alias contents') + verbose_name = _("alias content") + verbose_name_plural = _("alias contents") def __str__(self): - return '{} ({})'.format(self.name, self.language) + return f"{self.name} ({self.language})" @cached_property def placeholder(self): @@ -290,6 +307,7 @@ def placeholder(self): return self.placeholders.get(slot=self.placeholder_slotname) except Placeholder.DoesNotExist: from cms.utils.placeholder import rescan_placeholders_for_obj + rescan_placeholders_for_obj(self) return self.placeholders.get(slot=self.placeholder_slotname) @@ -300,7 +318,7 @@ def get_absolute_url(self): return get_object_preview_url(self) def get_template(self): - return 'djangocms_alias/alias_content.html' + return "djangocms_alias/alias_content.html" @transaction.atomic def delete(self, *args, **kwargs): @@ -325,7 +343,7 @@ def populate(self, replaced_placeholder=None, replaced_plugin=None, plugins=None id__in=[replaced_plugin.pk] + replaced_plugin._get_descendants_ids(), ) placeholder = replaced_plugin.placeholder - add_plugin_kwargs = {'position': 'left', 'target': replaced_plugin} + add_plugin_kwargs = {"position": "left", "target": replaced_plugin} copy_plugins_to_placeholder( plugins, @@ -337,14 +355,14 @@ def populate(self, replaced_placeholder=None, replaced_plugin=None, plugins=None new_plugin = add_plugin( placeholder, - plugin_type='Alias', + plugin_type="Alias", language=self.language, alias=self.alias, - **add_plugin_kwargs + **add_plugin_kwargs, ) if replaced_plugin: new_plugin.position = replaced_plugin.position - new_plugin.save(update_fields=['position']) + new_plugin.save(update_fields=["position"]) return new_plugin @@ -371,10 +389,10 @@ def copy_alias_content(original_content): for field in Placeholder._meta.fields # don't copy primary key because we're creating a new obj # and handle the source field later - if field.name not in [Placeholder._meta.pk.name, 'source'] + if field.name not in [Placeholder._meta.pk.name, "source"] } if placeholder.source: - placeholder_fields['source'] = new_content + placeholder_fields["source"] = new_content new_placeholder = Placeholder.objects.create(**placeholder_fields) # Copy plugins placeholder.copy_plugins(new_placeholder) @@ -387,20 +405,20 @@ def copy_alias_content(original_content): class AliasPlugin(CMSPlugin): alias = models.ForeignKey( Alias, - verbose_name=_('alias'), - related_name='cms_plugins', + verbose_name=_("alias"), + related_name="cms_plugins", on_delete=models.CASCADE, ) template = models.CharField( - verbose_name=_('template'), + verbose_name=_("template"), choices=get_templates(), default=TEMPLATE_DEFAULT, max_length=255, ) class Meta: - verbose_name = _('alias plugin model') - verbose_name_plural = _('alias plugin models') + verbose_name = _("alias plugin model") + verbose_name_plural = _("alias plugin models") def __str__(self): return force_str(self.alias.name) diff --git a/djangocms_alias/rendering.py b/djangocms_alias/rendering.py index 7b699af1..08430b1b 100644 --- a/djangocms_alias/rendering.py +++ b/djangocms_alias/rendering.py @@ -2,6 +2,6 @@ def render_alias_content(request, alias_content): - template = 'djangocms_alias/alias_content_preview.html' - context = {'alias_content': alias_content} + template = "djangocms_alias/alias_content_preview.html" + context = {"alias_content": alias_content} return TemplateResponse(request, template, context) diff --git a/djangocms_alias/templatetags/djangocms_alias_tags.py b/djangocms_alias/templatetags/djangocms_alias_tags.py index 22afbc10..0eb23699 100644 --- a/djangocms_alias/templatetags/djangocms_alias_tags.py +++ b/djangocms_alias/templatetags/djangocms_alias_tags.py @@ -36,7 +36,7 @@ def verbose_name(obj): @register.simple_tag(takes_context=True) def render_alias(context, instance, editable=False): - request = context['request'] + request = context["request"] toolbar = get_toolbar_from_request(request) renderer = toolbar.get_content_renderer() @@ -50,8 +50,8 @@ def render_alias(context, instance, editable=False): context=context, editable=editable, ) - return content or '' - return '' + return content or "" + return "" @register.tag @@ -67,50 +67,56 @@ class StaticAlias(Tag): static_code -- the unique identifier of the Alias site -- If site is supplied an Alias instance will be created per site. """ - name = 'static_alias' + + name = "static_alias" options = PlaceholderOptions( - Argument('static_code', resolve=False), - MultiValueArgument('extra_bits', required=False, resolve=False), + Argument("static_code", resolve=False), + MultiValueArgument("extra_bits", required=False, resolve=False), blocks=[ - ('endstatic_alias', 'nodelist'), + ("endstatic_alias", "nodelist"), ], ) def _get_alias(self, request, static_code, extra_bits): alias_filter_kwargs = { - 'static_code': static_code, + "static_code": static_code, } # Site current_site = get_current_site() - if 'site' in extra_bits: - alias_filter_kwargs['site'] = current_site + if "site" in extra_bits: + alias_filter_kwargs["site"] = current_site else: - alias_filter_kwargs['site_id__isnull'] = True + alias_filter_kwargs["site_id__isnull"] = True # Try and find an Alias to render alias = Alias.objects.filter(**alias_filter_kwargs).first() # If there is no alias found we need to create one if not alias: - # If versioning is enabled we can only create the records with a logged in user / staff member if is_versioning_enabled() and not request.user.is_authenticated: return None language = get_default_language_for_site(current_site) # Parlers get_or_create doesn't work well with translations, so we must perform our own get or create - default_category = Category.objects.filter(translations__name=DEFAULT_STATIC_ALIAS_CATEGORY_NAME).first() + default_category = Category.objects.filter( + translations__name=DEFAULT_STATIC_ALIAS_CATEGORY_NAME + ).first() if not default_category: - default_category = Category.objects.create(name=DEFAULT_STATIC_ALIAS_CATEGORY_NAME) + default_category = Category.objects.create( + name=DEFAULT_STATIC_ALIAS_CATEGORY_NAME + ) alias_creation_kwargs = { - 'static_code': static_code, - 'creation_method': Alias.CREATION_BY_TEMPLATE + "static_code": static_code, + "creation_method": Alias.CREATION_BY_TEMPLATE, } # Site - if 'site' in extra_bits: - alias_creation_kwargs['site'] = current_site + if "site" in extra_bits: + alias_creation_kwargs["site"] = current_site - alias = Alias.objects.create(category=default_category, **alias_creation_kwargs) + alias = Alias.objects.create( + category=default_category, **alias_creation_kwargs + ) alias_content = AliasContent.objects.create( alias=alias, name=static_code, @@ -125,13 +131,13 @@ def _get_alias(self, request, static_code, extra_bits): return alias def render_tag(self, context, static_code, extra_bits, nodelist=None): - request = context.get('request') + request = context.get("request") if not static_code or not request: # an empty string was passed in or the variable is not available in the context if nodelist: return nodelist.render(context) - return '' + return "" validate_placeholder_name(static_code) @@ -140,7 +146,7 @@ def render_tag(self, context, static_code, extra_bits, nodelist=None): alias = self._get_alias(request, static_code, extra_bits) if not alias: - return '' + return "" # Get draft contents in edit or preview mode? get_draft_content = False @@ -148,7 +154,9 @@ def render_tag(self, context, static_code, extra_bits, nodelist=None): get_draft_content = True language = get_language_from_request(request) - placeholder = alias.get_placeholder(language=language, show_draft_content=get_draft_content) + placeholder = alias.get_placeholder( + language=language, show_draft_content=get_draft_content + ) if placeholder: content = renderer.render_placeholder( @@ -157,4 +165,4 @@ def render_tag(self, context, static_code, extra_bits, nodelist=None): nodelist=nodelist, ) return content - return '' + return "" diff --git a/djangocms_alias/test_utils/text/cms_plugins.py b/djangocms_alias/test_utils/text/cms_plugins.py index bef37044..ba4003a6 100644 --- a/djangocms_alias/test_utils/text/cms_plugins.py +++ b/djangocms_alias/test_utils/text/cms_plugins.py @@ -7,6 +7,6 @@ @plugin_pool.register_plugin class TextPlugin(CMSPluginBase): model = Text - name = 'Text' + name = "Text" allow_children = True - render_template = 'text/text.html' + render_template = "text/text.html" diff --git a/djangocms_alias/urls.py b/djangocms_alias/urls.py index 161a1d98..28c5d6d5 100644 --- a/djangocms_alias/urls.py +++ b/djangocms_alias/urls.py @@ -6,35 +6,33 @@ urlpatterns = [ path( - 'create-alias/', + "create-alias/", views.create_alias_view, name=constants.CREATE_ALIAS_URL_NAME, ), path( - 'aliases//usage/', + "aliases//usage/", views.alias_usage_view, name=constants.USAGE_ALIAS_URL_NAME, ), path( - 'detach-alias//', + "detach-alias//", views.detach_alias_plugin_view, name=constants.DETACH_ALIAS_PLUGIN_URL_NAME, ), path( - 'delete-alias//', + "delete-alias//", views.delete_alias_view, name=constants.DELETE_ALIAS_URL_NAME, ), path( - 'select2/', + "select2/", views.AliasSelect2View.as_view(), name=constants.SELECT2_ALIAS_URL_NAME, ), path( - 'category-select2/', + "category-select2/", views.CategorySelect2View.as_view(), name=constants.CATEGORY_SELECT2_URL_NAME, ), - - ] diff --git a/djangocms_alias/utils.py b/djangocms_alias/utils.py index 2458a6b7..4bfbfccc 100644 --- a/djangocms_alias/utils.py +++ b/djangocms_alias/utils.py @@ -3,8 +3,9 @@ def is_versioning_enabled(): from .models import AliasContent + try: - app_config = apps.get_app_config('djangocms_versioning') + app_config = apps.get_app_config("djangocms_versioning") return app_config.cms_extension.is_content_model_versioned(AliasContent) except LookupError: return False diff --git a/djangocms_alias/views.py b/djangocms_alias/views.py index dd476481..430492f3 100644 --- a/djangocms_alias/views.py +++ b/djangocms_alias/views.py @@ -35,18 +35,18 @@ def detach_alias_plugin_view(request, plugin_pk): instance = get_object_or_404(AliasPlugin, pk=plugin_pk) - if request.method == 'GET': + if request.method == "GET": opts = Alias.model._meta context = { - 'has_change_permission': True, - 'opts': opts, - 'root_path': reverse('admin:index'), - 'is_popup': True, - 'app_label': opts.app_label, - 'object_name': _('Alias'), - 'object': instance.alias, + "has_change_permission": True, + "opts": opts, + "root_path": reverse("admin:index"), + "is_popup": True, + "app_label": opts.app_label, + "object_name": _("Alias"), + "object": instance.alias, } - return render(request, 'djangocms_alias/detach_alias.html', context) + return render(request, "djangocms_alias/detach_alias.html", context) language = get_language_from_request(request, check_path=True) @@ -93,8 +93,8 @@ def create_alias_view(request): else: initial_data = None - if request.method == 'GET' and not form.is_valid(): - return HttpResponseBadRequest('Form received unexpected values') + if request.method == "GET" and not form.is_valid(): + return HttpResponseBadRequest("Form received unexpected values") user = request.user @@ -107,24 +107,24 @@ def create_alias_view(request): if not create_form.is_valid(): opts = Alias.model._meta context = { - 'form': create_form, - 'has_change_permission': True, - 'opts': opts, - 'root_path': reverse('admin:index'), - 'is_popup': True, - 'app_label': opts.app_label, - 'media': (Alias().media + create_form.media), + "form": create_form, + "has_change_permission": True, + "opts": opts, + "root_path": reverse("admin:index"), + "is_popup": True, + "app_label": opts.app_label, + "media": (Alias().media + create_form.media), } - return render(request, 'djangocms_alias/create_alias.html', context) + return render(request, "djangocms_alias/create_alias.html", context) plugins = create_form.get_plugins() if not plugins: return HttpResponseBadRequest( - 'Plugins are required to create an alias', + "Plugins are required to create an alias", ) - replace = create_form.cleaned_data.get('replace') + replace = create_form.cleaned_data.get("replace") if not Alias.can_create_alias(user, plugins, replace): raise PermissionDenied @@ -132,8 +132,8 @@ def create_alias_view(request): emit_content_change([alias_content]) if replace: - plugin = create_form.cleaned_data.get('plugin') - placeholder = create_form.cleaned_data.get('placeholder') + plugin = create_form.cleaned_data.get("plugin") + placeholder = create_form.cleaned_data.get("placeholder") return render_replace_response( request, new_plugins=[alias_plugin], @@ -144,8 +144,9 @@ def create_alias_view(request): return HttpResponse(JAVASCRIPT_SUCCESS_RESPONSE) -def render_replace_response(request, new_plugins, source_placeholder=None, - source_plugin=None): +def render_replace_response( + request, new_plugins, source_placeholder=None, source_plugin=None +): move_plugins, add_plugins = [], [] for plugin in new_plugins: root = plugin.parent.get_bound_plugin() if plugin.parent else plugin @@ -158,46 +159,52 @@ def render_replace_response(request, new_plugins, source_placeholder=None, ) plugin_tree = get_plugin_tree_as_json(request, plugins) move_data = get_plugin_toolbar_info(plugin) - move_data['plugin_order'] = plugin_order + move_data["plugin_order"] = plugin_order move_data.update(json.loads(plugin_tree)) move_plugins.append(json.dumps(move_data)) - add_plugins.append(( - json.dumps(get_plugin_toolbar_info(plugin)), - plugin_tree, - )) + add_plugins.append( + ( + json.dumps(get_plugin_toolbar_info(plugin)), + plugin_tree, + ) + ) context = { - 'added_plugins': add_plugins, - 'moved_plugins': move_plugins, - 'is_popup': True, + "added_plugins": add_plugins, + "moved_plugins": move_plugins, + "is_popup": True, } if source_plugin is not None: - context['replaced_plugin'] = json.dumps( + context["replaced_plugin"] = json.dumps( get_plugin_toolbar_info(source_plugin), ) if source_placeholder is not None: - context['replaced_placeholder'] = json.dumps({ - 'placeholder_id': source_placeholder.pk, - 'deleted': True, - }) - return render(request, 'djangocms_alias/alias_replace.html', context) + context["replaced_placeholder"] = json.dumps( + { + "placeholder_id": source_placeholder.pk, + "deleted": True, + } + ) + return render(request, "djangocms_alias/alias_replace.html", context) class CategorySelect2View(ListView): - queryset = Category.objects.order_by('translations__name') + queryset = Category.objects.order_by("translations__name") def get(self, request, *args, **kwargs): self.object_list = self.get_queryset() context = self.get_context_data() - return JsonResponse({ - 'results': [ - { - 'text': str(obj), - 'id': obj.pk, - } - for obj in context['object_list'] - ], - 'more': context['page_obj'].has_next(), - }) + return JsonResponse( + { + "results": [ + { + "text": str(obj), + "id": obj.pk, + } + for obj in context["object_list"] + ], + "more": context["page_obj"].has_next(), + } + ) def dispatch(self, request, *args, **kwargs): if not request.user.is_staff: @@ -209,16 +216,14 @@ def get_queryset(self): Only show Categories that have an Alias attached. If site is selected, use that to filter further. """ - term = self.request.GET.get('term') - site = self.request.GET.get('site') + term = self.request.GET.get("term") + site = self.request.GET.get("site") queryset = super().get_queryset().distinct() # Only get categories that have aliases attached - queryset = queryset.filter( - aliases__isnull=False - ) + queryset = queryset.filter(aliases__isnull=False) try: - pk = int(self.request.GET.get('pk')) + pk = int(self.request.GET.get("pk")) except (TypeError, ValueError): pk = None @@ -233,25 +238,27 @@ def get_queryset(self): return queryset.filter(q) def get_paginate_by(self, queryset): - return self.request.GET.get('limit', 30) + return self.request.GET.get("limit", 30) class AliasSelect2View(ListView): - queryset = AliasModel.objects.order_by('category__translations__name', 'position') + queryset = AliasModel.objects.order_by("category__translations__name", "position") def get(self, request, *args, **kwargs): self.object_list = self.get_queryset() context = self.get_context_data() - return JsonResponse({ - 'results': [ - { - 'text': str(obj), - 'id': obj.pk, - } - for obj in context['object_list'] - ], - 'more': context['page_obj'].has_next(), - }) + return JsonResponse( + { + "results": [ + { + "text": str(obj), + "id": obj.pk, + } + for obj in context["object_list"] + ], + "more": context["page_obj"].has_next(), + } + ) def dispatch(self, request, *args, **kwargs): if not request.user.is_staff: @@ -259,16 +266,21 @@ def dispatch(self, request, *args, **kwargs): return super().dispatch(request, *args, **kwargs) def get_queryset(self): - term = self.request.GET.get('term') - category = self.request.GET.get('category') - site = self.request.GET.get('site') + term = self.request.GET.get("term") + category = self.request.GET.get("category") + site = self.request.GET.get("site") # Showing published and unpublished aliases - queryset = super().get_queryset().filter( - contents__language=get_current_language(), - ).distinct() + queryset = ( + super() + .get_queryset() + .filter( + contents__language=get_current_language(), + ) + .distinct() + ) try: - pk = int(self.request.GET.get('pk')) + pk = int(self.request.GET.get("pk")) except (TypeError, ValueError): pk = None @@ -285,7 +297,7 @@ def get_queryset(self): return queryset.filter(q) def get_paginate_by(self, queryset): - return self.request.GET.get('limit', 30) + return self.request.GET.get("limit", 30) def alias_usage_view(request, pk): @@ -294,23 +306,23 @@ def alias_usage_view(request, pk): alias = get_object_or_404(AliasModel.objects.all(), pk=pk) opts = Alias.model._meta - title = _('Objects using alias: {}'.format(alias)) + title = _(f"Objects using alias: {alias}") context = { - 'has_change_permission': True, - 'opts': opts, - 'root_path': reverse('admin:index'), - 'is_popup': True, - 'app_label': opts.app_label, - 'object_name': _('Alias'), - 'object': alias, - 'title': title, - 'original': title, - 'show_back_btn': request.GET.get('back'), - 'objects_list': sorted( + "has_change_permission": True, + "opts": opts, + "root_path": reverse("admin:index"), + "is_popup": True, + "app_label": opts.app_label, + "object_name": _("Alias"), + "object": alias, + "title": title, + "original": title, + "show_back_btn": request.GET.get("back"), + "objects_list": sorted( alias.objects_using, # First show Pages on list key=lambda obj: isinstance(obj, Page), reverse=True, ), } - return render(request, 'djangocms_alias/alias_usage.html', context) + return render(request, "djangocms_alias/alias_usage.html", context) diff --git a/setup.py b/setup.py index 02cae3c0..c8d62cf5 100644 --- a/setup.py +++ b/setup.py @@ -4,48 +4,48 @@ CLASSIFIERS = [ - 'Environment :: Web Environment', - 'Framework :: Django', - 'Intended Audience :: Developers', - 'License :: OSI Approved :: BSD License', - 'Operating System :: OS Independent', - 'Programming Language :: Python', - 'Topic :: Internet :: WWW/HTTP :: Dynamic Content', - 'Topic :: Software Development', - 'Topic :: Software Development :: Libraries :: Application Frameworks', - 'Programming Language :: Python :: 3.8', - 'Programming Language :: Python :: 3.9', - 'Programming Language :: Python :: 3.10', - 'Framework :: Django', - 'Framework :: Django :: 2.2', - 'Framework :: Django :: 3.0', - 'Framework :: Django :: 3.1', - 'Framework :: Django :: 3.2', - 'Framework :: Django :: 4.2', + "Environment :: Web Environment", + "Framework :: Django", + "Intended Audience :: Developers", + "License :: OSI Approved :: BSD License", + "Operating System :: OS Independent", + "Programming Language :: Python", + "Topic :: Internet :: WWW/HTTP :: Dynamic Content", + "Topic :: Software Development", + "Topic :: Software Development :: Libraries :: Application Frameworks", + "Programming Language :: Python :: 3.8", + "Programming Language :: Python :: 3.9", + "Programming Language :: Python :: 3.10", + "Framework :: Django", + "Framework :: Django :: 2.2", + "Framework :: Django :: 3.0", + "Framework :: Django :: 3.1", + "Framework :: Django :: 3.2", + "Framework :: Django :: 4.2", ] INSTALL_REQUIREMENTS = [ - 'Django>=3.2,<5.0', - 'django-parler>=1.4', - 'django-cms>4.0,<4.1', + "Django>=3.2,<5.0", + "django-parler>=1.4", + "django-cms>4.0,<4.1", ] setup( - name='djangocms-alias', - author='Divio AG', - author_email='info@divio.ch', - maintainer='Django CMS Association and contributors', - maintainer_email='info@django-cms.org', - url='https://github.com/django-cms/djangocms-alias', - license='BSD', + name="djangocms-alias", + author="Divio AG", + author_email="info@divio.ch", + maintainer="Django CMS Association and contributors", + maintainer_email="info@django-cms.org", + url="https://github.com/django-cms/djangocms-alias", + license="BSD", version=djangocms_alias.__version__, description=djangocms_alias.__doc__, - long_description=open('README.rst').read(), - long_description_content_type='text/x-rst', - platforms=['OS Independent'], + long_description=open("README.rst").read(), + long_description_content_type="text/x-rst", + platforms=["OS Independent"], classifiers=CLASSIFIERS, install_requires=INSTALL_REQUIREMENTS, packages=find_packages(), include_package_data=True, - test_suite='test_settings.run', + test_suite="test_settings.run", ) diff --git a/test_settings.py b/test_settings.py index c4b486b9..a3facd1f 100644 --- a/test_settings.py +++ b/test_settings.py @@ -3,106 +3,103 @@ BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) -ENABLE_VERSIONING = strtobool(os.environ.get('ENABLE_VERSIONING', "1")) +ENABLE_VERSIONING = strtobool(os.environ.get("ENABLE_VERSIONING", "1")) EXTRA_INSTALLED_APPS = [] if ENABLE_VERSIONING: - EXTRA_INSTALLED_APPS.append('djangocms_versioning') + EXTRA_INSTALLED_APPS.append("djangocms_versioning") HELPER_SETTINGS = { - 'SECRET_KEY': 'test1234', - 'TIME_ZONE': 'Europe/Zurich', - 'TOP_INSTALLED_APPS': [ - 'djangocms_alias', + "SECRET_KEY": "test1234", + "TIME_ZONE": "Europe/Zurich", + "TOP_INSTALLED_APPS": [ + "djangocms_alias", ], - 'INSTALLED_APPS': [ - 'parler', - 'djangocms_alias.test_utils.text', - ] + EXTRA_INSTALLED_APPS, - 'VERSIONING_ALIAS_MODELS_ENABLED': ENABLE_VERSIONING, - 'MIGRATION_MODULES': { - 'sites': None, - 'contenttypes': None, - 'auth': None, - 'cms': None, - 'menus': None, - 'text': None, - 'djangocms_alias': None, - 'djangocms_versioning': None, + "INSTALLED_APPS": [ + "parler", + "djangocms_alias.test_utils.text", + ] + + EXTRA_INSTALLED_APPS, + "VERSIONING_ALIAS_MODELS_ENABLED": ENABLE_VERSIONING, + "MIGRATION_MODULES": { + "sites": None, + "contenttypes": None, + "auth": None, + "cms": None, + "menus": None, + "text": None, + "djangocms_alias": None, + "djangocms_versioning": None, }, - 'CMS_PERMISSION': True, + "CMS_PERMISSION": True, # At present, testing requires bootstrap to be disabled. # 'ALDRYN_BOILERPLATE_NAME': 'bootstrap3', - 'LANGUAGES': ( - ('en', 'English'), - ('de', 'German'), - ('fr', 'French'), - ('it', 'Italiano'), + "LANGUAGES": ( + ("en", "English"), + ("de", "German"), + ("fr", "French"), + ("it", "Italiano"), ), - 'CMS_LANGUAGES': { + "CMS_LANGUAGES": { 1: [ + {"code": "en", "name": "English", "fallbacks": ["de", "fr"]}, { - 'code': 'en', - 'name': 'English', - 'fallbacks': ['de', 'fr'] + "code": "de", + "name": "Deutsche", + "fallbacks": ["en"], # FOR TESTING DO NOT ADD 'fr' HERE }, { - 'code': 'de', - 'name': 'Deutsche', - 'fallbacks': ['en'] # FOR TESTING DO NOT ADD 'fr' HERE + "code": "fr", + "name": "Française", + "fallbacks": ["en"], # FOR TESTING DO NOT ADD 'de' HERE }, { - 'code': 'fr', - 'name': 'Française', - 'fallbacks': ['en'] # FOR TESTING DO NOT ADD 'de' HERE - }, - { - 'code': 'it', - 'name': 'Italiano', - 'fallbacks': ['fr'] # FOR TESTING, LEAVE AS ONLY 'fr' + "code": "it", + "name": "Italiano", + "fallbacks": ["fr"], # FOR TESTING, LEAVE AS ONLY 'fr' }, ], }, - 'TEMPLATE_DIRS': [ - os.path.join('tests', 'templates'), + "TEMPLATE_DIRS": [ + os.path.join("tests", "templates"), ], - 'CMS_TEMPLATES': ( + "CMS_TEMPLATES": ( ("fullwidth.html", "Fullwidth"), ("page.html", "Normal page"), - ('static_alias.html', 'Static Alias Template'), + ("static_alias.html", "Static Alias Template"), ), - 'PARLER_LANGUAGES': { + "PARLER_LANGUAGES": { 1: [ { - 'code': 'en', - 'fallbacks': ['de', 'fr'], - 'hide_untranslated': False, + "code": "en", + "fallbacks": ["de", "fr"], + "hide_untranslated": False, }, { - 'code': 'de', - 'fallbacks': ['en'], - 'hide_untranslated': False, + "code": "de", + "fallbacks": ["en"], + "hide_untranslated": False, }, { - 'code': 'fr', - 'fallbacks': ['en'], - 'hide_untranslated': False, + "code": "fr", + "fallbacks": ["en"], + "hide_untranslated": False, }, { - 'code': 'it', - 'fallbacks': ['fr'], # FOR TESTING, LEAVE AS ONLY 'fr' - 'hide_untranslated': False, + "code": "it", + "fallbacks": ["fr"], # FOR TESTING, LEAVE AS ONLY 'fr' + "hide_untranslated": False, }, ], - 'default': { - 'code': 'en', - 'fallbacks': ['en'], - 'hide_untranslated': False, - } + "default": { + "code": "en", + "fallbacks": ["en"], + "hide_untranslated": False, + }, }, - 'PARLER_ENABLE_CACHING': False, - 'LANGUAGE_CODE': 'en', - 'DJANGOCMS_ALIAS_TEMPLATES': [ - ('custom_alias_template', 'Custom Template Name'), + "PARLER_ENABLE_CACHING": False, + "LANGUAGE_CODE": "en", + "DJANGOCMS_ALIAS_TEMPLATES": [ + ("custom_alias_template", "Custom Template Name"), ], "DEFAULT_AUTO_FIELD": "django.db.models.AutoField", # Due to a recent temporary change in develop-4, we now need to confirm that we intend to use v4 @@ -112,7 +109,8 @@ def run(): from app_helper import runner - runner.cms('djangocms_alias', extra_args=[]) + + runner.cms("djangocms_alias", extra_args=[]) if __name__ == "__main__": diff --git a/tests/base.py b/tests/base.py index 41dfdb5e..eec011da 100644 --- a/tests/base.py +++ b/tests/base.py @@ -27,7 +27,6 @@ class BaseAliasPluginTestCase(CMSTestCase): - def get_create_alias_endpoint(self): return admin_reverse(CREATE_ALIAS_URL_NAME) @@ -51,25 +50,36 @@ def get_list_aliascontent_endpoint(self): def setUp(self): self.superuser = self.get_superuser() - self.language = 'en' - self.page = self._create_page('test') + self.language = "en" + self.page = self._create_page("test") self.placeholder = self.page.get_placeholders(self.language).get( - slot='content', + slot="content", ) self.plugin = add_plugin( self.placeholder, - 'TextPlugin', + "TextPlugin", language=self.language, - body='test', + body="test", ) - self.category = Category.objects.create(name='test category') + self.category = Category.objects.create(name="test category") def _get_draft_page_placeholder(self): - page_content = create_title(self.language, 'Draft Page', self.page, created_by=self.superuser) - return page_content.get_placeholders().get(slot='content') + page_content = create_title( + self.language, "Draft Page", self.page, created_by=self.superuser + ) + return page_content.get_placeholders().get(slot="content") - def _create_alias(self, plugins=None, name='test alias', category=None, position=0, - language=None, published=True, static_code=None, site=None): + def _create_alias( + self, + plugins=None, + name="test alias", + category=None, + position=0, + language=None, + published=True, + static_code=None, + site=None, + ): if language is None: language = self.language if category is None: @@ -90,7 +100,10 @@ def _create_alias(self, plugins=None, name='test alias', category=None, position if is_versioning_enabled(): from djangocms_versioning.models import Version - version = Version.objects.create(content=alias_content, created_by=self.superuser) + + version = Version.objects.create( + content=alias_content, created_by=self.superuser + ) if published: version.publish(self.superuser) @@ -102,18 +115,26 @@ def _get_version(self, grouper, version_state, language=None): language = language or self.language from djangocms_versioning.models import Version - versions = Version.objects.filter_by_grouper(grouper).filter(state=version_state) + + versions = Version.objects.filter_by_grouper(grouper).filter( + state=version_state + ) for version in versions: - if hasattr(version.content, 'language') and version.content.language == language: + if ( + hasattr(version.content, "language") + and version.content.language == language + ): return version def _publish(self, grouper, language=None): from djangocms_versioning.constants import DRAFT + version = self._get_version(grouper, DRAFT, language) version.publish(self.superuser) def _unpublish(self, grouper, language=None): from djangocms_versioning.constants import PUBLISHED + version = self._get_version(grouper, PUBLISHED, language) version.unpublish(self.superuser) @@ -121,27 +142,29 @@ def _create_page(self, title, language=None, site=None, published=True, **kwargs if language is None: language = self.language - if is_versioning_enabled() and not kwargs.get('created_by'): - kwargs['created_by'] = self.superuser + if is_versioning_enabled() and not kwargs.get("created_by"): + kwargs["created_by"] = self.superuser page = create_page( title=title, language=language, - template='page.html', - menu_title='', + template="page.html", + menu_title="", in_navigation=True, limit_visibility_in_menu=None, site=site, - **kwargs + **kwargs, ) if is_versioning_enabled() and published: self._publish(page, language) return page - def get_alias_request(self, alias, lang_code='en', *args, **kwargs): + def get_alias_request(self, alias, lang_code="en", *args, **kwargs): request = self._get_instance_request(alias, *args, **kwargs) request.current_page = None - request = self._process_request_by_toolbar_middleware(request, obj=alias.get_content(lang_code)) + request = self._process_request_by_toolbar_middleware( + request, obj=alias.get_content(lang_code) + ) return request def get_page_request(self, page, obj=None, *args, **kwargs): @@ -150,9 +173,17 @@ def get_page_request(self, page, obj=None, *args, **kwargs): request = self._process_request_by_toolbar_middleware(request, obj) return request - def _get_instance_request(self, instance, user, path=None, edit=False, - preview=False, structure=False, lang_code='en', - disable=False): + def _get_instance_request( + self, + instance, + user, + path=None, + edit=False, + preview=False, + structure=False, + lang_code="en", + disable=False, + ): if not path: if edit: path = get_object_edit_url(instance) @@ -167,20 +198,20 @@ def _get_instance_request(self, instance, user, path=None, edit=False, request.session = {} request.user = user request.LANGUAGE_CODE = lang_code - request.GET = QueryDict('', mutable=True) + request.GET = QueryDict("", mutable=True) if edit: - request.GET['edit'] = None + request.GET["edit"] = None else: - request.GET['edit_off'] = None + request.GET["edit_off"] = None if disable: - request.GET[get_cms_setting('CMS_TOOLBAR_URL__DISABLE')] = None + request.GET[get_cms_setting("CMS_TOOLBAR_URL__DISABLE")] = None return request def _process_request_by_toolbar_middleware(self, request, obj=None): middleware = ToolbarMiddleware(request) middleware.process_request(request) - if hasattr(request, 'toolbar'): + if hasattr(request, "toolbar"): if obj: request.toolbar.set_object(obj) request.toolbar.populate() @@ -190,33 +221,45 @@ def _process_request_by_toolbar_middleware(self, request, obj=None): def _add_default_permissions(self, user): # Text plugin permissions - user.user_permissions.add(Permission.objects.get(codename='add_text')) - user.user_permissions.add(Permission.objects.get(codename='delete_text')) - user.user_permissions.add(Permission.objects.get(codename='change_text')) + user.user_permissions.add(Permission.objects.get(codename="add_text")) + user.user_permissions.add(Permission.objects.get(codename="delete_text")) + user.user_permissions.add(Permission.objects.get(codename="change_text")) # Page permissions - user.user_permissions.add(Permission.objects.get(codename='publish_page')) - user.user_permissions.add(Permission.objects.get(codename='add_page')) - user.user_permissions.add(Permission.objects.get(codename='change_page')) - user.user_permissions.add(Permission.objects.get(codename='delete_page')) + user.user_permissions.add(Permission.objects.get(codename="publish_page")) + user.user_permissions.add(Permission.objects.get(codename="add_page")) + user.user_permissions.add(Permission.objects.get(codename="change_page")) + user.user_permissions.add(Permission.objects.get(codename="delete_page")) def add_alias_plugin_to_page(self, page, alias, language=None): if language is None: language = self.language add_plugin( - page.get_placeholders(language).get(slot='content'), - 'Alias', + page.get_placeholders(language).get(slot="content"), + "Alias", language=language, alias=alias, ) def get_staff_user_with_alias_permissions(self): staff_user = self._create_user("alias staff", is_staff=True, is_superuser=False) # noqa: E501 - self.add_permission(staff_user, get_permission_codename('add', AliasModel._meta)) # noqa: E501 - self.add_permission(staff_user, get_permission_codename('change', AliasModel._meta)) # noqa: E501 - self.add_permission(staff_user, get_permission_codename('delete', AliasModel._meta)) # noqa: E501 - self.add_permission(staff_user, get_permission_codename('add', AliasContent._meta)) # noqa: E501 - self.add_permission(staff_user, get_permission_codename('change', AliasContent._meta)) # noqa: E501 - self.add_permission(staff_user, get_permission_codename('delete', AliasContent._meta)) # noqa: E501 - self.add_permission(staff_user, get_permission_codename('add', Category._meta)) # noqa: E501 + self.add_permission( + staff_user, get_permission_codename("add", AliasModel._meta) + ) # noqa: E501 + self.add_permission( + staff_user, get_permission_codename("change", AliasModel._meta) + ) # noqa: E501 + self.add_permission( + staff_user, get_permission_codename("delete", AliasModel._meta) + ) # noqa: E501 + self.add_permission( + staff_user, get_permission_codename("add", AliasContent._meta) + ) # noqa: E501 + self.add_permission( + staff_user, get_permission_codename("change", AliasContent._meta) + ) # noqa: E501 + self.add_permission( + staff_user, get_permission_codename("delete", AliasContent._meta) + ) # noqa: E501 + self.add_permission(staff_user, get_permission_codename("add", Category._meta)) # noqa: E501 return staff_user diff --git a/tests/test_admin.py b/tests/test_admin.py index 6b74cb5b..0289ea0d 100644 --- a/tests/test_admin.py +++ b/tests/test_admin.py @@ -21,13 +21,12 @@ class AliasContentManagerTestCase(BaseAliasPluginTestCase): - - @skipUnless(not is_versioning_enabled(), 'Test only relevant when no versioning') + @skipUnless(not is_versioning_enabled(), "Test only relevant when no versioning") def test_alias_content_manager_rendering_without_versioning_actions(self): """ When rendering aliascontent manager without versioning """ - category = Category.objects.create(name='Language Filter Category') + category = Category.objects.create(name="Language Filter Category") alias = AliasModel.objects.create( category=category, position=0, @@ -47,15 +46,12 @@ def test_alias_content_manager_rendering_without_versioning_actions(self): # Check Column Headings self.assertInHTML( - 'Category', + "Category", response_content_decoded, ) # Check Alias content row values - self.assertIn( - category.name, - response_content_decoded - ) + self.assertIn(category.name, response_content_decoded) self.assertIn( expected_en_content.name, response_content_decoded, @@ -65,12 +61,15 @@ def test_alias_content_manager_rendering_without_versioning_actions(self): response_content_decoded, ) - usage_url = admin_reverse(USAGE_ALIAS_URL_NAME, args=[expected_en_content.alias.pk]) + usage_url = admin_reverse( + USAGE_ALIAS_URL_NAME, args=[expected_en_content.alias.pk] + ) change_category_and_site_url = admin_reverse( - '{}_{}_change'.format( + "{}_{}_change".format( expected_en_content._meta.app_label, - expected_en_content.alias._meta.model_name - ), args=(expected_en_content.alias.pk,) + expected_en_content.alias._meta.model_name, + ), + args=(expected_en_content.alias.pk,), ) self.assertNotIn( @@ -83,9 +82,9 @@ def test_alias_content_manager_rendering_without_versioning_actions(self): ) # check for add content admin link add_alias_link = admin_reverse( - '{}_{}_add'.format( + "{}_{}_add".format( expected_en_content._meta.app_label, - expected_en_content._meta.model_name + expected_en_content._meta.model_name, ) ) self.assertNotIn( @@ -96,15 +95,15 @@ def test_alias_content_manager_rendering_without_versioning_actions(self): ) self.assertNotIn( '', # noqa: E501 - response_content_decoded + response_content_decoded, ) - @skipUnless(is_versioning_enabled(), 'Test only relevant for versioning') + @skipUnless(is_versioning_enabled(), "Test only relevant for versioning") def test_alias_content_manager_rendering_with_versioning_actions(self): """ When rendering aliascontent manager with versioning actions """ - category = Category.objects.create(name='Language Filter Category') + category = Category.objects.create(name="Language Filter Category") alias = AliasModel.objects.create( category=category, position=0, @@ -118,10 +117,11 @@ def test_alias_content_manager_rendering_with_versioning_actions(self): from djangocms_versioning.helpers import proxy_model from djangocms_versioning.models import Version - alias_version = Version.objects.create(content=expected_en_content, created_by=self.superuser) + alias_version = Version.objects.create( + content=expected_en_content, created_by=self.superuser + ) with self.login_user_context(self.superuser): - base_url = self.get_admin_url(AliasContent, "changelist") # en is the default language configured for the site response = self.client.get(base_url) @@ -130,31 +130,28 @@ def test_alias_content_manager_rendering_with_versioning_actions(self): # Check Column Headings self.assertInHTML( - 'Category', + "Category", response_content_decoded, ) self.assertInHTML( - 'Author', + "Author", response_content_decoded, ) self.assertInHTML( - 'Modified', + "Modified", response_content_decoded, ) self.assertInHTML( - 'State', + "State", response_content_decoded, ) self.assertInHTML( - 'Actions', + "Actions", response_content_decoded, ) # Check Alias content row values - self.assertIn( - category.name, - response_content_decoded - ) + self.assertIn(category.name, response_content_decoded) self.assertIn( expected_en_content.name, response_content_decoded, @@ -176,18 +173,22 @@ def test_alias_content_manager_rendering_with_versioning_actions(self): response_content_decoded, ) - usage_url = admin_reverse(USAGE_ALIAS_URL_NAME, args=[expected_en_content.alias.pk]) + usage_url = admin_reverse( + USAGE_ALIAS_URL_NAME, args=[expected_en_content.alias.pk] + ) change_category_and_site_url = admin_reverse( - '{}_{}_change'.format( + "{}_{}_change".format( expected_en_content._meta.app_label, - expected_en_content.alias._meta.model_name - ), args=(expected_en_content.alias.pk,) + expected_en_content.alias._meta.model_name, + ), + args=(expected_en_content.alias.pk,), ) rename_alias_url = admin_reverse( - '{}_{}_change'.format( + "{}_{}_change".format( expected_en_content._meta.app_label, - expected_en_content._meta.model_name - ), args=(expected_en_content.pk,) + expected_en_content._meta.model_name, + ), + args=(expected_en_content.pk,), ) version = proxy_model(alias_version, expected_en_content) edit_alias_url = admin_reverse( @@ -209,17 +210,14 @@ def test_alias_content_manager_rendering_with_versioning_actions(self): change_category_and_site_url, response_content_decoded, ) - self.assertIn( - edit_alias_url, - response_content_decoded - ) + self.assertIn(edit_alias_url, response_content_decoded) - @skipUnless(is_versioning_enabled(), 'Test only relevant for versioning') + @skipUnless(is_versioning_enabled(), "Test only relevant for versioning") def test_alias_content_manager_rendering_preview_add_url(self): """ When rendering aliascontent manager with versioning actions and preview """ - category = Category.objects.create(name='Language Filter Category') + category = Category.objects.create(name="Language Filter Category") alias = AliasModel.objects.create( category=category, position=0, @@ -233,7 +231,9 @@ def test_alias_content_manager_rendering_preview_add_url(self): from djangocms_versioning.helpers import proxy_model from djangocms_versioning.models import Version - alias_version = Version.objects.create(content=expected_en_content, created_by=self.superuser) + alias_version = Version.objects.create( + content=expected_en_content, created_by=self.superuser + ) version = proxy_model(alias_version, expected_en_content) edit_alias_url = admin_reverse( "{app}_{model}_edit_redirect".format( @@ -258,13 +258,13 @@ def test_alias_content_manager_rendering_preview_add_url(self): ) self.assertNotIn( '', # noqa: E501 - response_content_decoded + response_content_decoded, ) # check for add content admin link add_aliascontent_url = admin_reverse( - '{}_{}_add'.format( + "{}_{}_add".format( expected_en_content._meta.app_label, - expected_en_content._meta.model_name + expected_en_content._meta.model_name, ) ) self.assertNotIn( @@ -278,39 +278,54 @@ def _create_alias_and_categories(self, category_name, alias_content_name=None): category = Category.objects.create(name=category_name) alias = AliasModel.objects.create(category=category, position=0) alias_content = AliasContent.objects.create( - alias=alias, - name=alias_content_name, - language="en" + alias=alias, name=alias_content_name, language="en" ) return category, alias, alias_content - @skipUnless(is_versioning_enabled(), 'Test only relevant for versioning') + @skipUnless(is_versioning_enabled(), "Test only relevant for versioning") def test_category_field_ordering_versioned(self): """ Related category can be ordered by name, both in ascending and descending order, with versioning """ # Create a number of categories, aliases, and alias content to order - first_category, first_alias, first_alias_content = self._create_alias_and_categories("First Order Test Case") + ( + first_category, + first_alias, + first_alias_content, + ) = self._create_alias_and_categories("First Order Test Case") # Previously lowercase and upper case would be sorted separately, test they are ordered together - first_category_lower, first_alias_lower, first_alias_content_lower = self._create_alias_and_categories( - "first order test case lower" - ) - middle_category, middle_alias, middle_alias_content = self._create_alias_and_categories( - "Middle Order Test Case" - ) + ( + first_category_lower, + first_alias_lower, + first_alias_content_lower, + ) = self._create_alias_and_categories("first order test case lower") + ( + middle_category, + middle_alias, + middle_alias_content, + ) = self._create_alias_and_categories("Middle Order Test Case") # Previously lowercase and upper case would be sorted separately, test they are ordered together - last_category_lower, last_alias_lower, last_alias_content_lower = self._create_alias_and_categories( - "z order test case lower" - ) - last_category, last_alias, last_alias_content = self._create_alias_and_categories( - "Z Order Test Case Upper" - ) + ( + last_category_lower, + last_alias_lower, + last_alias_content_lower, + ) = self._create_alias_and_categories("z order test case lower") + ( + last_category, + last_alias, + last_alias_content, + ) = self._create_alias_and_categories("Z Order Test Case Upper") # Create the versions for each alias content from djangocms_versioning.models import Version + Version.objects.create(content=first_alias_content, created_by=self.superuser) - Version.objects.create(content=first_alias_content_lower, created_by=self.superuser) + Version.objects.create( + content=first_alias_content_lower, created_by=self.superuser + ) Version.objects.create(content=middle_alias_content, created_by=self.superuser) - Version.objects.create(content=last_alias_content_lower, created_by=self.superuser) + Version.objects.create( + content=last_alias_content_lower, created_by=self.superuser + ) Version.objects.create(content=last_alias_content, created_by=self.superuser) with self.login_user_context(self.superuser): @@ -345,27 +360,39 @@ def test_category_field_ordering_versioned(self): self.assertEqual(results[1].text, last_alias_content_lower.name) self.assertEqual(results[0].text, last_alias_content.name) - @skipUnless(not is_versioning_enabled(), 'Test only relevant for versioning') + @skipUnless(not is_versioning_enabled(), "Test only relevant for versioning") def test_category_field_ordering_unversioned(self): """ Related category can be ordered by name, both in ascending and descending order, without versioning """ # Create a number of categories, aliases, and alias content to order - first_category, first_alias, first_alias_content = self._create_alias_and_categories("First Order Test Case") + ( + first_category, + first_alias, + first_alias_content, + ) = self._create_alias_and_categories("First Order Test Case") # Previously lowercase and upper case would be sorted separately, test they are ordered together - first_category_lower, first_alias_lower, first_alias_content_lower = self._create_alias_and_categories( - "first order test case lower" - ) - middle_category, middle_alias, middle_alias_content = self._create_alias_and_categories( - "Middle Order Test Case" - ) + ( + first_category_lower, + first_alias_lower, + first_alias_content_lower, + ) = self._create_alias_and_categories("first order test case lower") + ( + middle_category, + middle_alias, + middle_alias_content, + ) = self._create_alias_and_categories("Middle Order Test Case") # Previously lowercase and upper case would be sorted separately, test they are ordered together - last_category_lower, last_alias_lower, last_alias_content_lower = self._create_alias_and_categories( - "z order test case lower" - ) - last_category, last_alias, last_alias_content = self._create_alias_and_categories( - "Z Order Test Case Upper" - ) + ( + last_category_lower, + last_alias_lower, + last_alias_content_lower, + ) = self._create_alias_and_categories("z order test case lower") + ( + last_category, + last_alias, + last_alias_content, + ) = self._create_alias_and_categories("Z Order Test Case Upper") with self.login_user_context(self.superuser): base_url = self.get_admin_url(AliasContent, "changelist") @@ -405,32 +432,32 @@ def test_aliascontent_list_view(self): about the objects """ category1 = Category.objects.create( - name='Category 1', + name="Category 1", ) category2 = Category.objects.create( - name='Category 2', + name="Category 2", ) plugin = add_plugin( self.placeholder, - 'TextPlugin', + "TextPlugin", language=self.language, - body='This is basic content', + body="This is basic content", ) alias1 = self._create_alias( [plugin], - name='Alias 1', + name="Alias 1", category=category1, ) alias2 = self._create_alias( [plugin], - name='Alias 2', + name="Alias 2", category=category2, ) alias3 = self._create_alias( [plugin], - name='Alias 3', + name="Alias 3", category=category1, published=False, ) @@ -443,9 +470,9 @@ def test_aliascontent_list_view(self): self.assertEqual(response.status_code, 200) self.assertContains(response, category1.name) self.assertContains(response, category2.name) - self.assertContains(response, 'Alias 1') - self.assertContains(response, 'Alias 2') - self.assertContains(response, 'Alias 3') + self.assertContains(response, "Alias 1") + self.assertContains(response, "Alias 2") + self.assertContains(response, "Alias 3") if is_versioning_enabled(): # we have both published and draft aliases so both should @@ -482,50 +509,49 @@ def test_aliascontent_list_view(self): class CategoryAdminViewsTestCase(BaseAliasPluginTestCase): - def test_changelist(self): Category.objects.all().delete() category1 = Category.objects.create() category2 = Category.objects.create() - category1.translations.create(language_code='en', name='Category 1') - category2.translations.create(language_code='en', name='Category 2') - category1.translations.create(language_code='de', name='Kategorie 1') - category2.translations.create(language_code='fr', name='Catégorie 2') - category1.translations.create(language_code='it', name='Categoria 1') + category1.translations.create(language_code="en", name="Category 1") + category2.translations.create(language_code="en", name="Category 2") + category1.translations.create(language_code="de", name="Kategorie 1") + category2.translations.create(language_code="fr", name="Catégorie 2") + category1.translations.create(language_code="it", name="Categoria 1") with self.login_user_context(self.superuser): - with force_language('en'): + with force_language("en"): en_response = self.client.get(self.get_category_list_endpoint()) - with force_language('de'): + with force_language("de"): de_response = self.client.get(self.get_category_list_endpoint()) - with force_language('fr'): + with force_language("fr"): fr_response = self.client.get(self.get_category_list_endpoint()) - with force_language('it'): + with force_language("it"): it_response = self.client.get(self.get_category_list_endpoint()) - self.assertContains(en_response, 'Category 1') - self.assertContains(en_response, 'Category 2') - self.assertNotContains(en_response, 'Kategorie 1') - self.assertNotContains(en_response, 'Catégorie 2') - self.assertNotContains(en_response, 'Categoria 1') - - self.assertContains(de_response, 'Kategorie 1') - self.assertContains(de_response, 'Category 2') # fallback - self.assertNotContains(de_response, 'Category 1') - self.assertNotContains(de_response, 'Catégorie 2') - self.assertNotContains(de_response, 'Categoria 1') - - self.assertContains(fr_response, 'Category 1') # fallback - self.assertContains(fr_response, 'Catégorie 2') - self.assertNotContains(fr_response, 'Category 2') - self.assertNotContains(fr_response, 'Kategorie 1') - self.assertNotContains(fr_response, 'Categoria 2') - - self.assertContains(it_response, 'Catégorie 2') # fallback - self.assertNotContains(it_response, 'Category 1') - self.assertNotContains(it_response, 'Category 2') - self.assertNotContains(it_response, 'Kategorie 1') - self.assertNotContains(it_response, 'Categoria 2') + self.assertContains(en_response, "Category 1") + self.assertContains(en_response, "Category 2") + self.assertNotContains(en_response, "Kategorie 1") + self.assertNotContains(en_response, "Catégorie 2") + self.assertNotContains(en_response, "Categoria 1") + + self.assertContains(de_response, "Kategorie 1") + self.assertContains(de_response, "Category 2") # fallback + self.assertNotContains(de_response, "Category 1") + self.assertNotContains(de_response, "Catégorie 2") + self.assertNotContains(de_response, "Categoria 1") + + self.assertContains(fr_response, "Category 1") # fallback + self.assertContains(fr_response, "Catégorie 2") + self.assertNotContains(fr_response, "Category 2") + self.assertNotContains(fr_response, "Kategorie 1") + self.assertNotContains(fr_response, "Categoria 2") + + self.assertContains(it_response, "Catégorie 2") # fallback + self.assertNotContains(it_response, "Category 1") + self.assertNotContains(it_response, "Category 2") + self.assertNotContains(it_response, "Kategorie 1") + self.assertNotContains(it_response, "Categoria 2") def test_changelist_standard_user(self): """ @@ -543,9 +569,11 @@ def test_changelist_staff_user_without_permission(self): def test_changelist_staff_user_with_permission(self): user = self.get_staff_user_with_std_permissions() - user.user_permissions.add(Permission.objects.get( - content_type__app_label='djangocms_alias', - codename='change_category')) + user.user_permissions.add( + Permission.objects.get( + content_type__app_label="djangocms_alias", codename="change_category" + ) + ) with self.login_user_context(user): response = self.client.get(self.get_category_list_endpoint()) self.assertEqual(response.status_code, 200) @@ -555,8 +583,7 @@ def test_changelist_edit_button(self): response = self.client.get(self.get_category_list_endpoint()) self.assertContains( - response, - 'Aliases') + self.assertNotContains(response, "Aliases") def test_add_aliases_submenu_to_admin_menu(self): user = self.get_staff_user_with_std_permissions() - user.user_permissions.add(Permission.objects.get( - content_type__app_label='djangocms_alias', - codename='change_category')) + user.user_permissions.add( + Permission.objects.get( + content_type__app_label="djangocms_alias", codename="change_category" + ) + ) page_url = get_object_edit_url(self.page.get_title_obj(self.language)) with self.login_user_context(user): response = self.client.get(page_url) - self.assertContains(response, 'Aliases') + self.assertContains(response, "Aliases") def test_aliases_link_placement(self): request = self.get_page_request(self.page, user=self.superuser) admin_menu = request.toolbar.get_or_create_menu(ADMIN_MENU_IDENTIFIER) break_item = admin_menu.find_first(Break, identifier=ADMINISTRATION_BREAK) # noqa: E501 item_positioned_before_admin_break = admin_menu.items[break_item.index - 1] # noqa: E501 - self.assertEqual(item_positioned_before_admin_break.name, 'Aliases...') + self.assertEqual(item_positioned_before_admin_break.name, "Aliases...") def test_add_alias_menu_showing_only_on_alias_plugin_views(self): alias = self._create_alias([self.plugin]) @@ -72,20 +67,24 @@ def test_add_alias_menu_showing_only_on_alias_plugin_views(self): self.get_list_aliascontent_endpoint(), self.page.get_absolute_url(language=self.language), ]: - request = self.get_page_request(page=None, path=endpoint, user=self.superuser) + request = self.get_page_request( + page=None, path=endpoint, user=self.superuser + ) alias_menu = request.toolbar.get_menu(ALIAS_MENU_IDENTIFIER) self.assertEqual(alias_menu, None) def _test_alias_endpoint(**kwargs): - kwargs.update({ - 'alias': alias, - 'path': endpoint, - 'user': self.superuser, - }) + kwargs.update( + { + "alias": alias, + "path": endpoint, + "user": self.superuser, + } + ) # py34 compat request = self.get_alias_request(**ChainMap(kwargs)) alias_menu = request.toolbar.get_menu(ALIAS_MENU_IDENTIFIER) - self.assertEqual(alias_menu.name, 'Alias') + self.assertEqual(alias_menu.name, "Alias") _test_alias_endpoint() _test_alias_endpoint(edit=True) @@ -127,18 +126,18 @@ def test_alias_toolbar_language_menu(self): menu.name: [menu_item.name for menu_item in menu.items] for key, menu in language_menu.menus.items() } - self.assertIn('Add Translation', language_menu_dict.keys()) - self.assertIn('Delete Translation', language_menu_dict.keys()) + self.assertIn("Add Translation", language_menu_dict.keys()) + self.assertIn("Delete Translation", language_menu_dict.keys()) self.assertEqual( - set(['Deutsche...', 'Française...', 'Italiano...']), - set(language_menu_dict['Add Translation']), + {"Deutsche...", "Française...", "Italiano..."}, + set(language_menu_dict["Add Translation"]), ) self.assertEqual( - set(['English...']), - set(language_menu_dict['Delete Translation']), + {"English..."}, + set(language_menu_dict["Delete Translation"]), ) - alias_content = alias.contents.create(name='test alias 2', language='fr') + alias_content = alias.contents.create(name="test alias 2", language="fr") alias_content.populate(replaced_placeholder=self.placeholder) alias_content.alias.clear_cache() @@ -147,7 +146,8 @@ def test_alias_toolbar_language_menu(self): from djangocms_versioning.models import Version Version.objects.create( - content=alias_content, created_by=self.superuser, state=PUBLISHED) + content=alias_content, created_by=self.superuser, state=PUBLISHED + ) request = self.get_alias_request( alias=alias, @@ -162,59 +162,64 @@ def test_alias_toolbar_language_menu(self): for key, menu in language_menu.menus.items() } self.assertEqual( - set(['Deutsche...', 'Italiano...']), - set(language_menu_dict['Add Translation']), + {"Deutsche...", "Italiano..."}, + set(language_menu_dict["Add Translation"]), ) self.assertEqual( - set(['Française...', 'English...']), - set(language_menu_dict['Delete Translation']), + {"Française...", "English..."}, + set(language_menu_dict["Delete Translation"]), ) self.assertEqual( - set(['from Française']), - set(language_menu_dict['Copy all plugins']), + {"from Française"}, + set(language_menu_dict["Copy all plugins"]), ) language_menu_first_items = { - menu.name: next(filter( - lambda item: item.name in ['Française...', 'Deutsche...', 'from Française'], - menu.items, - )) + menu.name: next( + filter( + lambda item: item.name + in ["Française...", "Deutsche...", "from Française"], + menu.items, + ) + ) for key, menu in language_menu.menus.items() } # First item is Deutsche... for Add Translation self.assertIn( - '/en/admin/djangocms_alias/aliascontent/add/', - language_menu_first_items['Add Translation'].url, + "/en/admin/djangocms_alias/aliascontent/add/", + language_menu_first_items["Add Translation"].url, ) self.assertIn( - 'language=de', - language_menu_first_items['Add Translation'].url, + "language=de", + language_menu_first_items["Add Translation"].url, ) self.assertIn( - 'alias={}'.format(alias.pk), - language_menu_first_items['Add Translation'].url, + f"alias={alias.pk}", + language_menu_first_items["Add Translation"].url, ) self.assertEqual( # First item is Française... for Delete Translation - '/en/admin/djangocms_alias/aliascontent/{}/delete/?language=fr'.format( - alias.get_content('fr').pk, + "/en/admin/djangocms_alias/aliascontent/{}/delete/?language=fr".format( + alias.get_content("fr").pk, ), - language_menu_first_items['Delete Translation'].url, + language_menu_first_items["Delete Translation"].url, ) self.assertRegex( - language_menu_first_items['Copy all plugins'].action, - r'en\/admin\/([\w\/]+)\/copy-plugins\/', + language_menu_first_items["Copy all plugins"].action, + r"en\/admin\/([\w\/]+)\/copy-plugins\/", ) def test_language_switcher_when_toolbar_object_is_alias_content(self): alias = self._create_alias([self.plugin]) - alias_content = alias.contents.create(name='test alias 2', language='fr') - expected_result = ['English', 'Française'] + alias_content = alias.contents.create(name="test alias 2", language="fr") + expected_result = ["English", "Française"] if is_versioning_enabled(): from djangocms_versioning.constants import DRAFT from djangocms_versioning.models import Version + Version.objects.create( - content=alias_content, created_by=self.superuser, state=DRAFT) + content=alias_content, created_by=self.superuser, state=DRAFT + ) alias_content.populate(replaced_placeholder=self.placeholder) alias_content.alias.clear_cache() @@ -234,22 +239,20 @@ def test_language_switcher_when_toolbar_object_isnt_alias_content(self): preview=True, ) language_menu = request.toolbar.get_menu(LANGUAGE_MENU_IDENTIFIER) - expected_result = ['English', 'Deutsche', 'Française', 'Italiano'] + expected_result = ["English", "Deutsche", "Française", "Italiano"] # Versioning changes the toolbar language selector and only shows # languages that have translations if is_versioning_enabled(): - expected_result = ['English'] + expected_result = ["English"] # Dont change default language switcher that is used for Pages - self.assertEqual( - [item.name for item in language_menu.items], expected_result - ) + self.assertEqual([item.name for item in language_menu.items], expected_result) def test_change_alias_settings_button_is_visible_on_alias_edit_view(self): - button_label = 'Change alias settings...' - alias_change_viewname = 'djangocms_alias_alias_change' + button_label = "Change alias settings..." + alias_change_viewname = "djangocms_alias_alias_change" alias = self._create_alias() - with force_language('en'): + with force_language("en"): request = self.get_alias_request( alias=alias, user=self.superuser, @@ -259,7 +262,7 @@ def test_change_alias_settings_button_is_visible_on_alias_edit_view(self): search_result = alias_menu.find_first(item_type=ModalItem, name=button_label) self.assertIsNotNone(search_result) button = search_result.item - self.assertEqual(button.on_close, 'REFRESH_PAGE') + self.assertEqual(button.on_close, "REFRESH_PAGE") self.assertEqual( button.url, admin_reverse( @@ -275,7 +278,7 @@ def test_alias_usage_button(self): user=self.superuser, edit=True, ) - button_label = 'View usage...' + button_label = "View usage..." alias_menu = request.toolbar.get_menu(ALIAS_MENU_IDENTIFIER) search_result = alias_menu.find_first(item_type=ModalItem, name=button_label) self.assertIsNotNone(search_result) @@ -289,7 +292,7 @@ def test_alias_usage_button(self): ) self.assertEqual( button.on_close, - 'REFRESH_PAGE', + "REFRESH_PAGE", ) def test_create_wizard_button_enabled(self): @@ -301,7 +304,7 @@ def test_create_wizard_button_enabled(self): create_button = self._get_wizard_create_button(request) self.assertEqual(create_button.disabled, False) - @skipUnless(not is_versioning_enabled(), 'Test only relevant when no versioning') + @skipUnless(not is_versioning_enabled(), "Test only relevant when no versioning") def test_delete_button_show_on_edit_alias_view_no_versioning(self): """ When versioning is not installed deletion should be possible. The delete button @@ -313,7 +316,7 @@ def test_delete_button_show_on_edit_alias_view_no_versioning(self): user=self.superuser, edit=True, ) - button_label = 'Delete alias...' + button_label = "Delete alias..." alias_menu = request.toolbar.get_menu(ALIAS_MENU_IDENTIFIER) search_result = alias_menu.find_first(item_type=ModalItem, name=button_label) @@ -329,7 +332,7 @@ def test_delete_button_show_on_edit_alias_view_no_versioning(self): self.get_list_aliascontent_endpoint(), ) - @skipUnless(is_versioning_enabled(), 'Test only relevant for versioning') + @skipUnless(is_versioning_enabled(), "Test only relevant for versioning") def test_delete_button_not_shown_on_edit_alias_view_with_versioning(self): """ When versioning is installed no deletion should be possible. The delete button @@ -341,7 +344,7 @@ def test_delete_button_not_shown_on_edit_alias_view_with_versioning(self): user=self.superuser, edit=True, ) - button_label = 'Delete alias...' + button_label = "Delete alias..." alias_menu = request.toolbar.get_menu(ALIAS_MENU_IDENTIFIER) search_result = alias_menu.find_first(item_type=ModalItem, name=button_label) @@ -355,17 +358,20 @@ def test_rename_alias_show_on_edit_alias_view(self): user=self.superuser, edit=True, ) - button_label = 'Rename alias...' + button_label = "Rename alias..." alias_menu = request.toolbar.get_menu(ALIAS_MENU_IDENTIFIER) search_result = alias_menu.find_first(item_type=ModalItem, name=button_label) self.assertIsNotNone(search_result) button = search_result.item self.assertEqual(button.name, button_label) - self.assertEqual(button.url, admin_reverse( - 'djangocms_alias_aliascontent_change', - args=[alias.get_content().pk], - )) - self.assertEqual(button.on_close, 'REFRESH_PAGE') + self.assertEqual( + button.url, + admin_reverse( + "djangocms_alias_aliascontent_change", + args=[alias.get_content().pk], + ), + ) + self.assertEqual(button.on_close, "REFRESH_PAGE") def test_disable_buttons_when_in_preview_mode(self): alias = self._create_alias() @@ -378,7 +384,7 @@ def test_disable_buttons_when_in_preview_mode(self): search_results = alias_menu.find_items(item_type=ModalItem) self.assertNotEqual(bool(search_results), False) for result in search_results: - if result.item.name == 'View usage...': + if result.item.name == "View usage...": self.assertEqual(result.item.disabled, False) else: self.assertEqual(result.item.disabled, True) @@ -395,7 +401,7 @@ def test_disable_buttons_when_not_have_perms(self): search_results = alias_menu.find_items(item_type=ModalItem) self.assertNotEqual(bool(search_results), False) for result in search_results: - if result.item.name == 'View usage...': + if result.item.name == "View usage...": self.assertEqual(result.item.disabled, False) else: self.assertEqual(result.item.disabled, True) @@ -423,7 +429,7 @@ def test_site_dropdown_url_renders_admin_changelist(self): site_aliases_url, ) - content = response.content.decode('utf-8') + content = response.content.decode("utf-8") self.assertEqual(response.status_code, 200) # Rendered content should contain admin changelist header @@ -433,8 +439,8 @@ def test_site_dropdown_url_renders_admin_changelist_url(self): request = self.get_page_request(self.page, user=self.superuser) admin_menu = request.toolbar.get_or_create_menu(ADMIN_MENU_IDENTIFIER) site_aliases_url = admin_menu.items[3].url - admin_changelist_aliases_url = reverse("admin:{}_aliascontent_changelist".format( - AliasContent._meta.app_label) + admin_changelist_aliases_url = reverse( + "admin:{}_aliascontent_changelist".format(AliasContent._meta.app_label) ) with self.login_user_context(self.superuser): diff --git a/tests/test_views.py b/tests/test_views.py index 2d05ed46..ccc7295d 100644 --- a/tests/test_views.py +++ b/tests/test_views.py @@ -27,7 +27,6 @@ class AliasViewsTestCase(BaseAliasPluginTestCase): - def test_create_alias_view_get_no_data(self): with self.login_user_context(self.superuser): response = self.client.get(self.get_create_alias_endpoint()) @@ -39,25 +38,31 @@ def test_create_alias_view_non_staff_denied_access(self): def test_create_alias_view_get_show_form_plugin(self): with self.login_user_context(self.superuser): - response = self.client.get(self.get_create_alias_endpoint(), data={ - 'plugin': self.plugin.pk, - 'language': self.language, - }) + response = self.client.get( + self.get_create_alias_endpoint(), + data={ + "plugin": self.plugin.pk, + "language": self.language, + }, + ) self.assertEqual(response.status_code, 200) self.assertEqual( - response.context['form'].initial['plugin'].pk, + response.context["form"].initial["plugin"].pk, self.plugin.pk, ) def test_create_alias_view_get_show_form_placeholder(self): with self.login_user_context(self.superuser): - response = self.client.get(self.get_create_alias_endpoint(), data={ - 'placeholder': self.placeholder.pk, - 'language': self.language, - }) + response = self.client.get( + self.get_create_alias_endpoint(), + data={ + "placeholder": self.placeholder.pk, + "language": self.language, + }, + ) self.assertEqual(response.status_code, 200) self.assertEqual( - response.context['form'].initial['placeholder'], + response.context["form"].initial["placeholder"], self.placeholder, ) @@ -68,27 +73,33 @@ def test_create_alias_view_show_form_replace_hidden(self): content_type=ContentType.objects.get_for_model( Alias, ), - codename='add_alias', + codename="add_alias", ) ) with self.login_user_context(user): - response = self.client.get(self.get_create_alias_endpoint(), data={ - 'plugin': self.plugin.pk, - 'language': self.language, - }) + response = self.client.get( + self.get_create_alias_endpoint(), + data={ + "plugin": self.plugin.pk, + "language": self.language, + }, + ) self.assertEqual(response.status_code, 200) self.assertTrue( - response.context['form'].fields['replace'].widget.is_hidden, + response.context["form"].fields["replace"].widget.is_hidden, ) def test_create_alias_view_post_plugin(self): with self.login_user_context(self.superuser): - response = self.client.post(self.get_create_alias_endpoint(), data={ - 'plugin': self.plugin.pk, - 'category': self.category.pk, - 'language': self.language, - 'name': 'test alias', - }) + response = self.client.post( + self.get_create_alias_endpoint(), + data={ + "plugin": self.plugin.pk, + "category": self.category.pk, + "language": self.language, + "name": "test alias", + }, + ) self.assertEqual(response.status_code, 200) alias = Alias.objects.last() @@ -117,19 +128,22 @@ def test_create_alias_view_post_plugin_replace(self): plugin = add_plugin( placeholder, - 'TextPlugin', - language='en', - body='test 222', + "TextPlugin", + language="en", + body="test 222", ) with self.login_user_context(self.superuser): - response = self.client.post(self.get_create_alias_endpoint(), data={ - 'plugin': plugin.pk, - 'category': self.category.pk, - 'name': 'test alias', - 'language': self.language, - 'replace': True, - }) + response = self.client.post( + self.get_create_alias_endpoint(), + data={ + "plugin": plugin.pk, + "category": self.category.pk, + "name": "test alias", + "language": self.language, + "replace": True, + }, + ) self.assertEqual(response.status_code, 200) @@ -147,66 +161,81 @@ def test_create_alias_view_post_plugin_replace(self): def test_create_alias_view_name(self): with self.login_user_context(self.superuser): - response = self.client.post(self.get_create_alias_endpoint(), data={ - 'plugin': self.plugin.pk, - 'category': self.category.pk, - 'name': 'test alias', - 'language': self.language, - }) + response = self.client.post( + self.get_create_alias_endpoint(), + data={ + "plugin": self.plugin.pk, + "category": self.category.pk, + "name": "test alias", + "language": self.language, + }, + ) self.assertEqual(response.status_code, 200) alias = Alias.objects.last() if is_versioning_enabled(): self._publish(alias) - self.assertEqual(alias.name, 'test alias') + self.assertEqual(alias.name, "test alias") def test_create_alias_view_post_no_plugin_or_placeholder(self): with self.login_user_context(self.superuser): - response = self.client.post(self.get_create_alias_endpoint(), data={ - 'category': self.category.pk, - 'name': 'test alias', - 'language': self.language, - }) + response = self.client.post( + self.get_create_alias_endpoint(), + data={ + "category": self.category.pk, + "name": "test alias", + "language": self.language, + }, + ) self.assertEqual(response.status_code, 200) - self.assertFalse(response.context['form'].is_valid()) + self.assertFalse(response.context["form"].is_valid()) def test_create_alias_view_post_both_plugin_and_placeholder(self): with self.login_user_context(self.superuser): - response = self.client.post(self.get_create_alias_endpoint(), data={ - 'plugin': self.plugin.pk, - 'placeholder': self.placeholder.pk, - 'category': self.category.pk, - 'name': 'test alias', - 'language': self.language, - }) + response = self.client.post( + self.get_create_alias_endpoint(), + data={ + "plugin": self.plugin.pk, + "placeholder": self.placeholder.pk, + "category": self.category.pk, + "name": "test alias", + "language": self.language, + }, + ) self.assertEqual(response.status_code, 200) - self.assertFalse(response.context['form'].is_valid()) + self.assertFalse(response.context["form"].is_valid()) def test_create_alias_view_post_empty_placeholder(self): - placeholder = Placeholder(slot='empty') + placeholder = Placeholder(slot="empty") placeholder.save() with self.login_user_context(self.superuser): - response = self.client.post(self.get_create_alias_endpoint(), data={ - 'placeholder': placeholder.pk, - 'category': self.category.pk, - 'name': 'test alias', - 'language': self.language, - }) + response = self.client.post( + self.get_create_alias_endpoint(), + data={ + "placeholder": placeholder.pk, + "category": self.category.pk, + "name": "test alias", + "language": self.language, + }, + ) self.assertEqual(response.status_code, 400) self.assertEqual( response.content.decode(), - 'Plugins are required to create an alias', + "Plugins are required to create an alias", ) def test_create_alias_view_post_placeholder(self): with self.login_user_context(self.superuser): - response = self.client.post(self.get_create_alias_endpoint(), data={ - 'placeholder': self.placeholder.pk, - 'category': self.category.pk, - 'name': 'test alias', - 'language': self.language, - }) + response = self.client.post( + self.get_create_alias_endpoint(), + data={ + "placeholder": self.placeholder.pk, + "category": self.category.pk, + "name": "test alias", + "language": self.language, + }, + ) self.assertEqual(response.status_code, 200) # Source plugins are kept in original placeholder @@ -239,19 +268,22 @@ def test_create_alias_view_post_placeholder_replace(self): placeholder.get_plugins().delete() text_plugin = add_plugin( placeholder, - 'TextPlugin', - language='en', - body='test 2', + "TextPlugin", + language="en", + body="test 2", ) with self.login_user_context(self.superuser): - response = self.client.post(self.get_create_alias_endpoint(), data={ - 'placeholder': placeholder.pk, - 'category': self.category.pk, - 'name': 'test alias', - 'language': self.language, - 'replace': True, - }) + response = self.client.post( + self.get_create_alias_endpoint(), + data={ + "placeholder": placeholder.pk, + "category": self.category.pk, + "name": "test alias", + "language": self.language, + "replace": True, + }, + ) self.assertEqual(response.status_code, 200) alias = Alias.objects.last() @@ -276,12 +308,15 @@ def test_create_alias_view_post_placeholder_replace(self): def test_create_alias_view_post_no_create_permission(self): with self.login_user_context(self.get_staff_user_with_no_permissions()): # noqa: E501 - response = self.client.post(self.get_create_alias_endpoint(), data={ - 'plugin': self.plugin.pk, - 'category': self.category.pk, - 'name': 'test alias', - 'language': self.language, - }) + response = self.client.post( + self.get_create_alias_endpoint(), + data={ + "plugin": self.plugin.pk, + "category": self.category.pk, + "name": "test alias", + "language": self.language, + }, + ) self.assertEqual(response.status_code, 403) def test_create_alias_view_post_no_replace_permission(self): @@ -291,17 +326,20 @@ def test_create_alias_view_post_no_replace_permission(self): content_type=ContentType.objects.get_for_model( Alias, ), - codename='add_alias', + codename="add_alias", ) ) with self.login_user_context(user): - response = self.client.post(self.get_create_alias_endpoint(), data={ - 'plugin': self.plugin.pk, - 'category': self.category.pk, - 'name': 'test alias', - 'language': self.language, - 'replace': True, - }) + response = self.client.post( + self.get_create_alias_endpoint(), + data={ + "plugin": self.plugin.pk, + "category": self.category.pk, + "name": "test alias", + "language": self.language, + "replace": True, + }, + ) self.assertEqual(response.status_code, 403) def test_detach_view_no_permission_to_add_plugins_from_alias(self): @@ -314,8 +352,8 @@ def test_detach_view_get(self): alias = self._create_alias([self.plugin]) plugin = add_plugin( self.placeholder, - 'Alias', - language='en', + "Alias", + language="en", alias=alias, ) with self.login_user_context(self.superuser): @@ -328,8 +366,8 @@ def test_detach_view_non_staff_denied_access(self): alias = self._create_alias([self.plugin]) plugin = add_plugin( self.placeholder, - 'Alias', - language='en', + "Alias", + language="en", alias=alias, ) user = self.get_staff_user_with_no_permissions() @@ -356,27 +394,27 @@ def test_detach_view(self): alias = self._create_alias() add_plugin( placeholder, - 'TextPlugin', + "TextPlugin", language=self.language, - body='test', + body="test", ) plugin = add_plugin( placeholder, - 'Alias', - language='en', + "Alias", + language="en", alias=alias, ) add_plugin( alias.get_placeholder(self.language), - 'TextPlugin', + "TextPlugin", language=self.language, - body='test 2', + body="test 2", ) add_plugin( alias.get_placeholder(self.language), - 'TextPlugin', + "TextPlugin", language=self.language, - body='test 88', + body="test 88", ) plugins = placeholder.get_plugins() self.assertEqual(plugins.count(), 2) @@ -389,9 +427,9 @@ def test_detach_view(self): plugins = placeholder.get_plugins() self.assertEqual(plugins.count(), 3) - self.assertEqual(plugins[0].get_bound_plugin().body, 'test') - self.assertEqual(plugins[1].get_bound_plugin().body, 'test 2') - self.assertEqual(plugins[2].get_bound_plugin().body, 'test 88') + self.assertEqual(plugins[0].get_bound_plugin().body, "test") + self.assertEqual(plugins[1].get_bound_plugin().body, "test 2") + self.assertEqual(plugins[2].get_bound_plugin().body, "test 88") def test_alias_content_preview_view(self): alias = self._create_alias([self.plugin]) @@ -403,27 +441,37 @@ def test_alias_content_preview_view(self): self.assertContains(response, self.plugin.body) def test_view_aliases_using_site_filter(self): - site1 = Site.objects.create(domain='site1.com', name='1') - site2 = Site.objects.create(domain='site2.com', name='2') + site1 = Site.objects.create(domain="site1.com", name="1") + site2 = Site.objects.create(domain="site2.com", name="2") site1_plugin = add_plugin( self.placeholder, - 'TextPlugin', - language='en', - body='This is text in English', + "TextPlugin", + language="en", + body="This is text in English", ) site2_plugin = add_plugin( self.placeholder, - 'TextPlugin', - language='en', - body='Das ist Text auf Deutsch', + "TextPlugin", + language="en", + body="Das ist Text auf Deutsch", + ) + site1_alias = self._create_alias( + plugins=[site1_plugin], + site=site1, + name="site1_alias", + category=self.category, + ) + site2_alias = self._create_alias( + plugins=[site2_plugin], + site=site2, + name="site2_alias", + category=self.category, ) - site1_alias = self._create_alias(plugins=[site1_plugin], site=site1, name='site1_alias', category=self.category) - site2_alias = self._create_alias(plugins=[site2_plugin], site=site2, name='site2_alias', category=self.category) aliascontent_list_url = admin_reverse(LIST_ALIASCONTENT_URL_NAME) # when no filter used both objects are displayed with self.login_user_context(self.superuser): - with force_language('en'): + with force_language("en"): list_response = self.client.get(aliascontent_list_url) self.assertContains(list_response, site1_alias.name) @@ -431,8 +479,10 @@ def test_view_aliases_using_site_filter(self): # when no filtering by site 1 only first object displayed with self.login_user_context(self.superuser): - with force_language('en'): - site1_aliases_filter_url = f"{aliascontent_list_url}?site={site1_alias.site.id}" + with force_language("en"): + site1_aliases_filter_url = ( + f"{aliascontent_list_url}?site={site1_alias.site.id}" + ) list_response = self.client.get(site1_aliases_filter_url) self.assertContains(list_response, site1_alias.name) @@ -440,35 +490,40 @@ def test_view_aliases_using_site_filter(self): # when no filtering by site 2 only first object displayed with self.login_user_context(self.superuser): - with force_language('en'): - site2_aliases_filter_url = f"{aliascontent_list_url}?site={site2_alias.site.id}" + with force_language("en"): + site2_aliases_filter_url = ( + f"{aliascontent_list_url}?site={site2_alias.site.id}" + ) list_response = self.client.get(site2_aliases_filter_url) self.assertNotContains(list_response, site1_alias.name) self.assertContains(list_response, site2_alias.name) - @skipIf(is_versioning_enabled(), 'Test only relevant without versioning enabled') + @skipIf(is_versioning_enabled(), "Test only relevant without versioning enabled") def test_create_alias_name_unique_per_category_and_language(self): self._create_alias( - name='test alias', + name="test alias", category=self.category, ) with self.login_user_context(self.superuser): - response = self.client.post(self.get_create_alias_endpoint(), data={ - 'plugin': self.plugin.pk, - 'category': self.category.pk, - 'name': 'test alias', - 'language': self.language, - }) + response = self.client.post( + self.get_create_alias_endpoint(), + data={ + "plugin": self.plugin.pk, + "category": self.category.pk, + "name": "test alias", + "language": self.language, + }, + ) self.assertEqual(response.status_code, 200) self.assertContains( response, - 'Alias with this Name and Category already exists.', + "Alias with this Name and Category already exists.", ) self.assertEqual( AliasContent.objects.filter( - name='test alias', + name="test alias", language=self.language, alias__category=self.category, ).count(), @@ -484,21 +539,22 @@ def test_select2_view_no_permission(self): self.assertEqual(response.status_code, 403) def test_select2_view(self): - alias1 = self._create_alias(name='test 2') - alias2 = self._create_alias(name='foo', position=1) - alias3 = self._create_alias(name='foo4', position=1, published=False) + alias1 = self._create_alias(name="test 2") + alias2 = self._create_alias(name="foo", position=1) + alias3 = self._create_alias(name="foo4", position=1, published=False) if is_versioning_enabled(): from djangocms_versioning.constants import DRAFT from djangocms_versioning.models import Version # This will show because it's a new draft version of the same alias - draft_content = alias2.contents.create(name='foo', language=self.language) + draft_content = alias2.contents.create(name="foo", language=self.language) Version.objects.create( - content=draft_content, created_by=self.superuser, state=DRAFT) + content=draft_content, created_by=self.superuser, state=DRAFT + ) # This shouldn't show because it hasn't content in current language - self._create_alias(name='foo2', language='fr', position=1) + self._create_alias(name="foo2", language="fr", position=1) with self.login_user_context(self.superuser): response = self.client.get( admin_reverse( @@ -507,29 +563,29 @@ def test_select2_view(self): ) result = [alias1.pk, alias2.pk, alias3.pk] - text_result = ['test 2'] + text_result = ["test 2"] if is_versioning_enabled(): # The following versions have draft content - text_result.append('foo (Not published)') - text_result.append('foo4 (Not published)') + text_result.append("foo (Not published)") + text_result.append("foo4 (Not published)") else: - text_result.append('foo') - text_result.append('foo4') + text_result.append("foo") + text_result.append("foo4") self.assertEqual(response.status_code, 200) - self.assertEqual([a['id'] for a in response.json()['results']], result) + self.assertEqual([a["id"] for a in response.json()["results"]], result) self.assertEqual( - [a['text'] for a in response.json()['results']], + [a["text"] for a in response.json()["results"]], text_result, ) def test_select2_view_order_by_category_and_position(self): - category2 = Category.objects.create(name='foo') - alias1 = self._create_alias(name='test 2') - alias2 = self._create_alias(name='foo', position=1) - alias3 = self._create_alias(name='bar', category=category2) - alias4 = self._create_alias(name='baz', category=category2, position=1) + category2 = Category.objects.create(name="foo") + alias1 = self._create_alias(name="test 2") + alias2 = self._create_alias(name="foo", position=1) + alias3 = self._create_alias(name="bar", category=category2) + alias4 = self._create_alias(name="baz", category=category2, position=1) with self.login_user_context(self.superuser): response = self.client.get( admin_reverse( @@ -539,30 +595,30 @@ def test_select2_view_order_by_category_and_position(self): self.assertEqual(response.status_code, 200) self.assertEqual( - [a['id'] for a in response.json()['results']], + [a["id"] for a in response.json()["results"]], [alias3.pk, alias4.pk, alias1.pk, alias2.pk], ) def test_select2_view_set_limit(self): - self._create_alias(name='test 2') - self._create_alias(name='foo', position=1) - self._create_alias(name='three', position=2) + self._create_alias(name="test 2") + self._create_alias(name="foo", position=1) + self._create_alias(name="three", position=2) with self.login_user_context(self.superuser): response = self.client.get( admin_reverse( SELECT2_ALIAS_URL_NAME, ), - data={'limit': 2}, + data={"limit": 2}, ) content = response.json() self.assertEqual(response.status_code, 200) - self.assertTrue(content['more']) - self.assertEqual(len(content['results']), 2) + self.assertTrue(content["more"]) + self.assertEqual(len(content["results"]), 2) def test_select2_view_text_repr(self): - alias1 = self._create_alias(name='test 2') + alias1 = self._create_alias(name="test 2") with self.login_user_context(self.superuser): response = self.client.get( admin_reverse( @@ -572,64 +628,64 @@ def test_select2_view_text_repr(self): self.assertEqual(response.status_code, 200) self.assertEqual( - response.json()['results'][0]['text'], + response.json()["results"][0]["text"], alias1.name, ) def test_select2_view_term(self): - alias1 = self._create_alias(name='test 2') - self._create_alias(name='foo', position=1) - alias3 = self._create_alias(name='three', position=2) + alias1 = self._create_alias(name="test 2") + self._create_alias(name="foo", position=1) + alias3 = self._create_alias(name="three", position=2) with self.login_user_context(self.superuser): response = self.client.get( admin_reverse( SELECT2_ALIAS_URL_NAME, ), - data={'term': 't'}, + data={"term": "t"}, ) self.assertEqual(response.status_code, 200) self.assertEqual( - [a['id'] for a in response.json()['results']], + [a["id"] for a in response.json()["results"]], [alias1.pk, alias3.pk], ) def test_select2_view_category(self): - category2 = Category.objects.create(name='test 2') - alias1 = self._create_alias(name='test 2', category=category2) - alias2 = self._create_alias(name='foo', category=category2, position=1) + category2 = Category.objects.create(name="test 2") + alias1 = self._create_alias(name="test 2", category=category2) + alias2 = self._create_alias(name="foo", category=category2, position=1) # This shouldnt show because it's in different Category - self._create_alias(name='three') + self._create_alias(name="three") with self.login_user_context(self.superuser): response = self.client.get( admin_reverse( SELECT2_ALIAS_URL_NAME, ), - data={'category': category2.pk}, + data={"category": category2.pk}, ) self.assertEqual(response.status_code, 200) self.assertEqual( - [a['id'] for a in response.json()['results']], + [a["id"] for a in response.json()["results"]], [alias1.pk, alias2.pk], ) def test_select2_view_category_and_term(self): - category2 = Category.objects.create(name='test 2') - alias1 = self._create_alias(name='test 2', category=category2) - self._create_alias(name='foo', category=category2, position=1) - self._create_alias(name='three') + category2 = Category.objects.create(name="test 2") + alias1 = self._create_alias(name="test 2", category=category2) + self._create_alias(name="foo", category=category2, position=1) + self._create_alias(name="three") with self.login_user_context(self.superuser): response = self.client.get( admin_reverse( SELECT2_ALIAS_URL_NAME, ), - data={'category': category2.pk, 'term': 't'}, + data={"category": category2.pk, "term": "t"}, ) self.assertEqual(response.status_code, 200) self.assertEqual( - [a['id'] for a in response.json()['results']], + [a["id"] for a in response.json()["results"]], [alias1.pk], ) @@ -648,12 +704,12 @@ def test_select2_view_site(self): admin_reverse( SELECT2_ALIAS_URL_NAME, ), - data={'site': site.pk}, + data={"site": site.pk}, ) self.assertEqual(response.status_code, 200) self.assertEqual( - [a['id'] for a in response.json()['results']], + [a["id"] for a in response.json()["results"]], [alias1.pk, alias2.pk], ) @@ -662,7 +718,7 @@ def test_select2_view_site_and_category(self): The list is filtered based on only matching alias with a specific site and category if it is provided """ - category = Category.objects.create(name='category') + category = Category.objects.create(name="category") site = get_current_site() self._create_alias(site=site) alias2 = self._create_alias(site=site, category=category, position=1) @@ -675,32 +731,32 @@ def test_select2_view_site_and_category(self): SELECT2_ALIAS_URL_NAME, ), data={ - 'site': site.pk, - 'category': category.pk, + "site": site.pk, + "category": category.pk, }, ) self.assertEqual(response.status_code, 200) self.assertEqual( - [a['id'] for a in response.json()['results']], + [a["id"] for a in response.json()["results"]], [alias2.pk], ) def test_select2_view_pk(self): - alias1 = self._create_alias(name='test 2') - self._create_alias(name='foo', position=1) - self._create_alias(name='three') + alias1 = self._create_alias(name="test 2") + self._create_alias(name="foo", position=1) + self._create_alias(name="three") with self.login_user_context(self.superuser): response = self.client.get( admin_reverse( SELECT2_ALIAS_URL_NAME, ), - data={'pk': alias1.pk}, + data={"pk": alias1.pk}, ) self.assertEqual(response.status_code, 200) self.assertEqual( - [a['id'] for a in response.json()['results']], + [a["id"] for a in response.json()["results"]], [alias1.pk], ) @@ -712,21 +768,21 @@ def test_aliascontent_add_view(self): alias = Alias.objects.create(category=self.category) with self.login_user_context(self.superuser): response = self.client.post( - admin_reverse('djangocms_alias_aliascontent_add'), + admin_reverse("djangocms_alias_aliascontent_add"), data={ - 'language': 'de', - 'name': 'alias test de 1', - 'alias': alias.pk, + "language": "de", + "name": "alias test de 1", + "alias": alias.pk, }, ) self.assertEqual(response.status_code, 302) if is_versioning_enabled(): - self._publish(alias, 'de') + self._publish(alias, "de") self.assertEqual(alias.contents.count(), 1) alias_content = alias.contents.first() - self.assertEqual(alias_content.language, 'de') - self.assertEqual(alias_content.name, 'alias test de 1') + self.assertEqual(alias_content.language, "de") + self.assertEqual(alias_content.name, "alias test de 1") @skip( "It is not currently possible to add an alias from the django admin changelist issue " @@ -738,15 +794,15 @@ def test_aliascontent_add_view_get(self): response = self.client.get( add_url_parameters( admin_reverse( - 'djangocms_alias_aliascontent_add', + "djangocms_alias_aliascontent_add", ), - language='fr', + language="fr", alias=alias.pk, ) ) self.assertContains(response, 'type="hidden" name="language" value="fr"') - self.assertContains(response, 'type="hidden" name="alias" value="{}"'.format(alias.pk)) + self.assertContains(response, f'type="hidden" name="alias" value="{alias.pk}"') @skip( "It is not currently possible to add an alias from the django admin changelist issue " @@ -755,25 +811,25 @@ def test_aliascontent_add_view_get(self): def test_aliascontent_add_view_invalid_data(self): alias = Alias.objects.create(category=self.category) self._create_alias( - name='test alias', + name="test alias", category=self.category, language=self.language, published=True, ) with self.login_user_context(self.superuser): response = self.client.post( - admin_reverse('djangocms_alias_aliascontent_add'), + admin_reverse("djangocms_alias_aliascontent_add"), data={ - 'language': self.language, - 'name': 'test alias', - 'alias': alias.pk, + "language": self.language, + "name": "test alias", + "alias": alias.pk, }, ) self.assertEqual(response.status_code, 200) self.assertContains( response, - 'Alias with this Name and Category already exists', + "Alias with this Name and Category already exists", ) @skip( @@ -784,18 +840,18 @@ def test_aliascontent_add_view_valid_data(self): alias = Alias.objects.create(category=self.category) if is_versioning_enabled(): self._create_alias( - name='test alias', + name="test alias", category=self.category, language=self.language, published=False, ) with self.login_user_context(self.superuser): response = self.client.post( - admin_reverse('djangocms_alias_aliascontent_add'), + admin_reverse("djangocms_alias_aliascontent_add"), data={ - 'language': self.language, - 'name': 'test alias', - 'alias': alias.pk, + "language": self.language, + "name": "test alias", + "alias": alias.pk, }, ) @@ -803,7 +859,7 @@ def test_aliascontent_add_view_valid_data(self): if is_versioning_enabled(): self._publish(alias) alias_content = alias.contents.first() - self.assertEqual(alias_content.name, 'test alias') + self.assertEqual(alias_content.name, "test alias") def test_alias_usage_view(self): alias = self._create_alias() @@ -817,12 +873,12 @@ def test_alias_usage_view(self): ), ) - self.assertContains(response, 'Page') - self.assertNotContains(response, 'Alias') + self.assertContains(response, "Page") + self.assertNotContains(response, "Alias") add_plugin( root_alias.get_placeholder(self.language), - 'Alias', + "Alias", language=self.language, alias=alias, ) @@ -835,8 +891,8 @@ def test_alias_usage_view(self): ), ) - self.assertContains(response, 'Page') - self.assertContains(response, 'Alias') + self.assertContains(response, "Page") + self.assertContains(response, "Alias") self.assertRegex( str(response.content), r'href="{}"[\w+]?>{}<\/a>'.format( @@ -863,7 +919,7 @@ def test_alias_usage_view(self): back=1, ) ), - 'View usage', + "View usage", ), ) @@ -871,7 +927,7 @@ def test_delete_alias_view_get(self): alias = self._create_alias([self.plugin]) add_plugin( self.placeholder, - 'Alias', + "Alias", language=self.language, alias=alias, ) @@ -884,14 +940,14 @@ def test_delete_alias_view_get(self): ) self.assertContains( response, - 'Are you sure you want to delete the alias "{}"?'.format(alias.name), # noqa: E501 + f'Are you sure you want to delete the alias "{alias.name}"?', # noqa: E501 ) def test_delete_alias_view_get_using_objects(self): alias = self._create_alias([self.plugin]) add_plugin( self.placeholder, - 'Alias', + "Alias", language=self.language, alias=alias, ) @@ -902,8 +958,10 @@ def test_delete_alias_view_get_using_objects(self): args=[alias.pk], ), ) - self.assertContains(response, 'This alias is used by following objects:') - test = r'
  • [\s\\n]*Page:[\s\\n]*test<\/a>[\s\\n]*<\/li>' + self.assertContains(response, "This alias is used by following objects:") + test = ( + r"
  • [\s\\n]*Page:[\s\\n]*test<\/a>[\s\\n]*<\/li>" + ) self.assertRegex(str(response.content), test) def test_delete_alias_view_get_alias_not_used_on_any_page(self): @@ -915,10 +973,11 @@ def test_delete_alias_view_get_alias_not_used_on_any_page(self): args=[alias.pk], ), ) - self.assertContains(response, 'This alias wasn\'t used by any object.') + self.assertContains(response, "This alias wasn't used by any object.") def test_delete_alias_view_post(self): from djangocms_alias.views import JAVASCRIPT_SUCCESS_RESPONSE + alias = self._create_alias([self.plugin]) self.assertIn(alias, Alias.objects.all()) with self.login_user_context(self.superuser): @@ -927,7 +986,7 @@ def test_delete_alias_view_post(self): DELETE_ALIAS_URL_NAME, args=[alias.pk], ), - data={'post': 'yes'}, + data={"post": "yes"}, ) self.assertContains(response, JAVASCRIPT_SUCCESS_RESPONSE) self.assertFalse(Alias.objects.filter(pk=alias.pk).exists()) @@ -950,7 +1009,7 @@ def test_delete_alias_view_user_with_no_perms(self): DELETE_ALIAS_URL_NAME, args=[alias.pk], ), - data={'post': 'yes'}, + data={"post": "yes"}, ) self.assertEqual(response.status_code, 403) @@ -964,7 +1023,7 @@ def test_delete_alias_view_alias_not_being_used(self): args=[alias.pk], ), ) - self.assertContains(response, 'Are you sure you want to delete') + self.assertContains(response, "Are you sure you want to delete") self.assertContains(response, 'type="submit"') with self.login_user_context(staff_user): # noqa: E501 response = self.client.post( # noqa @@ -972,7 +1031,7 @@ def test_delete_alias_view_alias_not_being_used(self): DELETE_ALIAS_URL_NAME, args=[alias.pk], ), - data={'post': 'yes'}, + data={"post": "yes"}, ) self.assertEqual(response.status_code, 200) self.assertFalse(Alias.objects.filter(pk=alias.pk).exists()) @@ -981,13 +1040,13 @@ def test_delete_alias_view_alias_being_used_on_pages(self): alias = self._create_alias([self.plugin]) add_plugin( self.placeholder, - 'Alias', + "Alias", language=self.language, alias=alias, ) # this user only can delete alias when alias not being used anywhere staff_user = self.get_staff_user_with_no_permissions() - self.add_permission(staff_user, 'delete_alias') + self.add_permission(staff_user, "delete_alias") with self.login_user_context(staff_user): # noqa: E501 response = self.client.get( @@ -1004,7 +1063,7 @@ def test_delete_alias_view_alias_being_used_on_pages(self): DELETE_ALIAS_URL_NAME, args=[alias.pk], ), - data={'post': 'yes'}, + data={"post": "yes"}, ) self.assertEqual(response.status_code, 403) @@ -1012,41 +1071,41 @@ def test_custom_template_alias_view(self): alias = self._create_alias() add_plugin( alias.get_placeholder(self.language), - 'TextPlugin', + "TextPlugin", language=self.language, - body='custom alias content', + body="custom alias content", ) add_plugin( self.placeholder, - 'Alias', + "Alias", language=self.language, alias=alias, - template='custom_alias_template', + template="custom_alias_template", ) response = self.client.get(self.page.get_absolute_url()) - self.assertContains(response, 'custom alias content') + self.assertContains(response, "custom alias content") @override_settings(CMS_PLACEHOLDER_CACHE=False) def test_alias_multisite_support(self): - site1 = Site.objects.create(domain='site1.com', name='1') - site2 = Site.objects.create(domain='site2.com', name='2') + site1 = Site.objects.create(domain="site1.com", name="1") + site2 = Site.objects.create(domain="site2.com", name="2") alias = self._create_alias() alias_placeholder = alias.get_placeholder(self.language) add_plugin( alias_placeholder, - 'TextPlugin', + "TextPlugin", language=self.language, - body='test alias multisite', + body="test alias multisite", ) site1_page = self._create_page( - title='Site1', + title="Site1", language=self.language, site=site1, ) site2_page = self._create_page( - title='Site2', + title="Site2", language=self.language, site=site2, ) @@ -1055,32 +1114,31 @@ def test_alias_multisite_support(self): with override_settings(SITE_ID=site1.pk): response = self.client.get(site1_page.get_absolute_url()) - self.assertContains(response, 'test alias multisite') + self.assertContains(response, "test alias multisite") with override_settings(SITE_ID=site2.pk): response = self.client.get(site2_page.get_absolute_url()) - self.assertContains(response, 'test alias multisite') + self.assertContains(response, "test alias multisite") add_plugin( alias_placeholder, - 'TextPlugin', + "TextPlugin", language=self.language, - body='Another alias plugin', + body="Another alias plugin", ) with override_settings(SITE_ID=site1.pk): response = self.client.get(site1_page.get_absolute_url()) - self.assertContains(response, 'test alias multisite') - self.assertContains(response, 'Another alias plugin') + self.assertContains(response, "test alias multisite") + self.assertContains(response, "Another alias plugin") with override_settings(SITE_ID=site2.pk): response = self.client.get(site2_page.get_absolute_url()) - self.assertContains(response, 'test alias multisite') - self.assertContains(response, 'Another alias plugin') + self.assertContains(response, "test alias multisite") + self.assertContains(response, "Another alias plugin") class AliasCategorySelect2ViewTestCase(BaseAliasPluginTestCase): - def test_select2_view_no_permission(self): """ The category list view is private @@ -1098,9 +1156,9 @@ def test_select2_view_alias_not_set(self): When categories exist but are not attached to an alias they are ignored """ - Category.objects.create(name='Category 1') - category_2 = Category.objects.create(name='Category 2') - Category.objects.create(name='Category 3') + Category.objects.create(name="Category 1") + category_2 = Category.objects.create(name="Category 2") + Category.objects.create(name="Category 3") self._create_alias(category=category_2) with self.login_user_context(self.superuser): @@ -1113,7 +1171,7 @@ def test_select2_view_alias_not_set(self): expected_result = [category_2.pk] self.assertEqual(response.status_code, 200) - self.assertEqual([a['id'] for a in response.json()['results']], expected_result) + self.assertEqual([a["id"] for a in response.json()["results"]], expected_result) def test_select2_view_alias_site_set(self): """ @@ -1121,9 +1179,9 @@ def test_select2_view_alias_site_set(self): are returned """ site = get_current_site() - category_1 = Category.objects.create(name='Category 1') - category_2 = Category.objects.create(name='Category 2') - category_3 = Category.objects.create(name='Category 3') + category_1 = Category.objects.create(name="Category 1") + category_2 = Category.objects.create(name="Category 2") + category_3 = Category.objects.create(name="Category 3") self._create_alias(category=category_1, site=site) self._create_alias(category=category_2) self._create_alias(category=category_3) @@ -1133,21 +1191,21 @@ def test_select2_view_alias_site_set(self): admin_reverse( CATEGORY_SELECT2_URL_NAME, ), - data={'site': site.pk}, + data={"site": site.pk}, ) expected_result = [category_1.pk] self.assertEqual(response.status_code, 200) - self.assertEqual([a['id'] for a in response.json()['results']], expected_result) + self.assertEqual([a["id"] for a in response.json()["results"]], expected_result) def test_select2_view_set_limit(self): """ Ensure that the page limit is respected """ - category_1 = Category.objects.create(name='Category 1') - category_2 = Category.objects.create(name='Category 2') - category_3 = Category.objects.create(name='Category 3') + category_1 = Category.objects.create(name="Category 1") + category_2 = Category.objects.create(name="Category 2") + category_3 = Category.objects.create(name="Category 3") self._create_alias(category=category_1) self._create_alias(category=category_2, position=1) self._create_alias(category=category_3, position=2) @@ -1157,22 +1215,22 @@ def test_select2_view_set_limit(self): admin_reverse( CATEGORY_SELECT2_URL_NAME, ), - data={'limit': 2}, + data={"limit": 2}, ) content = response.json() self.assertEqual(response.status_code, 200) - self.assertTrue(content['more']) - self.assertEqual(len(content['results']), 2) + self.assertTrue(content["more"]) + self.assertEqual(len(content["results"]), 2) def test_select2_view_text_repr(self): """ Ensure that the display / text representation of the object is output to the user. """ - category = Category.objects.create(name='Category 1') - self._create_alias(name='test 2', category=category) + category = Category.objects.create(name="Category 1") + self._create_alias(name="test 2", category=category) with self.login_user_context(self.superuser): response = self.client.get( @@ -1183,7 +1241,7 @@ def test_select2_view_text_repr(self): self.assertEqual(response.status_code, 200) self.assertEqual( - response.json()['results'][0]['text'], + response.json()["results"][0]["text"], category.name, ) @@ -1192,10 +1250,10 @@ def test_select2_view_term(self): Given a term, the response should return only categories that match the term. """ - category_1 = Category.objects.create(name='ategory 1') - category_2 = Category.objects.create(name='Category 2') - category_3 = Category.objects.create(name='tegory 3') - category_4 = Category.objects.create(name='tegory 4') + category_1 = Category.objects.create(name="ategory 1") + category_2 = Category.objects.create(name="Category 2") + category_3 = Category.objects.create(name="tegory 3") + category_4 = Category.objects.create(name="tegory 4") self._create_alias(category=category_1) self._create_alias(category=category_2) self._create_alias(category=category_3) @@ -1206,12 +1264,12 @@ def test_select2_view_term(self): admin_reverse( CATEGORY_SELECT2_URL_NAME, ), - data={'term': 'ate'}, + data={"term": "ate"}, ) self.assertEqual(response.status_code, 200) self.assertEqual( - [a['id'] for a in response.json()['results']], + [a["id"] for a in response.json()["results"]], [category_2.pk, category_1.pk], ) @@ -1219,9 +1277,9 @@ def test_select2_view_pk(self): """ When a pk is provided that record should be returned """ - category_1 = Category.objects.create(name='Category 1') - category_2 = Category.objects.create(name='Category 2') - category_3 = Category.objects.create(name='Category 3') + category_1 = Category.objects.create(name="Category 1") + category_2 = Category.objects.create(name="Category 2") + category_3 = Category.objects.create(name="Category 3") self._create_alias(category=category_1) self._create_alias(category=category_2) self._create_alias(category=category_3) @@ -1231,117 +1289,132 @@ def test_select2_view_pk(self): admin_reverse( CATEGORY_SELECT2_URL_NAME, ), - data={'pk': category_2.pk}, + data={"pk": category_2.pk}, ) self.assertEqual(response.status_code, 200) self.assertEqual( - [a['id'] for a in response.json()['results']], + [a["id"] for a in response.json()["results"]], [category_2.pk], ) class AliasViewsUsingVersioningTestCase(BaseAliasPluginTestCase): - - @skipUnless(is_versioning_enabled(), 'Test only relevant for versioning') + @skipUnless(is_versioning_enabled(), "Test only relevant for versioning") def test_create_alias_view_name_draft_alias(self): with self.login_user_context(self.superuser): - name = 'test alias' - response = self.client.post(self.get_create_alias_endpoint(), data={ - 'plugin': self.plugin.pk, - 'category': self.category.pk, - 'name': name, - 'language': self.language, - }) + name = "test alias" + response = self.client.post( + self.get_create_alias_endpoint(), + data={ + "plugin": self.plugin.pk, + "category": self.category.pk, + "name": name, + "language": self.language, + }, + ) self.assertEqual(response.status_code, 200) alias = Alias.objects.last() # AliasContent not published - self.assertEqual(alias.name, '{} (Not published)'.format(name)) + self.assertEqual(alias.name, f"{name} (Not published)") - @skipUnless(is_versioning_enabled(), 'Test only relevant for versioning') + @skipUnless(is_versioning_enabled(), "Test only relevant for versioning") def test_create_alias_view_creating_version(self): with self.login_user_context(self.superuser): - response = self.client.post(self.get_create_alias_endpoint(), data={ - 'plugin': self.plugin.pk, - 'category': self.category.pk, - 'name': 'test alias', - 'language': self.language, - }) + response = self.client.post( + self.get_create_alias_endpoint(), + data={ + "plugin": self.plugin.pk, + "category": self.category.pk, + "name": "test alias", + "language": self.language, + }, + ) self.assertEqual(response.status_code, 200) alias = Alias.objects.last() if is_versioning_enabled(): from djangocms_versioning.models import Version + self.assertEqual(Version.objects.filter_by_grouper(alias).count(), 1) - @skipUnless(is_versioning_enabled(), 'Test only relevant for versioning') + @skipUnless(is_versioning_enabled(), "Test only relevant for versioning") def test_create_alias_name_without_uniqness(self): alias1 = self._create_alias( - name='test alias', + name="test alias", category=self.category, published=True, ) with self.login_user_context(self.superuser): - response = self.client.post(self.get_create_alias_endpoint(), data={ - 'plugin': self.plugin.pk, - 'category': self.category.pk, - 'name': 'test alias', - 'language': self.language, - }) + response = self.client.post( + self.get_create_alias_endpoint(), + data={ + "plugin": self.plugin.pk, + "category": self.category.pk, + "name": "test alias", + "language": self.language, + }, + ) self.assertEqual(response.status_code, 200) self.assertContains( response, - 'Alias with this Name and Category already exists.', + "Alias with this Name and Category already exists.", ) self._unpublish(alias1) with self.login_user_context(self.superuser): - response = self.client.post(self.get_create_alias_endpoint(), data={ - 'plugin': self.plugin.pk, - 'category': self.category.pk, - 'name': 'test alias', - 'language': self.language, - }) + response = self.client.post( + self.get_create_alias_endpoint(), + data={ + "plugin": self.plugin.pk, + "category": self.category.pk, + "name": "test alias", + "language": self.language, + }, + ) self.assertEqual(response.status_code, 200) self.assertNotContains( response, - 'Alias with this Name and Category already exists.', + "Alias with this Name and Category already exists.", ) alias = Alias.objects.last() self._publish(alias) qs = AliasContent.objects.filter( - name='test alias', + name="test alias", language=self.language, alias__category=self.category, ) self.assertEqual(qs.count(), 1) self.assertEqual(qs.first(), alias.get_content(self.language)) - @skipUnless(is_versioning_enabled(), 'Test only relevant for versioning') + @skipUnless(is_versioning_enabled(), "Test only relevant for versioning") def test_create_alias_view_post_placeholder_from_draft_page(self): - page1 = self._create_page('test alias page') + page1 = self._create_page("test alias page") placeholder = page1.get_placeholders(self.language).get( - slot='content', + slot="content", ) plugin = add_plugin( placeholder, - 'TextPlugin', + "TextPlugin", language=self.language, - body='test alias', + body="test alias", ) self._unpublish(page1) with self.login_user_context(self.superuser): - response = self.client.post(self.get_create_alias_endpoint(), data={ - 'placeholder': placeholder.pk, - 'category': self.category.pk, - 'name': 'test alias', - 'language': self.language, - }) + response = self.client.post( + self.get_create_alias_endpoint(), + data={ + "placeholder": placeholder.pk, + "category": self.category.pk, + "name": "test alias", + "language": self.language, + }, + ) self.assertEqual(response.status_code, 200) # Source plugins are kept in original placeholder @@ -1364,27 +1437,30 @@ def test_create_alias_view_post_placeholder_from_draft_page(self): target.get_bound_plugin().body, ) - @skipUnless(is_versioning_enabled(), 'Test only relevant for versioning') + @skipUnless(is_versioning_enabled(), "Test only relevant for versioning") def test_create_alias_with_replace_plugin_with_versioning_checks(self): # 403 if you try to edit placeholder of published page with self.login_user_context(self.superuser): - response = self.client.post(self.get_create_alias_endpoint(), data={ - 'placeholder': self.placeholder.pk, - 'category': self.category.pk, - 'name': 'test alias 5', - 'language': self.language, - 'replace': True, - }) + response = self.client.post( + self.get_create_alias_endpoint(), + data={ + "placeholder": self.placeholder.pk, + "category": self.category.pk, + "name": "test alias 5", + "language": self.language, + "replace": True, + }, + ) self.assertEqual(response.status_code, 403) - @skipUnless(is_versioning_enabled(), 'Test only relevant for versioning') + @skipUnless(is_versioning_enabled(), "Test only relevant for versioning") def test_detach_view_with_versioning_checks(self): # 403 when placeholder from non-draft page alias = self._create_alias() plugin = add_plugin( self.placeholder, - 'Alias', - language='en', + "Alias", + language="en", alias=alias, ) with self.login_user_context(self.superuser): @@ -1393,7 +1469,7 @@ def test_detach_view_with_versioning_checks(self): ) self.assertEqual(response.status_code, 403) - @skipUnless(is_versioning_enabled(), 'Only valid in versioning scenario') + @skipUnless(is_versioning_enabled(), "Only valid in versioning scenario") def test_alias_not_shown_when_draft_when_visiting_page(self): """ When visiting a published page with a draft alias the alias @@ -1407,16 +1483,11 @@ def test_alias_not_shown_when_draft_when_visiting_page(self): ).first() alias_placeholder = content.placeholder - body = 'unpublished alias' - add_plugin( - alias_placeholder, - 'TextPlugin', - language=self.language, - body=body - ) + body = "unpublished alias" + add_plugin(alias_placeholder, "TextPlugin", language=self.language, body=body) page = self._create_page( - title='New page', + title="New page", language=self.language, ) @@ -1430,50 +1501,57 @@ def test_view_multilanguage(self): """ en_plugin = add_plugin( self.placeholder, - 'TextPlugin', - language='en', - body='This is text in English', + "TextPlugin", + language="en", + body="This is text in English", ) de_plugin = add_plugin( self.placeholder, - 'TextPlugin', - language='de', - body='Das ist Text auf Deutsch', + "TextPlugin", + language="de", + body="Das ist Text auf Deutsch", ) fr_plugin = add_plugin( self.placeholder, - 'TextPlugin', - language='fr', - body='C\'est le texte en français', + "TextPlugin", + language="fr", + body="C'est le texte en français", ) alias = self._create_alias([en_plugin], name="English AliasContent object") alias_content_de = AliasContent.objects.create( alias=alias, - name='German AliasContent object', - language='de', + name="German AliasContent object", + language="de", ) alias_content_de.populate(plugins=[de_plugin]) alias_content_fr = AliasContent.objects.create( alias=alias, - name='French AliasContent object', - language='fr', + name="French AliasContent object", + language="fr", ) alias_content_fr.populate(plugins=[fr_plugin]) # when versioning is enabled a Version must be created and published for each language if is_versioning_enabled(): from djangocms_versioning.models import Version - version_de = Version.objects.create(content=alias_content_de, created_by=self.superuser) + + version_de = Version.objects.create( + content=alias_content_de, created_by=self.superuser + ) version_de.publish(user=self.superuser) - version_fr = Version.objects.create(content=alias_content_fr, created_by=self.superuser) + version_fr = Version.objects.create( + content=alias_content_fr, created_by=self.superuser + ) version_fr.publish(user=self.superuser) with self.login_user_context(self.superuser): - with force_language('en'): + with force_language("en"): if is_versioning_enabled(): # we need to call get_absolute_url on the AliasContent object when versioning is enabled, # otherwise we are taken to the version list url - detail_response = self.client.get(alias.get_content(language="en").get_absolute_url()) + detail_response = self.client.get( + alias.get_content(language="en").get_absolute_url() + ) else: detail_response = self.client.get(alias.get_absolute_url()) list_response = self.client.get( @@ -1487,11 +1565,13 @@ def test_view_multilanguage(self): self.assertNotContains(list_response, alias_content_de.name) with self.login_user_context(self.superuser): - with force_language('de'): + with force_language("de"): if is_versioning_enabled(): # we need to call get_absolute_url on the AliasContent object when versioning is enabled, # otherwise we are taken to the version list url - detail_response = self.client.get(alias_content_de.get_absolute_url()) + detail_response = self.client.get( + alias_content_de.get_absolute_url() + ) else: detail_response = self.client.get(alias.get_absolute_url()) list_response = self.client.get( @@ -1505,11 +1585,13 @@ def test_view_multilanguage(self): self.assertNotContains(list_response, alias.name) with self.login_user_context(self.superuser): - with force_language('fr'): + with force_language("fr"): if is_versioning_enabled(): # we need to call get_absolute_url on the AliasContent object when versioning is enabled, # otherwise we are taken to the version list url - detail_response = self.client.get(alias_content_fr.get_absolute_url()) + detail_response = self.client.get( + alias_content_fr.get_absolute_url() + ) else: detail_response = self.client.get(alias.get_absolute_url()) list_response = self.client.get( diff --git a/tests/test_wizards.py b/tests/test_wizards.py index 5df58ac7..42803484 100644 --- a/tests/test_wizards.py +++ b/tests/test_wizards.py @@ -12,7 +12,6 @@ class WizardsTestCase(BaseAliasPluginTestCase): - def _get_wizard_instance(self, wizard_name): return [ wizard @@ -22,22 +21,24 @@ def _get_wizard_instance(self, wizard_name): def _get_form_kwargs(self, data, language=None): language = language or self.language - request = self.get_request('/', language=language) + request = self.get_request("/", language=language) request.user = self.superuser return { - 'data': data, - 'wizard_language': language, - 'wizard_site': Site.objects.get_current(), - 'wizard_request': request, + "data": data, + "wizard_language": language, + "wizard_site": Site.objects.get_current(), + "wizard_request": request, } def test_create_alias_wizard_instance(self): - wizard = self._get_wizard_instance('CreateAliasWizard') - self.assertEqual(wizard.title, 'New alias') + wizard = self._get_wizard_instance("CreateAliasWizard") + self.assertEqual(wizard.title, "New alias") self.assertTrue(wizard.user_has_add_permission(self.superuser)) self.assertTrue( - wizard.user_has_add_permission(self.get_staff_user_with_alias_permissions()), # noqa: E501 + wizard.user_has_add_permission( + self.get_staff_user_with_alias_permissions() + ), # noqa: E501 ) self.assertFalse( wizard.user_has_add_permission(self.get_staff_user_with_no_permissions()), # noqa: E501 @@ -47,11 +48,11 @@ def test_create_alias_wizard_instance(self): ) def test_create_alias_wizard_form(self): - wizard = self._get_wizard_instance('CreateAliasWizard') + wizard = self._get_wizard_instance("CreateAliasWizard") data = { - 'name': 'Content #1', - 'category': self.category.pk, - 'site': get_current_site().pk + "name": "Content #1", + "category": self.category.pk, + "site": get_current_site().pk, } form_class = step2_form_factory( mixin_cls=WizardStep2BaseForm, @@ -62,42 +63,43 @@ def test_create_alias_wizard_form(self): self.assertTrue(form.is_valid()) alias = form.save() - self.assertEqual(form.fields['site'].initial, get_current_site()) + self.assertEqual(form.fields["site"].initial, get_current_site()) with self.login_user_context(self.superuser): response = self.client.get(alias.get_absolute_url()) - self.assertContains(response, data['name']) + self.assertContains(response, data["name"]) if is_versioning_enabled(): from djangocms_versioning.models import Version + self.assertEqual(Version.objects.filter_by_grouper(alias).count(), 1) def test_create_alias_wizard_form_with_no_category_fallback_language(self): """When creating an Alias via the Wizard an error can occur if the category doesn't have a valid translation """ - translation.activate('en') + translation.activate("en") # A japanese translation that does not have any fallback settings! - Category.objects.language('ja').create(name='Japanese category') + Category.objects.language("ja").create(name="Japanese category") - wizard = self._get_wizard_instance('CreateAliasWizard') + wizard = self._get_wizard_instance("CreateAliasWizard") data = { - 'name': 'Content #1', - 'category': None, + "name": "Content #1", + "category": None, } form_class = step2_form_factory( mixin_cls=WizardStep2BaseForm, entry_form_class=wizard.form, ) form = form_class(**self._get_form_kwargs(data)) - category_form_queryset = form.declared_fields['category'].queryset + category_form_queryset = form.declared_fields["category"].queryset - self.assertEqual(form.fields['site'].initial, get_current_site()) + self.assertEqual(form.fields["site"].initial, get_current_site()) # Be sure that we have untranslated categories that enforces a fair test self.assertNotEqual( category_form_queryset.all().count(), - category_form_queryset.active_translations().count() + category_form_queryset.active_translations().count(), ) for category in category_form_queryset.all(): @@ -107,12 +109,14 @@ def test_create_alias_wizard_form_with_no_category_fallback_language(self): self.assertTrue(category_name) def test_create_alias_category_wizard_instance(self): - wizard = self._get_wizard_instance('CreateAliasCategoryWizard') - self.assertEqual(wizard.title, 'New alias category') + wizard = self._get_wizard_instance("CreateAliasCategoryWizard") + self.assertEqual(wizard.title, "New alias category") self.assertTrue(wizard.user_has_add_permission(self.superuser)) self.assertTrue( - wizard.user_has_add_permission(self.get_staff_user_with_alias_permissions()), # noqa: E501 + wizard.user_has_add_permission( + self.get_staff_user_with_alias_permissions() + ), # noqa: E501 ) self.assertFalse( wizard.user_has_add_permission(self.get_staff_user_with_no_permissions()), # noqa: E501 @@ -122,9 +126,9 @@ def test_create_alias_category_wizard_instance(self): ) def test_create_alias_category_wizard_form(self): - wizard = self._get_wizard_instance('CreateAliasCategoryWizard') + wizard = self._get_wizard_instance("CreateAliasCategoryWizard") data = { - 'name': 'Category 1', + "name": "Category 1", } form_class = step2_form_factory( @@ -138,4 +142,4 @@ def test_create_alias_category_wizard_form(self): with self.login_user_context(self.superuser): response = self.client.get(category.get_absolute_url()) - self.assertContains(response, data['name']) + self.assertContains(response, data["name"])