diff --git a/src/codemodder/scripts/generate_docs.py b/src/codemodder/scripts/generate_docs.py index dc1c58d3..c834ab8a 100644 --- a/src/codemodder/scripts/generate_docs.py +++ b/src/codemodder/scripts/generate_docs.py @@ -234,6 +234,10 @@ class DocMetadata: importance="Low", guidance_explained="Manual instantiation of `asyncio.Task` is discouraged. We believe this change is safe and will not cause any issues.", ), + "django-model-without-dunder-str": DocMetadata( + importance="Low", + guidance_explained="This codemod is a great starting point for models with few fields. We encourage you to write custom `__str__` methods that best suit your Django application.", + ), } METADATA = CORE_METADATA | { diff --git a/src/core_codemods/django_model_without_dunder_str.py b/src/core_codemods/django_model_without_dunder_str.py index 120bfff3..3239d0d6 100644 --- a/src/core_codemods/django_model_without_dunder_str.py +++ b/src/core_codemods/django_model_without_dunder_str.py @@ -16,7 +16,7 @@ class DjangoModelWithoutDunderStrTransformer( LibcstResultTransformer, NameResolutionMixin ): - change_description = "todoMoved @receiver to the top." + change_description = "Add `__str__` definition to `django` Model class." def leave_ClassDef( self, original_node: cst.ClassDef, updated_node: cst.ClassDef @@ -76,10 +76,12 @@ def dunder_str_method() -> cst.FunctionDef: DjangoModelWithoutDunderStr = CoreCodemod( metadata=Metadata( name="django-model-without-dunder-str", - summary="TODOEnsure Django @receiver is the first decorator", - review_guidance=ReviewGuidance.MERGE_WITHOUT_REVIEW, + summary="Ensure Django Model Classes Implement A `__str__` Method", + review_guidance=ReviewGuidance.MERGE_AFTER_REVIEW, references=[ - Reference(url="todohttps://docs.djangoproject.com/en/4.1/topics/signals/"), + Reference( + url="https://docs.djangoproject.com/en/5.0/ref/models/instances/#django.db.models.Model.__str__" + ), ], ), transformer=LibcstTransformerPipeline(DjangoModelWithoutDunderStrTransformer), diff --git a/src/core_codemods/docs/pixee_python_django-debug-flag-on.md b/src/core_codemods/docs/pixee_python_django-debug-flag-on.md index 2e00be75..80dbfb6c 100644 --- a/src/core_codemods/docs/pixee_python_django-debug-flag-on.md +++ b/src/core_codemods/docs/pixee_python_django-debug-flag-on.md @@ -1,4 +1,4 @@ -This codemod will flip django's `DEBUG` flag to `False` if it's `True` on the `settings.py` file within django's default directory structure. +This codemod will flip Django's `DEBUG` flag to `False` if it's `True` on the `settings.py` file within Django's default directory structure. Having the debug flag on may result in sensitive information exposure. When an exception occurs while the `DEBUG` flag in on, it will dump metadata of your environment, including the settings module. The attacker can purposefully request a non-existing url to trigger an exception and gather information about your system. diff --git a/src/core_codemods/docs/pixee_python_django-model-without-dunder-str.md b/src/core_codemods/docs/pixee_python_django-model-without-dunder-str.md new file mode 100644 index 00000000..4ce94f53 --- /dev/null +++ b/src/core_codemods/docs/pixee_python_django-model-without-dunder-str.md @@ -0,0 +1,30 @@ +If you've ever actively developed or debugged a Django application, you may have noticed Django models and their instances can sometimes be hard to read or distinguish one instance from another. Loading models in the interactive Django console or viewing them in the admin interface can be puzzling. This is because Django is trying to display your model objects as a plain strings. + +We've written this codemod to make your model objects human-readable. It will automatically detect all of your model's fields and display them as a nice string. + +For example, the `Question` model from Django's popular Poll App tutorial will look like this: +```diff +from django.db import models + +class Question(models.Model): + question_text = models.CharField(max_length=200) + pub_date = models.DateTimeField("date published") ++ ++ def __str__(self): ++ model_name = self.__class__.__name__ ++ fields_str = ", ".join([f"{field.name}={getattr(self, field.name)}" for field in self._meta.fields]) ++ return f"{model_name}({fields_str})" +``` + +Without this change, the `Question` objects look like this in the interactive Django shell: +``` +>>> Question.objects.all() +]> +``` +With this codemod's addition of `__str__`, it now looks like: +``` +>>> Question.objects.all() +]> +``` + +You'll notice this change works great for models with only a handful of fields. We encourage you to use this codemod's change as a starting point for further customization. diff --git a/src/core_codemods/docs/pixee_python_django-session-cookie-secure-off.md b/src/core_codemods/docs/pixee_python_django-session-cookie-secure-off.md index 970876c3..594de2e5 100644 --- a/src/core_codemods/docs/pixee_python_django-session-cookie-secure-off.md +++ b/src/core_codemods/docs/pixee_python_django-session-cookie-secure-off.md @@ -1,4 +1,4 @@ -This codemod will set django's `SESSION_COOKIE_SECURE` flag to `True` if it's `False` or missing on the `settings.py` file within django's default directory structure. +This codemod will set Django's `SESSION_COOKIE_SECURE` flag to `True` if it's `False` or missing on the `settings.py` file within Django's default directory structure. ```diff + SESSION_COOKIE_SECURE = True