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
Best way to add dynamic default argument to view_config
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
  5 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
 
Jason  
View profile  
 More options Aug 28 2012, 5:07 pm
From: Jason <ja...@deadtreepages.com>
Date: Tue, 28 Aug 2012 14:07:22 -0700 (PDT)
Local: Tues, Aug 28 2012 5:07 pm
Subject: Best way to add dynamic default argument to view_config

Hello,

I'm trying to move on from pyramid_handlers to using config.add_route and
view_config for increased configuration options (and I think it will be
better organized), but there is one throwback I want to keep. Almost all of
my view-callables will need the view_config option "matchparam={'action':
<function_name>}" where function_name is the name of the view-callable def.
What would be the best way to achieve this?

I think I could subclass view_config and override the __call__ method to
add it as a parameter as seen below, but is this advisable?

class action_config(view_config):
    def __call__(self, wrapped):
        self.matchparam = {'action': wrapped.__name__}
        return view_config.__call__(self, wrapped)

Thanks for feedback,

Jason


 
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.
John Anderson  
View profile  
 More options Aug 28 2012, 7:31 pm
From: John Anderson <son...@gmail.com>
Date: Tue, 28 Aug 2012 18:30:56 -0500
Local: Tues, Aug 28 2012 7:30 pm
Subject: Re: Best way to add dynamic default argument to view_config
I think what you are looking for his view_defaults:

http://docs.pylonsproject.org/projects/pyramid/en/1.3-branch/narr/vie...
http://docs.pylonsproject.org/projects/pyramid/en/1.3-branch/api/view...


 
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.
Jason  
View profile  
 More options Aug 29 2012, 9:21 am
From: Jason <ja...@deadtreepages.com>
Date: Wed, 29 Aug 2012 06:21:21 -0700 (PDT)
Local: Wed, Aug 29 2012 9:21 am
Subject: Re: Best way to add dynamic default argument to view_config

On Tuesday, August 28, 2012 7:31:01 PM UTC-4, Sontek wrote:

> I think what you are looking for his view_defaults:

> http://docs.pylonsproject.org/projects/pyramid/en/1.3-branch/narr/vie...

> http://docs.pylonsproject.org/projects/pyramid/en/1.3-branch/api/view...

> Unfortunately the "function_name" part of the predicate will be different

for every view-callable, so view_defaults won't work -- unless a custom
predicate could inspect the given request to see the current view-callable
def name?

--
Jason


 
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.
Chris Lambacher  
View profile  
 More options Aug 29 2012, 11:32 am
From: Chris Lambacher <ch...@kateandchris.net>
Date: Wed, 29 Aug 2012 11:31:59 -0400
Local: Wed, Aug 29 2012 11:31 am
Subject: Re: Best way to add dynamic default argument to view_config

Hi,

I converted away from handlers to using the match_param view_defaults and
view_config with the match_param option. I found that often enough I wanted
to override the method name as the action that is was no big deal to always
use match_param (since I was very frequently using the name option of the
action decorator). I have a semi fleshed out example below. If you *really*
want to default to the name of the method, then you'll need to write your
own decorator that then calls view_config as you show below. Note that your
example you use matchparam instead of match_param.

config.add_route('someroute_index', 'someroute')
config.add_route('someroute', 'someroute/{action}')

@view_defaults(route_name='someroute')
class SomeView(object):
    def __init__(self, request):
          self.request = request

    @view_config(route_name='someroute_index',
renderer='index_renderer.mak')
    def index(self):
        ....

    @view_config(match_param='action=edit', request_method='POST',
renderer='edit_renderer.mak')
     def save(self):
         ....

    @view_config(match_param='action=edit', renderer='edit_renderer.mak')
     def edit(self):
         ...

     @view_config(match_param='action=jsonaction', renderer='json')
     def json_action(self):
         ...

I also played around with setting up action to allow empty string instead
of using the someaction_index route, but that was pretty annoying in
request.route_path where you would need to do
request.route_path('someroute', action=''), it just ended up looking
cleaner to have a separate route for the "index" condition.

-Chris

--
Christopher Lambacher
ch...@kateandchris.net

 
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.
Jason  
View profile  
 More options Aug 29 2012, 1:39 pm
From: Jason <ja...@deadtreepages.com>
Date: Wed, 29 Aug 2012 10:39:37 -0700 (PDT)
Local: Wed, Aug 29 2012 1:39 pm
Subject: Re: Best way to add dynamic default argument to view_config

from pyramid.view import view_config

class *action_config*(view_config):

    *""" Extends Pyramid.view.view_config to always use the wrapped
functions*

*        as a predicate match for the 'action' parameter in the **matchdict*
*.*

*        *

*        This requires the matching route to have {action} in its **
matchdict**.*

*    """*

    def *__call__*(*self*, wrapped):

        # if no name is given use the name of the function to match

        # against the 'action' matchdict parameter

        name = getattr(*self*, *'name'*, wrapped.__name__)

        try:    

            mp = *self*.match_param

        except AttributeError:

            mp = *'action={0}'*.format(name)

        else:

            if not isinstance(mp, tuple):

                mp = (mp,)

            mpdict = dict(paramstr.split(*'='*) for paramstr in mp)

            mpdict[*'action'*] = name

            mp = (*'{0}={1}'*.format(k, v) for k,v in mpdict.items())

        *self*.match_param = mp

        # This doesn't work, but copying the code into this method (as
below)

        # works fine???

        #return view_config.__call__(self, wrapped)

        settings = *self*.__dict__.copy()

        def *callback*(context, name, ob):

            config = context.config.with_package(info.module)

            config.add_view(view=ob, **settings)

        info = *self*.venusian.attach(wrapped, callback, category=*'pyramid'
*)

        if info.scope == *'class'*:

            # if the decorator was attached to a method in a class, or

            # otherwise executed at class scope, we need to set an

            # 'attr' into the settings if one isn't already in there

            if settings.get(*'**attr**'*) is None:

                settings[*'**attr**'*] = wrapped.__name__

        settings[*'_info'*] = info.codeinfo # fbo "action_method"
        return wrapped

Everything below my commented out return statement is exactly copied from
pyramid.view.view_config.__call__, but I don't know why calling it directly
doesn't work.

I'll be doing the same as you in my initial move from pyramid_handlers and
using view_defaults(route_name='bleh') on the handler classes.

--
Jason


 
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 »