[albatross-users] Re: Albatross-users digest, Vol 1 #186 - 12 msgs

Cameron Blackwood korg at darkqueen.org
Sat Jul 5 11:57:23 EST 2003


From: dougal.scott at member.sage-au.org.au:
  | Date: Fri, 04 Jul 2003 16:50:11 +1000
  | Subject: [albatross-users] Newbie question
  | 
  | 
  | In order to better learn Albatross I'm converting an existing cgi. This
  | is a fairly simple cgi which gets some info from the user and then
  | calls itself a few times with different options to generate some
  | embedded images.
  | 
  | I'm having problems converting this to Albatross. How do I repeatedly
  | pass a different context to another module.
  | 
  | I can do it the bad way by generating an expression that is an img tag
  | such as  "<img src=.../genimg?imgtype=1&var1=foo&var2-bar>". This
  | involves going through the locals and generating the arguments from
  | that - and adding some other ones, in this case imgtype.
  | 
  | However this is a hack and I would prefer to do it neatly. Also it
  | means that the context is accepted from the URL, where I would prefer
  | it not to be, so people can't play with the options.
  | 
  | This is where either Albatross or my limited understanding of Albatross
  | breaks down. Albatross seems to be based around the concept of a page -
  | and one invocation of a module per page. What I need is multiple
  | modules being invoked to generate a single page.
  | 
  | Can anyone point me in the right direction?



I uses something like this [below].... (it may need hacking to get going, but 
you should be able modify the dict in application to get it starting..

(Oh and modify the urls that are coded into the template).

It uses the store_server_side_file (SimpleSessionFileApp) to store 
context, and uses a URL hack to flag image requests.

The function:

      data=image_generator.map_plot()

returns a png. You could pass ctx.locals.whatever to it if you need
to or do the processing in the page_display() etc.

Often I subclass all my albatross pages
from another class so that image checking is less work.

This works for me :). 


PLEASE tell me if Im doing something wrong or abusive of albatross ;)


I think it is fine because the template page loads first (and processes 
any ctx/form changes and then it requests the image, so the image page 
has the latest context as generated from the last template page). To 
be really save, I guess you should skip any processing in page_process() 
if it is an image.

Actually, Ive been meaning to ask, is there a way to flush data out of
a request and NOT store/process the request? Ie, if it is an image,
then just write the data and dont do anything with the context (like
save or modify it?).


  | Dougal Scott                    Connect Internet Solutions
cam

--
 / `Rev Dr'   cam  at darkqueen.org            Roleplaying, virtual goth \
<   http://darkqueen.org        Poly, *nix, Python, C/C++, genetics, ATM  >
 \  [+61 3] 9809 1523[h]         skeptic, Evil GM(tm). Sysadmin for hire /
                      ---------- Random Quote ----------
Don't go to bed with no price on your head.
		-- Baretta



------------------- 8< cut -----------------------
#!/usr/bin/python

import os,string,re
import image_gen

from albatross        import SimpleSessionFileApp  as Albatross_Application
from albatross        import SessionFileAppContext as Albatross_Context
from albatross.cgiapp import Request               as Albatross_Request

# I used to use this on the url before the click data context
# COORDS=re.compile("(?P<x>[0-9][0-9]*),(?P<y>[0-9][0-9]*)")

image_generator=image_gen.load('test')


PageList={}

def addpage(p):
  PageList[ p.__class__.__name__ ]=p
def DEBUG(msg):
  open("/tmp/debug",'a').write('%s\n'%(msg))



class Start:
  def page_enter(self, ctx):
    DEBUG("page entered!!")
    ctx.locals.username=None
    ctx.locals.passwd=None
    ctx.locals.square='morons'
    ctx.locals.pos=[0,0]
    ctx.locals.click=None
    ctx.locals.zoom=None
    ctx.add_session_vars('username','passwd','square','pos','click')

  def  page_process(s,ctx):
    DEBUG('b:%s'%(ctx.get_uri()))
    DEBUG('b:%s'%(ctx.base_url()))
    DEBUG('c:%s'%(ctx.current_url()))
    if ctx.locals.zoom:
      DEBUG("setting zoom = %s"%(ctx.locals.zoom))
      image_generator.set_zoom(ctx.locals.zoom)
    if ctx.locals.click:
      ctx.locals.pos=[ ctx.locals.pos[0]+ctx.locals.click.x,
                       ctx.locals.pos[1]+ctx.locals.click.y ]
      ctx.locals.click=None
    if ctx.locals.square=='tree':
      DEBUG('tree!!!')
    DEBUG('%s'%(ctx.locals))

  def page_display(self, ctx):
    page=os.environ['REQUEST_URI'][len(os.environ['SCRIPT_NAME'])+1:]
    DEBUG("page is %s"%(page))
    if page=='map_image':
      data=image_generator.map_plot()
      ctx.request.write_header('Content-Type','image/png') 
      ctx.request.write_header('Content-Length',str(len(data)))
      ctx.request.write_header('Pragma', 'no-cache')
      DEBUG("image squre %s"%(ctx.locals.square))
      ctx.write_content(data)
    else:
      DEBUG("page squre %s"%(ctx.locals.square))
      ctx.run_template('template_map.html')

#class Image:

addpage(Start())
#addpage(Image())


#############################################################
# Albatross objects
#############################################################


class request(Albatross_Request):
  def __init__(s):
    Albatross_Request.__init__(s)

class context(Albatross_Context):
  def __init__(s,app):
    Albatross_Context.__init__(s,app)


class application(Albatross_Application):
  def __init__(s):
    dict= {  'base_url':'20circle',
             'template_path':'/home/korg/projects/20circle/data/web_template/',
             'start_page':'Start',
             'secret':'-=-secret-=-',
             'session_appid':'dumb',          # server file stored
             'session_dir':'/tmp/session/',   # server file stored
             }
    Albatross_Application.__init__(s,**dict)
    if PageList:
      for n,v in PageList.items(): s.register_page( n, v )

  def create_context(s):
      ctx=context(s)
      ctx.load_template('macros.html').to_html(ctx)
      return ctx


def main():
  a = application()
  a.run(request())

if __name__=="__main__":  main()



------------------- 8< cut -----------------------

<html>
<head>
  <title>Map screen</title>
</head>

<body>


<h1>Welcome</h1>
<al-form method="post">

<hr>
<h2>Games Details:</h2>

pos: <al-value expr="username"><br>
pos: <al-value expr="passwd"><br>
pos: <al-value expr="square"><br>
pos: <al-value expr="pos"><br>

<!-- albatross 1.01 used this and url regexes to get coords -->
<!--a href="http://10.0.0.254/cgi-bin/20circle"-->
<!--al-img ismap expr="'http://10.0.0.254/cgi-bin/20circle/map_image'"-->
<!--/a-->

<!-- albatross 1.10pre1 uses this and click data type-->
<al-input type="image" name="click" src="http://10.0.0.254/cgi-bin/20circle/map_image" whitespace>

<al-input type="submit" name="square" value="square">
<al-input type="submit" name="square" value="circle">
<al-input type="submit" name="square" value="tree">

<al-input type="submit" name="zoom" value="3.">
<al-input type="submit" name="zoom" value="2.">
<al-input type="submit" name="zoom" value="1.">
<al-input type="submit" name="zoom" value=".5">

<hr>

</al-form>

<hr>

<al-expand name="copyright"/>

</body>
</html>






More information about the Albatross-users mailing list