is it safe to store data on handlers/views in "self", or should it be "self.request" ?

46 views
Skip to first unread message

Jonathan Vanasco

unread,
Feb 3, 2012, 1:32:14 PM2/3/12
to pylons-discuss
under pylons I had a lot of code that did misc prep work for certain
methods in the request.

because every view was an instance, it was pretty safe to just store
stuff in the instance of the 'handler' class, and i didn't need to
store it into the request object.

i'm not sure how pyramid is set up. it seems that every view creates
a new instance of the handler class as well -- but i wanted to be
sure.

the paradigms i used sort of look like the following, where i have a
'setup' helper and a multi-method form submission process:

---

def _shared_setup_a():
"used by one or more methods , but not all"
self.db = newdb()
self.gaq= new_google_analaytics()

def login(self):
self._shared_setup_a()
self._login_form= Form()
if submit:
return self._login_submit()
return self._login_print()

def _login_print(self):
render( template, self._login_form )

def _login_validate(self):
if not validate(self._login_form, self.request):
return self._login_print()
redirect( success page )

Mike Orr

unread,
Feb 3, 2012, 3:08:59 PM2/3/12
to pylons-...@googlegroups.com
On Fri, Feb 3, 2012 at 10:32 AM, Jonathan Vanasco <jona...@findmeon.com> wrote:
> under pylons I had a lot of code that did misc prep work for certain
> methods in the request.
>
> because every view was an instance, it was pretty safe to just store
> stuff in the instance of the 'handler' class, and i didn't need to
> store it into the request object.
>
> i'm not sure how pyramid is set up.  it seems that every view creates
> a new instance of the handler class as well -- but i wanted to be
> sure.

Chris has recommended storing stuff in the view, and the
Pyramid/Pylons guide I'm working on will document it as an alternative
to t'mpl_context'.

===
class MyViewClass(object):
def __init__(self, request):
self.request = request
self.variable_for_site_template = "value"
# The above variable is used only in HTML rendering; it's not
part of the view's return dict for JSON, etc.

@view_config(route_name="...", renderer="...")
def index(self):
return {"project": "Hello"}

# Site template
<head>
<meta name="keywords" content="${view.variable_for_site_template}" />
...

# Index template
<p>Welcome to ${project}.</p>
</head>
===


> def _shared_setup_a():
>    "used by one or more methods , but not all"
>    self.db = newdb()
>    self.gaq= new_google_analaytics()

This looks reasonable. It's essentially what I do in Pylons, and what
I'd do in Pyramid. I like this approach for processing common input
such as record IDs: the method validates the parameter type (numeric),
fetches the database record, and attaches it to 'self', or aborts the
request if the record doesn't exist.

--
Mike Orr <slugg...@gmail.com>

Jonathan Vanasco

unread,
Feb 3, 2012, 3:12:00 PM2/3/12
to pylons-discuss
great. that looks good.

I need to read up more on views vs handlers.

since i use "render_to_response" for most things, I should have an
easy transition

Chris McDonough

unread,
Feb 3, 2012, 3:18:09 PM2/3/12
to pylons-...@googlegroups.com

Handlers are views too. So this works there as well, if you'd rather
continue using those.

- C


Mike Orr

unread,
Feb 3, 2012, 5:25:10 PM2/3/12
to pylons-...@googlegroups.com

Chris, Blaise, something to think about for the documentation:

People are getting the idea that handlers are some big separate thing;
i.e., more than they are. I think this is due to how pyramid_handlers
was originally marketed. Part of it I think was an expectation that
ex-Pylons developers wouldn't accept the standard Pyramid API, whereas
actual experience has shown that they're more accepting of it than we
thought.

So in my Pyramid/Pylons guide (and in the Akhet manual) I've tried to
emphasize that a handler is just another name for a view class (i.e.,
a class containing view methods). What pyramid_handlers really gives
you is config.add_handler() and @action. It doesn't give you a base
class: that's an Akhet invention (to parallel Pylons' Base
controller). So I think that where the docs talk about view classes,
they should mention that handler is a common term for a view class,
but it means nothing more than that, and that pyramid_handlers is just
one way of managing them.

