Mercurial > public > src > moin > extensions
changeset 617:07e8932e3594
add code from MacroMarket/FormCreate (form_create-1.7.tgz) - outdated code for moin 1.7/1.8
author | Thomas Waldmann <tw AT waldmann-edv DOT de> |
---|---|
date | Mon, 25 Mar 2013 16:16:43 +0100 |
parents | 36ea97c06541 |
children | e6a6740166c8 |
files | data/plugin/macro/FormBase.py data/plugin/macro/FormCheckbox.py data/plugin/macro/FormField.py data/plugin/macro/FormFooter.py data/plugin/macro/FormHeader.py data/plugin/macro/FormRadio.py data/plugin/macro/FormSelect.py data/plugin/macro/FormSubmit.py data/plugin/macro/FormText.py data/plugin/macro/FormTextarea.py data/plugin/macro/FormUpload.py |
diffstat | 11 files changed, 519 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/data/plugin/macro/FormBase.py Mon Mar 25 16:16:43 2013 +0100 @@ -0,0 +1,108 @@ +# -*- coding: iso-8859-1 -*- +""" + MoinMoin - FormBase Macro + + Base stuff for all form macros. + Macro generates HTML form elements in following sequence: + - parse_args parse defined parameters + - validate validate correct definition of parameters + - build build HTML element and stores it into self.output + + @copyright: 2008 by Peter Bodi <petrdll@centrum.sk> + @license: GNU GPL, see COPYING for details. +""" + +import sys +from MoinMoin import wikiutil + +class FormBase: + + debug = False + + def __init__(self, macro, args): + self.macro = macro + self.request = macro.request + self._ = self.request.getText + self.module = self.__module__.split(".").pop() + self.args = args + + self.msg = "" + self.output = "" + + ### Macro parameters ### + self._main = {} # Main parameters + self._attribs = {} # in FormField macros used as HTML tag attributes + self._params = {} # Additional parameters + ### ========== ### + + def parse_args(self): + """ Parse parameters into specific macro properties """ + self._main, self._attribs, self._params = wikiutil.parse_quoted_separated(self.args) + + def validate(self): + """ Validates macro parameters """ + for name, value in self._attribs.iteritems(): + if not hasattr(self, name): + self.msg = self._("Invalid attribute [%(attr)s] specified in macro [%(mod)s]" % {'attr':name, 'mod':self.module}) + raise FormValidationError(self.msg) + + def build(self): + """ Build HTML element """ + raise NotImplementedError + + def render(self): + """ Executes core methods: parse_args, validate, build + + Returns output as HTML element or error message. + """ + if not self.debug: + try: + self.parse_args() + self.validate() + self.build() + except FormValidationError: + return self.msg + except Exception, e: + """ TODO: Log exception """ + self.output = self._("Error - %s") % e + + return self.output + + else: + try: + self.parse_args() + self.validate() + self.build() + except FormValidationError: + return self.msg + + return self.output + + + + +############################################################################# +### Protected methods +############################################################################# + + def _build_attribs(self, attribs): + """ Converts HTML element attributes from dictionary to string """ + attrstr = "" + for name, value in attribs.iteritems(): + if value: + attrstr += "%(name)s=\"%(value)s\" " % {'name':name, 'value':value} + + return attrstr + + def _build_input(self, type, name, args): + """ Builds HTML input element """ + attrs = self._build_attribs(args) + if name is None: + input = "<input type=\"%(type)s\" %(attrs)s/>\n" % {'type':type, 'attrs':attrs} + else: + input = "<input type=\"%(type)s\" name=\"%(name)s\" %(attrs)s/>\n" % {'type':type, 'name':name, 'attrs':attrs} + return input + +class FormValidationError(Exception): + """ Exception raised for errors during validation """ + \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/data/plugin/macro/FormCheckbox.py Mon Mar 25 16:16:43 2013 +0100 @@ -0,0 +1,61 @@ +# -*- coding: iso-8859-1 -*- +""" + MoinMoin - FormCheckbox Macro + + + + @copyright: 2008 by Peter Bodi <petrdll@centrum.sk> + @license: GNU GPL, see COPYING for details. +""" + +#from wikiconfig.plugin.macro.FormField import FormField +from FormField import FormField + +class FormCheckbox(FormField): + + def __init__(self, macro, args): + FormField.__init__(self, macro, args) + + self.true = self._("True") + self.false = self._("False") + self.checked = "" + self.size = "" + + def parse_args(self): + FormField.parse_args(self) + length = len(self._main) + if length == 2: + self.true = self._main[1] + elif length == 3: + self.true = self._main[1] + self.false = self._main[2] + + def build(self): + FormField.build(self) + self.output += """ + <script type=\"text/javascript\"> + function checkbox(box, input) + { + if(box.checked==false) + { + input.value = "%(false)s" + } + else + { + input.value = "%(true)s" + } + } + </script> + """ % {'true':self.true, 'false':self.false} + inputId = self.label + checkboxId = self.label+"box" + + self._attribs["id"] = checkboxId + self._attribs["onclick"] = "checkbox(document.getElementById('"+checkboxId+"'), document.getElementById('"+inputId+"'))" + + self.output += self._build_input("hidden", self.label, {'id':inputId, 'value':self.false}) + self.output += self._build_input("checkbox", None, self._attribs) + +def execute(macro, args): + return FormCheckbox(macro, args).render() +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/data/plugin/macro/FormField.py Mon Mar 25 16:16:43 2013 +0100 @@ -0,0 +1,52 @@ +# -*- coding: iso-8859-1 -*- +""" + MoinMoin - FormField Macro + + Base stuff for field macros. All form fields should + be derived from FormField class. + + @copyright: 2008 by Peter Bodi <petrdll@centrum.sk> + @license: GNU GPL, see COPYING for details. +""" +from MoinMoin import wikiutil + +#from wikiconfig.plugin.macro.FormBase import FormBase, FormValidationError +from FormBase import FormBase, FormValidationError + +class FormField(FormBase): + + def __init__(self, macro, args): + FormBase.__init__(self, macro, args) + + self.label = "" + self.width = "" + self.height = "" + + #if not hasattr(self.request, "fieldindex"): + # self.request.fieldindex = 0 + #else: + # self.request.fieldindex += 1 + #self.index = str(self.request.fieldindex) + + def parse_args(self): + FormBase.parse_args(self) + self.label = self._main[0] + + def validate(self): + FormBase.validate(self) + if not self.label: + self.msg = self._("Field label is not defined") + raise FormValidationError(self.msg) + + + def build(self): + if not hasattr(self.request, "labels"): + self.request.labels = [] + self.request.labels.append(self.label) + else: + if self.request.labels.count(self.label) == 0: + self.request.labels.append(self.label) + + #self.output += self._build_input("hidden", self.label+"_index", {'value':self.index}) + +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/data/plugin/macro/FormFooter.py Mon Mar 25 16:16:43 2013 +0100 @@ -0,0 +1,20 @@ +# -*- coding: iso-8859-1 -*- +""" + MoinMoin - FormFooter Macro + + + + @copyright: 2008 by Peter Bodi <petrdll@centrum.sk> + @license: GNU GPL, see COPYING for details. +""" + +from FormBase import FormBase + +class FormFooter(FormBase): + + def build(self): + self.output += self._build_input("hidden", "labels", {'value':';'.join(self.request.labels)}) + self.output += "</form>\n" + +def execute(macro, args): + return FormFooter(macro, args).render()
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/data/plugin/macro/FormHeader.py Mon Mar 25 16:16:43 2013 +0100 @@ -0,0 +1,75 @@ +# -*- coding: iso-8859-1 -*- +""" + MoinMoin - FormHeader Macro + + Generates form header with some additional parameters. + + @copyright: 2008 by Peter Bodi <petrdll@centrum.sk> + @license: GNU GPL, see COPYING for details. +""" + +from MoinMoin.Page import Page +from FormBase import FormBase + +class FormHeader(FormBase): + + def parse_args(self): + FormBase.parse_args(self) + + self.action = "" + self.actions = () + self.targetfile = "" + self.targetemail = "" + self.targetpage = "" + + length = len(self._main) + + if length > 1: + self.action = "loadactions" + self.actions = self._main + elif length == 1: + self.action = self._main[0] + + if self._attribs.has_key('targetfile'): + self.targetfile = self._attribs['targetfile'] + + if self._attribs.has_key('targetemail'): + self.targetemail = self._attribs['targetemail'] + + if self._attribs.has_key('targetpage'): + self.targetpage = self._attribs['targetpage'] + + def validate(self): + FormBase.validate(self) + if not self.action: + self.msg = self._("No action was specified in macro [%(macro)s]" % {'macro':self.module}) + raise FormValidationError(self.msg) + + def build(self): + self.output += "<form enctype=\"multipart/form-data\" action=\"\" method=\"post\">\n" + self.output += self._build_input("hidden", "doit", {'value':"Do it"}) + self.output += self._build_input("hidden", "action", {'value':self.action}) + + if self.actions: + for action in self.actions: + self.output += self._build_input("hidden", "actions[]", {'value':action}) + + if self.targetfile: + self.output += self._build_input("hidden", "targetfile", {'value':self.targetfile}) + + if self.targetemail: + self.output += self._build_input("hidden", "targetemail", {'value':self.targetemail}) + + if self.targetpage: + page = Page(self.request, self.targetpage) + if page.isStandardPage(False): + self.output += self._build_input("hidden", "targetpage", {'value':self.targetpage}) + else: + self.output += self._("Targetpage [%(targetpage)s] does not exists" % {'targetpage':self.targetpage}) + + + +def execute(macro, args): + return FormHeader(macro, args).render() + +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/data/plugin/macro/FormRadio.py Mon Mar 25 16:16:43 2013 +0100 @@ -0,0 +1,42 @@ +# -*- coding: iso-8859-1 -*- +""" + MoinMoin - FormRadio Macro + + + + @copyright: 2008 by Peter Bodi <petrdll@centrum.sk> + @license: GNU GPL, see COPYING for details. +""" + +from FormField import FormField + +class FormRadio(FormField): + + def __init(self, macro, args): + FormField.__init__(self, macro, args) + + self.value = "" + self.checked = "" + self.size = "" + + def parse_args(self): + FormField.parse_args(self) + + self.value = self._main[1] + + def validate(self): + FormField.validate(self) + if not self.value: + self.msg = self._("Field value is not defined") + raise FormValidationError(self.msg) + + def build(self): + FormField.build(self) + self._attribs["value"] = self.value + self._attribs["checked"] = "checked" + #self.output += self._build_input("hidden", "labels[]", {'value':self.label}) + self.output += self._build_input("radio", self.label, self._attribs) + +def execute(macro, args): + return FormRadio(macro, args).render() +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/data/plugin/macro/FormSelect.py Mon Mar 25 16:16:43 2013 +0100 @@ -0,0 +1,39 @@ +# -*- coding: iso-8859-1 -*- +""" + MoinMoin - FormSelect Macro + + + + @copyright: 2008 by Peter Bodi <petrdll@centrum.sk> + @license: GNU GPL, see COPYING for details. +""" + +from FormField import FormField + +class FormSelect(FormField): + + def __init__(self, macro, args): + FormField.__init__(self, macro, args) + + self.list = () + self.size = "" + self.multiple = "" + self.disabled = "" + + def parse_args(self): + FormField.parse_args(self) + self.list = self._main + self.list.pop(0) + + def build(self): + FormField.build(self) + #self.output += self._build_input("hidden", "labels[]", {'value':self.label}) + self.output += "<select name=\"%(label)s\" %(attribs)s>\n" % {'label':self.label, 'attribs':self._attribs} + self.output += "<option value=\"\"></option>\n" + for item in self.list: + self.output += "<option value=\"%(item)s\">%(item)s</option>\n" % {'item':item} + self.output += "</select>\n" + +def execute(macro, args): + return FormSelect(macro, args).render() +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/data/plugin/macro/FormSubmit.py Mon Mar 25 16:16:43 2013 +0100 @@ -0,0 +1,31 @@ +# -*- coding: iso-8859-1 -*- +""" + MoinMoin - FormSubmit Macro + + + + @copyright: 2008 by Peter Bodi <petrdll@centrum.sk> + @license: GNU GPL, see COPYING for details. +""" +from MoinMoin.security.textcha import TextCha +from FormField import FormField + +class FormSubmit(FormField): + + def __init__(self, macro, args): + FormField.__init__(self, macro, args) + + self.value = "" + self.size = "" + + def parse_args(self): + FormField.parse_args(self) + self.value = self.label + + def build(self): + self.output += TextCha(self.request).render(); + self.output += self._build_input("submit", None, {'value':self.value}) + +def execute(macro, args): + return FormSubmit(macro, args).render() +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/data/plugin/macro/FormText.py Mon Mar 25 16:16:43 2013 +0100 @@ -0,0 +1,31 @@ +# -*- coding: iso-8859-1 -*- +""" + MoinMoin - FormText Macro + + + + @copyright: 2008 by Peter Bodi <petrdll@centrum.sk> + @license: GNU GPL, see COPYING for details. +""" + +from FormField import FormField + +class FormText(FormField): + + def __init__(self, macro, args): + FormField.__init__(self, macro, args) + + self.maxlength = "" + self.size = "" + self.autocomplete = "" + self.disabled = "" + self.readonly = "" + + def build(self): + FormField.build(self) + #self.output += self._build_input("hidden", "labels[]", {'value':self.label}) + self.output += self._build_input("text", self.label, self._attribs) + +def execute(macro, args): + return FormText(macro, args).render() +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/data/plugin/macro/FormTextarea.py Mon Mar 25 16:16:43 2013 +0100 @@ -0,0 +1,30 @@ +# -*- coding: iso-8859-1 -*- +""" + MoinMoin - FormTextarea Macro + + + + @copyright: 2008 by Peter Bodi <petrdll@centrum.sk> + @license: GNU GPL, see COPYING for details. +""" + +from FormField import FormField + +class FormTextarea(FormField): + + def __init__(self, macro, args): + FormField.__init__(self, macro, args) + + self.rows = "" + self.cols = "" + self.disabled = "" + self.readonly = "" + + def build(self): + FormField.build(self) + #self.output += self._build_input("hidden", "labels[]", {'value':self.label}) + self.output += "<textarea name=\"%(label)s\" %(attribs)s></textarea>" % {'attribs':self._build_attribs(self._attribs), 'label':self.label} + +def execute(macro, args): + return FormTextarea(macro, args).render() +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/data/plugin/macro/FormUpload.py Mon Mar 25 16:16:43 2013 +0100 @@ -0,0 +1,30 @@ +# -*- coding: iso-8859-1 -*- +""" + MoinMoin - FormUpload Macro + + + + @copyright: 2008 by Peter Bodi <petrdll@centrum.sk> + @license: GNU GPL, see COPYING for details. +""" + +from FormField import FormField + +class FormUpload(FormField): + + def __init__(self, macro, args): + FormField.__init__(self, macro, args) + + self.size = "" + + def parse_args(self): + FormField.parse_args(self) + + def build(self): + FormField.build(self) + self.output += self._build_input("hidden", "uploadlabel", {'value':self.label}) + self.output += self._build_input("file", "file", self._attribs) + +def execute(macro, args): + return FormUpload(macro, args).render() +