Newer
Older
Digital_Repository / Old / pysocks / editor.py
nstanger on 12 Nov 2005 4 KB - Second attempt at importing!
  1. #! /usr/bin/python2.2
  2.  
  3. # a simple page editor for the pysocks system
  4.  
  5.  
  6. #------------------------------------------------------------------------
  7. BASE_DIR = '/home/crypt/Work/pysocks/'
  8. TEMPLATE_DIR = BASE_DIR + 'Templates/'
  9.  
  10.  
  11. import cgi
  12. import access
  13. import os
  14. import constants
  15. from utils import *
  16.  
  17. #------------------------------------------------------------------------
  18.  
  19. class Edit:
  20. def __init__(self, db, page_info=None, extra_info=None):
  21. print extra_info
  22. self.__action = extra_info['action']
  23. self.__extra_info = extra_info
  24. if extra_info.has_key('page_id'):
  25. self.__page_id = extra_info['page_id']
  26.  
  27. self.__jump_table = {'show':self.show, 'list':self.list,
  28. 'create_and_edit':self.create_and_edit,
  29. 'edit':self.edit, 'new':self.new, 'save':self.save}
  30.  
  31.  
  32. def do(self):
  33. ' perform the right action for this page '
  34. print self.__action
  35. print self.__jump_table.keys()
  36. if self.__action in self.__jump_table.keys():
  37. return self.__jump_table[self.__action]()
  38.  
  39. def show(self):
  40. header = 'this is the header'
  41. page_template = get_file_as_string(TEMPLATE_DIR+'default_template')
  42. page_side_bar = get_file_as_string(TEMPLATE_DIR+'default_sidebar')
  43. page_content = get_file_as_string(TEMPLATE_DIR+'edit_screen')
  44. footer = 'end of page'
  45.  
  46. return page_template%(header, page_side_bar, page_content, footer)
  47.  
  48. def new(self):
  49. '''\
  50. create a new page entry in the db and then display a blank
  51. page for people to put stuff in'''
  52.  
  53. # create a new entry in the db after checking that there isn't
  54. # already an item of that name existing.
  55. #
  56. # use some of the code from the table editor class
  57.  
  58.  
  59. # db stuff goes here.
  60. header = 'new page'
  61. page_template = get_file_as_string(TEMPLATE_DIR+'default_template')
  62. page_side_bar = get_file_as_string(TEMPLATE_DIR+'default_sidebar')
  63. page_content = get_file_as_string(TEMPLATE_DIR+'new_screen')
  64. footer = 'end of page'
  65.  
  66. return page_template%(header, page_side_bar, page_content, footer)
  67. def create_and_edit(self):
  68. print 'create and edit method'
  69.  
  70. def edit(self):
  71. print 'edit method'
  72. def save(self):
  73. print 'save method'
  74. # what tables do we need to update here.
  75. #self.__db.set('')
  76.  
  77. def list(self):
  78. ' provide a list of pages that can be editied. '
  79.  
  80. list_template_line = '<a href='+\
  81. '"./editor.cgi?page=edit&action=show&page_id=%(page_id)s">'+\
  82. '%(description)s</a><br>'
  83. header = 'new page'
  84. page_template = get_file_as_string(TEMPLATE_DIR+'default_template')
  85. page_side_bar = get_file_as_string(TEMPLATE_DIR+'default_sidebar')
  86.  
  87. # query the db to locate all top level pages and then build a table
  88.  
  89. page_content = []
  90.  
  91. #pages = self.db.get_dict(....)
  92. # debug stuff
  93. pages = [ {'page_id':1, 'description':'first page'},
  94. {'page_id':2, 'description':'second page'}]
  95. for i in pages:
  96. page_content.append(list_template_line%i)
  97. footer = 'end of page'
  98.  
  99. return page_template%(header, page_side_bar,
  100. '\n'.join(page_content), footer)
  101.  
  102.  
  103. #------------------------------------------------------------------------
  104.  
  105.  
  106. page_types = {'edit':Edit}
  107.  
  108. def get_default_page():
  109. header = 'this is the header'
  110. page_template = get_file_as_string(TEMPLATE_DIR+'default_template')
  111. page_side_bar = get_file_as_string(TEMPLATE_DIR+'default_sidebar')
  112. page_content = get_file_as_string(TEMPLATE_DIR+'default_content')
  113. footer = 'end of page'
  114.  
  115. return page_template%(header, page_side_bar, page_content, footer)
  116. def get_extra_info(form):
  117. data = {}
  118. for i in form.keys():
  119. data[i] = form[i].value
  120.  
  121. return data
  122. def main():
  123. print 'Content-type: text/html'
  124. print
  125.  
  126. form = cgi.FieldStorage()
  127.  
  128. #db = access.Access(dbname='pysocks' user='crypt')
  129.  
  130. db = page_info = extra_info = None
  131.  
  132. extra_info = get_extra_info(form)
  133.  
  134. print extra_info
  135.  
  136. if form.has_key('page'):
  137. if form['page'].value in page_types.keys():
  138. page = page_types[form['page'].value](db, page_info, extra_info)
  139. ret = page.do()
  140. else:
  141. ret = get_default_page()
  142.  
  143. print ret
  144. pass
  145.  
  146.  
  147. if __name__ == '__main__':
  148. main()