Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Sync pre-commit checks (#216) #226

Merged
merged 4 commits into from
Feb 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 5 additions & 14 deletions djangocms_alias/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,7 @@ def get_actions_list(self) -> list:
"""Add alias usage list actions"""
return super().get_actions_list() + [self._get_alias_usage_link]

def can_change_content(
self, request: HttpRequest, content_obj: AliasContent
) -> bool:
def can_change_content(self, request: HttpRequest, content_obj: AliasContent) -> bool:
"""Returns True if user can change content_obj"""
if content_obj and is_versioning_enabled():
version = Version.objects.get_for_content(content_obj)
Expand All @@ -111,9 +109,7 @@ def has_delete_permission(self, request: HttpRequest, obj: Alias = None) -> bool
return request.user.is_superuser
return False

def save_model(
self, request: HttpRequest, obj: Alias, form: forms.Form, change: bool
) -> None:
def save_model(self, request: HttpRequest, obj: Alias, form: forms.Form, change: bool) -> None:
super().save_model(request, obj, form, change)

# Only emit content changes if Versioning is not installed because
Expand Down Expand Up @@ -147,9 +143,7 @@ def delete_model(self, request: HttpRequest, obj: Alias):
sender=self.model,
)

def _get_alias_usage_link(
self, obj: Alias, request: HttpRequest, disabled: bool = False
) -> str:
def _get_alias_usage_link(self, obj: Alias, request: HttpRequest, disabled: bool = False) -> str:
url = admin_reverse(USAGE_ALIAS_URL_NAME, args=[obj.pk])
return self.admin_action_button(url, "info", _("View usage"), disabled=disabled)

Expand All @@ -169,9 +163,7 @@ class AliasContentAdmin(admin.ModelAdmin):
actions = None
change_form_template = "admin/djangocms_alias/aliascontent/change_form.html"

def changelist_view(
self, request: HttpRequest, extra_context: dict = None
) -> HttpResponse:
def changelist_view(self, request: HttpRequest, extra_context: dict = None) -> HttpResponse:
"""Needed for the Alias Content Admin breadcrumbs"""
return HttpResponseRedirect(
admin_reverse(
Expand All @@ -191,8 +183,7 @@ def change_view(
if not obj:
raise Http404()
return HttpResponseRedirect(
admin_reverse(CHANGE_ALIAS_URL_NAME, args=(obj.alias_id,))
+ f"?language={obj.language}"
admin_reverse(CHANGE_ALIAS_URL_NAME, args=(obj.alias_id,)) + f"?language={obj.language}"
)

def has_module_permission(self, request: HttpRequest) -> bool:
Expand Down
11 changes: 3 additions & 8 deletions djangocms_alias/cms_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,9 @@ class AliasCMSConfig(CMSAppConfig):
cms_toolbar_enabled_models = [(AliasContent, render_alias_content)]
moderated_models = [AliasContent]

djangocms_moderation_enabled = getattr(
settings, "MODERATING_ALIAS_MODELS_ENABLED", True
)
djangocms_moderation_enabled = getattr(settings, "MODERATING_ALIAS_MODELS_ENABLED", True)
djangocms_versioning_enabled = (
getattr(settings, "VERSIONING_ALIAS_MODELS_ENABLED", True)
and djangocms_versioning_installed
getattr(settings, "VERSIONING_ALIAS_MODELS_ENABLED", True) and djangocms_versioning_installed
)

if djangocms_versioning_enabled:
Expand All @@ -42,9 +39,7 @@ class AliasCMSConfig(CMSAppConfig):
),
]

djangocms_references_enabled = getattr(
settings, "REFERENCES_ALIAS_MODELS_ENABLED", True
)
djangocms_references_enabled = getattr(settings, "REFERENCES_ALIAS_MODELS_ENABLED", True)
reference_fields = [
(AliasPlugin, "alias"),
]
Expand Down
4 changes: 1 addition & 3 deletions djangocms_alias/cms_menus.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ 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

Expand Down
13 changes: 3 additions & 10 deletions djangocms_alias/cms_plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,7 @@ class Alias(CMSPluginBase):
form = AliasPluginForm

def get_render_template(self, context, instance, placeholder):
if (
isinstance(instance.placeholder.source, AliasContent)
and instance.is_recursive()
):
if isinstance(instance.placeholder.source, AliasContent) and instance.is_recursive():
return "djangocms_alias/alias_recursive.html"
return f"djangocms_alias/{instance.template}/alias.html"

Expand Down Expand Up @@ -115,9 +112,7 @@ 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

return all(
Expand All @@ -142,9 +137,7 @@ def can_detach(cls, user, target_placeholder, plugins):

@classmethod
def detach_alias_plugin(cls, plugin, language):
source_placeholder = plugin.alias.get_placeholder(
language, show_draft_content=True
) # We're in edit mode
source_placeholder = plugin.alias.get_placeholder(language, show_draft_content=True) # We're in edit mode
target_placeholder = plugin.placeholder

# Deleting uses a copy of a plugin to preserve pk on existing
Expand Down
30 changes: 7 additions & 23 deletions djangocms_alias/cms_toolbars.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,7 @@ def add_aliases_link_to_admin_menu(self):

url = admin_reverse(LIST_ALIAS_URL_NAME)
obj = self.toolbar.get_object()
language = (
obj.language
if hasattr(obj, "language")
else get_language_from_request(self.request)
)
language = obj.language if hasattr(obj, "language") else get_language_from_request(self.request)
if language is None:
language = get_default_language()
url += f'?{urlencode({"language": language})}'
Expand Down Expand Up @@ -152,19 +148,15 @@ def get_insert_position(cls, admin_menu, item_name):
return end.index

def enable_create_wizard_button(self):
button_lists = [
result.item for result in self.toolbar.find_items(item_type=ButtonList)
]
button_lists = [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])
)

