[Top][All Lists]

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

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

From: Joar Wandborg
Subject: [GMG-Devel] [proposal] Class-based views
Date: Mon, 23 Apr 2012 21:36:01 +0200
User-agent: Mutt/1.5.21 (2010-09-15)

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):
        def as_json(self):
            import simplejson

        def as_html(self):
            return render_to_response(
                 'comments': comments,
                 'pagination': pagination

    class MediaEntryMixin(object):
        def media(self):
            if self._media:
                return self._media
                self._media = self.request.db.MediaEntry.find_one(

    class View(object):
        def __init__(self, request):
            self.request = request

        def __call__(self, request):

            if wants_json:
                return self.as_json()
                return self.as_html()

reply via email to

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