def load_body(self): # Case 1: nothing body = self.soup_message.get_body() if not body: return {} # Case 2: urlencoded type, type_parameters = self.get_header('content-type') if type == 'application/x-www-form-urlencoded': return decode_query(body) # Case 3: multipart if type.startswith('multipart/'): boundary = type_parameters.get('boundary') boundary = '--%s' % boundary form = {} for part in body.split(boundary)[1:-1]: if part.startswith('\r\n'): part = part[2:] elif part.startswith('\n'): part = part[1:] # Parse the entity entity = Entity() entity.load_state_from_string(part) # Find out the parameter name header = entity.get_header('Content-Disposition') value, header_parameters = header name = header_parameters['name'] # Load the value body = entity.get_body() if 'filename' in header_parameters: filename = header_parameters['filename'] if filename: # Strip the path (for IE). filename = filename.split('\\')[-1] # Default content-type, see # http://tools.ietf.org/html/rfc2045#section-5.2 if entity.has_header('content-type'): mimetype = entity.get_header('content-type')[0] else: mimetype = 'text/plain' form[name] = filename, mimetype, body else: if name not in form: form[name] = body else: if isinstance(form[name], list): form[name].append(body) else: form[name] = [form[name], body] return form # Case 4: ? return {'body': body}
def init_context(self): soup_message = self.soup_message path = self.path # The request method self.method = soup_message.get_method() # The query query = soup_message.get_query() self.query = decode_query(query) # The URI as it was typed by the client xfp = soup_message.get_header('X_FORWARDED_PROTO') src_scheme = xfp or 'http' xff = soup_message.get_header('X-Forwarded-Host') if xff: xff = xff.split(',', 1)[0].strip() hostname = soup_message.get_host() src_host = xff or soup_message.get_header('Host') or hostname if query: uri = '%s://%s%s?%s' % (src_scheme, src_host, path, query) else: uri = '%s://%s%s' % (src_scheme, src_host, path) self.uri = get_reference(uri) # Split the path into path and method ("a/b/c/;view") path = path if type(path) is Path else Path(path) name = path.get_name() if name and name[0] == ';': self.path = path[:-1] self.view_name = name[1:] else: self.path = path self.view_name = None # Cookies self.cookies = {} # Media files (CSS, javascript) # Set the list of needed resources. The method we are going to # call may need external resources to be rendered properly, for # example it could need an style sheet or a javascript file to # be included in the html head (which it can not control). This # attribute lets the interface to add those resources. self.styles = [] self.scripts = [] # The authenticated user self.authenticate() # The Site Root self.find_site_root() self.site_root.before_traverse(self) # Hook
def tabs(self): context = self.context user = context.user if user is None: return [] excluded_roles = self.tabs_hidden_roles if excluded_roles: user_name = user.name site_root = context.site_root for role in excluded_roles: if site_root.has_user_role(user_name, role): return [] # Build tabs (Same that upper class) # Get resource & access control context = self.context here = context.resource here_link = context.get_link(here) # Tabs tabs = [] for link, view in here.get_views(): active = False # ACL if view.access != 'is_allowed_to_view': continue # From method?param1=value1¶m2=value2&... # we separate method and arguments, then we get a dict with # the arguments and the subview active state if '?' in link: name, args = link.split('?') args = decode_query(args) else: name, args = link, {} # Active if context.view == here.get_view(name, args): active = True # Add the menu tabs.append({ 'name': '%s/;%s' % (here_link, link), 'label': view.get_title(context), 'active': active, 'class': active and 'active' or None}) # Do not show if only one item if self.tabs_hide_if_only_one_item and len(tabs) == 1: return [] return tabs
def _get_action(self, resource, context): """Default function to retrieve the name of the action from a form """ form = context.get_form() action = form.get('action') if action is None: context.form_action = 'action' return action = 'action_%s' % action # Save the query of the action into context.form_query if '?' in action: action, query = action.split('?') # Deserialize query using action specific schema schema = getattr(self, '%s_query_schema' % action, None) context.form_query = decode_query(query, schema) context.form_action = action
def get_form(cls, context): """Default function to retrieve the name of the action from a form """ # 1. Get the action name form = context.get_form() action = form.get('action') action = ('action_%s' % action) if action else 'action' # 2. Check whether the action has a query if '?' in action: action, query = action.split('?') # Deserialize query using action specific schema schema = getattr(context.view, '%s_query_schema' % action, None) context.form_query = decode_query(query, schema) # 3. Save the action name (used by get_schema) context.form_action = action # (2) Automatically validate and get the form input (from the schema). context.form = context.view._get_form(context.resource, context)
def get_action(self, resource, context): """Default function to retrieve the name of the action from a form """ # 1. Get the action name form = context.get_form() action = form.get('action') action = ('action_%s' % action) if action else 'action' # 2. Check whether the action has a query if '?' in action: action, query = action.split('?') # Deserialize query using action specific schema schema = getattr(self, '%s_query_schema' % action, None) context.form_query = decode_query(query, schema) # 3. Save the action name (used by get_schema) context.form_action = action # 4. Return the method return getattr(self, action, None)
def tabs(self): """Return tabs and subtabs as a dict {tabs, subtabs} of list of dicts [{name, label, active, style}...]. """ # Get resource & access control context = self.context if context.user is None: return [] here = context.resource here_link = context.get_link(here) # Fix link at root if here_link == '/': here_link = '' # Tabs tabs = [] for link, view in here.get_views(): # From method?param1=value1¶m2=value2&... # we separate method and arguments, then we get a dict with # the arguments and the subview active state if '?' in link: name, args = link.split('?') args = decode_query(args) else: name, args = link, {} # Active unbound_view = context.view.__bases__[0] active = (unbound_view is here.get_view(name, args).__bases__[0]) # Add the menu tabs.append({ 'name': '%s/;%s' % (here_link, link), 'label': view.get_title(context), 'active': active, 'class': 'active' if active else None }) return tabs
def tabs(self): """Return tabs and subtabs as a dict {tabs, subtabs} of list of dicts [{name, label, active, style}...]. """ # Get resource & access control context = self.context if context.user is None: return [] here = context.resource here_link = context.get_link(here) # Fix link at root if here_link == '/': here_link = '' # Tabs tabs = [] for link, view in here.get_views(): # From method?param1=value1¶m2=value2&... # we separate method and arguments, then we get a dict with # the arguments and the subview active state if '?' in link: name, args = link.split('?') args = decode_query(args) else: name, args = link, {} # Active unbound_view = context.view.__bases__[0] active = (unbound_view is here.get_view(name, args).__bases__[0]) # Add the menu tabs.append({ 'name': '%s/;%s' % (here_link, link), 'label': view.get_title(context), 'active': active, 'class': 'active' if active else None}) return tabs
def test_query(self): input = 'a&a' output = encode_query(decode_query(input)) self.assertEqual(output, input)
def tabs(self): context = self.context user = context.user if user is None: return [] # Do not show tabs on some site root views nav = (context.site_root.class_control_panel + [ 'control_panel', 'contact', 'site_search', 'website_new_resource' ]) if (context.site_root == context.resource and context.view_name in nav): return [] # Build tabs (Same that upper class) # Get resource & access control context = self.context here = context.resource here_link = context.get_link(here) is_folder = isinstance(here, Folder) # Tabs tabs = [] for link, view in here.get_views(): active = False # From method?param1=value1¶m2=value2&... # we separate method and arguments, then we get a dict with # the arguments and the subview active state if '?' in link: name, args = link.split('?') args = decode_query(args) else: name, args = link, {} # Active if context.view == here.get_view(name, args): active = True # Ignore new_resource if link == 'new_resource': continue # Add the menu tabs.append({ 'name': '%s/;%s' % (here_link, link), 'icon': getattr(view, 'adminbar_icon', None), 'rel': getattr(view, 'adminbar_rel', None), 'label': view.get_title(context), 'active': active, 'class': active and 'active' or None }) # New resources if is_folder: document_types = here.get_document_types() if len(document_types) > 0: active = context.view_name == 'new_resource' href = './;new_resource' label = MSG(u'Add content') if len(document_types) == 1: href += '?type=' + document_types[0].class_id class_title = document_types[0].class_title label = MSG(u"Add '{x}'").gettext(x=class_title) tabs.append({ 'name': href, 'label': label, 'icon': 'page-white-add', 'rel': None, 'class': active and 'active' or None }) return tabs
def init_from_environ(self, environ, user=None): from server import get_server # Set environ self.environ = environ path = environ.get('PATH_INFO') self.path = path self.header_response = [] self.content_type = None self.status = None # Get database server = get_server() self.server = server self.database = server.database # Root self.root = self.database.get_resource('/') # The request method self.method = environ.get('REQUEST_METHOD') # Get body self.body = self.get_body_from_environ() # The query query = environ.get('QUERY_STRING') self.query = decode_query(query) # Accept language accept_language = self.environ.get('HTTP_ACCEPT_LANGUAGE', '') if accept_language is None: accept_language = '' try: self.accept_language = AcceptLanguageType.decode(accept_language) except: # Cannot decode accept language pass # The URI as it was typed by the client xfp = environ.get('HTTP_X_FORWARDED_PROTO') src_scheme = xfp or 'http' xff = environ.get('HTTP_X-Forwarded-Host') if xff: xff = xff.split(',', 1)[0].strip() src_host = xff or environ.get('HTTP_HOST') if query: uri = '%s://%s%s?%s' % (src_scheme, src_host, path, query) else: uri = '%s://%s%s' % (src_scheme, src_host, path) self.uri = get_reference(uri) # Split the path into path and method ("a/b/c/;view") path = path if type(path) is Path else Path(path) name = path.get_name() if name and name[0] == ';': self.path = path[:-1] self.view_name = name[1:] else: self.path = path self.view_name = None # Cookies self.cookies = {} # Media files (CSS, javascript) # Set the list of needed resources. The method we are going to # call may need external resources to be rendered properly, for # example it could need an style sheet or a javascript file to # be included in the html head (which it can not control). This # attribute lets the interface to add those resources. self.styles = [] self.scripts = [] # Log user if user is given if user: self.login(user) # The authenticated user self.authenticate() # Search self._context_user_search = self._user_search(self.user) # The Site Root self.find_site_root() self.site_root.before_traverse(self) # Hook # Not a cron self.is_cron = False # Set header self.set_header('Server', 'ikaaro.web')
def get_form_body(self, body): return decode_query(body)
def init_from_environ(self, environ, user=None): from server import get_server # Set environ self.environ = environ path = environ.get('PATH_INFO') self.path = path self.header_response = [] self.content_type = None self.status = None # Get database server = get_server() self.server = server self.database = server.get_database() # Root self.root = self.database.get_resource('/') # The request method self.method = environ.get('REQUEST_METHOD') # Get body self.body = self.get_body_from_environ() # The query query = environ.get('QUERY_STRING') self.query = decode_query(query) # Accept language accept_language = self.environ.get('HTTP_ACCEPT_LANGUAGE', '') if accept_language is None: accept_language = '' try: self.accept_language = AcceptLanguageType.decode(accept_language) except: # Cannot decode accept language pass # The URI as it was typed by the client xfp = environ.get('HTTP_X_FORWARDED_PROTO') src_scheme = xfp or 'http' xff = environ.get('HTTP_X-Forwarded-Host') if xff: xff = xff.split(',', 1)[0].strip() src_host = xff or environ.get('HTTP_HOST') if query: uri = '%s://%s%s?%s' % (src_scheme, src_host, path, query) else: uri = '%s://%s%s' % (src_scheme, src_host, path) self.uri = get_reference(uri) # Split the path into path and method ("a/b/c/;view") path = path if type(path) is Path else Path(path) name = path.get_name() if name and name[0] == ';': self.path = path[:-1] self.view_name = name[1:] else: self.path = path self.view_name = None # Cookies self.cookies = {} # Media files (CSS, javascript) # Set the list of needed resources. The method we are going to # call may need external resources to be rendered properly, for # example it could need an style sheet or a javascript file to # be included in the html head (which it can not control). This # attribute lets the interface to add those resources. self.styles = [] self.scripts = [] # Log user if user is given if user: self.login(user) # The authenticated user self.authenticate() # Search self._context_user_search = self._user_search(self.user) # The Site Root self.find_site_root() self.site_root.before_traverse(self) # Hook # Not a cron self.is_cron = False # Set header self.set_header('Server', 'ikaaro.web')
def tabs(self): context = self.context user = context.user if user is None: return [] # Do not show tabs on some site root views nav = (context.site_root.class_control_panel + ['control_panel', 'contact', 'site_search', 'website_new_resource']) if (context.site_root == context.resource and context.view_name in nav): return [] # Build tabs (Same that upper class) # Get resource & access control context = self.context here = context.resource here_link = context.get_link(here) is_folder = isinstance(here, Folder) # Tabs tabs = [] for link, view in here.get_views(): active = False # From method?param1=value1¶m2=value2&... # we separate method and arguments, then we get a dict with # the arguments and the subview active state if '?' in link: name, args = link.split('?') args = decode_query(args) else: name, args = link, {} # Active if context.view == here.get_view(name, args): active = True # Ignore new_resource if link == 'new_resource': continue # Add the menu tabs.append({ 'name': '%s/;%s' % (here_link, link), 'icon': getattr(view, 'adminbar_icon', None), 'rel': getattr(view, 'adminbar_rel', None), 'label': view.get_title(context), 'active': active, 'class': active and 'active' or None}) # New resources if is_folder: document_types = here.get_document_types() if len(document_types) > 0: active = context.view_name == 'new_resource' href = './;new_resource' label = MSG(u'Add content') if len(document_types) == 1: href += '?type='+ document_types[0].class_id class_title = document_types[0].class_title label = MSG(u"Add '{x}'").gettext(x=class_title) tabs.append({'name': href, 'label': label, 'icon': 'page-white-add', 'rel': None, 'class': active and 'active' or None}) return tabs
def tabs(self): context = self.context user = context.user if user is None: return [] # Do not show tabs on some site root views nav = (context.site_root.class_control_panel + ['control_panel', 'contact', 'site_search', 'website_new_resource']) if (context.site_root == context.resource and context.view_name in nav): return [] # Build tabs (Same that upper class) # Get resource & access control context = self.context here = context.resource here_link = context.get_link(here) is_folder = isinstance(here, Folder) # Tabs tabs = [] for link, view in here.get_views(): active = False # From method?param1=value1¶m2=value2&... # we separate method and arguments, then we get a dict with # the arguments and the subview active state if '?' in link: name, args = link.split('?') args = decode_query(args) else: name, args = link, {} # Active if context.view == here.get_view(name, args): active = True # Add the menu tabs.append({ 'name': '%s/;%s' % (here_link, link), 'icon': getattr(view, 'adminbar_icon', None), 'rel': getattr(view, 'adminbar_rel', None), 'label': view.get_title(context), 'active': active, 'class': active and 'active' or None}) # Manage content active = context.view_name == 'manage_content' if is_folder: icon = 'folder-explore' name = './;manage_content' else: icon = 'folder-back' name = '../;manage_content' tabs.append({'name': name, 'label': MSG(u'Navigation'), 'rel': 'popup', 'icon': icon, 'class': active and 'active' or None}) # New resources if is_folder: if len(here.get_document_types()) > 0: active = context.view_name == 'new_resource' tabs.append({'name': './;new_resource', 'label': MSG(u'Add content'), 'icon': 'page-white-add', 'rel': None, 'class': active and 'active' or None}) return tabs