def data(self): """Return all the data for this model (including dependencies) in a structured dict """ empty_template = '{}' param_map = {PATH: 'Path', GET: 'GET', POST: 'POST', COOKIE: 'Cookie'} url = QUrl(self.transition.url) path_dict = path_to_dict(url.path()) get_keys, post_keys = set(), set() output = {} if self.override: output['variables'] = [] for param_type, key, template, parent_model in self.override: parent = parent_model if isinstance( parent_model, basestring) else parent_model.data() output['variables'].append({ 'key': key, 'origin': param_map[param_type], 'source': parent, 'template': template, }) if param_type == PATH: path_dict[key] = empty_template elif param_type == GET: get_keys.add(key) elif param_type == POST: post_keys.add(key) url.setPath(dict_to_path(path_dict)) qs_items = [(key, empty_template if key in get_keys else value) for (key, value) in url.queryItems() if (GET, key) not in self.ignored] url.setEncodedQueryItems(qs_items) output['url'] = url.toString() data = dict([(key, empty_template if key in post_keys else value) for (key, value) in self.transition.data if (POST, key) not in self.ignored]) if data: output['data'] = data #if self.ignored: # output['ignored'] = [(param_map[param_type], value) for (param_type, value) in self.ignored] if self.selector is not None: output['columns'] = {'data': str(self.selector)} elif self.columns is not None: output['columns'] = { field: str(selector) for (field, selector) in self.columns.items() } output['headers'] = [(str(key), str(value)) for (key, value) in self.transition.request_headers if str(key).lower() not in ('content-length', )] output['verb'] = self.transition.verb return output
def encode_data(items, content_type): """Convert these querystring items into a string of data """ if items: if 'json' in content_type: result = json.dumps(dict(items)) else: url = QUrl('') url.setEncodedQueryItems(items) result = str(url.toString())[1:] else: result = '' return result
def gen_request(transition, override_params=None, ignored=None): """Generate a request modifying the transitions for this model with the provided parameters Parameters ---------- transition: Transition. A specific transition to use rather than the first one for this model override_params: a list of (param_type, key, value) pairs to override the parameters for this transition ignored: list of (param_type, key) a list of (param_type, key) pairs of parameters that can be left out Returns ------- dictionary with data sufficient for sending a query in the web browser """ override_params = override_params or [] ignored = ignored or [] url = QUrl(transition.url) get_dict = dict([(key, value) for (param_type, key, value) in override_params if param_type == GET]) post_dict = dict([(key, value) for (param_type, key, value) in override_params if param_type == POST]) path_dict = path_to_dict(transition.path) for param_type, key, value in override_params: if param_type == PATH: path_dict[key] = value url.setPath(dict_to_path(path_dict)) qs_items = transition.qs data_items = transition.data qs_items = [(key, urllib.quote_plus(get_dict[key].encode('utf-8')) if key in get_dict else value) for (key, value) in qs_items if (GET, key) not in ignored] url.setEncodedQueryItems(qs_items) data_items = [(key, post_dict[key] if key in post_dict else value) for (key, value) in data_items if (POST, key) not in ignored] return dict(url=url, headers=transition.request_headers, data=encode_data(data_items, transition.content_type))