mediagoblin-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [GMG-Devel] [proposal] Class-based views


From: Christopher Allan Webber
Subject: Re: [GMG-Devel] [proposal] Class-based views
Date: Mon, 23 Apr 2012 14:57:06 -0500
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.0.92 (gnu/linux)

I think this is good.  We should allow methods to still provide normal
function based views for simple views but develop a common pattern for
class based views as described below.

The one thing I am concerned about is, how to keep things from getting
to be confusing for new contributors?  Current method based views are
easy to read.  This might not be as much.

Otherwise, I'm happy to see this moving forward, esp with API work
underway.

 - Chris


Joar Wandborg <address@hidden> writes:

> This is my proposal for the new view structure, feedback welcome.
>
> These are changes that would be implemented to help in the development of
> the MediaGoblin API.
>
> Currently we have method-based views
>
> - Views are methods with at least one parameter (request) that holds request
>   information and additional instantiated objects and methods such as `db`,
>   `user`, `staticdirect`, `session`. 
> - View methods are decorated to, for example:
>   - Force login on the current page.
>   - Enable pagination
>   - Fetch a model to a view based on request data
>
> What we would need is
>
> - Separate common logic from rendering logic
> - Have multiple rendering methods (`as_json()`, `as_html()`)
> - Provide classes for, for example pagination and fetching a model based on
>   request data ('mixin's?)
>
>     class MediaView(View, MediaEntryMixin):
>         def setup(self):
>             pass
>         
>         def as_json(self):
>             import simplejson
>             simplejson.dumps(self.media.public_data())
>
>         def as_html(self):
>             return render_to_response(
>                 request,
>                 get_media_manager(media.media_type)['display_template'],
>                 {'media': self.media,
>                  'comments': comments,
>                  'pagination': pagination
>                  ...}
>
>
>     class MediaEntryMixin(object):
>         @property
>         def media(self):
>             if self._media:
>                 return self._media
>             else:
>                 self._media = self.request.db.MediaEntry.find_one(
>                     ...)
>     
>
>     class View(object):
>         def __init__(self, request):
>             self.request = request
>
>         def __call__(self, request):
>             self.setup()
>
>             if wants_json:
>                 return self.as_json()
>             else:
>                 return self.as_html()
> _______________________________________________
> devel mailing list
> address@hidden
> http://lists.mediagoblin.org/listinfo/devel


reply via email to

[Prev in Thread] Current Thread [Next in Thread]