Back to index

moin  1.9.0~rc2
newpage.py
Go to the documentation of this file.
00001 """
00002     MoinMoin - newpage action
00003 
00004     Create a new page with optional template. Can be used with NewPage.py macro.
00005 
00006     @copyright: 2004 Vito Miliano (vito_moinnewpagewithtemplate@perilith.com),
00007                 2004 Nir Soffer <nirs@freeshell.org>,
00008                 2004 Alexander Schremmer <alex AT alexanderweb DOT de>
00009                 2008 MoinMoin:ReimarBauer
00010     @license: GNU GPL, see COPYING for details.
00011 """
00012 
00013 import time
00014 from MoinMoin.Page import Page
00015 
00016 class NewPage:
00017     """ Open editor for a new page, using template """
00018 
00019     def __init__(self, request, referrer):
00020         self.request = request
00021         self.referrer = referrer # The page the user came from
00022         self.pagename = self.request.values.get('pagename')
00023         self.nametemplate = self.request.values.get('nametemplate', '%s')
00024         self.nametemplate = self.nametemplate.replace('\x00', '')
00025 
00026     def checkAndCombineArguments(self):
00027         """ Check arguments in form, return error msg
00028 
00029         @rtype: unicode
00030         @return: error message
00031         """
00032         _ = self.request.getText
00033         need_replace = '%s' in self.nametemplate
00034         if not self.pagename and need_replace:
00035             return _("Cannot create a new page without a page name."
00036                      "  Please specify a page name.")
00037         if need_replace:
00038         # generate a string that can be safely used as the pagename
00039         # template variable
00040             repl = 'A@'
00041             i = 0
00042             while repl in self.nametemplate:
00043                 repl += ['#', '&', '$', 'x', 'X', ':', '@'][i]
00044                 i += 1
00045                 i = i % 7
00046             template = self.nametemplate.replace('%s', repl)
00047         else:
00048             template = self.nametemplate
00049         template = template.encode('utf-8')
00050         template = time.strftime(template, self.request.user.getTime(time.time()))
00051         template = template.decode('utf-8')
00052         if need_replace:
00053             self.pagename = template.replace(repl, self.pagename)
00054         else:
00055             self.pagename = template
00056         return ''
00057 
00058     def checkPermissions(self):
00059         """ Check write permission in form, return error msg
00060 
00061         @rtype: unicode
00062         @return: error message
00063         """
00064         _ = self.request.getText
00065         page = Page(self.request, self.pagename)
00066         if not (page.isWritable() and self.request.user.may.read(self.pagename)):
00067             # Same error as the edit page for localization reasons
00068             return _('You are not allowed to edit this page.')
00069         return ''
00070 
00071     def render(self):
00072         """ Redirect to the new page, using edit action and template """
00073         error = self.checkAndCombineArguments() or self.checkPermissions()
00074         if error:
00075             # Send back to the page you came from, with an error msg
00076             page = Page(self.request, self.referrer)
00077             self.request.theme.add_msg(error, "error")
00078             page.send_page()
00079         else:
00080             # Redirect to new page using edit action. No error checking
00081             # is needed because it is done later in new request.
00082             pagename = self.pagename
00083             query = {'action': 'edit', 'backto': self.referrer}
00084 
00085             template = self.request.values.get('template', '')
00086             if template:
00087                 query['template'] = template
00088 
00089             parent = self.request.values.get('parent', '')
00090             if parent:
00091                 pagename = "%s/%s" % (parent, pagename)
00092 
00093             url = Page(self.request, pagename).url(self.request, query)
00094             self.request.http_redirect(url)
00095 
00096         return ''
00097 
00098 def execute(pagename, request):
00099     """ Temporary glue code for current moin action system """
00100     if request.method != 'POST':
00101         return False, u''
00102 
00103     return NewPage(request, pagename).render()
00104