Skip to main content

All Basic is a simple Django app to to add some basic functionality to use the DRF API.

Project description

All Basic

All Basic is a simple Django app to to add some basic functionality to use the DRF API. It consists of separating business logic and the application of nested data that comes from user requests and validations that support nested data.

Quick start

Add allbasic to your INSTALLED_APPS setting like this:

INSTALLED_APPS = [
    ...
    'allbasic',
]

Create an application (what name is up) and create several files:

myapp/
    domains.py
    presentations.py
    validations.py

In your validations.py:

from allbasic.validations import BaseValidation

class ValidationUserDetailUpdate(BaseValidation):
    def schema(self):
        return {
            'last_name': {
                'type': 'string',
                'required': True,
                'empty': False
            },
            'first_name': {
                'type': 'string',
                'required': True,
                'empty': False
            }
        }

In your domains.py:

from allbasic.domains import BaseDomain

class DomainUserUpdate(BaseDomain):
    @transaction.atomic()
    def apply(self):
        data = self.context.get('data')
        user = self.context.get('user')

        user.first_name = data.get('first_name', user.first_name)
        user.last_name = data.get('last_name', user.last_name)

        return user.save()

In your presentations.py:

from allbasic.presentations import BasePresentation

class UserDetailPresentation(BasePresentation):
    def present(self):
        user = self.context.get('user')

        return {
            'id': user.pk,
            'username': user.username,
            'email': user.email,
            'last_name': user.last_name,
            'first_name': user.first_name
        }

In your views.py:

from allbasic.domains import ContextDomain
from allbasic.presentations import ContextPresentation
from allbasic.validations import ContextValidation

class UserDetailView(APIView):
    authentication_classes = (
        JSONWebTokenAuthentication,
        SessionAuthentication,
        BasicAuthentication
    )
    permission_classes = (IsAuthenticated,)

    def put(self, request):
        validation = ContextValidation(ValidationUserDetailUpdate(request.data))

        if validation.validate():
            ContextDomain(DomainUserUpdate(context={'data': validation.data, 'user': request.user})).do_apply()
            presentation = ContextPresentation(UserDetailPresentation(context={'user': request.user}))
            return Response(presentation.do_present())

        return Response(validation.errors, status=status.HTTP_400_BAD_REQUEST)

Project details


Release history Release notifications | RSS feed

This version

0.1

Download files

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

Source Distribution

django-allbasic-0.1.tar.gz (4.3 kB view details)

Uploaded Source

File details

Details for the file django-allbasic-0.1.tar.gz.

File metadata

  • Download URL: django-allbasic-0.1.tar.gz
  • Upload date:
  • Size: 4.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.6.7

File hashes

Hashes for django-allbasic-0.1.tar.gz
Algorithm Hash digest
SHA256 677fe34db3bf3b8808953ddf1759e7ade049807bd0c4b7172024dfede1a418f1
MD5 8d0b7f723cbda448420ab3dc8a672b82
BLAKE2b-256 17d5cb4e7cc2bebe2ad515c16b6a4bcf3ecd09f5964e1a7a1d20bb78c0d8d953

See more details on using hashes here.

Supported by

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