Skip to content

Commit

Permalink
feat: support for BaseContext, Context, ContextPopException and…
Browse files Browse the repository at this point in the history
… `RequestContext` compatibility imports removal
  • Loading branch information
browniebroke committed Sep 11, 2021
1 parent 1492be1 commit b3f58bd
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 1 deletion.
12 changes: 11 additions & 1 deletion django_codemod/visitors/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@
)
from .shortcuts import RenderToResponseTransformer
from .signals import SignalDisconnectWeakTransformer
from .template_context import (
BaseContextTransformer,
ContextPopExceptionTransformer,
ContextTransformer,
RequestContextTransformer,
)
from .template_tags import AssignmentTagTransformer
from .timezone import FixedOffsetTransformer
from .translations import (
Expand All @@ -54,8 +60,11 @@
"AbsPathTransformer",
"AssignmentTagTransformer",
"AvailableAttrsTransformer",
"BaseContextTransformer",
"BoundFieldTransformer",
"ContextDecoratorTransformer",
"ContextPopExceptionTransformer",
"ContextTransformer",
"CookieDateTransformer",
"DatastructuresEmptyResultSetTransformer",
"FieldDoesNotExistTransformer",
Expand All @@ -76,9 +85,11 @@
"ModelsPermalinkTransformer",
"NullBooleanFieldTransformer",
"OnDeleteTransformer",
"PrettyNameTransformer",
"QueryEmptyResultSetTransformer",
"QuerySetPaginatorTransformer",
"RenderToResponseTransformer",
"RequestContextTransformer",
"SignalDisconnectWeakTransformer",
"SmartTextTransformer",
"SqlEmptyResultSetTransformer",
Expand All @@ -91,5 +102,4 @@
"URLTransformer",
"UnescapeEntitiesTransformer",
"UnicodeCompatibleTransformer",
"PrettyNameTransformer",
)
38 changes: 38 additions & 0 deletions django_codemod/visitors/template_context.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
from django_codemod.constants import DJANGO_1_7, DJANGO_3_1
from django_codemod.visitors.base import BaseRenameTransformer


class BaseContextTransformer(BaseRenameTransformer):
"""Replace `django.template.base.BaseContext` compatibility import."""

deprecated_in = DJANGO_1_7
removed_in = DJANGO_3_1
rename_from = "django.template.base.BaseContext"
rename_to = "django.template.context.BaseContext"


class ContextTransformer(BaseRenameTransformer):
"""Replace `django.template.base.Context` compatibility import."""

deprecated_in = DJANGO_1_7
removed_in = DJANGO_3_1
rename_from = "django.template.base.Context"
rename_to = "django.template.context.Context"


class RequestContextTransformer(BaseRenameTransformer):
"""Replace `django.template.base.RequestContext` compatibility import."""

deprecated_in = DJANGO_1_7
removed_in = DJANGO_3_1
rename_from = "django.template.base.RequestContext"
rename_to = "django.template.context.RequestContext"


class ContextPopExceptionTransformer(BaseRenameTransformer):
"""Replace `django.template.base.ContextPopException` compatibility import."""

deprecated_in = DJANGO_1_7
removed_in = DJANGO_3_1
rename_from = "django.template.base.ContextPopException"
rename_to = "django.template.context.ContextPopException"
6 changes: 6 additions & 0 deletions docs/codemods.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,12 @@ Applied by passing the `--removed-in 3.1` or `--deprecated-in 1.8` option:
- Replace compatibility imports of `FieldDoesNotExist`
in `django.db.models.fields` by import from `django.core.exceptions`.

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

- Replace compatibility imports of `BaseContext`, `Context`,
`ContextPopException` and `RequestContext` in `django.template.base`
by import from `django.template.context`.

## Removed in Django 4.0

Applied by passing the `--removed-in 4.0` or `--deprecated-in 3.0` option:
Expand Down
79 changes: 79 additions & 0 deletions tests/visitors/test_template_context.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
from django_codemod.visitors import (
BaseContextTransformer,
ContextPopExceptionTransformer,
ContextTransformer,
RequestContextTransformer,
)
from tests.visitors.base import BaseVisitorTest


class TestBaseContextTransformer(BaseVisitorTest):

transformer = BaseContextTransformer

def test_simple_substitution(self) -> None:
before = """
from django.template.base import BaseContext
BaseContext({})
"""
after = """
from django.template.context import BaseContext
BaseContext({})
"""
self.assertCodemod(before, after)


class TestContextTransformer(BaseVisitorTest):

transformer = ContextTransformer

def test_simple_substitution(self) -> None:
before = """
from django.template.base import Context
Context({})
"""
after = """
from django.template.context import Context
Context({})
"""
self.assertCodemod(before, after)


class TestRequestContextTransformer(BaseVisitorTest):

transformer = RequestContextTransformer

def test_simple_substitution(self) -> None:
before = """
from django.template.base import RequestContext
RequestContext({})
"""
after = """
from django.template.context import RequestContext
RequestContext({})
"""
self.assertCodemod(before, after)


class TestContextPopExceptionTransformer(BaseVisitorTest):

transformer = ContextPopExceptionTransformer

def test_simple_substitution(self) -> None:
before = """
from django.template.base import ContextPopException
raise ContextPopException()
"""
after = """
from django.template.context import ContextPopException
raise ContextPopException()
"""
self.assertCodemod(before, after)

0 comments on commit b3f58bd

Please sign in to comment.