I'm creating an app which has a Cordova client (smartphone) side and a Django backend. The Django backend will retrieve data from another API (the League of Legends API) and process it in a python file containing a python function. In order to keep the app fast, I'm using Tastypie and AJAX from jQuery Mobile. So basically:
Django + Tastypie API ---JSON--> Cordova + AJAX
I have everything set up so that data can be put into and taken out of a database, but this is all I can do so far.
My plan is to be able to retrieve data from the LoL API, run the data processing function, add it to a database, and return the processed data to the client--all on request of the AJAX client. This might be a 'noobish' question, but how can I run this certain function every time someone accesses my Tastypie API? And where should this function be called? Perhaps in the api.py file?
You can run this function in the api.py
or in the models.py
, depending on what exactly you want to do.
The example below shows an example to run a function if you do a post at the url /lol/
:
from tastypie.resources import ModelResource
from tastypie.utils import trailing_slash
class LolResource(ModelResource):
class Meta:
resource_name = 'lol'
allowed_methods = ['post'] # note that this resource just accept posts
def base_urls(self):
return [
url(r"^(?P<resource_name>%s)%s$" % (self._meta.resource_name, trailing_slash()), self.wrap_view('lol_api_function'), name="api_lol_api_function"),
]
def lol_api_function(self, request, **kwargs):
# you function
pass
Do not forget to add the LolResource
at your urls.py
.