adrf


Nameadrf JSON
Version 0.1.8 PyPI version JSON
download
home_pagehttps://github.com/em1208/adrf
SummaryAsync support for Django REST framework
upload_time2024-10-12 04:45:40
maintainerNone
docs_urlNone
authorEnrico Massa
requires_python>=3.8
licenseMIT
keywords async django rest framework drf
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # Async Django REST framework

**Async support for Django REST framework**

# Requirements

* Python 3.8+
* Django 4.1+

We **highly recommend** and only officially support the latest patch release of
each Python and Django series.

# Installation

Install using `pip`...

    pip install adrf

Add `'adrf'` to your `INSTALLED_APPS` setting.
```python
INSTALLED_APPS = [
    ...
    'adrf',
]
```

# Examples

# Async Views

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:

```python
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."})
```
# Async ViewSets

For viewsets, all handler methods must be async too.

views.py
```python
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
```python
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)),
]

```

# Async Serializers

serializers.py

```python
from adrf.serializers import Serializer
from rest_framework import serializers

class AsyncSerializer(Serializer):
    username = serializers.CharField()
    password = serializers.CharField()
    age = serializers.IntegerField()
```

views.py

```python
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
```

# Async Generics

models.py

```python
from django.db import models

class Order(models.Model):
    name = models.TextField()
```

serializers.py

```python
from adrf.serializers import ModelSerializer
from .models import Order

class OrderSerializer(ModelSerializer):
    class Meta:
        model = Order
        fields = ('name', )
```

views.py

```python
from adrf.generics import ListCreateAPIView
from .models import Order
from .serializers import OrderSerializer

class ListCreateOrderView(ListCreateAPIView):
    queryset = Order.objects.all()
    serializer_class = OrderSerializer
