Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Middleware class to properly format arbitrary data
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  2 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Kevin  
View profile  
 More options Feb 14 2012, 6:14 am
From: Kevin <kveron...@gmail.com>
Date: Tue, 14 Feb 2012 03:14:38 -0800 (PST)
Local: Tues, Feb 14 2012 6:14 am
Subject: Middleware class to properly format arbitrary data
Hello everyone,

  Before I go ahead and build a simple middleware for this myself, I
would like to know if such a middleware already exists in the wild, so
that I can prevent re-inventing the wheel.  Here's an example, if
nobody is following what the subject actually means:

  Say I have a view that does not return a django.http.HTTPResponse
object.  Instead, it returns a dictionary or a file-like object.  I
would like said middleware to detect this and format the output data
accordingly.  In the case of a dictionary, it will automatically add a
JSON header, encode the dictionary and output it.  For the sake of a
file-like object, it will use it's .read() method to provide the
output data.  I would also like it to check if a Form object is being
returned and automatically provide a template and a RequestContext.

According to a DRY principle, this type of feature only makes sense to
have built into Django.  However, if I attempt to return anything but
an HTTPResponse object, I get a big fat exception saying that it just
won't work, and I need to manually construct my HTTPResponse object
specifically for my data.  Constructing HTTPResponses and Contexts is
one of my most hated parts of Django, it's not really fun and get's
very repetitive and old really fast.

Thanks.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Tom Evans  
View profile  
 More options Feb 14 2012, 6:29 am
From: Tom Evans <tevans...@googlemail.com>
Date: Tue, 14 Feb 2012 11:29:12 +0000
Local: Tues, Feb 14 2012 6:29 am
Subject: Re: Middleware class to properly format arbitrary data

On Tue, Feb 14, 2012 at 11:14 AM, Kevin <kveron...@gmail.com> wrote:
> Hello everyone,

>  Before I go ahead and build a simple middleware for this myself, I
> would like to know if such a middleware already exists in the wild, so
> that I can prevent re-inventing the wheel.  Here's an example, if
> nobody is following what the subject actually means:

Why would you do it as middleware? This would mean two things:

1) The middleware will have to do lots of nasty isinstance/other type checking.
2) You break the contract of what a view is - a view is a callable
returning a response. Your 'view' functions will just be random
functions returning arbitrary objects, which may or may not be
translated into a response by middleware. Hacky.

Instead, simply subclass HttpResponse, and make it do what you want it
to do. As an example, as your specified use case was to simplify
generating JSON responses, I give you JSONResponse:

http://djangosnippets.org/snippets/154/

Cheers

Tom


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »