jgorset / django-respite Goto Github PK
View Code? Open in Web Editor NEWRespite conforms Django to Representational State Transfer (and, incidentally, HTTP)
License: MIT License
Respite conforms Django to Representational State Transfer (and, incidentally, HTTP)
License: MIT License
DEFAULT_FORMAT overrides formats set in the view class' supported_formats
attribute.
Route names generated by Resource
should be documented.
I'm trying to do something like:
from django.conf.urls.defaults import *
from respite.urls import resource
from some.views import v1, v2
urlpatterns = resource(prefix = '/bar', views = v1)
urlpatterns += resource(prefix = '/foo', views = v2)
It doesn't work because the first call to resource
empties routes.all
. I have a dirt and quick hack but it'd be great if code like above is supported out of the box.
Thanks.
JsonMiddleware
should use the encoding given in the requests' Content-Type
header to determine which codec it should use with json.loads
, and default to Latin 1
.
Respite incorrectly assumes that any media type that doesn't specify an encoding are encoded in ISO-8859-1
, but this is only true for media subtypes of the text
type. application/json
, for example, defaults to UTF-8
:
JSON text SHALL be encoded in Unicode. The default encoding is UTF-8. โ RFC-2616
So old versions of webkit browser and IE has stupid default accept headers. This is a problem if you have set your respite view to support xml
.
To give an example, older versions of webkit (seems to be fixed around march 2011) sends this as default accept headers:
Accept: application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
It prefers, xhtml
over html
, thats bad, but okay i guess, but it actually prefers application/xml
and image/png
over text/html
as well. This is of course a problem, cause that meens that visitors that uses these browser will get XML(!) instad of HTML.
See this article with good examples for a more thorough explanation.
I don't know how to fix this, since it's the browsers error, but removing "application/xml" and just relying on "text/xml" should fix it, but thats not to nice either.
This is not a problem if your supported formats list does not include xml
It would be nice if Respite could facilitate for parsing content types besides application/x-www-form-urlencoded
into request.POST
and request.PUT
.
Respite currently uses Model._meta.fields to retrieve the properties of a model, this does not include fields that are not actually present in the database table for that model. E.g foreign keys located on the related model and ManyToMany-fields.
Respite's default views render a HTML representation of the 404 Not Found page regardless of accepted formats. It should facilitate for serializing the error message as JSON and XML, too.
Respite should facilitate for several view functions sharing the same path and delegating the request based on the request method, like the default actions work.
Using Django 1.4 and given the following test:
from django.test import TestCase, Client
class ResourceIndexView(TestCase):
def test_put_should_update_resource(self):
"""
A PUT request should result in the resource being updated.
"""
resource = Resource(name="bob")
resource.save()
response = Client().post('/tasks/' + str(task.id), {
"_method": "PUT",
'name': "sally"
})
self.assertTrue(Resource.objects.all().exists())
I receive this error
======================================================================
ERROR: test_put_should_update_resource (resources.tests.ResourceIndexView)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/Users/user/Workspace/Git/django-todo/resources/tests.py", line 105, in test_put_should_update_resource
'description': "sally"
File "/Users/user/Workspace/Git/django-todo/venv/lib/python2.7/site-packages/django/test/client.py", line 449, in post
response = super(Client, self).post(path, data=data, content_type=content_type, **extra)
File "/Users/user/Workspace/Git/django-todo/venv/lib/python2.7/site-packages/django/test/client.py", line 262, in post
return self.request(**r)
File "/Users/user/Workspace/Git/django-todo/venv/lib/python2.7/site-packages/django/core/handlers/base.py", line 89, in get_response
response = middleware_method(request)
File "/Users/user/Workspace/Git/django-todo/venv/lib/python2.7/site-packages/respite/middleware.py", line 25, in process_request
request._raw_post_data = re.sub(r'_method=(PUT|PATCH|DELETE)&?', '', request.raw_post_data)
File "/Users/user/Workspace/Git/django-todo/venv/lib/python2.7/site-packages/django/http/__init__.py", line 337, in raw_post_data
return self.body
File "/Users/user/Workspace/Git/django-todo/venv/lib/python2.7/site-packages/django/http/__init__.py", line 326, in body
raise Exception("You cannot access body after reading from request's data stream")
Exception: You cannot access body after reading from request's data stream
If I instead use client.put
the view does not have a populated request.POST
dictionary. When I output request.raw_post_data
I get the following unexpected string:
'--BoUnDaRyStRiNg\r\nContent-Disposition: form-data; name="name"\r\n\r\nsally\r\n--BoUnDaRyStRiNg--\r\n'
Is there an example in this repo of how to use the test client with a PUT
request or is this truly a bug?
Thanks
According to the HTTP specification...
If a resource has been created on the origin server, the response SHOULD be 201 (Created) and contain an entity which describes the status of the request and refers to the new resource, and a Location header (see section 14.30).
We should look into the HTTP PATCH method and consider implementing it; updating resources is a common use-case.
The serialization functions in Serializer#process
should have docstrings describing their output.
Since issuing an OPTIONS-request from a webbrowser is not particularly common, we should consider excluding the HTML-format from the OPTIONS-view by default using @override_supported_formats.
Conveniently, doing so would also solve an issue with Microsoft Office Protocol Discovery which will kick in once somebody copy-pastes images into a Microsoft Word-backed e-mail client. To determine server capabilities this protocol discovery service will request OPTIONS - with an accept header that encompasses HTML - which in turn throws a 500 if no template is available.
HttpMethodOverrideMiddleware
should discard the _method
parameter.
Issuing HTTP OPTIONS requests for a format that Respite doesn't know how to serialize (such as HTML) will result in an UnknownSerializer
exception.
Views#options
should either override the supported formats to a list of serializable formats, or reference a template that may be populated by the end-user (and preferably fail gracefully if none exists).
Missing 'respite.views' in packages.
Respite names views for models in MixedCase incorrectly. For example, the view name for a model PhoneNumber
in the phone_numbers
module should be phone_numbers_phone_number
, not phone_numbers_phonenumber
.
Apparently, you can suffix URLs with arbitrary data and they will still work. For example, http://example.org/resources/new
is interchangeable with http://example.org/resources/newwwwwww
.
Huh.
The default serializer of a model would follow a foreignkey to another model, and then serialize that model. This causes a problem if the parent model also serializes the reverse relationship of the foreignkey. In development in only shuts down the runserver, but i'm not sure what would happen in production.
Here is an example that would cause this recursive bug
class Question(models.Model):
question = models.CharField()
created = models.DateTimeField()
def serialize(self):
return {
'id': self.id,
'question': self.question,
'created': self.created,
'votes': self.votes.all()
}
class Vote(models.Model):
question = models.ForeignKey('Question', related_name="votes")
choice = models.IntegerField()
Since self.votes.all()
would serialize a list of votes, that again with the default serializer would serialize self.question
we get a recursive problem.
I haven't tried it on a apache server, but when you run the devserver (runserver) it justs quits the runserver without any warnings what so ever.
Respite should eiter:
request.POST
should remain populated when the request method has been overridden.
After overriding a view resource (destroy) only to have it accept more than one keyword argument (id), it hit me that that resources that takes the 'id' argument should probably accept '**kwargs' insted of 'id'. And then do a kwargs lookup. This way you don't have to override a 'id' dependent view, just because your routing sends the resource several kwargs.
I haven't actually tried not overriding the view, but logically it would complain about being fed to many kwargs if i didn't.
We really, really, really need tests.
when trying to localize the prefix using ugettext_lazy, strange things happen:
http://localhost:8000/nachrichten/%3Cdjango.utils.functional.__proxy__%20object%20at%200x2161910%3Eindex.html
It seems that JSON is encoded as ASCII with unicode code points. It would probably be a better idea to encode it as UTF-8.
Respite should facilitate for JSON serialization.
JSONMiddleware
cannot propagate nested JSON objects to request.POST
due to limitations in the QueryDict
class, which can only be initialized from an urlencoded string. We need to find a way to initialize a QueryDict
instance in a way that isn't detrimental to our data integrity.
Respite is incompatible with the csrf_exempt
decorator since we're using an intermediate view to delegate the request according to its HTTP method.
Respite should respond to HTTP OPTIONS requests.
Respite raises a NoReverseMatch exception upon creating a new item if the "show" action isn't routed.
Respite should namespace its routes (e.g. news:articles
instead of news_articles
).
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.