Useful utilities for your django project
Project description
Django-ext-utils
Useful utilities for your django project
Requirements
- Python >= 3.0
- Django >= 2.0
- Pillow
- (optional) Djangorestframework
Installation
pip install django-ext-utils
Add to your settings
settings.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
...
'django_ext_utils',
]
SERIALIZERS_PATH = 'api.v1.serializers' # path to serializers folder
Usage
ResizeImageMixin
Resize image on the fly before save to ImageField
:
from django.contrib.auth.models import AbstractUser
from django_ext_utils.utils import ResizeImageMixin
class User(AbstractUser, ResizeImageMixin):
avatar = models.ImageField(upload_to='avatars')
...
def save(self, *args, **kwargs):
if self.pk is None:
self.resize(self.avatar, (200, 200)) # here!
super().save(*args, **kwargs)
DeletedModelMixin
On delete
mark is_mark_as_delete
instead
from django_ext_utils.utils import DeletedModelMixin
SingletonModelMixin
Create only one instance of model
from django_ext_utils.utils import SingletonModelMixin
Service
Service-form-oriented helper (I really like services)
from django_ext_utils.utils import Service
from .models import Subscription
# Services/service.py
class AddSubscriptionService(Service):
def go(self):
email = self.cleaned_data.get('email')
name = self.cleaned_data.get('name')
subscription, created = Subscription.objects.get_or_create(
email = email,
defaults = {
'name': name
}
)
if created:
# notify user
......
return subscription
# views.py
subscription = NotificationService.exec({
'email': 'foo@example.com',
'name': 'foo'
})
...
return Response(subscription.serialize_data)
Rest api helpers
ResponsesMixin
- wrapper for
rest_framework.Response
with statuses simple_text_response
return text with200
codesuccess_objects_response
return object with200
codeerror_response
returnError
object with400
code
class UserProfileViewSet(ResponsesMixin, GenericAPIView):
serializer_class = UserSerializer
def post(self, request, *args, **kwargs):
user = request.user
serializer = UserSerializer(instance=request.user,
data=request.data,
context={'request': request})
if serializer.is_valid():
serializer.save()
return self.success_objects_response(serializer.data) # here
return self.error_response(serializer.errors) # here
def get(self, request, *args, **kwargs):
user = request.user
data = user._serialize_data(request)
return self.success_objects_response(data) # here
Base
Abstract class to help serialize your models
-
get_serializer # Return serializer by name (ModelSerializer)
-
get_short_serializer # Retrun another serializer by name (ModelShortSerializer)
-
serialize_data # Find serializer by name and return
serialize data
-
_serialize_data(request) # Find serializer by name and serialize data with
request
-
IMPORTANT: you must create serializer by rule: ModelNameSerializer or ModelNameShortSerializer
from django_ext_utils.rest_utils import Base
# models.py
class User(AbstractUser, Base, ResizeImageMixin):
avatar = models.ImageField(upload_to='avatars')
....
# serializer.py
class UserSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = [
'pk',
'first_name',
'last_name'
]
# views.py
from rest_framework.generics import GenericAPIView
class UserApiView(GenericAPIView):
def get(self, request, *args, **kwargs):
user = request.user
data = user._serialize_data(request) # here!
return self.success_objects_response(data)
ImageBase64Field
- Wrapper for
serializers.ImageField()
- Got
base64
string and save in modelImageField
- Return
link to image
(http://example.com/media/avatars/my.jpeg)
# serializer.py
class SomeUserApiView(serializers.ModelSerializer):
image = ImageBase64Field()
created_at = TimestampField(read_only=True)
class Meta:
model = User
fields = [
'uid',
'image',
'created_at'
]
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file django-ext-utils-0.0.1.tar.gz
.
File metadata
- Download URL: django-ext-utils-0.0.1.tar.gz
- Upload date:
- Size: 5.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.57.0 CPython/3.8.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | beab27c128414fc7aa0a0c181e728e8702b48108e291fd52a6d474c36b34b90c |
|
MD5 | 60df11287f14e5ce3ee102e7de157ef2 |
|
BLAKE2b-256 | b40f925de0a610855d7e562b061c55f850989d6c8a5e1b2456c72f4c0a500a8f |
File details
Details for the file django_ext_utils-0.0.1-py3-none-any.whl
.
File metadata
- Download URL: django_ext_utils-0.0.1-py3-none-any.whl
- Upload date:
- Size: 6.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.57.0 CPython/3.8.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ef05b346c6081b8239cb79b8ad21c237fd10fe92791f5e105db9674b33dba9e7 |
|
MD5 | 10668d4946ee22276283b165f4b7898a |
|
BLAKE2b-256 | 3647505ea7a2908c803258b9301100bb70d65c66b5d4930447ff942e827ebe7c |