Is it possible to inherit views from a view_default base calss?

96 views
Skip to first unread message

Tjelvar

unread,
Mar 28, 2013, 7:55:48 AM3/28/13
to pylons-...@googlegroups.com
Dear all,

Some of my resources will have identical views for some request methods (see below for an example). I would therefore like to be able to inherit these from a base class. However, the code below does not appear to work (e.g. POST returns 404 Not Found rather than 405 Method Not Allowed).

Any thoughts on how I could achieve type behaviour?

class _BaseView(object):

    @view_config(request_method='OPTIONS')
    def options(self):
        "Return OPTIONS response."
        return Response(allow=['HEAD', 'GET', 'OPTIONS'])

    @view_config()
    def other_request_methods(self):
        "Disallow other request methods."
        return HTTPMethodNotAllowed()

@view_defaults(route_name='real_view')
class RealView(_BaseView):

    def __init__(self, request):
        self.request = request

    @view_config(request_method='GET')
    def get(self):
        return Response('This is a view...')

Michael Merickel

unread,
Mar 28, 2013, 4:15:49 PM3/28/13
to Pylons
view_config does not work with inheritance. One option if you really want something like this is to write a class decorator with venusian that can automatically register certain views for you (this is similar to how cornice works).



--
You received this message because you are subscribed to the Google Groups "pylons-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pylons-discus...@googlegroups.com.
To post to this group, send email to pylons-...@googlegroups.com.
Visit this group at http://groups.google.com/group/pylons-discuss?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Tjelvar

unread,
Apr 1, 2013, 11:41:26 AM4/1/13
to pylons-...@googlegroups.com
Dear Michael,

Thank you for your reply. I have been reading up on decorators, venusian and cornice. However, I'm still struggling to put together something that works.

I think that what I'm failing to do is to identify the route that the view should be added to.

Does anyone have any idea why the code below does not work?

Tjelvar

...

def base_views():
    def wrapper(klass):

        def _options():

            "Return OPTIONS response."
            return Response(allow=['HEAD', 'GET', 'OPTIONS'])

        def callback(context, name, ob):
            config = context.config.with_package(info.module)
            config.add_view(_options, request_method='OPTIONS')

        info = venusian.attach(wrapper , callback, category='pyramid')

        return klass
    return wrapper

@view_defaults(route_name='root')
@base_views()
class Root(object):

    def __init__(self, request):
        self.request = request

#   @view_config(request_method='OPTIONS')
#   def options(self):
#       "Return OPTIONS response."
#       return Response(allow=['HEAD', 'GET', 'OPTIONS'])

...

Michael Merickel

unread,
Apr 1, 2013, 12:34:24 PM4/1/13
to Pylons
This isn't tested, but should basically handle what you've been asking for, I think.



Tjelvar

unread,
Apr 3, 2013, 3:58:19 AM4/3/13
to pylons-...@googlegroups.com
Michael, thank you for all your help. I will experiment with the solution you proposed over the next week or so.

All the best,

Tjelvar
Reply all
Reply to author
Forward
0 new messages