--
Mike Orr <slugg...@gmail.com>

Michael Merickel

unread,
Feb 3, 2012, 5:27:46 PM2/3/12
to pylons-...@googlegroups.com
"class-based view" is the appropriate term here. I think that "handler" should only really be used in the context of pyramid_handlers.


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


Parnell Springmeyer

unread,
Feb 3, 2012, 9:52:45 PM2/3/12
to pylons-...@googlegroups.com
I set up my views using classes (the actual views are methods) that
extend a base class. In the base class I set up globally accessed
variables to self so that all classes extending that base class have
access to them - it's more or less for convenience.

I also use the Pylon-esque context object pattern to make a globally
accessible "c.attrib" object available in my templates.

I also use this, mostly for the session's user object which I cache into
redis.

Mike Orr <slugg...@gmail.com> writes:

--
Parnell "ixmatus" Springmeyer (http://ixmat.us)

Chris McDonough

unread,
Feb 3, 2012, 10:15:35 PM2/3/12
to pylons-...@googlegroups.com

I'd hope it'd be enough to put somewhere in the handlers docs that
handlers are just special class-based views. I'm not apt to introduce
the term "handlers" into the main Pyramid docs to describe class-based
views, because then we don't have a name for the bundle of logic that we
currently call handlers.

- C


>
> --
> Mike Orr <slugg...@gmail.com>
>


Jonathan Vanasco

unread,
Feb 3, 2012, 10:47:10 PM2/3/12
to pylons-discuss
could we rename pyramid_handlers to pyramid_controllers ? its the
pylons appropriate name for them, and would probably solve a handful
of issues.

Chris McDonough

unread,
Feb 3, 2012, 10:53:51 PM2/3/12
to pylons-...@googlegroups.com

Ben explicitly didn't want them to be called controllers for fear of
people getting them confused with Pylons controllers, which work
slightly differently, FWIW.

- C

Blaise Laflamme

unread,
Feb 3, 2012, 11:03:45 PM2/3/12
to pylons-...@googlegroups.com
same opinion than Chris

Mike Orr

unread,
Feb 3, 2012, 11:12:19 PM2/3/12
to pylons-...@googlegroups.com
On Fri, Feb 3, 2012 at 8:03 PM, Blaise Laflamme <bla...@laflamme.org> wrote:
> same opinion than Chris

Renaming pyramid_handlers would raise backward compatibility issues,
and there's already a proliferation of things being renamed and
retired.

I'm just suggesting a sentence in passing, "Class-based views are
sometimes called 'handlers'", so that users know what that term means
and won't misunderstand it.

--
Mike Orr <slugg...@gmail.com>

Iain Duncan

unread,
Feb 6, 2012, 12:58:33 PM2/6/12
to pylons-...@googlegroups.com
Re the original question, storing state on your view as a class is fine, because the class is always instantiated and called once per request, with no shared state ( unless you explicitly make it shared through class attributes ). This has the very happy side effect of making testing a lot easier because if you store the intermediate stages of your view logic as attributes on the view object, you can interrogate the view itself at the end of your tests to make sure it did the right thing.

Iain

--
You received this message because you are subscribed to the Google Groups "pylons-discuss" group.

Jonathan Vanasco

unread,
Mar 27, 2012, 10:42:32 AM3/27/12
to pylons-discuss
FWIW, I started working on this approach a few weeks ago, and have
been loving it.

I attach two Pylons-style Attribute-Safe objects onto the request at
init :
request.app_meta
request.workspace

app_meta contains stuff like "is the user logged in?" , timestamp /
datetimes for the request, links to packages that store 'constants',
etc. All that is generated on init.

workspace is where i progressively store request specific data that
the application logic works on, like SqlAlchemy objects or cached
dicts. unlike storing this onto the view class, it becomes available
anywhere i have a request object.

I found this works ridiculously well.
Reply all
Reply to author
Forward
0 new messages