When the script begins with main(f), it gets a KeyError and goes to
the login page, but when the form data is submitted it returns a 404.
Am I not setting/getting the cookie properly? Absolutely nothing is
printed until zptIO is called.

import os
import Cookie
from spanishlabs.conf import *
from spanishlabs.zpt import *
from spanishlabs.sql import *

def main(f):
   try:
      c = os.environ["HTTP_COOKIE"]
   except KeyError:
      login(f, fail=False)
   else:
      fetchCookie(f)

def login(f, fail):
   if f:
      authenticate(f)
   zpt = 'login.zpt'
   context = {'here':Render(fail)}
   zptIO(zpt, context)

def authenticate(f):
   if f.has_key('author') and f.has_key('password'):
      author = f.['author'].value
      password = f.['password'].value
      q = Users.select(AND(Users.q.author==author,
                           Users.q.password==password))
      if len(list(q))>0:
         setCookie(f, author)
   else:
      login(f, fail=True)

def chapters(f, author):
   if f.has_key('event') and f.has_key('chapter'):
      event = f['event'].value
      chapter = f['chapter'].value
      q = Chapters.select(Chapters.q.chapter==chapter)

      if event == 'create' and len(list(q))>0:
         exists = True
      else:
         create = chapter

      if event == 'edit' and len(list(q))>0:
         edit = chapter

   zpt = 'chapters.zpt'
   context = {'here':Render(exists=None, create=None, edit=None)}
   zptIO(zpt, context)

def setCookie(f, author):
   c1 = Cookie.SimpleCookie()
   c1['author'] = author
   c1['author']['max-age'] = 7200
   c1['author']['expires'] = 7200
   c1['author']['version'] = 1
   print c1
   chapters(f, author)

def fetchCookie(f):
   c2 = Cookie.SimpleCookie()
   c2.load(os.environ["HTTP_COOKIE"])
   author = c2['author'].value
   chapters(f, author)

def zptIO(zpt, context):
   pt = PageTemplate()
   template = open(os.path.join(templates, zpt))
   pt.write(template.read())
   template.close()
   print 'Content-type: text/html\n\n'
   print pt(context=context)

A previous message I sent to the list about **kwds mostly makes sense
to me now. The missing dot was a typo.
_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to