def _return_alternate(self, req): if(self.alternate): return self.alternate if(self.log_missing_files): req.log_error('File does not exist: %s' % self.true_path) from modu.web import app app.raise404(req['REQUEST_URI'])
def prepare_custom(self, req, itemdef): """ Manage use of custom resources in the admin interface. @param req: the current request @type req: L{modu.web.app.Request} @param itemdef: an itemdef with custom resource info @type itemdef: L{modu.editable.define.itemdef} """ result = itemdef.config.get("resource") if isinstance(result, (list, tuple)): rsrc_class, args, kwargs = result else: rsrc_class = result args = [] kwargs = {} if not (rsrc_class): app.raise404("There is no resource at the path: %s" % req["REQUEST_URI"]) if not (resource.ITemplate.implementedBy(rsrc_class)): app.raise500("The resource at %s is invalid, it must be an ITemplate implementor." % req["REQUEST_URI"]) rsrc = rsrc_class(*args, **kwargs) req["modu.resource"] = rsrc for key in self.get_slots(): rsrc.set_slot(key, self.get_slot(key)) content = rsrc.get_response(req) raise web.HTTPStatus("200 OK", req.get_headers(), content)
def prepare_content(self, req): """ @see: L{modu.web.resource.IContent.prepare_content()} """ if not(req.postpath): self.set_slot('content', 'Welcome to your new project...') return page_code = req.postpath[0] req.store.ensure_factory('page', page.Page, force=True) p = req.store.load_one('page', {'active':1, 'url_code':page_code}) if(p is None): app.raise404(page_code) self.set_slot('content', p.data)
def prepare_content(self, req): """ @see: L{modu.web.resource.IContent.prepare_content()} """ if not (req.postpath): self.set_slot("content", "Welcome to your new project...") return page_code = req.postpath[0] req.store.ensure_factory("page", page.Page, force=True) p = req.store.load_one("page", {"active": 1, "url_code": page_code}) if p is None: app.raise404(page_code) self.set_slot("content", p.data)
def prepare_content(self, req): """ @see: L{modu.web.resource.IContent.prepare_content()} """ if not(req.postpath): self.set_slot('title', 'modu: a web toolkit') self.set_slot('content', None) return else: page_code = req.postpath[0] req.store.ensure_factory('page', page.Page, force=True) p = req.store.load_one('page', {'active':1, 'url_code':page_code}) if(p is None): app.raise404(page_code) self.set_slot('title', p.title) self.set_slot('content', p.data)
def prepare_content(self, req): """ @see: L{modu.web.resource.IContent.prepare_content()} """ if not(req.postpath): app.redirect(req.get_path('/')) req.store.ensure_factory('blog', blog.Blog) try: blog_id = int(req.postpath[0]) except ValueError: b = req.store.load_one('blog', active=1, url_code=req.postpath[0]) else: b = req.store.load_one('blog', active=1, id=blog_id) if(b): app.redirect(req.get_path('blog', b.url_code), permanent=True) if(b is None): app.raise404(req.postpath[0]) self.set_slot('title', b.title) self.set_slot('blog', b)
def prepare_content(self, req): """ @see: L{modu.web.resource.IContent.prepare_content()} """ if not(req.postpath): app.raise404() if(req.postpath[0] == 'test'): url = req.data.get('url', None).value pattern = req.data.get('pattern', None).value if not(url and pattern): app.raise400('Missing parameters.') try: result = urllib2.urlopen(url) except Exception, e: app.raise400('Invalid URL: %s' % e) base_url = os.path.dirname(url) text = feed.fix_urls(result.read(), base_url) self.content_type = "application/json; charset=UTF-8" self.content = generate_json_test(pattern, text)
try: result = urllib2.urlopen(url) except Exception, e: app.raise400('Invalid URL: %s' % e) base_url = os.path.dirname(url) text = feed.fix_urls(result.read(), base_url) self.content_type = "application/json; charset=UTF-8" self.content = generate_json_test(pattern, text) else: req.store.ensure_factory('feed', model_class=feed.Feed) f = req.store.load_one('feed', url_code=req.postpath[0]) if not(f): app.raise404() self.content_type = 'application/rss+xml; charset=UTF-8' self.content = f.to_xml(req) def get_content(self, req): """ @see: L{modu.web.resource.IResource.get_response()} """ return self.content def get_content_type(self, req): """ @see: L{modu.web.resource.IContent.get_content_type()} """ return self.content_type
def prepare_detail(self, req, itemdef): """ Handle creation and display of the detail page. @param req: the current request @type req: L{modu.web.app.Request} @param itemdef: the itemdef to use to generate the form @type itemdef: L{modu.editable.define.itemdef} """ self.template = itemdef.config.get("detail_template", "admin-detail.html.tmpl") self.set_slot("form", None) if len(req.postpath) > 2: item_id = req.postpath[2] table_name = itemdef.config.get("table", itemdef.name) if item_id == "new": if not (itemdef.creatable(req)): app.raise403("You are not allowed to delete `%s` records." % storable.get_table()) if "model_class" in itemdef.config: selected_item = itemdef.config["model_class"]() else: selected_item = storable.Storable(table_name) # Populate the new item if necessary query_data = form.parse_query_string(req) if "__init__" in query_data: for k, v in query_data["__init__"].items(): setattr(selected_item, k, v.value) # we can be sure the factory is there, because configure_store # already took care of it during prepare_content factory = req.store.get_factory(table_name) selected_item.set_factory(factory) else: try: selected_item = req.store.load_one(table_name, {"id": int(item_id)}) if selected_item is None: app.raise404("Couldn't load %s record #%s" % (table_name, item_id)) except ValueError: app.raise404("There is no detail view at the path: %s" % req["REQUEST_URI"]) frm = itemdef.get_form(req, selected_item) if "theme" in itemdef.config: frm.theme = itemdef.config["theme"] frm["referer"](type="hidden", value=req.get("HTTP_REFERER", "")) if frm.execute(req): # we regenerate the form because some fields don't know their # value until after the form is saved (e.g., postwrite fields) new_frm = itemdef.get_form(req, selected_item) # this is technically only necessary if errors are reported # on form items but they do not prevent completion (e.g., return False) new_frm.errors = frm.errors frm = new_frm else: # If we haven't submitted the form, errors should definitely be empty def error_formatter(req, field, err): return tags.a(href="#form-item-%s" % field)[err] frm.escalate_errors(req, formatter=error_formatter) template_variable_callback = itemdef.config.get("template_variable_callback") if callable(template_variable_callback): result = template_variable_callback(req, frm, selected_item) if isinstance(result, dict): for key, value in result.items(): self.set_slot(key, value) self.set_slot("form", frm) self.set_slot("theme", frm.get_theme(req)) self.set_slot("selected_item", selected_item) if "title_column" in itemdef.config and item_id != "new": item_name = "'%s'" % getattr(selected_item, itemdef.config["title_column"]) else: item_name = "#" + str(selected_item.get_id()) default_title = "Details for %s %s" % (itemdef.name.title(), item_name) custom_title = itemdef.config.get("detail_title", default_title) self.set_slot("title", tags.encode_htmlentities(custom_title)) else: app.raise404("There is no detail view at the path: %s" % req["REQUEST_URI"])
def prepare_content(self, req): """ @see: L{modu.web.resource.IContent.prepare_content()} """ req.content.report( "header", tags.style(type="text/css")["@import '%s';" % req.get_path("assets", "admin-styles.css")] ) site_stylesheet = getattr(req.app, "admin_site_stylesheet", None) if callable(site_stylesheet): site_stylesheet = site_stylesheet(req) if site_stylesheet: req.content.report("header", tags.style(type="text/css")["@import '%s';" % site_stylesheet]) user = req["modu.user"] if user and user.get_id(): if req.postpath and req.postpath[0] == "logout": req.session.set_user(None) if "auth_redirect" in req.session: del req.session["auth_redirect"] app.redirect(req.get_path(req.prepath)) itemdefs = define.get_itemdefs(module_list=self.options.get("itemdef_module", None)) # get_itemdef_layout adds some data and clones the itemdef self.itemdef_layout = define.get_itemdef_layout(req, itemdefs) # FIXME: This is inelegant -- we find the current itemdef # by using itemdefs.get(req.postpath[1]), so we recreate # the itemdef list from the itemdef layout to stop # malicious URL access. # TODO: Limit the itemdefs by user *first*, then modify # get_itemdef_layout() to organize, but not limit. if self.itemdef_layout: itemdefs = dict( [(itemdef.name, itemdef) for itemdef in reduce(lambda x, y: x + y, self.itemdef_layout.values())] ) else: itemdefs = {} self.set_slot("itemdef_layout", self.itemdef_layout) if len(req.postpath) > 1: if req.postpath[0] == "custom": itemdef_name = "/".join(req.postpath[1:]) if itemdef_name not in itemdefs: itemdef_name = req.postpath[1] else: itemdef_name = req.postpath[1] # we just need to select the right itemdef selected_itemdef = itemdefs.get(itemdef_name) self.set_slot("selected_itemdef", selected_itemdef) if selected_itemdef is not None: configure_store(req, selected_itemdef) if req.postpath[0] == "detail": self.prepare_detail(req, selected_itemdef) elif req.postpath[0] == "autocomplete": self.prepare_autocomplete(req, selected_itemdef) elif req.postpath[0] == "custom": self.prepare_custom(req, selected_itemdef) elif req.postpath[0] in ("listing", "export") and not selected_itemdef.config.get("hidden", False): self.prepare_listing(req, selected_itemdef) else: app.raise404() else: app.raise403() else: default_path = self.options.get("default_path") if callable(default_path): default_path = default_path(req) if default_path: redirect_path = req.get_path(default_path) app.redirect(redirect_path) app.raise404("There is no item list at the path: %s" % req["REQUEST_URI"]) else: self.set_slot("itemdef_layout", None) self.set_slot("selected_itemdef", None) self.prepare_login(req)