# EZDjangoCommon
`ez_django_common` is a Django package designed to simplify and standardize common tasks in Django projects. It provides a set of utilities, custom responses, admin enhancements, and other reusable components that can be easily integrated into any Django project. This package aims to reduce boilerplate code and improve consistency across your Django applications.
## Features
- **Custom Admin Enhancements**: Enhanced Django admin interface with custom widgets, inline models, and improved form handling.
- **Custom Responses**: Standardized API responses with support for pagination, error handling, and success messages.
- **JWT Utilities**: Custom JWT token views for authentication.
- **Model Mixins**: Reusable mixins for common CRUD operations in Django viewsets.
- **Permissions**: Customizable Django model permissions with support for additional permissions.
- **Utilities**: Helper functions for logging, SMS, and file uploads.
## Installation
You can install `ez_django_common` via pip:
``` bash
pip install ez-django-common
```
Or if you want to install from source, you can run this command to install the package:
``` bash
pip install git+https://github.com/ezhoosh/EZDjangoCommon.git
```
Then add these packages to INSTALLED_APPS:
``` python
INSTALLED_APPS = [
'ez_django_common',
'image_uploader_widget',
'tinymce',
...
]
```
## Usage
### Custom Admin Enhancements
#### BaseModelAdmin
The `BaseModelAdmin` class provides a set of default configurations for Django admin models, including custom widgets for text and file fields.
```python
from ez_django_common.admin import BaseModelAdmin
from django.contrib import admin
from .models import MyModel
@admin.register(MyModel)
class MyModelAdmin(BaseModelAdmin):
list_display = ('name', 'created_at')
```
### Custom Inlines
The package provides custom inline classes (TabularInline, StackedInline, NonrelatedTabularInline, NonrelatedStackedInline) that can be used to organize inline models in the admin interface.
``` python
from ez_django_common.admin import TabularInline
from django.contrib import admin
from .models import MyModel, RelatedModel
class RelatedModelInline(TabularInline):
model = RelatedModel
@admin.register(MyModel)
class MyModelAdmin(BaseModelAdmin):
inlines = [RelatedModelInline]
```
### Unfold
To use unfold for panel admin, first add unfold to INSTALLED_APPS:
``` python
INSTALLED_APPS = [
'unfold', # before django.admin.contrib
...
]
```
then use BaseModelAdmin instead of admin.ModelAdmin in admin classes:
``` python
from ez_django_common.admin import BaseModelAdmin
@admin.register(ModelName)
class ModelNameAdmin(BaseModelAdmin):
...
```
### Custom Responses
The enveloper function wraps your serializers in a standardized response format, including fields for data, error, and message.
``` python
from ez_django_common.custom_responses.enveloper import enveloper
@extend_schema(
responses=enveloper(SampleRetrieveSerializer, many=False),
)
def retrieve(self, request, *args, **kwargs):
return super().retrieve(request, *args, **kwargs)
```
Or if your response is a list of objects:
``` python
from ez_django_common.custom_responses.enveloper import enveloper_pagination
@extend_schema(
...
responses=enveloper_pagination(SampleRetrieveSerializer, many=True),
)
def list(self, request, *args, **kwargs):
return super().list(request, *args, **kwargs)
```
### Custom Exception Handler
The custom_exception_handler provides a standardized way to handle exceptions in your Django REST Framework views.
``` python
REST_FRAMEWORK = {
...
"EXCEPTION_HANDLER": "ez_django_common.custom_responses.exception.custom_exception_handler",
...
}
```
### JWT Utilities
* CustomTokenViewBase
The CustomTokenViewBase class provides a base for custom JWT token views, including token refresh and verification.
``` python
from ez_django_common.custom_responses.jwt import CustomTokenRefreshView, CustomTokenVerifyView
from rest_framework_simplejwt.views import TokenRefreshView, TokenVerifyView
urlpatterns = [
path('token/refresh/', CustomTokenRefreshView.as_view(), name='token_refresh'),
path('token/verify/', CustomTokenVerifyView.as_view(), name='token_verify'),
]
```
### Model Mixins
The package includes several mixins for common CRUD operations in Django viewsets.
``` python
from ez_django_common.custom_responses.viewsets import CustomModelViewSet
from .models import MyModel
from .serializers import MySerializer
class MyModelViewSet(CustomModelViewSet):
queryset = MyModel.objects.all()
serializer_class = MySerializer
```
### Permissions
* CustomDjangoModelPermissions
The CustomDjangoModelPermissions class extends Django's default model permissions, allowing you to add additional permissions.
``` python
from ez_django_common.permissions import get_custom_model_permissions
from rest_framework.permissions import IsAuthenticated
permission_classes = [IsAuthenticated, get_custom_model_permissions(['myapp.view_mymodel'])]
```
### Utilities
* Upload_to
The upload_to function provides a standardized way to handle file uploads in Django models.
``` python
from ez_django_common.storages import upload_to
from django.db import models
def upload_to_path(instance, filename):
return upload_to(instance, filename, folder="path")
class MyModel(models.Model):
file = models.FileField(upload_to=upload_to_path)
```
Raw data
{
"_id": null,
"home_page": "https://github.com/ezhoosh/EZDjangoCommon",
"name": "ez-django-common",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3",
"maintainer_email": null,
"keywords": "common",
"author": "ezhoosh",
"author_email": "ezhoosh@ezhoosh.com",
"download_url": "https://files.pythonhosted.org/packages/c3/a5/bc09adacef1d3ad2fefd9c4d97ff31619e7cef378f77c065d872b3c8b632/ez-django-common-1.0.11.tar.gz",
"platform": "any",
"description": "# EZDjangoCommon\n\n`ez_django_common` is a Django package designed to simplify and standardize common tasks in Django projects. It provides a set of utilities, custom responses, admin enhancements, and other reusable components that can be easily integrated into any Django project. This package aims to reduce boilerplate code and improve consistency across your Django applications.\n\n## Features\n\n- **Custom Admin Enhancements**: Enhanced Django admin interface with custom widgets, inline models, and improved form handling.\n- **Custom Responses**: Standardized API responses with support for pagination, error handling, and success messages.\n- **JWT Utilities**: Custom JWT token views for authentication.\n- **Model Mixins**: Reusable mixins for common CRUD operations in Django viewsets.\n- **Permissions**: Customizable Django model permissions with support for additional permissions.\n- **Utilities**: Helper functions for logging, SMS, and file uploads.\n\n## Installation\n\nYou can install `ez_django_common` via pip:\n\n``` bash\npip install ez-django-common\n```\n\nOr if you want to install from source, you can run this command to install the package:\n\n``` bash\npip install git+https://github.com/ezhoosh/EZDjangoCommon.git\n```\n\nThen add these packages to INSTALLED_APPS:\n\n``` python\nINSTALLED_APPS = [\n 'ez_django_common',\n 'image_uploader_widget',\n 'tinymce',\n ...\n]\n```\n\n## Usage\n\n### Custom Admin Enhancements\n\n#### BaseModelAdmin\n\nThe `BaseModelAdmin` class provides a set of default configurations for Django admin models, including custom widgets for text and file fields.\n\n```python\nfrom ez_django_common.admin import BaseModelAdmin\nfrom django.contrib import admin\nfrom .models import MyModel\n\n@admin.register(MyModel)\nclass MyModelAdmin(BaseModelAdmin):\n list_display = ('name', 'created_at')\n```\n\n### Custom Inlines\n\nThe package provides custom inline classes (TabularInline, StackedInline, NonrelatedTabularInline, NonrelatedStackedInline) that can be used to organize inline models in the admin interface.\n\n``` python\nfrom ez_django_common.admin import TabularInline\nfrom django.contrib import admin\nfrom .models import MyModel, RelatedModel\n\nclass RelatedModelInline(TabularInline):\n model = RelatedModel\n\n@admin.register(MyModel)\nclass MyModelAdmin(BaseModelAdmin):\n inlines = [RelatedModelInline]\n```\n\n### Unfold\nTo use unfold for panel admin, first add unfold to INSTALLED_APPS:\n\n``` python\nINSTALLED_APPS = [\n 'unfold', # before django.admin.contrib\n ...\n]\n```\n\nthen use BaseModelAdmin instead of admin.ModelAdmin in admin classes:\n\n``` python\nfrom ez_django_common.admin import BaseModelAdmin\n\n@admin.register(ModelName)\nclass ModelNameAdmin(BaseModelAdmin):\n ...\n```\n\n### Custom Responses\nThe enveloper function wraps your serializers in a standardized response format, including fields for data, error, and message.\n\n``` python\nfrom ez_django_common.custom_responses.enveloper import enveloper\n\n@extend_schema(\n responses=enveloper(SampleRetrieveSerializer, many=False),\n)\ndef retrieve(self, request, *args, **kwargs):\n return super().retrieve(request, *args, **kwargs)\n```\n\nOr if your response is a list of objects:\n\n``` python\nfrom ez_django_common.custom_responses.enveloper import enveloper_pagination\n\n@extend_schema(\n ...\n responses=enveloper_pagination(SampleRetrieveSerializer, many=True),\n)\ndef list(self, request, *args, **kwargs):\n return super().list(request, *args, **kwargs)\n```\n\n\n### Custom Exception Handler\n\nThe custom_exception_handler provides a standardized way to handle exceptions in your Django REST Framework views.\n\n``` python\nREST_FRAMEWORK = {\n ...\n \"EXCEPTION_HANDLER\": \"ez_django_common.custom_responses.exception.custom_exception_handler\",\n ...\n}\n```\n\n### JWT Utilities\n* CustomTokenViewBase\n\nThe CustomTokenViewBase class provides a base for custom JWT token views, including token refresh and verification.\n\n``` python\nfrom ez_django_common.custom_responses.jwt import CustomTokenRefreshView, CustomTokenVerifyView\nfrom rest_framework_simplejwt.views import TokenRefreshView, TokenVerifyView\n\nurlpatterns = [\n path('token/refresh/', CustomTokenRefreshView.as_view(), name='token_refresh'),\n path('token/verify/', CustomTokenVerifyView.as_view(), name='token_verify'),\n]\n```\n\n### Model Mixins\n\nThe package includes several mixins for common CRUD operations in Django viewsets.\n\n``` python\nfrom ez_django_common.custom_responses.viewsets import CustomModelViewSet\nfrom .models import MyModel\nfrom .serializers import MySerializer\n\nclass MyModelViewSet(CustomModelViewSet):\n queryset = MyModel.objects.all()\n serializer_class = MySerializer\n```\n\n### Permissions\n* CustomDjangoModelPermissions\n\nThe CustomDjangoModelPermissions class extends Django's default model permissions, allowing you to add additional permissions.\n\n``` python\nfrom ez_django_common.permissions import get_custom_model_permissions\nfrom rest_framework.permissions import IsAuthenticated\n\npermission_classes = [IsAuthenticated, get_custom_model_permissions(['myapp.view_mymodel'])]\n```\n\n### Utilities\n* Upload_to\n\nThe upload_to function provides a standardized way to handle file uploads in Django models.\n\n``` python\nfrom ez_django_common.storages import upload_to\nfrom django.db import models\n\ndef upload_to_path(instance, filename):\n return upload_to(instance, filename, folder=\"path\")\n\nclass MyModel(models.Model):\n file = models.FileField(upload_to=upload_to_path)\n```\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "EZDjangoCommon",
"version": "1.0.11",
"project_urls": {
"Download": "https://pypi.python.org/pypi/ez-django-common/",
"Homepage": "https://github.com/ezhoosh/EZDjangoCommon"
},
"split_keywords": [
"common"
],
"urls": [
{
"comment_text": null,
"digests": {
"blake2b_256": "5607c27e2c0106622afd35598afa5b08e1deb42f956d9a6515ffe3b76f2721e1",
"md5": "d1290b2b2f0a7e6d938f5b69d9b7d146",
"sha256": "a5006c1ebd1d2040571b8ae0d658bfbe49190cfedae30594c2350efa1abc9b82"
},
"downloads": -1,
"filename": "ez_django_common-1.0.11-py3-none-any.whl",
"has_sig": false,
"md5_digest": "d1290b2b2f0a7e6d938f5b69d9b7d146",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3",
"size": 57316,
"upload_time": "2025-02-08T13:26:35",
"upload_time_iso_8601": "2025-02-08T13:26:35.213755Z",
"url": "https://files.pythonhosted.org/packages/56/07/c27e2c0106622afd35598afa5b08e1deb42f956d9a6515ffe3b76f2721e1/ez_django_common-1.0.11-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": null,
"digests": {
"blake2b_256": "c3a5bc09adacef1d3ad2fefd9c4d97ff31619e7cef378f77c065d872b3c8b632",
"md5": "538c9c08fbe9250af900861f33cabbab",
"sha256": "85fde216864a0defea345dce542068396beff9f37586a8ec04ae1ccaa965c636"
},
"downloads": -1,
"filename": "ez-django-common-1.0.11.tar.gz",
"has_sig": false,
"md5_digest": "538c9c08fbe9250af900861f33cabbab",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3",
"size": 54136,
"upload_time": "2025-02-08T13:26:36",
"upload_time_iso_8601": "2025-02-08T13:26:36.472387Z",
"url": "https://files.pythonhosted.org/packages/c3/a5/bc09adacef1d3ad2fefd9c4d97ff31619e7cef378f77c065d872b3c8b632/ez-django-common-1.0.11.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2025-02-08 13:26:36",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "ezhoosh",
"github_project": "EZDjangoCommon",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "ez-django-common"
}