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
Hashes for django_ext_utils-0.0.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ef05b346c6081b8239cb79b8ad21c237fd10fe92791f5e105db9674b33dba9e7 |
|
MD5 | 10668d4946ee22276283b165f4b7898a |
|
BLAKE2b-256 | 3647505ea7a2908c803258b9301100bb70d65c66b5d4930447ff942e827ebe7c |