def handle_field_body(self, prefix, fieldname, field, value, uvalue, meta, display): commonattr = dict(name=prefix + fieldname, id=prefix + fieldname) readonly = display.get("readonly") if readonly: if isinstance(field, ds.DatasourceField): raise ValueError("%s: DatasourceField cannot be readonly." % fieldname) commonattr["disabled"] = "disabled" if isinstance(field, ds.DatasourceField): datasource = field.datasource size = 0 opt = XmlWriter(oldhtmlcomp=self.oldhtmlcomp, pretty=self.pretty) for uval, label in datasource.ds_iter_unicode(): attr = {} if (isinstance(field, ds.Many) and uval in uvalue) or (isinstance(field, ds.One) and uval == uvalue): attr["selected"] = "selected" attr["value"] = uval opt.start_element("option", **attr) opt.text_node(label) opt.end_element() size += 1 attr = commonattr.copy() if isinstance(field, ds.Many): attr["multiple"] = "multiple" if size > 10: size = 10 self.w.start_element("select", size=str(size), **attr) self.w.raw_node(opt.create()) self.w.end_element() else: if isinstance(field, String): maxlength = field.maxlength if maxlength > 50: self.w.start_element("textarea", cols="70", rows="3", **commonattr) self.w.text_node(uvalue) self.w.end_element() else: self.w.empty_element("input", type="text", maxlength=str(maxlength), value=uvalue, **commonattr) elif isinstance(field, Text): self.w.start_element("textarea", cols="70", rows="12", **commonattr) self.w.text_node(uvalue) self.w.end_element() elif isinstance(field, Bool): attr = commonattr.copy() if value == True: attr["checked"] = "checked" self.w.empty_element("input", type="checkbox", value="yes", **attr) else: self.w.empty_element("input", type="text", value=uvalue, **commonattr)
def __init__(self, stylesheet, encoding=settings.encoding, pretty=False, startpage=default_startpage): """ @param stylesheet: The URL to a XSLT document to associate with the output. @param encoding: The encoding to output in. Defaults to L{enkel.settings.encoding}. @param pretty: Forwarded to L{enkel.xmlutils.writer.XmlWriter.__init__}. @param startpage: A function which creates the startpage. Defaults to L{default_startpage}. """ self.apps = {} self.pretty = pretty self.applist = XmlWriter(pretty=pretty) self.applist.start_element("applist") self.encoding = encoding self.stylesheet = stylesheet self.startpage = startpage
def create(self): self.w = XmlWriter(pretty=self.pretty) super(Form, self).create() return self.w.create()
def __call__(self, env, start_response): """ env["enkel.reuse.admin.stylesheet"] must contain the URL to the XSLT stylesheet. """ path = env["SCRIPT_NAME"] + env["PATH_INFO"] appid = None if not path.endswith("start/"): match = self.patt.match(path) if match: appid, action = match.groups() app, label, args, kw = self.apps[appid] else: raise Http404() w = XmlWriter(pretty=self.pretty) w.pi("xml", version="1.0", encoding=self.encoding) w.pi("xml-stylesheet", type="text/xsl", href=self.stylesheet) w.start_element("admin", xmlns=XMLNS_ADMIN) w.raw_node(self.applist_xml) if appid: w.empty_element("selected", app=appid, action=action) w.start_element("section", xmlns=XMLNS_MARKUP) if appid: app(env, w, appid, action, label, *args, **kw) else: self.startpage(env, w) w.end_element() start_response("200 OK", [ ("content-type", "text/xml; charset=%s" % self.encoding) ]) yield w.create().encode(self.encoding)
class Admin(object): """ A administration-panel interface using exml. """ def __init__(self, stylesheet, encoding=settings.encoding, pretty=False, startpage=default_startpage): """ @param stylesheet: The URL to a XSLT document to associate with the output. @param encoding: The encoding to output in. Defaults to L{enkel.settings.encoding}. @param pretty: Forwarded to L{enkel.xmlutils.writer.XmlWriter.__init__}. @param startpage: A function which creates the startpage. Defaults to L{default_startpage}. """ self.apps = {} self.pretty = pretty self.applist = XmlWriter(pretty=pretty) self.applist.start_element("applist") self.encoding = encoding self.stylesheet = stylesheet self.startpage = startpage def add(self, id, app, label, *args, **kw): """ @type id: str @param id: A unique indentifier for the app within this Admin object. @param app: A L{AdminApp} instance. @type label: unicode. @param label: The label to show to the user when referencing the app. @param args: Arguments to the app.__init__. @param kw: Keyword arguments to the app.__init__. """ self.apps[id] = app, label, args, kw self.applist.start_element("app", id=id, label=label) for action in app.INDEX: self.applist.empty_element("action", id=action, label=app.ACTIONS[action]) self.applist.end_element() def compile(self): """ Compile the route. Must be called once after all AdminApp's has been L{add}ed. """ ids = "|".join(self.apps.keys()) self.patt = compile(".*(?P<appid>(?:%s))/(?P<action>\w+)$" % ids) self.applist_xml = self.applist.create() del self.applist def __call__(self, env, start_response): """ env["enkel.reuse.admin.stylesheet"] must contain the URL to the XSLT stylesheet. """ path = env["SCRIPT_NAME"] + env["PATH_INFO"] appid = None if not path.endswith("start/"): match = self.patt.match(path) if match: appid, action = match.groups() app, label, args, kw = self.apps[appid] else: raise Http404() w = XmlWriter(pretty=self.pretty) w.pi("xml", version="1.0", encoding=self.encoding) w.pi("xml-stylesheet", type="text/xsl", href=self.stylesheet) w.start_element("admin", xmlns=XMLNS_ADMIN) w.raw_node(self.applist_xml) if appid: w.empty_element("selected", app=appid, action=action) w.start_element("section", xmlns=XMLNS_MARKUP) if appid: app(env, w, appid, action, label, *args, **kw) else: self.startpage(env, w) w.end_element() start_response("200 OK", [ ("content-type", "text/xml; charset=%s" % self.encoding) ]) yield w.create().encode(self.encoding)
def preprocess(postlist_filename, taglist_filename, posts_folder): """ Create a xml-file containing listing all the posts in the cms and a file containing all the tags is the cms. The format of the file output-files are defined in relax-ng/posts.rng and relax-ng/tags.rng. @param postlist_filename: The file where the post info is written. @param taglist_filename: The file where the tag info is written. @param posts_folder: The folder containing all the post-files. """ tags = {} w = XmlWriter(pretty=True) w.start_element("posts") for post_id in listdir(posts_folder): path = join(posts_folder, post_id) w.empty_element("post", id=str(post_id), src=abspath(path), mtime=strftime("%Y-%m-%d %H:%M", gmtime(getmtime(path)))) parser = make_parser() parser.setContentHandler(PostHandler(post_id, tags)) parser.parse(path) w.end_element() # write post list postslist = w.create().encode("utf-8") log.debug("%s: %s" % (postlist_filename, postslist)) open(postlist_filename, "wb").write(postslist) # write tag list w = XmlWriter(pretty=True) w.start_element("tags") for tag_id in tags: tag_name, posts = tags[tag_id] w.start_element("tag", id=tag_id, name=tag_name) for post_id in posts: path = abspath(join(posts_folder, post_id)) w.empty_element("post", id=post_id, src=path, mtime=strftime("%Y-%m-%d %H:%M", gmtime(getmtime(path)))) w.end_element() w.end_element() taglist = w.create().encode("utf-8") log.debug("%s: %s" % (taglist_filename, taglist)) open(taglist_filename, "wb").write(taglist)
class Form(Form): """ XHTML form generator. @note: The output is defined in form.rng. @ivar css_class: The css class used on the div's surrounding every field. @ivar method: The http method used to send the form. @ivar oldhtmlcomp: Insert a whitspace before all standalone xml tag endings to be compatible with very old html browsers. @ivar pretty: Make readable xml. Inserts extra whitespace, which might lead to problems with certain (buggy) parsers. @ivar xmlns: The xml namespace used on the root tag of the result xml node. If None, no xmlns is used. """ css_class = "form_field" method = "post" oldhtmlcomp = False pretty = False xmlns = "http://www.w3.org/1999/xhtml" def start_form(self): args = {"method": "post"} if self.xmlns: args["xmlns"] = self.xmlns if self.id: args["id"] = self.id if self.method == "multipart": args["enctype"] = "multipart/form-data" elif self.method == "get": args["method"] = "get" self.w.start_element("form", action=self.action, **args) def start_group(self, title): if title: self.w.start_element("fieldset") self.w.start_element("legend") self.w.text_node(title) self.w.end_element() def handle_field_body(self, prefix, fieldname, field, value, uvalue, meta, display): commonattr = dict(name=prefix + fieldname, id=prefix + fieldname) readonly = display.get("readonly") if readonly: if isinstance(field, ds.DatasourceField): raise ValueError("%s: DatasourceField cannot be readonly." % fieldname) commonattr["disabled"] = "disabled" if isinstance(field, ds.DatasourceField): datasource = field.datasource size = 0 opt = XmlWriter(oldhtmlcomp=self.oldhtmlcomp, pretty=self.pretty) for uval, label in datasource.ds_iter_unicode(): attr = {} if (isinstance(field, ds.Many) and uval in uvalue) or (isinstance(field, ds.One) and uval == uvalue): attr["selected"] = "selected" attr["value"] = uval opt.start_element("option", **attr) opt.text_node(label) opt.end_element() size += 1 attr = commonattr.copy() if isinstance(field, ds.Many): attr["multiple"] = "multiple" if size > 10: size = 10 self.w.start_element("select", size=str(size), **attr) self.w.raw_node(opt.create()) self.w.end_element() else: if isinstance(field, String): maxlength = field.maxlength if maxlength > 50: self.w.start_element("textarea", cols="70", rows="3", **commonattr) self.w.text_node(uvalue) self.w.end_element() else: self.w.empty_element("input", type="text", maxlength=str(maxlength), value=uvalue, **commonattr) elif isinstance(field, Text): self.w.start_element("textarea", cols="70", rows="12", **commonattr) self.w.text_node(uvalue) self.w.end_element() elif isinstance(field, Bool): attr = commonattr.copy() if value == True: attr["checked"] = "checked" self.w.empty_element("input", type="checkbox", value="yes", **attr) else: self.w.empty_element("input", type="text", value=uvalue, **commonattr) def handle_field(self, prefix, fieldname, field, value, uvalue, meta, display): if display.get("hidden"): if isinstance(field, ds.Many): values = uvalue else: values = [uvalue] for v in values: self.w.empty_element("input", type="hidden", value=v, name=prefix + fieldname) return self.w.start_element("div", attrdict={"class": self.css_class}) self.w.start_element("label", attrdict={"for": prefix + fieldname}) self.w.text_node(meta.get("label", fieldname)) self.w.end_element() self.handle_field_body(prefix, fieldname, field, value, uvalue, meta, display) self.w.start_element("div", attrdict={"class": self.css_class + "_shorthelp"}) self.w.text_node(meta.get("shorthelp", "")) self.w.end_element() error = display.get("error") if error: self.w.start_element("div", attrdict={"class": self.css_class + "_error"}) self.w.text_node(error) self.w.end_element() self.w.end_element() # </div> def end_group(self, title): if title: self.w.end_element() # </fieldset> def end_form(self): self.w.empty_element("input", type="submit", value=self.submit_label) self.w.end_element() # </form> def create(self): self.w = XmlWriter(oldhtmlcomp=self.oldhtmlcomp, pretty=self.pretty) super(Form, self).create() return self.w.create()
def setUp(self): w = XmlWriter() w.pi("xml", version="1.0") w.pi_raw("pros", "shit") w.start_element("html") w.start_element("body") w.text_node(u"\u00e5ge") w.empty_element("div", attrdict={"class": "test"}) w.end_element(2) self.w = w self.b = w.create()
def preprocess(postlist_filename, taglist_filename, posts_folder): """ Create a xml-file containing listing all the posts in the cms and a file containing all the tags is the cms. The format of the file output-files are defined in relax-ng/posts.rng and relax-ng/tags.rng. @param postlist_filename: The file where the post info is written. @param taglist_filename: The file where the tag info is written. @param posts_folder: The folder containing all the post-files. """ tags = {} w = XmlWriter(pretty=True) w.start_element("posts") for post_id in listdir(posts_folder): path = join(posts_folder, post_id) w.empty_element("post", id = str(post_id), src = abspath(path), mtime = strftime("%Y-%m-%d %H:%M", gmtime(getmtime(path))) ) parser = make_parser() parser.setContentHandler(PostHandler(post_id, tags)) parser.parse(path) w.end_element() # write post list postslist = w.create().encode("utf-8") log.debug("%s: %s" % (postlist_filename, postslist)) open(postlist_filename, "wb").write(postslist) # write tag list w = XmlWriter(pretty=True) w.start_element("tags") for tag_id in tags: tag_name, posts = tags[tag_id] w.start_element("tag", id=tag_id, name=tag_name) for post_id in posts: path = abspath(join(posts_folder, post_id)) w.empty_element("post", id = post_id, src = path, mtime = strftime("%Y-%m-%d %H:%M", gmtime(getmtime(path))) ) w.end_element() w.end_element() taglist = w.create().encode("utf-8") log.debug("%s: %s" % (taglist_filename, taglist)) open(taglist_filename, "wb").write(taglist)
def save(self): manip = forminput_to_manip(self.MODEL, FormInput(self.env), "e_") try: manip.validate() except base.FieldValidationError: self._create_edit_form(manip, validate=True) else: p = XmlWriter(pretty=True) p.pi("xml", version="1.0", encoding=ENCODING) p.start_element("post", xmlns=XMLNS_STATICBLOG) p.start_element("summary") p.text_node(manip.summary) p.end_element() for tag in manip.tags.split(","): p.start_element("tag") p.text_node(tag.strip()) p.end_element() p.start_element("section", xmlns=XMLNS_MARKUP, attrdict={"xmlns:s": XMLNS_STATICBLOG}) p.start_element("h") p.text_node(manip.heading) p.end_element() p.raw_node(manip.post) p.end_element() p.end_element() # </post> path = join(self.posts_folder, manip.id) post = self.UNIVERSAL_NEWLINE_PATT.sub(r"\n", p.create()) codecs.open(path, "wb", ENCODING).write(post) self.w.start_element("p") self.w.text_node(N_("Save successful")) self.w.end_element()
class Form(Form): """ EXML Form generator. @ivar xmlns: The xml namespace used on the root tag of the result xml node. Defaults to L{info.XMLNS_FORM}. If None, no xmlns is used. @ivar pretty: Make readable xml. Inserts extra whitespace, which might lead to problems with certain (buggy) parsers. """ xmlns = XMLNS_FORM pretty = False def start_form(self): kw = {} if self.xmlns: kw["xmlns"] = self.xmlns if self.id: kw["id"] = self.id self.w.start_element("form", action=self.action, method=self.method, submit_label=self.submit_label, **kw) def start_group(self, title): self.w.start_element("group", title=title) def handle_field(self, prefix, fieldname, field, value, uvalue, meta, display): if display.get("hidden"): if isinstance(field, Many): values = uvalue else: values = [uvalue] for v in values: self.w.start_element("hidden", id=prefix+fieldname) self.w.text_node(v) self.w.end_element() return readonly = display.get("readonly") if readonly: if isinstance(field, DatasourceField): raise ValueError( "%s: DatasourceField cannot be readonly." % fieldname) name = None for ttype, name in ( (String, "string"), (Int, "int"), (Long, "long"), (Float, "float"), (Text, "text"), (Date, "date"), (DateTime, "datetime"), (Time, "time"), (Many, "many"), (One, "one"), (Bool, "bool") ): if isinstance(field, ttype): break if not name: raise ValueError( """All form fields must be instances of one of the base field types defined in enkel.model.field.base. Or one of the two datasource fields defined in enkel.model.ds. """) elif name == "string" and \ field.maxlength > field.LONG_STRING: name = "longstring" elif readonly: name = "readonly" if field.required: required = "yes" else: required = "no" self.w.start_element(name, # start field element id = prefix + fieldname, typehint = field.__class__.__name__, required = required) self.w.start_element("label") self.w.text_node(meta.get("label", fieldname)) self.w.end_element() self.w.start_element("tooltip") self.w.text_node(meta.get("shorthelp", "")) self.w.end_element() if isinstance(field, One): datasource = field.datasource self.w.start_element("onevalue") for val, label in datasource.ds_iter_unicode(): if val == uvalue: name = "sel_item" else: name = "item" self.w.start_element(name, value=val) self.w.text_node(label) self.w.end_element() self.w.end_element() elif isinstance(field, Many): datasource = field.datasource self.w.start_element("manyvalue") for val, label in datasource.ds_iter_unicode(): if val in uvalue: name = "sel_item" else: name = "item" self.w.start_element(name, value=val) self.w.text_node(label) self.w.end_element() self.w.end_element() else: self.w.start_element("value") self.w.text_node(uvalue) self.w.end_element() error = display.get("error") if error: self.w.start_element("error") self.w.text_node(error) self.w.end_element() self.w.end_element() # end field element def end_group(self, title): self.w.end_element() def end_form(self): self.w.end_element() def create(self): self.w = XmlWriter(pretty=self.pretty) super(Form, self).create() return self.w.create()
def __call__(self, env, start_response): """ env["enkel.reuse.admin.stylesheet"] must contain the URL to the XSLT stylesheet. """ path = env["SCRIPT_NAME"] + env["PATH_INFO"] appid = None if not path.endswith("start/"): match = self.patt.match(path) if match: appid, action = match.groups() app, label, args, kw = self.apps[appid] else: raise Http404() w = XmlWriter(pretty=self.pretty) w.pi("xml", version="1.0", encoding=self.encoding) w.pi("xml-stylesheet", type="text/xsl", href=self.stylesheet) w.start_element("admin", xmlns=XMLNS_ADMIN) w.raw_node(self.applist_xml) if appid: w.empty_element("selected", app=appid, action=action) w.start_element("section", xmlns=XMLNS_MARKUP) if appid: app(env, w, appid, action, label, *args, **kw) else: self.startpage(env, w) w.end_element() start_response( "200 OK", [("content-type", "text/xml; charset=%s" % self.encoding)]) yield w.create().encode(self.encoding)
class Form(Form): """ EXML Form generator. @ivar xmlns: The xml namespace used on the root tag of the result xml node. Defaults to L{info.XMLNS_FORM}. If None, no xmlns is used. @ivar pretty: Make readable xml. Inserts extra whitespace, which might lead to problems with certain (buggy) parsers. """ xmlns = XMLNS_FORM pretty = False def start_form(self): kw = {} if self.xmlns: kw["xmlns"] = self.xmlns if self.id: kw["id"] = self.id self.w.start_element("form", action=self.action, method=self.method, submit_label=self.submit_label, **kw) def start_group(self, title): self.w.start_element("group", title=title) def handle_field(self, prefix, fieldname, field, value, uvalue, meta, display): if display.get("hidden"): if isinstance(field, Many): values = uvalue else: values = [uvalue] for v in values: self.w.start_element("hidden", id=prefix + fieldname) self.w.text_node(v) self.w.end_element() return readonly = display.get("readonly") if readonly: if isinstance(field, DatasourceField): raise ValueError("%s: DatasourceField cannot be readonly." % fieldname) name = None for ttype, name in ((String, "string"), (Int, "int"), (Long, "long"), (Float, "float"), (Text, "text"), (Date, "date"), (DateTime, "datetime"), (Time, "time"), (Many, "many"), (One, "one"), (Bool, "bool")): if isinstance(field, ttype): break if not name: raise ValueError( """All form fields must be instances of one of the base field types defined in enkel.model.field.base. Or one of the two datasource fields defined in enkel.model.ds. """) elif name == "string" and \ field.maxlength > field.LONG_STRING: name = "longstring" elif readonly: name = "readonly" if field.required: required = "yes" else: required = "no" self.w.start_element( name, # start field element id=prefix + fieldname, typehint=field.__class__.__name__, required=required) self.w.start_element("label") self.w.text_node(meta.get("label", fieldname)) self.w.end_element() self.w.start_element("tooltip") self.w.text_node(meta.get("shorthelp", "")) self.w.end_element() if isinstance(field, One): datasource = field.datasource self.w.start_element("onevalue") for val, label in datasource.ds_iter_unicode(): if val == uvalue: name = "sel_item" else: name = "item" self.w.start_element(name, value=val) self.w.text_node(label) self.w.end_element() self.w.end_element() elif isinstance(field, Many): datasource = field.datasource self.w.start_element("manyvalue") for val, label in datasource.ds_iter_unicode(): if val in uvalue: name = "sel_item" else: name = "item" self.w.start_element(name, value=val) self.w.text_node(label) self.w.end_element() self.w.end_element() else: self.w.start_element("value") self.w.text_node(uvalue) self.w.end_element() error = display.get("error") if error: self.w.start_element("error") self.w.text_node(error) self.w.end_element() self.w.end_element() # end field element def end_group(self, title): self.w.end_element() def end_form(self): self.w.end_element() def create(self): self.w = XmlWriter(pretty=self.pretty) super(Form, self).create() return self.w.create()
def create(self): self.w = XmlWriter(oldhtmlcomp=self.oldhtmlcomp, pretty=self.pretty) super(Form, self).create() return self.w.create()
class Form(Form): """ XHTML form generator. @note: The output is defined in form.rng. @ivar css_class: The css class used on the div's surrounding every field. @ivar method: The http method used to send the form. @ivar oldhtmlcomp: Insert a whitspace before all standalone xml tag endings to be compatible with very old html browsers. @ivar pretty: Make readable xml. Inserts extra whitespace, which might lead to problems with certain (buggy) parsers. @ivar xmlns: The xml namespace used on the root tag of the result xml node. If None, no xmlns is used. """ css_class = "form_field" method = "post" oldhtmlcomp = False pretty = False xmlns = "http://www.w3.org/1999/xhtml" def start_form(self): args = {"method": "post"} if self.xmlns: args["xmlns"] = self.xmlns if self.id: args["id"] = self.id if self.method == "multipart": args["enctype"] = "multipart/form-data" elif self.method == "get": args["method"] = "get" self.w.start_element("form", action=self.action, **args) def start_group(self, title): if title: self.w.start_element("fieldset") self.w.start_element("legend") self.w.text_node(title) self.w.end_element() def handle_field_body(self, prefix, fieldname, field, value, uvalue, meta, display): commonattr = dict(name=prefix+fieldname, id=prefix+fieldname) readonly = display.get("readonly") if readonly: if isinstance(field, ds.DatasourceField): raise ValueError( "%s: DatasourceField cannot be readonly." % fieldname) commonattr["disabled"] = "disabled" if isinstance(field, ds.DatasourceField): datasource = field.datasource size = 0 opt = XmlWriter(oldhtmlcomp=self.oldhtmlcomp, pretty=self.pretty) for uval, label in datasource.ds_iter_unicode(): attr = {} if \ (isinstance(field, ds.Many) and\ uval in uvalue) or\ (isinstance(field, ds.One) and\ uval == uvalue): attr["selected"] = "selected" attr["value"] = uval opt.start_element("option", **attr) opt.text_node(label) opt.end_element() size += 1 attr = commonattr.copy() if isinstance(field, ds.Many): attr["multiple"] = "multiple" if size > 10: size = 10 self.w.start_element("select", size=str(size), **attr) self.w.raw_node(opt.create()) self.w.end_element() else: if isinstance(field, String): maxlength = field.maxlength if maxlength > 50: self.w.start_element("textarea", cols="70", rows="3", **commonattr) self.w.text_node(uvalue) self.w.end_element() else: self.w.empty_element("input", type = "text", maxlength = str(maxlength), value = uvalue, **commonattr) elif isinstance(field, Text): self.w.start_element("textarea", cols="70", rows="12", **commonattr) self.w.text_node(uvalue) self.w.end_element() elif isinstance(field, Bool): attr = commonattr.copy() if value == True: attr["checked"] = "checked" self.w.empty_element("input", type = "checkbox", value = "yes", **attr) else: self.w.empty_element("input", type = "text", value = uvalue, **commonattr) def handle_field(self, prefix, fieldname, field, value, uvalue, meta, display): if display.get("hidden"): if isinstance(field, ds.Many): values = uvalue else: values = [uvalue] for v in values: self.w.empty_element("input", type = "hidden", value = v, name = prefix+fieldname) return self.w.start_element("div", attrdict={"class": self.css_class}) self.w.start_element("label", attrdict={"for": prefix+fieldname}) self.w.text_node(meta.get("label", fieldname)) self.w.end_element() self.handle_field_body(prefix, fieldname, field, value, uvalue, meta, display) self.w.start_element("div", attrdict={"class": self.css_class+"_shorthelp"}) self.w.text_node(meta.get("shorthelp", "")) self.w.end_element() error = display.get("error") if error: self.w.start_element("div", attrdict={"class": self.css_class+"_error"}) self.w.text_node(error) self.w.end_element() self.w.end_element() # </div> def end_group(self, title): if title: self.w.end_element() # </fieldset> def end_form(self): self.w.empty_element("input", type="submit", value=self.submit_label) self.w.end_element() # </form> def create(self): self.w = XmlWriter(oldhtmlcomp=self.oldhtmlcomp, pretty=self.pretty) super(Form, self).create() return self.w.create()
def handle_field_body(self, prefix, fieldname, field, value, uvalue, meta, display): commonattr = dict(name=prefix+fieldname, id=prefix+fieldname) readonly = display.get("readonly") if readonly: if isinstance(field, ds.DatasourceField): raise ValueError( "%s: DatasourceField cannot be readonly." % fieldname) commonattr["disabled"] = "disabled" if isinstance(field, ds.DatasourceField): datasource = field.datasource size = 0 opt = XmlWriter(oldhtmlcomp=self.oldhtmlcomp, pretty=self.pretty) for uval, label in datasource.ds_iter_unicode(): attr = {} if \ (isinstance(field, ds.Many) and\ uval in uvalue) or\ (isinstance(field, ds.One) and\ uval == uvalue): attr["selected"] = "selected" attr["value"] = uval opt.start_element("option", **attr) opt.text_node(label) opt.end_element() size += 1 attr = commonattr.copy() if isinstance(field, ds.Many): attr["multiple"] = "multiple" if size > 10: size = 10 self.w.start_element("select", size=str(size), **attr) self.w.raw_node(opt.create()) self.w.end_element() else: if isinstance(field, String): maxlength = field.maxlength if maxlength > 50: self.w.start_element("textarea", cols="70", rows="3", **commonattr) self.w.text_node(uvalue) self.w.end_element() else: self.w.empty_element("input", type = "text", maxlength = str(maxlength), value = uvalue, **commonattr) elif isinstance(field, Text): self.w.start_element("textarea", cols="70", rows="12", **commonattr) self.w.text_node(uvalue) self.w.end_element() elif isinstance(field, Bool): attr = commonattr.copy() if value == True: attr["checked"] = "checked" self.w.empty_element("input", type = "checkbox", value = "yes", **attr) else: self.w.empty_element("input", type = "text", value = uvalue, **commonattr)
def setUp(self): w = XmlWriter() w.pi("xml", version="1.0") w.pi_raw("pros", "shit") w.start_element("html") w.start_element("body") w.text_node(u"\u00e5ge") w.empty_element("div", attrdict={"class":"test"}) w.end_element(2) self.w = w self.b = w.create()
class Admin(object): """ A administration-panel interface using exml. """ def __init__(self, stylesheet, encoding=settings.encoding, pretty=False, startpage=default_startpage): """ @param stylesheet: The URL to a XSLT document to associate with the output. @param encoding: The encoding to output in. Defaults to L{enkel.settings.encoding}. @param pretty: Forwarded to L{enkel.xmlutils.writer.XmlWriter.__init__}. @param startpage: A function which creates the startpage. Defaults to L{default_startpage}. """ self.apps = {} self.pretty = pretty self.applist = XmlWriter(pretty=pretty) self.applist.start_element("applist") self.encoding = encoding self.stylesheet = stylesheet self.startpage = startpage def add(self, id, app, label, *args, **kw): """ @type id: str @param id: A unique indentifier for the app within this Admin object. @param app: A L{AdminApp} instance. @type label: unicode. @param label: The label to show to the user when referencing the app. @param args: Arguments to the app.__init__. @param kw: Keyword arguments to the app.__init__. """ self.apps[id] = app, label, args, kw self.applist.start_element("app", id=id, label=label) for action in app.INDEX: self.applist.empty_element("action", id=action, label=app.ACTIONS[action]) self.applist.end_element() def compile(self): """ Compile the route. Must be called once after all AdminApp's has been L{add}ed. """ ids = "|".join(self.apps.keys()) self.patt = compile(".*(?P<appid>(?:%s))/(?P<action>\w+)$" % ids) self.applist_xml = self.applist.create() del self.applist def __call__(self, env, start_response): """ env["enkel.reuse.admin.stylesheet"] must contain the URL to the XSLT stylesheet. """ path = env["SCRIPT_NAME"] + env["PATH_INFO"] appid = None if not path.endswith("start/"): match = self.patt.match(path) if match: appid, action = match.groups() app, label, args, kw = self.apps[appid] else: raise Http404() w = XmlWriter(pretty=self.pretty) w.pi("xml", version="1.0", encoding=self.encoding) w.pi("xml-stylesheet", type="text/xsl", href=self.stylesheet) w.start_element("admin", xmlns=XMLNS_ADMIN) w.raw_node(self.applist_xml) if appid: w.empty_element("selected", app=appid, action=action) w.start_element("section", xmlns=XMLNS_MARKUP) if appid: app(env, w, appid, action, label, *args, **kw) else: self.startpage(env, w) w.end_element() start_response( "200 OK", [("content-type", "text/xml; charset=%s" % self.encoding)]) yield w.create().encode(self.encoding)