Skip to main content

Django multi-action endpoint

Project description

Django Multi-action View

Overview

This tool can be used to route multiple actions to one endpoint in a Django project

Installation

Install using pip

pip install django-multi-action

Usage

  • Create an action by inheriting BaseAction
from multiaction import  BaseAction
from django.contrib.auth.models import User
from django.http import JsonResponse


class UserAction(BaseAction):

    def create_user(self, request, *args, **kwargs):
        username = request.GET.get('username')
        user = User.objects.create(username=username)
        
        return JsonResponse(data={"id": user.id, "username": user.username}, status=201)
  • Create a connector that inherit BaseConnector
from multiaction import  BaseConnector


class MyView(BaseConnector):
    actions = {'user': UserAction}
# urls.py

urlpatterns = [
      path('/actions', MyView.as_view())
]
  • Sample curl request
curl --location --request GET 'http://127.0.0.1:8000/actions?action_id=user&target=create_user&username=sirrobot01' \
  • When making a request to MyView, the following are required
  1. action_id : This is the action to be implemented
  2. target : This is the target/method to be implemented

Note:

  • Each target/method on the action class has two positional arguments, self and request
  • When making a GET request, your parameters will be accessible from request.GET, meanwhile, it will be accessible from request.POST when making a POST request
  • If you want to use django rest framework and it's browsable API, you will need to add APIView to your action class
from multiaction import  BaseAction
from django.contrib.auth.models import User
from rest_framework.response import Response
from rest_framework.views import APIView


class UserAction(BaseAction, APIView):

    def create_user(self, request, *args, **kwargs):
        username = request.GET.get('username')
        user = User.objects.create(username=username)
        
        return Response(data={"id": user.id, "username": user.username}, status=201)

You can check Reference for more usage

Contribute

Well, no big drama, fork the repo and make pull requests.

Follow me (I am not boring, I promise)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-multi-action-0.0.1.tar.gz (3.2 kB view details)

Uploaded Source

File details

Details for the file django-multi-action-0.0.1.tar.gz.

File metadata

  • Download URL: django-multi-action-0.0.1.tar.gz
  • Upload date:
  • Size: 3.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.6.0 importlib_metadata/4.8.2 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.6.5

File hashes

Hashes for django-multi-action-0.0.1.tar.gz
Algorithm Hash digest
SHA256 a997fd74f5c32f90b5138eb2a449d4a8fb33400aa51973ece42913c170596d51
MD5 57af51c6f1a9a3a85db4da9ab70234fc
BLAKE2b-256 a7dd4e69e498410b338462a25c451854d9a356eb91030a38a91eb70bedd6051a

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page