Skip to content
Open
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
8 changes: 7 additions & 1 deletion djangocms_alias/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
HttpResponse,
HttpResponseRedirect,
)
from django.template.defaultfilters import escape
from django.utils.safestring import mark_safe
from django.utils.translation import gettext_lazy as _
from parler.admin import TranslatableAdmin
Expand Down Expand Up @@ -92,7 +93,12 @@ def get_list_display(self, request: HttpRequest) -> Iterable[str]:

@admin.display(description=_("Name"), ordering=models.functions.Lower("contents__name"))
def content_name(self, obj: Alias) -> str:
return self.get_content_field(obj, "name") or obj.static_code or self.EMPTY_CONTENT_VALUE
return (
self.get_content_field(obj, "name")
or obj.static_code
or mark_safe(f"<i>{escape(obj.name)}</i>")
or self.EMPTY_CONTENT_VALUE
)

@admin.display(description=_("Used"), boolean=True, ordering="cmsplugins_count")
def used(self, obj: Alias) -> bool | None:
Expand Down
17 changes: 10 additions & 7 deletions djangocms_alias/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
from parler.models import TranslatableModel, TranslatedFields

from .constants import CHANGE_ALIAS_URL_NAME, CHANGE_CATEGORY_URL_NAME
from .utils import is_versioning_enabled

__all__ = [
"Category",
Expand Down Expand Up @@ -158,15 +157,18 @@

def get_name(self, language=None):
content = self.get_content(language, show_draft_content=True)
if not content:
content = next(iter(self._content_cache.values()), None)
name = getattr(content, "name", f"Alias {self.pk} (No content)")
if is_versioning_enabled() and content:
try:
from djangocms_versioning.constants import DRAFT

version = content.versions.first()

if version.state == DRAFT:
return f"{name} (Not published)"

except (ImportError, ModuleNotFoundError, AttributeError):

Check notice

Code scanning / CodeQL

Empty except Note

'except' clause does nothing but pass and there is no explanatory comment.

Copilot Autofix

AI 1 day ago

In general, empty except blocks should be replaced with explicit, documented handling: narrow the exceptions where possible, add comments explaining why they are safely ignored, and/or log unexpected issues. Here, the goal is to annotate alias names as “(Not published)” when draft versioning is available, and otherwise silently fall back to the plain name. We can preserve that behavior while removing the “empty except” smell by (1) guarding against content being None before entering the try, and (2) adding a short explanatory comment in the except block to clarify that missing versioning or versions is expected and intentionally ignored.

Concretely, in Alias.get_name in djangocms_alias/models.py, we will:

  • Add an early if content is None: return name to avoid AttributeError from content.versions.
  • Keep the try importing DRAFT and accessing content.versions.first().
  • Keep catching the same exceptions, but add a brief comment explaining that the project may not have versioning installed or the content may not be versioned, and that in those cases we intentionally fall back to the base name.
    This removes the “empty except” issue while keeping the same observable behavior (plain name when versioning is unavailable or when any of the anticipated exceptions is raised).

No new imports or helper methods are required.


Suggested changeset 1
djangocms_alias/models.py

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/djangocms_alias/models.py b/djangocms_alias/models.py
--- a/djangocms_alias/models.py
+++ b/djangocms_alias/models.py
@@ -160,15 +160,18 @@
         if not content:
             content = next(iter(self._content_cache.values()), None)
         name = getattr(content, "name", f"Alias {self.pk} (No content)")
+        if content is None:
+            return name
         try:
             from djangocms_versioning.constants import DRAFT
 
             version = content.versions.first()
 
-            if version.state == DRAFT:
+            if version and version.state == DRAFT:
                 return f"{name} (Not published)"
         except (ImportError, ModuleNotFoundError, AttributeError):
-            pass
+            # Versioning is not installed or content is not versioned; fall back to the base name.
+            return name
         return name
 
     def get_content(self, language=None, show_draft_content=False):
EOF
@@ -160,15 +160,18 @@
if not content:
content = next(iter(self._content_cache.values()), None)
name = getattr(content, "name", f"Alias {self.pk} (No content)")
if content is None:
return name
try:
from djangocms_versioning.constants import DRAFT

version = content.versions.first()

if version.state == DRAFT:
if version and version.state == DRAFT:
return f"{name} (Not published)"
except (ImportError, ModuleNotFoundError, AttributeError):
pass
# Versioning is not installed or content is not versioned; fall back to the base name.
return name
return name

def get_content(self, language=None, show_draft_content=False):
Copilot is powered by AI and may make mistakes. Always verify output.
@fsbraun fsbraun committed this autofix suggestion 1 day ago.
pass
return name

def get_content(self, language=None, show_draft_content=False):
Expand All @@ -180,10 +182,11 @@
qs = self.contents(manager="admin_manager").latest_content()
else:
qs = self.contents.all()
qs = qs.filter(language=language)

self._content_cache[language] = qs.first()
return self._content_cache[language]
for content in qs:
self._content_cache.setdefault(content.language, content)
return self._content_cache.setdefault(
language, self._content_cache.get(language)
) # Update to cache "no content" as None

def get_placeholder(self, language=None, show_draft_content=False):
content = self.get_content(language=language, show_draft_content=show_draft_content)
Expand Down