Newer
Older
Digital_Repository / Old / pysocks / editor.py
nstanger on 12 Nov 2005 4 KB - Second attempt at importing!
#! /usr/bin/python2.2

# a simple page editor for the pysocks system


#------------------------------------------------------------------------
BASE_DIR = '/home/crypt/Work/pysocks/'
TEMPLATE_DIR = BASE_DIR + 'Templates/'


import cgi
import access
import os
import constants
from utils import *

#------------------------------------------------------------------------

class Edit:
    
    def __init__(self, db, page_info=None, extra_info=None):
        print extra_info
        self.__action = extra_info['action'] 
        self.__extra_info = extra_info
        if extra_info.has_key('page_id'):
            self.__page_id = extra_info['page_id']

        self.__jump_table = {'show':self.show, 'list':self.list,
            'create_and_edit':self.create_and_edit,
            'edit':self.edit, 'new':self.new, 'save':self.save}


    def do(self):
        ' perform the right action for this page '
        print self.__action
        print self.__jump_table.keys()
        if self.__action in self.__jump_table.keys():
            return self.__jump_table[self.__action]()

    def show(self):
        header = 'this is the header'
        page_template = get_file_as_string(TEMPLATE_DIR+'default_template')
        page_side_bar = get_file_as_string(TEMPLATE_DIR+'default_sidebar')
        page_content =  get_file_as_string(TEMPLATE_DIR+'edit_screen')
        footer = 'end of page'

        return page_template%(header, page_side_bar, page_content, footer)

    def new(self):
        '''\
        create a new page entry in the db and then display a blank
        page for people to put stuff in'''

        # create a new entry in the db after checking that there isn't
        # already an item of that name existing.
        #
        # use some of the code from the table editor class


        # db stuff goes here.
        header = 'new page'
        page_template = get_file_as_string(TEMPLATE_DIR+'default_template')
        page_side_bar = get_file_as_string(TEMPLATE_DIR+'default_sidebar')
        page_content =  get_file_as_string(TEMPLATE_DIR+'new_screen')
        footer = 'end of page'

        return page_template%(header, page_side_bar, page_content, footer)
        
    def create_and_edit(self):
        print 'create and edit method'

    def edit(self):
        print 'edit method'
        
        
    def save(self):
        print 'save method'
        # what tables do we need to update here.
        #self.__db.set('')

    def list(self):
        ' provide a list of pages that can be editied. '

        list_template_line = '<a href='+\
            '"./editor.cgi?page=edit&action=show&page_id=%(page_id)s">'+\
            '%(description)s</a><br>'
        header = 'new page'
        page_template = get_file_as_string(TEMPLATE_DIR+'default_template')
        page_side_bar = get_file_as_string(TEMPLATE_DIR+'default_sidebar')

        # query the db to locate all top level pages and then build a table

        page_content =  []

        #pages = self.db.get_dict(....)
        
        # debug stuff
        pages = [ {'page_id':1, 'description':'first page'},
            {'page_id':2, 'description':'second page'}]
        for i in pages:
            page_content.append(list_template_line%i)
        footer = 'end of page'

        return page_template%(header, page_side_bar, 
            '\n'.join(page_content), footer)
        


#------------------------------------------------------------------------


page_types = {'edit':Edit}

def get_default_page():
    header = 'this is the header'
    page_template = get_file_as_string(TEMPLATE_DIR+'default_template')
    page_side_bar = get_file_as_string(TEMPLATE_DIR+'default_sidebar')
    page_content =  get_file_as_string(TEMPLATE_DIR+'default_content')
    footer = 'end of page'

    return page_template%(header, page_side_bar, page_content, footer)
    
def get_extra_info(form):
    data = {}
    for i in form.keys():
        data[i] = form[i].value

    return data
    
def main():
    print 'Content-type: text/html'
    print 

    form = cgi.FieldStorage()

    #db = access.Access(dbname='pysocks' user='crypt')

    db = page_info = extra_info = None

    extra_info = get_extra_info(form)

    print extra_info

    if form.has_key('page'):
        if form['page'].value  in page_types.keys():
            page = page_types[form['page'].value](db, page_info, extra_info)
            ret = page.do()
    else:
        ret = get_default_page()

    print ret
    pass


if __name__ == '__main__':
	main()