Skip to content

Commit

Permalink
feat: support for the admin's ACTION_CHECKBOX_NAME compatibility im…
Browse files Browse the repository at this point in the history
…port removal
  • Loading branch information
browniebroke committed Sep 11, 2021
1 parent b3f58bd commit cad7e87
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 4 deletions.
3 changes: 2 additions & 1 deletion django_codemod/visitors/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from .admin import InlineHasAddPermissionsTransformer
from .admin import ActionCheckboxNameTransformer, InlineHasAddPermissionsTransformer
from .core import URLResolversTransformer
from .crypto import GetRandomStringTransformer
from .decorators import AvailableAttrsTransformer, ContextDecoratorTransformer
Expand Down Expand Up @@ -58,6 +58,7 @@

__all__ = (
"AbsPathTransformer",
"ActionCheckboxNameTransformer",
"AssignmentTagTransformer",
"AvailableAttrsTransformer",
"BaseContextTransformer",
Expand Down
17 changes: 15 additions & 2 deletions django_codemod/visitors/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,13 @@
)
from libcst import matchers as m

from django_codemod.constants import DJANGO_2_1, DJANGO_3_0
from django_codemod.constants import DJANGO_1_3, DJANGO_2_1, DJANGO_3_0, DJANGO_3_1
from django_codemod.utils.calls import parse_arg, parse_param
from django_codemod.visitors.base import BaseDjCodemodTransformer, module_matcher
from django_codemod.visitors.base import (
BaseDjCodemodTransformer,
BaseRenameTransformer,
module_matcher,
)


class InlineHasAddPermissionsTransformer(BaseDjCodemodTransformer):
Expand Down Expand Up @@ -143,3 +147,12 @@ def leave_Call(self, original_node: Call, updated_node: Call) -> BaseExpression:
)
return updated_node.with_changes(args=updated_args)
return super().leave_Call(original_node, updated_node)


class ActionCheckboxNameTransformer(BaseRenameTransformer):
"""Replace `django.contrib.admin.ACTION_CHECKBOX_NAME` compatibility import"""

deprecated_in = DJANGO_1_3
removed_in = DJANGO_3_1
rename_from = "django.contrib.admin.ACTION_CHECKBOX_NAME"
rename_to = "django.contrib.admin.helpers.ACTION_CHECKBOX_NAME"
5 changes: 5 additions & 0 deletions docs/codemods.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@ Applied by passing the `--removed-in 3.1` or `--deprecated-in 1.7` option:
`ContextPopException` and `RequestContext` in `django.template.base`
by import from `django.template.context`.

Applied by passing the `--removed-in 3.1` or `--deprecated-in 1.3` option:

- Replace compatibility imports of `ACTION_CHECKBOX_NAME` in
`django.contrib.admin` by import from `django.contrib.admin.helpers`.

## Removed in Django 4.0

Applied by passing the `--removed-in 4.0` or `--deprecated-in 3.0` option:
Expand Down
19 changes: 18 additions & 1 deletion tests/visitors/test_admin.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
from parameterized import parameterized

from django_codemod.visitors import InlineHasAddPermissionsTransformer
from django_codemod.visitors import (
ActionCheckboxNameTransformer,
InlineHasAddPermissionsTransformer,
)
from tests.visitors.base import BaseVisitorTest


Expand Down Expand Up @@ -194,3 +197,17 @@ def has_add_permission(self, request):
return False
"""
self.assertCodemod(before, after)


class TestActionCheckboxNameTransformer(BaseVisitorTest):

transformer = ActionCheckboxNameTransformer

def test_simple_substitution(self) -> None:
before = """
from django.contrib.admin import ACTION_CHECKBOX_NAME
"""
after = """
from django.contrib.admin.helpers import ACTION_CHECKBOX_NAME
"""
self.assertCodemod(before, after)

0 comments on commit cad7e87

Please sign in to comment.