# 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")
][0]
create_wizard_button = [button for button in buttons if button.name == gettext("Create")][0]

from cms.wizards.wizard_pool import entry_choices

Expand Down Expand Up @@ -195,9 +187,7 @@ 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):
Expand All @@ -217,17 +207,13 @@ def change_language_menu(self):
current_placeholder = alias_content.placeholder

remove = [
(code, languages.get(code, code))
for code in alias_content.alias.get_languages()
if code in languages
(code, languages.get(code, code)) for code in alias_content.alias.get_languages() if code in languages
]
add = [code for code in languages.items() if code not in remove]
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:
Expand All @@ -241,9 +227,7 @@ def change_language_menu(self):
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:
Expand Down
18 changes: 4 additions & 14 deletions djangocms_alias/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,13 @@ def choices(self, changelist):
}
yield {
"selected": self.value() == SITE_FILTER_NO_SITE_VALUE,
"query_string": changelist.get_query_string(
{self.parameter_name: SITE_FILTER_NO_SITE_VALUE}
),
"query_string": changelist.get_query_string({self.parameter_name: SITE_FILTER_NO_SITE_VALUE}),
"display": _("No site"),
}
for lookup, title in self.lookup_choices:
yield {
"selected": self.value() == str(lookup),
"query_string": changelist.get_query_string(
{self.parameter_name: lookup}
),
"query_string": changelist.get_query_string({self.parameter_name: lookup}),
"display": title,
}

Expand All @@ -62,11 +58,7 @@ def lookups(self, request, model_admin):
qs = model_admin.get_queryset(request)
cat_id = qs.values_list("category", flat=True)
# Ensure the category is ordered by the name alphabetically by default
cat = (
Category.objects.filter(pk__in=cat_id)
.translated(get_language())
.order_by("translations__name")
)
cat = Category.objects.filter(pk__in=cat_id).translated(get_language()).order_by("translations__name")
for obj in cat:
yield str(obj.pk), smart_str(obj)

