|
| 1 | +Django REST Framework SaaS Plugin |
| 2 | +================================= |
| 3 | + |
| 4 | +### Overview |
| 5 | + |
| 6 | +This is a SaaS driven plugin for Django REST Framework. It offers a simple way |
| 7 | +to separate client customizations for your core API web services. Currently, this |
| 8 | +initial version only supports client API routing via ViewSets in conjunction with |
| 9 | +an extension of the Django REST Framework SimpleRouter. Future releases will |
| 10 | +have broader coverage of DRF features for custom client routing. |
| 11 | + |
| 12 | +### Install |
| 13 | +```pip install djangorestframework-saasy``` |
| 14 | + |
| 15 | +### Requirements |
| 16 | +- Python (2.7) |
| 17 | +- Django (1.4.2+) |
| 18 | +- Django rest framework (2.3.14+) |
| 19 | + |
| 20 | +### Example |
| 21 | + |
| 22 | +Define the client model in your django rest framework settings: |
| 23 | +```python |
| 24 | +REST_SETTINGS = { |
| 25 | + ... |
| 26 | + "SAAS": { |
| 27 | + "MODEL": "path.to.model.ClientModel" |
| 28 | + } |
| 29 | + ... |
| 30 | +} |
| 31 | +``` |
| 32 | + |
| 33 | +Use the saas client mixin provided by the SaaS plugin, and define the required class methods: |
| 34 | +```python |
| 35 | +from django.db import models |
| 36 | +from rest_framework_saasy.client import ClientMixin |
| 37 | + |
| 38 | + |
| 39 | +class ClientModel(models.Model, ClientMixin): |
| 40 | + """client model""" |
| 41 | + name = models.CharField(max_length=128) |
| 42 | + |
| 43 | + @staticmethod |
| 44 | + def saas_lookup_field(): |
| 45 | + """DRF-SaaS lookup field definition""" |
| 46 | + return 'name' |
| 47 | + |
| 48 | + def saas_client_module(self, saas_url_kw, *args, **kwargs): |
| 49 | + return 'customizations.{}'.format(self.name) |
| 50 | +``` |
| 51 | + |
| 52 | +#### ClientMixin methods |
| 53 | + |
| 54 | +- *saas_lookup_field* **[required]** |
| 55 | + |
| 56 | + This method defines what field in your client model to use when looking up |
| 57 | + the client in the database, to verify they exist. |
| 58 | + |
| 59 | +- *saas_client_module* **[required]** |
| 60 | + |
| 61 | + Parameters: |
| 62 | + - *saas_url_kw* - [string] - value from URL key word argument - the client |
| 63 | + idenfitication value. |
| 64 | + |
| 65 | + All client code should be separated from the core and also from each other. |
| 66 | + A good practice to follow is that there is a folder for all client specific code, |
| 67 | + separate from the core, with a folder for each client. That said, you can impose |
| 68 | + any kind of path rules you wish. |
| 69 | + |
| 70 | + ``` |
| 71 | + project |
| 72 | + ├── customizations |
| 73 | + │ └── client_name |
| 74 | + │ └── app |
| 75 | + │ └── subpackage |
| 76 | + │ └── module.py |
| 77 | + └── app |
| 78 | + └── subpackage |
| 79 | + └── module.py |
| 80 | + ``` |
| 81 | + |
| 82 | +### ViewSets |
| 83 | + |
| 84 | +The idea is there is a core web service ViewSet, *WebService*, defined |
| 85 | +in **app.subpackage.module** and in **customizations.client_name.app.subpackage.module** |
| 86 | +where there is also a class named *WebService* |
| 87 | + |
| 88 | +**app.subpackage.module** |
| 89 | +```python |
| 90 | +from rest_framework import viewsets |
| 91 | +from rest_framework_saasy import viewsets as saas_viewsets |
| 92 | + |
| 93 | +from .models import WebServiceModel |
| 94 | +from .serializers import WebServiceSerializer |
| 95 | + |
| 96 | +class WebService(saas_viewsets.ViewSetMixin, viewsets.ModelViewSet): |
| 97 | + queryset = WebServiceModel.objects.all() |
| 98 | + serializer_class = WebServiceSerializer |
| 99 | +``` |
| 100 | + |
| 101 | +**customizations.client.app.subpackage.module** |
| 102 | +```python |
| 103 | +from app.subpackage.module import WebService as CoreWebService |
| 104 | + |
| 105 | +class WebService(CoreWebService): |
| 106 | + # client customizations |
| 107 | +``` |
| 108 | + |
| 109 | +You can define the module path of client code and you can also define the subpackage |
| 110 | +path for the ViewSet mixed with the *saas_viewsets.ViewSetMixin*. |
| 111 | + |
| 112 | +What cannot be customized is the name of the class - the class name *WebService* in the |
| 113 | +core must be defined identically in the client custom module. |
| 114 | + |
| 115 | +#### ViewSetMixin methods |
| 116 | + |
| 117 | +- *saas_module* **[optional]** |
| 118 | + |
| 119 | + By default, viewset will be routed in a similar way as in the diagram above: |
| 120 | + |
| 121 | + ``` |
| 122 | + project |
| 123 | + ├── customizations |
| 124 | + │ └── client_name |
| 125 | + │ └── app |
| 126 | + │ └── subpackage |
| 127 | + │ └── module.py |
| 128 | + └── app |
| 129 | + └── subpackage |
| 130 | + └── module.py |
| 131 | + ``` |
| 132 | + |
| 133 | + However, the SaaS viewset has an optional method that can be defined, *saas_module* |
| 134 | + This returns the path that should be used in the client package. **It must be |
| 135 | + defined with the staticmethod decorator.** Let's slightly alter our *WebService* example above: |
| 136 | + |
| 137 | + ```python |
| 138 | + class WebService(saas_viewsets.ViewSetMixin, viewsets.ModelViewSet): |
| 139 | + ... |
| 140 | + @staticmethod |
| 141 | + def saas_module(): |
| 142 | + return 'other.package.name' |
| 143 | + ``` |
| 144 | + |
| 145 | + The expected file system package defintion for *WebService* customizations would be: |
| 146 | + |
| 147 | + ``` |
| 148 | + project |
| 149 | + ├── customizations |
| 150 | + │ └── client_name |
| 151 | + │ └── other |
| 152 | + │ └── package |
| 153 | + │ └── name.py |
| 154 | + └── app |
| 155 | + └── subpackage |
| 156 | + └── module.py |
| 157 | + ``` |
| 158 | + |
| 159 | +#### ViewSet attributes |
| 160 | + |
| 161 | +*saas_url_kw* is a new attribute made available to the ViewSet instance. |
| 162 | +The value of the valid identifier from the URL key word argument can be |
| 163 | +accessed at any time. If no client specific route was used, *saas_url_kw* |
| 164 | +defaults to None. |
| 165 | + |
| 166 | +### SaaS SimpleRouter |
| 167 | + |
| 168 | +You'll register your new SaaSy viewsets in exactly the same way Django |
| 169 | +REST Framework defines. |
| 170 | + |
| 171 | +#### app.urls |
| 172 | +```python |
| 173 | +from rest_framework_saasy import routers |
| 174 | +from .views import NoteViewSet |
| 175 | + |
| 176 | + |
| 177 | +router = routers.SimpleRouter() |
| 178 | +router.register(r'notes', NoteViewSet) |
| 179 | +``` |
| 180 | + |
| 181 | +Client specific routes will be made available immediately: |
| 182 | +``` |
| 183 | +^notes/$ [name='note-list'] |
| 184 | +^notes/(?P<pk>[^/]+)/$ [name='note-detail'] |
| 185 | +^(?P<saas_url_kw>.*)/notes/$ [name='note-list'] |
| 186 | +^(?P<saas_url_kw>.*)/notes/(?P<pk>[^/]+)/$ [name='note-detail'] |
| 187 | +``` |
| 188 | + |
| 189 | +**Note:** If a client key word argument is provided, but the client cannot |
| 190 | +be retreived from the database with the given identifier, the |
| 191 | +plugin will simply return a 404. |
| 192 | + |
1 | 193 | License |
2 | 194 | ======= |
3 | 195 | The MIT License (MIT) |
|
0 commit comments