brack3t/django-braces — pull request #51 by esoergel (2013-07-09 00:10:22+00:00)
Title: Added JsonRequestMixin to attempt to parse incoming requests as JSON 

A mixin that attempts to parse the request as JSON. If request is properly formatted, the json is saved to self.request_json as a Python object.

I'm somewhat new to contributing and testing, so I'd welcome any feedback on this pull request. I wrote tests for this mixin and it's marked as having full coverage, so that's something, I suppose. Still, I'd appreciate it if someone could look over the tests I wrote and tell me if I'm doing something stupid.

I wanted this mixin to be as unobtrusive as possible - you can add this to a view without it affecting anything; it just provides a request_json attribute and a HTTP400 "Bad Request" response (but you'll have to call it yourself). Also, since this Mixin inherits from JSONResponseMixin, perhaps "JsonRequestMixin" is a bit of a misnomer (as it also gives access to the response methods).

If the request cannot be turned into JSON, request_json becomes None. This allows you to check for failure (request_json is None), failure or empty requests (not request_json), or ignore it altogether. I thought about putting in more API features (in particular, a method to get_or_400 from the JSON would be nice), but I thought it made sense to have a minimum functionality version first without imposing too rigid a structure.

What do you guys think about the utility of this mixin? I recently needed to parse JSON requests and had no idea how. It's pretty simple, but I think this makes it simpler. Are there more features I should add? Better ways to implement these features?