Rest API instead of table in Django Proxy unmanaged models - django

In my Django arch. task I must connect multiple projects. Some can only communicate through REST API and don't have common database access. However, the task is to provide seamless communication on model level. I.e. my model should serve as a wrapper for read-only requests in such a way, that business layer does not see the difference between local models and models that wrap remote data. Assuming that inherited create-delete-update methods will do nothing and network communication and authentication is handled by some method:
read_remote_records(**kwargs)
This method returns any data that I would need for my model. All I need is to wrap it in query set api for seamless use in views.
I have read these:
Proxy models documentation
Unmanaged models documentation
Also documentation on creation of custom managers and querysets.
However, not a single good example for what I am set to achieve.
Perhaps you can provide a solid Django example how to modify manager and query set in this way?

Related

How to define relations in dynamically created model in loopback 4

I have a requirement where the model schema will be generated by the user from a user interface that will be exposed by REST API that means dynamic models, controllers and repositories. This already has been achieved.
Now the requirement is to allow users to specify relations between tables from the same user interface. For e.g. I am creating a table order then while defining its properties I should be able to map product_id property to product table.
How can I achieve this?
I am afraid LoopBack 4 does not offer first-class support for building model relations dynamically yet. We have been discussing this feature in GitHub issue loopback-next#2483
Essentially, there are two parts of the problem:
Create repository artifacts for navigating to related models.
This is IMO the easier part, since all you need to do is take the class constructor created by defineCrudRepositoryClass for the model you are trying to expose via REST API and create a subclass that will call methods like createHasManyRepositoryFactoryFor and registerInclusionResolver from the constructor.
Define a controller class implementing relation-specific REST API endpoints.
This is slightly more involved as you need to implement function that will define a new controller class with dynamically generated OpenAPI metadata for each model relation you want to expose via REST API. I would start with copy and paste of defineCrudRestController function and then reworking individual endpoints to provide navigational API for your relation. You should probably create multiple define{relation}Controller functions, one for each relation type (e.g. defineHasManyController).

Way of reusing loopback model

I have to implement reporting application that requires fetching data from existing ITS such as Jira.
I was able to login by extending StrongLoop's User model and calling REST API from JIRA.
Now, I want to share this codes by creating loopback component or something so that I could use this login method later on.
Please share your knowledge or best practice for creating loopback component.
Thanks
You probably want to look at writing a model "mixin", but without seeing your code it's hard to say exactly what that would look like. You might also just define an internal-only model that extends the built in User and then additional models that extend your new model and are actually implemented in the REST API.

CakePHP model to web service

I'm developing a web application that doesn't need database. All data comes via REST.
My question is: Is possible to use a model class (extended from AppModel) to manage data from a RESTfull service?, How to do that?, Does CakePHP any way to mapping directly a RESTfull web service?.
I have researched and I have found the $useTable attribute (http://book.cakephp.org/2.0/en/models/model-attributes.html#usetable) but I'm not sure if exists another way to do this in a better way.
My first idea is implement my own functions in a Model and put $useTable to false.
Thank you.
There is no such functionality that ships with CakePHP, but it's relatively easy to implement it on your own.
Check out datasources, they sit between the model and the data, but can also be used standalone, ie you can use datasource instances directly, or indirectly through the model layer (find/save/delete calls are using the datasource CRUD methods).
Checkout https://github.com/nodesagency/CakePHP-Rest-Datasource for some inspiration.

Django Tastypie, run an action

I was wondering if someone could point me in the right direction. I am currently trying to build an API with Django and Tastypie.
I would like to have a http request lookup a model via a primary key then run a model method/action.
I can't seem to identify and matching docs or methods to achieve what seems like a simple action..
Thanks for any pointers,
I don't believe TastyPie is used to run model methods. It wraps the model up as a Resource and exposes a set of methods, mainly get,post,put. Take a look at Django TastyPie QuickStart
The methods you can expose are those of a REST api and not custom methods.

Correct way of initializing internal data for API in Django with Piston/Django-tastypie vs jsonrpc

After some API implementations, using jsonrpclib in Python, I need to migrate them inside a Django Framework project. I am quite new in Django and Piston/tastypie, but have some experience using jsonrpc/xmlrpc libs in my Python apps.
Until now I have developed some modules, with a ServiceClass attached to the register of jsonrpc server who handle the request and call the methods in the ServiceClass.
When the class is attached to the register, a new instance of the ServiceClass is created, loading all the initial data and keeping it in memory, so every method called through jsonrpc can have access to the internal values in that instance.
Now, I am trying to do the same in Django with Piston or Tastypie. I followed this link http://www.robertshady.com/content/creating-very-basic-api-using-python-django-and-piston and other resources, and all the documentation I read is clear, showing the correct way to work with it:
Modify url.py to map requests like "/api/" to a specific handler.
Add a handler.py in the api application, extending the BaseHandler of
Piston/Tastypie.
So I am wondering if its the correct way of working with Django and APIs, to create the instance of my ServiceClass (init the data, provide the methods) inside handler.py when I create the instance of the Handler extending the BaseHandler. Is this Handler class instantiated once when the server starts? What if my ServiceClass relies on some Model to load the data from it?
I want to avoid the framework to instantiate my class everytime a new request arrive to the /api/ application.
I will be glad to hear about any recommendation,
Thanks,
Specifically for piston... You shouldnt really use the handler in terms of an instance. Its more like a metaclass that you set up with class attributes. These attributes control whicch model the handler will be bound to if any. And what fields it should show or what methods it supports.
Generally the request enter one of your methods and you then handle the request however you want, as an isolated state. If it needs to use a shared resource or use the model for queries, that part is up to you, being shared from some imported resource . You said you need a model which is why you would bind it to the handler as a class attribute and then query on it. You shouldnt really store state on the handler.