durusmail: quixote-users: cookies
cookies
2002-05-28
2002-05-28
2002-05-28
2002-05-28
2002-05-29
2002-05-29
2002-05-29
2002-05-29
2002-05-29
cookies
Greg Ward
2002-05-28
On 28 May 2002, Patrick K. O'Brien said:
> I'm having trouble figuring out how to work with cookies in Quixote. I'm
> sure I'm just overlooking something minor, but it's starting to bug me. If
> anyone has any simple examples of setting and getting cookies that they
> wouldn't mind sharing it would really help.

What code do you have now, and precisely how does it not work?

> What I'm trying to do is allow the user to select a style sheet and save the
> selection via a cookie.(I'm saving a simple string that works as the key to
> a dictionary of style sheet links, actually.) Then the function that builds
> my pages will get the user's style from the cookie, or default to the
> default style. I'll be happy to share this technique once I get it working.

Assuming the user selects a style sheet by submitting out a form with a
'style_sheet' variable:

  def process_style_sheet_form (request):
      ss = request.form.get('style_sheet')
      if not is_valid_ss(ss):
          raise QueryError("invalid style sheet selection")
      response.set_cookie("style_sheet", ss)

Then, later on when you expect the user to be carrying around a
"style_sheet" cookie:

  def show_some_page (request):
      ss = request.cookies.get("style_sheet", "default")
      if not is_valid_ss(ss):
          ss = "default"
          # or maybe, if you're paranoid/picky:
          raise PublishError("invalid style_sheet cookie: %r" % ss)
          # ...but I'm not sure if PublishError is the right
          # exception.  Probably defining your own exception class
          # (derived from PublishError) is more appropriate.

      # ...generate page using the style sheet denoted by 'ss'...

Of course, you really should be using Quixote's built-in session
management mechanism for this kind of thing.  Good thing it's going to
be documented in the next release!  ;-)  Here's how I'd do the above
with session management:

  class MySession (Session):
      def __init__ (self, request, id):
          Session.__init__(self, request, id)
          self.style_sheet = None

      def set_style_sheet (self, ss):
          if not is_valid_ss(ss):
              # Alternately, this could just raise QueryError directly --
              # but that assumes that set_style_sheet() is only called as
              # a result of processing a web form.
              raise ValueError("invalid style sheet selection")

  [...]

  def process_style_sheet_form (request):
      ss = request.form.get('style_sheet')
      try:
          request.session.set_style_sheet(ss)
      except ValueError, err:
          raise QueryError(err)

  def show_some_page (request):
      ss = request.session.style_sheet
      # ...generate page using the style sheet denoted by 'ss'...

Note that you no longer have to sanity-check a client-supplied cookie --
that's done for you by Quixote.  You just need to ensure that MySession
objects always have a sane value for their 'style_sheet' attribute, so
you only have to sanity-check the value once, on the way in.

        Greg
--
Greg Ward - software developer                gward@mems-exchange.org
MEMS Exchange                            http://www.mems-exchange.org


reply