Page Source

from utils.display import Display, Modal, Arguments
from utils.display.treedisplay import TreeDisplay
from utils.controls import *
from utils.controls.forestcontrols import *
from utils.controls import doccontrols
from utils.output import *
from utils.web_exc import WebError
from utils import docs, perms, bounce
import debugging

########################################
# If you are copying this code to create
# another page, then read this
#
# You will surely need to change MyTreeDisplay.Prefix
# to reflect the root of the doc tree which your 
# page is supposed to handle
#
# If you dont like the existing format of the page, or 
# in any case want to change display different kind of
# info, then you will also need to change the
# defn of MyNodeControl. In particular all you will
# need to do is to change doccontrols.*
# and replace it or add more of the doccontrols.
# Look at utils/controls/doccontrols to see what
# choices you have. If what you want is not there
# consider making a new one and putting it there
# and just use it here.
#
# - Murali
############################################

class MyNodeControl(CaseNodeControl,
                    doccontrols.TextOnlyNodeControl,
                    doccontrols.TextULNodeControl):
  """This is the node control which masquerades as a
  TextULNodeControl at level 1, and as a TextOnlyNodeControl at level 2.
  All the masquerading code is in CaseNodeControl. You have to inherit
  from the other controls for typecorrectness."""

  DictClasses = {
    1 : doccontrols.TextULNodeControl,
    2 : doccontrols.TextOnlyNodeControl
    }

# This is the display object which gets the URL arguments
# processes them and calls all the other controls
class DocsTreeDisplay(TreeDisplay, Arguments, Modal):

  Prefix = "/docs"

  error_messages = {
    'bad_arg' : ("Bad Argument","Specified path does not exist"),
    'perm'    : ("Permission Denied","You dont have permission to view this page") 
  }
     
  def permit_mode(self,mode):
    return mode in ('display','pretty')
  
  def pretty(self):
    """Return if show in pretty mode (no links, not editable....)"""
    return self.get_mode() == 'pretty'
  
  def setup_links(self,page):
    self.add_modes_to_page(page)
    if self.args['doc_node_id'] and not self.pretty():
      page.add_navigation("%(part:pubdocs)s/listing%(!root)s" % self.req.urls(self.args),
                          "List View")
      page.add_navigation("%(part:pubdocs)s/show%(!root)s" % self.req.urls(self.args),
                          "Show doc")
      # Add doc Link
      page.add_navigation("%(part:pubdocs)s/add%(!root)s" % self.req.urls(self.args),
                          "Add doc")

  def make_page(self,page):
    self.process_arguments()
    # check if in right mode and has permissions
    if self.get_mode() not in ('display','pretty'):
      if self.permit_mode('display'):
        bounce.bounce(self.req, self.req.urls['mode:display'])
      else:
        raise WebError("perm{}")
    # Set up navigational links
    self.setup_links(page) 
    # Do we need edit links
    elink = not self.pretty()
    #Setup the controls
    self.header = doccontrols.LongTextRootNodeControl(nodepathfield="root",
                                                      editlink=elink)
    # Above needed to set the correct title
    self.single_controls = [ self.header,
                             # fetch the nodepath from display.fields['root']
                             "\n<br>\n",
                             ForestControl(treenodecontrol=MyNodeControl,
                                           PathField="node_path",
                                           where_conditions= "private = 0",
                                           nodeattrs={'editlink':elink})
                             # node_path is path for the subtree displayed by this page.
                             # the fetch_records implemented by TreeDisplay uses it.
                             # where node_path is created. Pass the arguments in nodeattrs
                             # to all the nodecontrols, which ForestControl is going to
                             # create
                             ]
    # produce the output!
    produce_output(self.single_controls,self,page)
    # A hack. This assumes that header is an instance of RootNodeControl
    try:
      page.set_title(self.header.data['desc_text'])
    except:
      page.set_title("") # No title if header.data['desc_text'] does not make sense

def new(req):
    return DocsTreeDisplay(req)