Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Async support for Django REST framework
We highly recommend and only officially support the latest patch release of each Python and Django series.
Install using pip
...
pip install adrf
Add 'adrf'
to your INSTALLED_APPS
setting.
INSTALLED_APPS = [
...
'adrf',
]
When using Django 4.1 and above, this package allows you to work with async class and function based views.
For class based views, all handler methods must be async, otherwise Django will raise an exception. For function based views, the function itself must be async.
For example:
from adrf.views import APIView
class AsyncAuthentication(BaseAuthentication):
async def authenticate(self, request) -> tuple[User, None]:
return user, None
class AsyncPermission:
async def has_permission(self, request, view) -> bool:
if random.random() < 0.7:
return False
return True
async def has_object_permission(self, request, view, obj):
if obj.user == request.user or request.user.is_superuser:
return True
return False
class AsyncThrottle(BaseThrottle):
async def allow_request(self, request, view) -> bool:
if random.random() < 0.7:
return False
return True
def wait(self):
return 3
class AsyncView(APIView):
authentication_classes = [AsyncAuthentication]
permission_classes = [AsyncPermission]
throttle_classes = [AsyncThrottle]
async def get(self, request):
return Response({"message": "This is an async class based view."})
from adrf.decorators import api_view
@api_view(['GET'])
async def async_view(request):
return Response({"message": "This is an async function based view."})
For viewsets, all handler methods must be async too.
views.py
from django.contrib.auth import get_user_model
from rest_framework.response import Response
from adrf.viewsets import ViewSet
User = get_user_model()
class AsyncViewSet(ViewSet):
async def list(self, request):
return Response(
{"message": "This is the async `list` method of the viewset."}
)
async def retrieve(self, request, pk):
user = await User.objects.filter(pk=pk).afirst()
return Response({"user_pk": user and user.pk})
urls.py
from django.urls import path, include
from rest_framework import routers
from . import views
router = routers.DefaultRouter()
router.register(r"async_viewset", views.AsyncViewSet, basename="async")
urlpatterns = [
path("", include(router.urls)),
]
serializers.py
from adrf.serializers import Serializer
from rest_framework import serializers
class AsyncSerializer(Serializer):
username = serializers.CharField()
password = serializers.CharField()
age = serializers.IntegerField()
views.py
from .serializers import AsyncSerializer
from adrf.views import APIView
class AsyncView(APIView):
async def get(self, request):
data = {
"username": "test",
"password": "test",
"age": 10,
}
serializer = AsyncSerializer(data=data)
serializer.is_valid()
return await serializer.adata
models.py
from django.db import models
class Order(models.Model):
name = models.TextField()
serializers.py
from adrf.serializers import ModelSerializer
from .models import Order
class OrderSerializer(ModelSerializer):
class Meta:
model = Order
fields = ('name', )
views.py
from adrf.generics import ListCreateAPIView
from .models import Order
from .serializers import OrderSerializer
class ListCreateOrderView(ListCreateAPIView):
queryset = Order.objects.all()
serializer_class = OrderSerializer
FAQs
Async support for Django REST framework
We found that adrf demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.