How can I manage arguments of view_config automatically?

29 views
Skip to first unread message

Cosmia Luna

unread,
Feb 1, 2012, 11:11:00 PM2/1/12
to pylons...@googlegroups.com
I'm a beginner in both python and pyramid, sorry if my question is silly.
I'm using class-based view-callable in pyramid.

In package.views.someview, I usually write:

# -*- coding: utf-8 -*-
from pyramid.view import view_config
from pyramid.response import Response
from cgi import escape

class SomeView(object):
    def __init__(self, req):
        self.req=req
        # and something other to for things repeatly
    
    def __call__(self):
        # Usually not used
        return Response("This is the page from __call__ of %s" % escape(repr(self)))
    
    @view_config('add', context='package.views.someview.SomeView')
    def add(self):
        return Response("This is the page from add of %s" % escape(repr(self)))
    
    @view_config('edit', context='package.views.someview.SomeView')
    def edit(self):
        return Response("This is the page from edit of %s" % escape(repr(self)))

    @view_config('view', context='package.views.someview.SomeView')
    def view(self):
        return Response("This is the page from view of %s" % escape(repr(self)))

    @view_config('delete', context='package.views.someview.SomeView')
    def delete(self):
        return Response("This is the page from delete of %s" % escape(repr(self)))
        
This is really boring and makes no sense to specify the name and the context of the view every time, how can I do that automatically? I have to specify name because I use traversal approach to map URLs, and I have to specify the context because of view name conflict.

Thanks

Cosmia

Chris McDonough

unread,
Feb 2, 2012, 2:40:32 AM2/2/12
to pylons...@googlegroups.com

Cosmia Luna

unread,
Feb 2, 2012, 9:03:24 AM2/2/12
to pylons...@googlegroups.com
Since the 1.3-branch is in alpha status now, I have little confidence using it.
I tried simply copying the code from 1.3a6 but it failed to work.
It seems that I have to wait before 1.3-branch is stable.

Thanks 

Cosmia

Simon King

unread,
Feb 2, 2012, 9:14:02 AM2/2/12
to pylons...@googlegroups.com

I suspect that the quality of the 1.3 branch is actually very good,
but if you'd rather stick to 1.2, you could do something like this:

def my_view(name, **kwargs):
kwargs.setdefault('context', 'package.views.someview.SomeView')
return view_config(name, **kwargs)

And then use @my_view as a decorator instead of @view_config

Hope that helps,

Simon

Cosmia Luna

unread,
Feb 2, 2012, 10:05:08 AM2/2/12
to pylons...@googlegroups.com
How silly I was! I don't know why I couldn't get such a approach.

Thanks a lot, I forgot I could wrap a function or class...

I tested pyramid 1.3-branch and found it worked perfectly for me. But I do believe that Pyramid team have some reasonable reason to keep pyramid 1.3-branch alpha. Since I'm not an expert and am a beginner, I choose to accept advise from export and experienced. And I read the release notes before and found Pyramid team published the stable version of a branch in less than a month since first preview version available for download, I think I should not in hurry.

Thanks again to both King and McDonough

Cosmia

Chris McDonough

unread,
Feb 2, 2012, 10:17:03 AM2/2/12
to pylons...@googlegroups.com

It's up to you, but the alpha designator on the 1.3 branch isn't a
quality metric. We're likely to release a beta within a couple of
weeks, and no major features remain to be implemented.

- C


>
>
> Thanks
>
>
> Cosmia
>
> --
> You received this message because you are subscribed to the Google
> Groups "pylons-devel" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/pylons-devel/-/iVDSRdDIBG8J.
> To post to this group, send email to pylons...@googlegroups.com.
> To unsubscribe from this group, send email to pylons-devel
> +unsub...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/pylons-devel?hl=en.


Reply all
Reply to author
Forward
0 new messages