Giter VIP home page Giter VIP logo

rest_utils's Introduction

Codacy BadgeCircleCIPyPI version

ASYNC EASY UTILS

Simple tools to create RESTful backend API based on TorToiseORM and Starlette stack.

DESCRIPTION

The main goal of the project is to provide easy-to-use tools for creating simple backend applications, inspired by Django REST based on:

The project provides:

  • Serializer
  • View

Serializer

Tool similar to Django REST Serializer, which purpose is to:

  • interface with the database model while performing CRUD operations Provide save, update and delete method.
  • data serialization Provide methods to serialize model instance into dict, dict to model instance.
  • data validation Validates data integrity against the model class. Inside serializer it is possible to enclose custom validation logic by adding validation methods.
  • defining access to model attributes provides the ability to define the available fields of the database model.
  • creating dynamic model attributes Provide ability to define custom property methods for model that's are dynamic calculate.

View

Tool similar to Django REST ModelViewSet, which purpose is to:

  • provide basic views for CRUD operations. View implement GET, POST, PATCH, DELETE methods OOTB
  • provide ability to define queryset access. Inside view we can assign queryset which will be used

HOW TO USE

Serializer

Definition based on Django REST. Each Seriliazier must have a defined model and fields to operate on. These definitions must be inside the Meta class.

from tortoise_rest_utils.serializer import Serializer


class SampleSerializer(Serializer):
    class Meta:
            model = SampleModel
            fields = ('attribute_1', 'attribute_2')

Each value inside fields must be part od model attribute or belongs to serialized methods. We can define read only custom model properties by using methods like:

class SampleSerializer(Serializer):
    class Meta:
            model = SampleModel
            fields = (attribute_1', 'attribute_2', 'serialized_attribute')
            
    async def get_serialized_attribute(self):
        return 'Foo'

Methods that start with 'get_' are handled like model properties in pattern: 'get_<field_name>'. After creating serialized attribute You should inlcude field_name in fields. Each serialized attribute is corutine.

It is possible to create custom validation method for field from model attributes:

class SampleSerializer(Serializer):
    class Meta:
            model = SampleModel
            fields = (attribute_1', 'attribute_2')
            
    async def validate_attribute_1(self, data):
        if data is not 'Foo':
            return False

Methods that start with 'validate_' are handled like validators to model attribute in pattern: 'validate_<field_name>'. During validation Serializer pass initial data into each validator method. Each validator method is corutine.

It is possible to create foreign key slug field:

class SampleSerializer(Serializer):
    sample_slug = ForeignKeyField(slug_field='foo',
                                  queryset=lambda: Model.all(),
                                  many=False)

class Meta:
    model = SampleModel
    fields = (attribute_1', 'attribute_2', 'sample_slug')

rest_utils's People

Contributors

t1waz avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.