
Overview
--------

This library will help you to scaffold full **Restful API Resources** in seconds using only one command:

.. code:: console

    $ python manage.py dr_scaffold blog Post body:textfield author:foreignkey:Author

    🎉 Your RESTful Post api resource is ready 🎉

-  ``models.py`` with Models and fields generated by the CLI ⚡
-  ``admin.py`` with Models registered and ready ⚡
-  ``views.py`` with appropriate ViewSets ready⚡
-  ``urls.py`` with appropriate URLs ready.⚡
-  ``serializers.py`` with Model Serializers ready ⚡
-  and more ...



Installation and usage
----------------------

For a detailed guide read `scaffold django apis like a champion <https://www.abdenasser.com/scaffold-django-apis>`_
*******************************************************************************************************************

This library assumes that you have setup your project with **Django Rest
Framework**.
if not, please refer to `this guide <https://www.django-rest-framework.org/#installation>`_

1- Install dr_scaffold package :
================================

.. code:: console

    $ pip install dr-scaffold

2- Add ``dr_scaffold`` to your INSTALLED\_APPS like this:
=========================================================

.. code:: python

    INSTALLED_APPS = [
        ...
        'dr_scaffold'
    ]

3- If you are using a custom API structure, add ``CORE_FOLDER`` and ``API_FOLDER`` to your ``settings.py``:
===========================================================================================================

.. code:: python
    
    #include a forward slash at the end of each

    CORE_FOLDER = "my_core_folder/" # you can leave them empty
    API_FOLDER = "my_api_folder/"   # or set them to be the same

4- Run the your scaffolds like this:
===========================================

.. code:: console

    $ python manage.py dr_scaffold blog Post body:textfield author:foreignkey:Author --mixins CRUD

    🎉 Your RESTful Post api resource is ready 🎉




Generate tests
______________

We support generating tests for your models and apis, you can generate tests by adding `--tests` to your command like follow:

.. code:: console

    $ python manage.py dr_scaffold blog Author name:charfield --tests


This will generate factories for your models and their tests (ViewSets tests will be added soon), we depend on ``pytest`` and ``factory_boy`` so you should have them installed in order to run your tests.

Also bare in mind that you should run your migrations before running the tests.


   
Generate ViewSet Mixins
_______________________

We support two types of ViewSets, we support **ModelViewSet** and we support **ViewSets** with Mixins. 

- ModelViewSets are the default that get generated with the dr_scaffold command
- To generate a view with Mixins pass a value of what mixins you want to include like ``--mixins CRUD`` this will result in a view with the Create, List, Retrieve, Update, Destroy actions.

Let's generate an API that does only support the **Create** and **Read** methods (Read is both list and retrieve):

.. code:: console

    $ python manage.py dr_scaffold blog Author name:charfield --mixins CR
    
    🎉 Your RESTful Post api resource is ready 🎉


The command will generate an Author API with a ViewSet like the following:

.. code:: python

    class AuthorViewSet(
        mixins.CreateModelMixin,
        mixins.ListModelMixin,
        mixins.RetrieveModelMixin,
        viewsets.GenericViewSet
    ):
        queryset = Author.objects.all()
        serializer_class = AuthorSerializer
        #permission_classes = (permissions.IsAuthenticated,)

        def get_queryset(self):
            #user = self.request.user
            queryset = Author.objects.all()
            #insert specific queryset logic here
            return queryset

        def get_object(self):
            #insert specific get_object logic here
            return super().get_object()

        def create(self, request, *args, **kwargs):
            serializer = AuthorSerializer(data=request.data)
            serializer.is_valid(raise_exception=True)
            serializer.save()
            return Response(serializer.data)

        def list(self, request, *args, **kwargs):
            queryset = self.get_queryset()
            serializer = AuthorSerializer(queryset, many=True)
            return Response(serializer.data)

        def retrieve(self, request, *args, **kwargs):
            instance = self.get_object()
            serializer = AuthorSerializer(instance=instance)
            return Response(serializer.data)


   
Supported field types
---------------------

We support most of django field types.



TODO
----

-  add an option to include swagger documentation