```

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/em1208/adrf",
    "name": "adrf",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.8",
    "maintainer_email": null,
    "keywords": "async, django, rest, framework, drf",
    "author": "Enrico Massa",
    "author_email": "enrico.massa@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/6c/52/947f6ebd75ffd2ae58c5397ac66d6ec85ff48343346dfa1ec8ea331bc6da/adrf-0.1.8.tar.gz",
    "platform": null,
    "description": "# Async Django REST framework\n\n**Async support for Django REST framework**\n\n# Requirements\n\n* Python 3.8+\n* Django 4.1+\n\nWe **highly recommend** and only officially support the latest patch release of\neach Python and Django series.\n\n# Installation\n\nInstall using `pip`...\n\n    pip install adrf\n\nAdd `'adrf'` to your `INSTALLED_APPS` setting.\n```python\nINSTALLED_APPS = [\n    ...\n    'adrf',\n]\n```\n\n# Examples\n\n# Async Views\n\nWhen using Django 4.1 and above, this package allows you to work with async class and function based views.\n\nFor 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.\n\nFor example:\n\n```python\nfrom adrf.views import APIView\n\nclass AsyncAuthentication(BaseAuthentication):\n    async def authenticate(self, request) -> tuple[User, None]:\n        return user, None\n\nclass AsyncPermission:\n    async def has_permission(self, request, view) -> bool:\n        if random.random() < 0.7:\n            return False\n\n        return True\n\n    async def has_object_permission(self, request, view, obj):\n        if obj.user == request.user or request.user.is_superuser:\n            return True\n\n        return False\n\nclass AsyncThrottle(BaseThrottle):\n    async def allow_request(self, request, view) -> bool:\n        if random.random() < 0.7:\n            return False\n\n        return True\n\n    def wait(self):\n        return 3\n\nclass AsyncView(APIView):\n    authentication_classes = [AsyncAuthentication]\n    permission_classes = [AsyncPermission]\n    throttle_classes = [AsyncThrottle]\n\n    async def get(self, request):\n        return Response({\"message\": \"This is an async class based view.\"})\n\nfrom adrf.decorators import api_view\n\n@api_view(['GET'])\nasync def async_view(request):\n    return Response({\"message\": \"This is an async function based view.\"})\n```\n# Async ViewSets\n\nFor viewsets, all handler methods must be async too.\n\nviews.py\n```python\nfrom django.contrib.auth import get_user_model\nfrom rest_framework.response import Response\n\nfrom adrf.viewsets import ViewSet\n\n\nUser = get_user_model()\n\n\nclass AsyncViewSet(ViewSet):\n\n    async def list(self, request):\n        return Response(\n            {\"message\": \"This is the async `list` method of the viewset.\"}\n        )\n\n    async def retrieve(self, request, pk):\n        user = await User.objects.filter(pk=pk).afirst()\n        return Response({\"user_pk\": user and user.pk})\n\n```\n\nurls.py\n```python\nfrom django.urls import path, include\nfrom rest_framework import routers\n\nfrom . import views\n\nrouter = routers.DefaultRouter()\nrouter.register(r\"async_viewset\", views.AsyncViewSet, basename=\"async\")\n\nurlpatterns = [\n    path(\"\", include(router.urls)),\n]\n\n```\n\n# Async Serializers\n\nserializers.py\n\n```python\nfrom adrf.serializers import Serializer\nfrom rest_framework import serializers\n\nclass AsyncSerializer(Serializer):\n    username = serializers.CharField()\n    password = serializers.CharField()\n    age = serializers.IntegerField()\n```\n\nviews.py\n\n```python\nfrom .serializers import AsyncSerializer\nfrom adrf.views import APIView\n\nclass AsyncView(APIView):\n    async def get(self, request):\n        data = {\n            \"username\": \"test\",\n            \"password\": \"test\",\n            \"age\": 10,\n        }\n        serializer = AsyncSerializer(data=data)\n        serializer.is_valid()\n        return await serializer.adata\n```\n\n# Async Generics\n\nmodels.py\n\n```python\nfrom django.db import models\n\nclass Order(models.Model):\n    name = models.TextField()\n```\n\nserializers.py\n\n```python\nfrom adrf.serializers import ModelSerializer\nfrom .models import Order\n\nclass OrderSerializer(ModelSerializer):\n    class Meta:\n        model = Order\n        fields = ('name', )\n```\n\nviews.py\n\n```python\nfrom adrf.generics import ListCreateAPIView\nfrom .models import Order\nfrom .serializers import OrderSerializer\n\nclass ListCreateOrderView(ListCreateAPIView):\n    queryset = Order.objects.all()\n    serializer_class = OrderSerializer\n```\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Async support for Django REST framework",
    "version": "0.1.8",
    "project_urls": {
        "Homepage": "https://github.com/em1208/adrf",
        "Repository": "https://github.com/em1208/adrf"
    },
    "split_keywords": [
        "async",
        " django",
        " rest",
        " framework",
        " drf"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "3911bd3912efe83b5daa6e42aa3098a13bb37d36ccdfecc284bf04bd9fe59073",
                "md5": "cdf071a6f8f1c82dbe305b506916a56d",
                "sha256": "3032b987085d75cfd59eb3d4dcd7138fc20085de1782b065603559ccec69531f"
            },
            "downloads": -1,
            "filename": "adrf-0.1.8-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "cdf071a6f8f1c82dbe305b506916a56d",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8",
            "size": 20671,
            "upload_time": "2024-10-12T04:45:38",
            "upload_time_iso_8601": "2024-10-12T04:45:38.779672Z",
            "url": "https://files.pythonhosted.org/packages/39/11/bd3912efe83b5daa6e42aa3098a13bb37d36ccdfecc284bf04bd9fe59073/adrf-0.1.8-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "6c52947f6ebd75ffd2ae58c5397ac66d6ec85ff48343346dfa1ec8ea331bc6da",
                "md5": "d235528172daea51f8733fc864c41eb3",
                "sha256": "18844630dd9272c38cc3f761fce6bfb50f91c4f84dadf99846f86d4527f19c7f"
            },
            "downloads": -1,
            "filename": "adrf-0.1.8.tar.gz",
            "has_sig": false,
            "md5_digest": "d235528172daea51f8733fc864c41eb3",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8",
            "size": 17128,
            "upload_time": "2024-10-12T04:45:40",
            "upload_time_iso_8601": "2024-10-12T04:45:40.368502Z",
            "url": "https://files.pythonhosted.org/packages/6c/52/947f6ebd75ffd2ae58c5397ac66d6ec85ff48343346dfa1ec8ea331bc6da/adrf-0.1.8.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-10-12 04:45:40",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "em1208",
    "github_project": "adrf",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "tox": true,
    "lcname": "adrf"
}
        
Elapsed time: 0.32969s