Django-tastypie: how to ModelResource programmatically? - python

I have created a number of Django-tastypie resource definitions in resources.py. Most of them will be created by posting from javascript side (backbone-tastypie), but some of them I would like to be able to create right from python code/django views.
The reason for this is that object creation logic should be kept in one place, as well as authorization params.
Is there some neat way to create TastyPie ModelResource inside Python code? (may be making a "post" with "Requests" module?).

If i understand you right, your usecase is described in tastypie cookbook: http://django-tastypie.readthedocs.org/en/latest/cookbook.html#using-your-resource-in-regular-views

Related

Want to learn django with REST without rest frameworks

I am a php programmer, I have built some REST based solutions in php. Now I am learning python/django. I want to make a REST based solution in Django ( only for knowledge purpose ). I do not want to use any of REST frameworks/toolkits as This project is more a exploring django/python say how they work with raw REST concept.
I searched on net, But examples/tutorial filled on already built solutions. I also checkout for request method based filtering. I am thinking of two approaches.
Either urls.py have way to check request method and transfer to respective method in views.py.
Or I can add a pre load hook/class which determine request method on application initialize, And called respective method so overriding urls.py behavior (my preferred method).
If anybody can suggest a django way to do this?
Update : I found some interesting comments on SO, like https://stackoverflow.com/a/20898410/1230744 AND https://stackoverflow.com/a/1732520/1230744. Need to check if they can have the solution, I am searching.
Well I get the answer of my questions finally from following link. It is possible through using Class based Views + serialization.
Restful routes and Django
Snippet links in side above link gave pretty much example to gave quite picture of how one can create a REST Api using only Django Core. Also I used serialize https://docs.djangoproject.com/en/dev/topics/serialization/ for Json encoding
( Now if anybody prefer, he can flag duplicate the question. ;) )
You can start from learning the code of this projects:
http://tastypieapi.org/ Tastypie
http://www.django-rest-framework.org/ Django REST framework
They are snadrd de facto for REST API for Django and their code could be a good starting point.
Also, please review this questions:
Creating a REST API for a Django application
Adding REST to Django
Django and Restful APIs

Using Colander with Pyramid App

I have RESTFul API written in pyramid. View functions processes data in request.POST and request.matchdict and returns json response.
Eg: A method inside view class.
#view_config(route_name="temp_name", request_method="PUT")
def put_item(self):
# validates and processes self.request.POST
# validates and processes self.request.matchdict
# returns json reponse
As you can see, I'm doing validation inside view method, which I want to avoid.My intention is to separate validation from actual functionality.
How do I handle this?
I saw colander http://cornice.readthedocs.org/en/latest/validation.html#using-colander which looks really good in my case. But looks like it is integrated with cornice which I'm not using at all. And also, I can't convert whole app into cornice now. Is it possible to use colander in the same way as given in the above link with my app?
This is the first time I'm writing RESTFul API's, also just started learning pyramid and colander. Need your help. Thanks in advance.
You can use Colander independently of cornice. The most basic example for using Colander Schema in a pyramid application I remember you find here:
http://docs.pylonsproject.org/projects/pyramid_tutorials/en/latest/humans/zodb/index.html
This way you can encapsulate schema validation using colander schemas and validators.
A more recent introduction of pyramid 1.5 branch into that topic you find here:
http://pyramid.readthedocs.org/en/1.5-branch/quick_tutorial/forms.html
Oh, and look at that SO question. I liked it, may be it will be helpful to you as well:
Which one is the correct approach for form validation ? Colander's Schema validation or Deform's form validation?

How to check user agent inside Django template?

I want to check the useragent using Django inside my template. I know this is possible using JavaScript, but I wanted a server side solution.
I know I can use HttpRequest.META in some middleware class, which I am not currently looking for. I want to determine this using some code in the template itself, without any dependency on other files / classes.
Can anybody help?
You need to use context processors, more specifically django.core.context_processors.request.
This SO answer covers it quiet well:
How can I pass data to any template from any view in Django?
Especially this blog post, that is referenced in the SO answer:
http://www.b-list.org/weblog/2006/jun/14/django-tips-template-context-processors/

Implementing OData JSON interface on Django (Python)

We would like to have a OData JSON interface on our Django (Python 2.5.4) website. At the moment of writing there seems to be no library available.
I'm thinking of writing "some" logic to handle this ourselves.
Would it be a good idea to extend the Django JSON serializer?
Where and how to store the URI's related to the models?
I think it would be a good idea to extend the Django JSON serializer, but have a look at django-piston this might be the better route to go.
The URI's will have to be defined in your urls.py for your app, and then in your models you could define a function
get_odata_uri()
Which would work like the Django's get_absolute_url(). Instead of hardcoding it into your model, make sure you make use of the reverse function from django.core.urlresolvers

What is the best way to add an API to a Django application?

I am adding MetaWeblog API support to a Django CMS, and am not quite sure how to layer the application.
I am using django_xmlrpc, which allows me to map to parameterised functions for each request. It is just a case of what level do I hook in calls to the django application from the service functions (AddPage, EditPage etc)
For django-page-cms, and I suppose many django apps, the business logic and validation is contained within the forms. In this case there is PageForm(forms.ModelForm) and PageAdmin(ModelAdmin), which both contain a lot of logic and validation.
If I am to build an API to allow maintenance of pages and content, does this mean I should be programmatically creating and filling a PageAdmin instance? Then catching any exceptions, and converting to their api equivalent? Or would this be a bad idea - misusing what forms are intended for?
The other option is refactoring the code so that business and logic is kept outside of the form classes. Then I would have the form and api, both go through the separate business logic.
Any other alternatives?
What would be the best solution?
Web services API's are just more URL's.
These WS API URL's map to view functions.
The WS view functions handle GET and POST (and possibly PUT and DELETE).
The WS view functions use Forms as well as the Models to make things happen.
It is, in a way, like an admin interface. Except, there's no HTML.
The WS view functions respond with JSON messages or XML messages.
It seems python does not provide this out of the box. But there is something called abc module:
I quote from http://www.doughellmann.com/PyMOTW/abc/ "By defining an abstract base class, you can define a common API for a set of subclasses. This capability is especially useful in situations where a third-party is going to provide implementations..." => the goal of an API, defining a contract.

Categories

Resources