Package: django-directapps
This is a little application for direct access to all the models and their
data in a project. By default, the application has access for users withis_staffmarks. But this and much more can be changed.This is app might interest you if you use Django as the backend to some kind of
external client application. There are no templates for formatting and
displaying of data on the client. Only JSON. All data is taken directly from
the models, so the application can be used for a cool administrative interface.ImportantThe client application must support cookies, parse “csrftoken” and send
it asX-CSRFTokenheader inPOST,PUT,PATCHandDELETErequests.Installationpipinstalldjango-directappsChange your next project files.# settins.pyINSTALLED_APPS=(...'directapps',...)# urls.pyurlpatterns=[...url(r'^apps/',include('directapps.urls',namespace="directapps")),...]Start the development server Django, if it is not running.Now you can open a browser to this address to see a list of available
applications and links to data schematics for each.Enjoy!Using the REST APIGeneral informationThe REST API endpoints are built as follows:/<list_apps>/<app>/<model>[.<model_using>]/<object_id>/<relation>[.<relation_using>]/<relation_id>/You can perform actions on endpoints by specifying them with an underscore:.../<model>[.<model_using>]/_<model_action>/
.../<model>[.<model_using>]/<object_id>/_<object_action>/.../<relation>[.<relation_using>]/_<relation_action>/
.../<relation>[.<relation_using>]/<relation_id>/_<relation_object_action>/Parameters<model_using>and<relation_using>is define a database
connection by its ID.NoteThe parameter<model_using>is useless when you work with relations,
but you can it remain for convenience.For data sets (<model>and<relation>) apply the following GET-parameters:1. 'o' - ordering key (by default is from models definition)
2. 'c' - columns key (by default is all from allowed)
3. 'l' - limit key (by default is 10)
4. 'p' - page key (by default is 1)
5. 'q' - search key
6. 'f' - foreign key (used in conjunction with the search key, see bellow)All these keys can be overridden either together or separately in controller.
You can check their names in the full scheme of model.NoteUnlike many of the application with REST, a description of the data for
client applications is not transmitted with every call, and exists as a
separate resource, allowing you to do everything faster. This is a
characteristic feature of this app and it means that:The client gets the list of available applications.Gets application schema which describes what data can be provided
and on what resource they are.And only then begin to work with the data.The client application is responsible for the maintenance of relations
between data models for fields with external links have the attribute
“relation” that contains the full name of the relation.Let’s analyze it on the example ofdjango.contrib.authapplication.Getting the scheme of available applications and modelsShort scheme as list applications with version and checksum:GET /apps/Full scheme as list models in application:GET /apps/auth/NoteIn the response the permissions for the superuser are marked with just
the text “all”, and for other users there will be a list.Creating the dataMake groups:POST /apps/auth/group/ {'name': 'Administrators'}
POST /apps/auth/group/_create/ {'name': 'Managers'}
POST /apps/auth/group/_add/ {'name': 'Operators'}Getting the dataWhen you use several databases, you should use the indication of the database
from which you want to get the object. To do this, use<model_using>and<relation_using>parameters.Get the list users:GET /apps/auth/user/
GET /apps/auth/user/?o=-id,username
GET /apps/auth/user/?o=-id&l=1
GET /apps/auth/user/?q=blablaGET /apps/auth/group/1/user/
GET /apps/auth/group/1/user/?o=-id,username
GET /apps/auth/group/1/user/?o=-id&l=1
GET /apps/auth/group/1/user/?q=blablaGet the user by ID=1:GET /apps/auth/user/1/GET /apps/auth/group/1/user/1/Useforeign keyfor search available groups in ManyToManyField:GET /apps/auth/user/_fkey/?f=groups
GET /apps/auth/user/_fkey/?f=groups&q=rator
GET /apps/auth/user/_fkey/?f=groups&q=rator&o=-id&l=1Of course, theforeign keyyou can use with ForeignKey or OneToOneField too:GET /apps/auth/permission/_fkey/?f=content_typeModify data:PATCH /apps/auth/user/1/ { first_name: 'Johnny' }
PUT /apps/auth/user/1/ { first_name: 'John' }
POST /apps/auth/user/1/_patch/ { first_name: 'John Bo' }
POST /apps/auth/user/1/_put/ { first_name: 'John Bon' }
POST /apps/auth/user/1/_update/ { last_name: 'Jovi' }Delete data:DELETE /apps/auth/user/1/
POST /apps/auth/user/1/_delete/Using in browserYou can look at the example works in the JavaScript console and use it as a test.functiongetCookie(cname){varname=cname+'=';varca=document.cookie.split(';');for(vari=0;i<ca.length;i++){varc=ca[i];while(c.charAt(0)===' ')c=c.substring(1);if(c.indexOf(name)===0)returnc.substring(name.length,c.length);}return'';}functionmakeRequest(method,url,data,content_type){varxhr=newXMLHttpRequest(),content_type=content_type||'application/x-www-form-urlencoded';xhr.open(method,url,false);if(!(/^(GET|HEAD|OPTIONS|TRACE)$/.test(method.toUpperCase()))){xhr.setRequestHeader('Content-Type',content_type);xhr.setRequestHeader('X-CSRFToken',getCookie('csrftoken'));}xhr.send(data);if(xhr.status===200)returnJSON.parse(xhr.responseText);console.error(xhr.responseText);}vargroup1=makeRequest('post','/apps/auth/group/','name=Operators 1'),group2=makeRequest('post','/apps/auth/group/',JSON.stringify({name:'Operators 2'}),'application/json');makeRequest('get','/apps/auth/group/?o=name,-id&q=operators&p=1&l=3&id__gte=1');makeRequest('put','/apps/auth/group/'+group1.pk+'/','name=Operators 11');makeRequest('patch','/apps/auth/group/'+group2.pk+'/','name=Operators 22');makeRequest('get','/apps/auth/group/?o=name,-id&q=operators&p=1&l=3&id__gte=1');makeRequest('delete','/apps/auth/group/','id='+group1.pk+','+group2.pk);makeRequest('delete','/apps/auth/group/',JSON.stringify({id:[group1.pk,group2.pk]}),'application/json');SettingsAll next settings must be within the dictionaryDIRECTAPPS, when you
define them in the file settings.pyACCESS_FUNCTIONFunction that checks access to resources. You may want to use:directapps.access.authenticated- for authenticated users.directapps.access.staff- for employers and superusers.directapps.access.superuser- for superusers only.directapps.access.view_users- for users with view permission for User
model.any custom function.The default is the internal functiondirectapps.access.staff.ATTRIBUTE_NAMEThe name of the attribute in the model that is bound to the controller.
By default isdirectapps_controller.CHECKSUM_VERSIONThe options for the checksum compilation of the scheme.
By default is“1”.CONTROLLERSDictionary own controllers for models of third-party applications.
By default is blank.EXCLUDE_APPSThe list of excluded applications.
By default is blank.EXCLUDE_MODELSThe list of excluded models.
By default is blank.JSON_DUMPS_PARAMSThe options for creating JSON.
By default is{'indent': 2, 'ensure_ascii': False}.MASK_PASSWORD_FIELDSThe options for masking all the fields with the name “password”.
By default isTrue.MASTER_CONTROLLERClass (as string for import) of the master controller, which is used by default.
By default isNoneand uses internal class.USE_TIME_ISOFORMATThe options for the using ISO time with microseconds intoJSONEncoder.
By default isFalseandJSONEncoderused ECMA-262 format.SEARCH_KEYThe key by which data is received for search.
By default is ‘q’.FOREIGN_KEYThe key by which the name of the field or column with a relation
(for the “_fkey” action) is received from the client.
By default is ‘f’.COLUMNS_KEYThe key by which the list of fields for rendering is received.
By default is ‘c’.ORDERING_KEYThe key by which sorting is accepted from the client.
By default is ‘o’.LIMIT_KEYThe key by which the limit of records is accepted from the client.
By default is ‘l’.PAGE_KEYThe key by which the client receives the page number.
By default is ‘p’.LIMITThe global working limit of returned records.
By default is 10.MAX_LIMITThe global maximum limit of returned records, which does not allow to kill the
server with huge data sets.
By default is 50.Customizing of controllersTo change the behavior globally for all your controllers, make your main
controller based on the built-in and connect it:# myapp/controllers.pyimportloggingfromdirectappsimportcontrollerslogger=logging.getLogger(__name__)classCustomModelController(controllers.ModelController):# of course, your may be do it in the settings, but just for example :)search_key='search'limit=50max_limit=1000classCustomObjectController(controllers.ObjectController):defaction_get(self,request,object,*args,**kwargs):logger.info('%sopen%swith ID=%s',request.user,self,object,)returnsuper().action_get(request,object,*args,**kwargs)classCustomMasterController(controllers.MasterController):model_ctrl_class=CustomModelControllerobject_ctrl_class=CustomObjectController# settings.pyDIRECTAPPS={'MASTER_CONTROLLER':'myapp.controllers.CustomMasterController'}To change the behavior of only one controller, make your own based on the
built-in and connect it like this:# myapp/controllers.pyfromdjango.db.modelsimportCountfromdirectappsimportcontrollersclassUserModelController(controllers.ModelController):annotations=(# Method or property on instance of model.'get_full_name','get_short_name',# QuerySet annotation.'groups__count',)defget_queryset(self,*args,**kwargs):"""Returns modified QuerySet."""qs=super().get_queryset(*args,**kwargs)qs=qs.annotate(Count('groups'))returnqsdefinfo(self,request,qs):"""Returns information about the set. Redefined method."""all_users=self.get_queryset(request,using=qs.using)return{'total':all_users.count()}classUserController(controllers.MasterController):model_ctrl_class=UserModelController# settins.pyDIRECTAPPS={'CONTROLLERS':{'auth.user':'myapp.controllers.UserController',}}ContributingIf you want to translate the app into your language or to offer a more
competent application code, you can do so using the “Pull Requests” ongitlab.