Expand All @@ -83,8 +75,6 @@ def choices(self, changelist):
for lookup, title in self.lookup_choices:
yield {
"selected": self.value() == str(lookup),
"query_string": changelist.get_query_string(
{self.parameter_name: lookup}
),
"query_string": changelist.get_query_string({self.parameter_name: lookup}),
"display": title,
}
20 changes: 5 additions & 15 deletions djangocms_alias/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,7 @@ def clean(self):
placeholder = cleaned_data.get("placeholder")

if not plugin and not placeholder:
raise forms.ValidationError(
_("A plugin or placeholder is required to create an alias.")
)
raise forms.ValidationError(_("A plugin or placeholder is required to create an alias."))

if plugin and placeholder:
raise forms.ValidationError(
Expand Down Expand Up @@ -125,9 +123,7 @@ def clean(self):
language=cleaned_data.get("language"),
alias__category=cleaned_data.get("category"),
).exists():
raise forms.ValidationError(
_("Alias with this Name and Category already exists.")
)
raise forms.ValidationError(_("Alias with this Name and Category already exists."))

return cleaned_data

Expand Down Expand Up @@ -177,9 +173,7 @@ def save(self):


class CreateAliasWizardForm(forms.Form):
name = forms.CharField(
label=_("Name"), required=True, widget=AdminTextInputWidget()
)
name = forms.CharField(label=_("Name"), required=True, widget=AdminTextInputWidget())
site = forms.ModelChoiceField(
queryset=Site.objects.all(),
required=False,
Expand Down Expand Up @@ -276,9 +270,7 @@ class AliasPluginForm(forms.ModelForm):
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="",
Expand Down Expand Up @@ -334,8 +326,6 @@ def clean(self):
language=cleaned_data.get("language"),
alias__category=cleaned_data.get("category"),
).exists():
raise forms.ValidationError(
_("Alias with this Name and Category already exists.")
)
raise forms.ValidationError(_("Alias with this Name and Category already exists."))

return cleaned_data
4 changes: 1 addition & 3 deletions djangocms_alias/migrations/0001_initial.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,7 @@ class Migration(migrations.Migration):
),
(
"language_code",
models.CharField(
db_index=True, max_length=15, verbose_name="Language"
),
models.CharField(db_index=True, max_length=15, verbose_name="Language"),
),
(
"name",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ class Migration(migrations.Migration):
migrations.AlterField(
model_name="aliascontent",
name="language",
field=models.CharField(
default=django.utils.translation.get_language, max_length=10
),
field=models.CharField(default=django.utils.translation.get_language, max_length=10),
),
]
22 changes: 5 additions & 17 deletions djangocms_alias/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,7 @@ class Meta:
verbose_name = _("alias")
verbose_name_plural = _("aliases")
ordering = ["position"]
unique_together = (
("static_code", "site"),
) # Only restrict instances that have a site specified
unique_together = (("static_code", "site"),) # Only restrict instances that have a site specified

def __init__(self, *args, **kwargs):
self._plugins_cache = {}
Expand All @@ -130,9 +128,7 @@ 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__
Expand All @@ -147,11 +143,7 @@ def objects_using(self):
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)
]
[obj for model_class, ids in object_ids.items() for obj in model_class.objects.filter(pk__in=ids)]
)
return list(objects)

Expand Down Expand Up @@ -202,9 +194,7 @@ def get_content(self, language=None, show_draft_content=False):
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
)
content = self.get_content(language=language, show_draft_content=show_draft_content)
return getattr(content, "placeholder", None)

def get_plugins(self, language=None):
Expand Down Expand Up @@ -427,7 +417,5 @@ def is_recursive(self, language=None):
plugins = AliasPlugin.objects.filter(
placeholder_id=placeholder,
)
plugins = plugins.filter(
Q(pk=self) | Q(alias__contents__placeholders=placeholder)
)
plugins = plugins.filter(Q(pk=self) | Q(alias__contents__placeholders=placeholder))
return plugins.exists()
Loading
Loading