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

chore: Update pre-commit checks #217

Merged
merged 8 commits into from
Feb 9, 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
1 change: 1 addition & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ quote_type = single
[*.{scss,js,html}]
max_line_length = 120
indent_style = space
insert_final_newline = false
quote_type = double

[*.js]
Expand Down
47 changes: 11 additions & 36 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -1,42 +1,17 @@
name: Lint
name: Ruff

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

on: [push, pull_request]
on:
push:
pull_request:

jobs:
flake8:
name: flake8
ruff:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: "3.10"
- name: Install flake8
run: pip install --upgrade flake8
- name: Run flake8
uses: liskin/gh-problem-matcher-wrap@v3
with:
linters: flake8
run: flake8

isort:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: "3.10"
- run: python -m pip install isort
- name: isort
uses: liskin/gh-problem-matcher-wrap@v3
with:
linters: isort
run: isort --check-only --diff ./
- uses: actions/checkout@v4

- run: python -Im pip install --user ruff

- name: Run ruff
run: ruff --output-format=github djangocms_alias
23 changes: 10 additions & 13 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,16 @@ repos:
- id: django-upgrade
args: [--target-version, "3.2"]

- repo: https://github.com/PyCQA/flake8
rev: 6.1.0
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.2.0
hooks:
- id: flake8
additional_dependencies:
- flake8-bugbear
- flake8-builtins
- flake8-comprehensions
# - flake8-eradicate
- flake8-tidy-imports
- pep8-naming
- id: ruff
args: [--fix, --exit-non-zero-on-fix]
- id: ruff-format

- repo: https://github.com/pycqa/isort
rev: 5.13.2
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.5.0
hooks:
- id: isort
- id: check-yaml
- id: end-of-file-fixer
- id: trailing-whitespace
1 change: 0 additions & 1 deletion .tx/config
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,3 @@ source_file = djangocms_alias/locale/en/LC_MESSAGES/django.po
type = PO
minimum_perc = 0
resource_name = django.po

2 changes: 1 addition & 1 deletion LICENSE.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2 changes: 1 addition & 1 deletion djangocms_alias/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '2.0.0'
__version__ = "2.0.0"
81 changes: 55 additions & 26 deletions djangocms_alias/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@


__all__ = [
'AliasAdmin',
'CategoryAdmin',
'AliasContentAdmin',
"AliasAdmin",
"CategoryAdmin",
"AliasContentAdmin",
]

alias_admin_classes = [GrouperModelAdmin]
alias_admin_list_display = ['content__name', 'category', 'admin_list_actions']
alias_admin_list_display = ["content__name", "category", "admin_list_actions"]
djangocms_versioning_enabled = AliasCMSConfig.djangocms_versioning_enabled

if djangocms_versioning_enabled:
Expand All @@ -59,7 +59,7 @@

@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
Expand All @@ -77,9 +77,12 @@ def save_model(self, request, obj, form, change):
class AliasAdmin(*alias_admin_classes):
list_display = alias_admin_list_display
list_display_links = None
list_filter = (SiteFilter, CategoryFilter,)
fields = ('content__name', 'category', 'site', 'content__language')
readonly_fields = ('static_code', )
list_filter = (
SiteFilter,
CategoryFilter,
)
fields = ("content__name", "category", "site", "content__language")
readonly_fields = ("static_code",)
form = AliasGrouperAdminForm
extra_grouping_fields = ("language",)
EMPTY_CONTENT_VALUE = mark_safe(_("<i>Missing language</i>"))
Expand All @@ -91,7 +94,9 @@ 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 @@ -104,12 +109,14 @@ def has_delete_permission(self, request: HttpRequest, obj: Alias = None) -> bool
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 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 All @@ -121,10 +128,15 @@ def save_model(self, request: HttpRequest, obj: Alias, form: forms.Form, change:
)

def get_deleted_objects(self, objs, request: HttpRequest) -> tuple:
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 delete_model(self, request: HttpRequest, obj: Alias):
Expand All @@ -138,14 +150,20 @@ 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)

def _get_alias_delete_link(self, obj: Alias, request: HttpRequest) -> str:
url = admin_reverse(DELETE_ALIAS_URL_NAME, args=[obj.pk])
return self.admin_action_button(url, "bin", _("Delete Alias"),
disabled=not self.has_delete_permission(request, obj))
return self.admin_action_button(
url,
"bin",
_("Delete Alias"),
disabled=not self.has_delete_permission(request, obj),
)


@admin.register(AliasContent)
Expand All @@ -154,20 +172,31 @@ 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(
LIST_ALIAS_URL_NAME,
))

def change_view(self, request: HttpRequest, object_id: int, form_url: str = '', extra_context: dict = None) -> HttpResponse:
return HttpResponseRedirect(
admin_reverse(
LIST_ALIAS_URL_NAME,
)
)

def change_view(
self,
request: HttpRequest,
object_id: int,
form_url: str = "",
extra_context: dict = None,
) -> HttpResponse:
"""Needed for the Alias Content Admin breadcrumbs"""
obj = self.model.admin_manager.filter(pk=object_id).first()
if not obj:
raise Http404()
return HttpResponseRedirect(admin_reverse(
CHANGE_ALIAS_URL_NAME, args=(obj.alias_id,)
) + f"?language={obj.language}")
return HttpResponseRedirect(
admin_reverse(CHANGE_ALIAS_URL_NAME, args=(obj.alias_id,))
+ f"?language={obj.language}"
)

def has_module_permission(self, request: HttpRequest) -> bool:
"""Hides admin class in admin site overview"""
Expand Down
4 changes: 2 additions & 2 deletions djangocms_alias/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@


class AliasConfig(AppConfig):
name = 'djangocms_alias'
verbose_name = _('django CMS Alias')
name = "djangocms_alias"
verbose_name = _("django CMS Alias")
19 changes: 11 additions & 8 deletions djangocms_alias/cms_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -22,20 +22,22 @@ class AliasCMSConfig(CMSAppConfig):
moderated_models = [AliasContent]

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

if djangocms_versioning_enabled:

from cms.utils.i18n import get_language_tuple

from djangocms_versioning.datastructures import VersionableItem

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,
Expand All @@ -44,9 +46,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
Expand Down
5 changes: 2 additions & 3 deletions djangocms_alias/cms_menus.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading