def encoded_url(self, *args, **kw): if args: result = Href(args[0]) return result(*args[1:], **kw) else: result = Href() return result(**kw)[2:]
def make_database_for_trial(self, period, max_users_limit, database_name=None, installing_modules=None, saas_template_id=None, **kw): params = { "max_users_limit": max_users_limit, "period": period, "installing_modules": installing_modules or "", "saas_template_id": saas_template_id or "", } assert not(saas_template_id and installing_modules), "Both saas_template_id and installing_modules given" if request.env.user == request.env.ref("base.public_user"): return werkzeug.utils.redirect(Href("/web/signup")(params)) build = request.env["saas.db"].search([ ("type", "=", "build"), ("state", "=", "draft"), ("admin_user", "=", request.env.user.id), ], order='id DESC', limit=1) if not build: return request.redirect(Href("/my/builds/create")({ "redirect": request.httprequest.full_path })) request.env["contract.contract"].with_user(SUPERUSER_ID)._create_saas_contract_for_trial( build, max_users_limit, period, installing_modules=(installing_modules or "").split(","), saas_template_id=saas_template_id ) return request.redirect("/my/build/{}".format(build.id))
def __init__(self, environ, populate_request=True, shallow=False): ResponseBase.__init__(self) RequestBase.__init__(self, environ, populate_request, shallow) self.href = Href(self.script_root or '/', self.charset) self.abs_href = Href(self.url_root, self.charset) self.headers = Headers([('Content-Type', 'text/html')]) self.response = [] self.status_code = 200
def col_sortable_filter(context, title, is_alpha=False, id=None): id = title if id is None else id id = id.lower() # when sorting for numbers, we're interested in large numbers first next_sort = 'asc' if is_alpha else 'desc' sort_id, sort_order = context.parent.get('sort', 'a_b').split('_') classes = ['sortable'] if sort_id.lower() == id: sort_class = 'sort_' + sort_order next_sort = 'desc' if sort_order == 'asc' else 'asc' if is_alpha: sort_class += '_alpha' classes.append(sort_class) next_sort = id + '_' + next_sort # replace/set ?sort= in URL args = request.args.copy() args['sort'] = next_sort url = Href(request.base_url, sort=True) return '<a href="%s" class="%s">%s</a>' % (url(args), ' '.join(classes), title)
def test_sub_category_selection(self): url = "/g-cloud/search?q=&lot=cloud-software&otherfilter=somevalue&checkboxTreeExample=option+2.2" with self.app.test_request_context(url): selection = build_lots_and_categories_link_tree( self.framework, self.framework['lots'], self.category_filter_group, flask.request, flask.request.args, g9_builder, 'services', 'g-cloud-9', Href(flask.url_for('.search_services')), self.search_api_client ) assert len(selection) == 5 # all -> software -> option2 -> option2.2; option2 as a parent category filter tree_root = selection[0] # check that only siblings of the selected sub-category are shown, and other categories # have been removed lot_filters = tree_root['children'] selected_lot = next(f for f in lot_filters if f['selected']) category_filters = selected_lot['children'] assert len(category_filters) == 1 assert category_filters[0]['selected'] sub_category_filters = category_filters[0]['children'] assert len(sub_category_filters) == 2 assert [f for f in selection if f.get('name') == 'parentCategory'] == [ {'name': 'parentCategory', 'value': 'option 2'}]
def get_request_url_without_any_filters(request, filters, view_name, **kwargs): """ This function will returns the url path without any filters. It will still retain the categories, keyword and lots parameters as well as any others included. Args: request: Request Object (import from flask) of the current request. filters: list of all filters from digital marketplace framework Returns: URL path in string format. """ all_request_filters = MultiDict(request.args.copy()) for section in filters: for _filter in filters[section]['filters']: all_request_filters.poplist(_filter['name']) all_request_filters.poplist('page') all_request_filters.poplist('live-results') search_link_builder = Href(url_for('.{}'.format(view_name), **kwargs)) url = search_link_builder(all_request_filters) return url
def compose_task_report(task): html = HTMLBuilder('html') href = Href('http://localhost:5000') task['log'] = html.a('LOG', href=href(task['log'])) return task
def current_url(self, **kwargs): curl = current_url(self.grid.manager, strip_querystring=True, strip_host=True) href = Href(curl, sort=True) req_args = MultiDict(self.grid.manager.request_args()) # kwargs will be modified with new keys if there is a prefix, so copy the original set # of keys first. Otherwise, the loop may pick up new keys and apply the prefix again key_list = list(kwargs.keys()) for key in key_list: # arg keys may need to be prefixed if self.grid.qs_prefix: prefixed_key = self.grid.qs_prefix + key kwargs[prefixed_key] = kwargs[key] del kwargs[key] key = prefixed_key # multidicts extend, not replace, so we need to get rid of the # keys first try: del req_args[key] except KeyError: pass # convert to md first so that if we have lists in the kwargs, they # are converted appropriately req_args.update(MultiDict(kwargs)) return href(req_args)
def send_mail_spam(email, subject, template, spam, **kwargs): """ :param email: instance <Email> :param subject: Title Email :param template: directory template :param spam: instance <Spam> :param kwargs: :return: None """ _SENDER = current_app.config['MAIL_SENDER'] _DDNS = current_app.config['DDNS'] template = os.path.basename(template) msg = Message(subject, sender=_SENDER, recipients=[email.email]) href = Href(_DDNS) _url_coupon = href('/redirect', em=str(email.id), sp=str(spam.id)) msg.html = render_template(template, url_coupon=_url_coupon, **kwargs) app = current_app._get_current_object() try: thr = Thread(target=send_ascync_email, args=[app, msg]) thr.start() except SMTPException: spam.fail = spam.fail + 1 spam.success = spam.success + 1 db.session.add(spam) db.session.commit()
def login(user_info): user = User.create_or_update(user_info) token = create_jwt(identity=user.id) redirect_url = current_app.config.get('AUTH_REDIRECT_URL', None) if redirect_url is None: return token return redirect(Href(redirect_url)(token=token))
def set_link(self, page, per_page, total): """Extends set_link for convenience, adds rel, prev, self, next, and last when applicable Keyword arguments: page -- current page per_page -- items per page total -- total results """ links = [] total_pages = int(ceil(float(total) / per_page)) args = self.request.args.copy() href = Href(self.request.base_url) # self link l = supermod.linkType() l.rel = 'self' f = {k: v for k, v in args.items() if k not in ['_count', 'page']} l.href = href(f) links.append(l) if total_pages > 1: # first link if page > 1: args['page'] = 1 l = supermod.linkType() l.rel = 'first' l.href = href(args) links.append(l) # last link if page < total_pages: args['page'] = total_pages l = supermod.linkType() l.rel = 'last' l.href = href(args) links.append(l) # next link if page < total_pages - 1: # last link already generated args['page'] = page + 1 l = supermod.linkType() l.rel = 'next' l.href = href(args) links.append(l) # prev link if page > 2: args['page'] = page - 1 l = supermod.linkType() l.rel = 'previous' l.href = href(args) links.append(l) super(Bundle, self).set_link(links)
def slice_url(self): try: d = json.loads(self.params) except Exception as e: d = {} from werkzeug.urls import Href href = Href( "/panoramix/datasource/{self.datasource_type}/" "{self.datasource_id}/".format(self=self)) return href(d)
def test_href_past_root(): """Href() over root does not break the URL.""" raise SkipTest('currently not implemented, stdlib bug?') base_href = Href('http://www.blagga.com/1/2/3') assert base_href('../foo') == 'http://www.blagga.com/1/2/foo' assert base_href('../../foo') == 'http://www.blagga.com/1/foo' assert base_href('../../../foo') == 'http://www.blagga.com/foo' assert base_href('../../../../foo') == 'http://www.blagga.com/foo' assert base_href('../../../../../foo') == 'http://www.blagga.com/foo' assert base_href('../../../../../../foo') == 'http://www.blagga.com/foo'
def slice_url(self): try: slice_params = json.loads(self.params) except Exception as e: slice_params = {} slice_params['slice_id'] = self.id slice_params['slice_name'] = self.slice_name from werkzeug.urls import Href href = Href("/panoramix/explore/{self.datasource_type}/" "{self.datasource_id}/".format(self=self)) return href(slice_params)
def get_url(self, **kwargs): d = self.orig_form_data.copy() if 'action' in d: del d['action'] d.update(kwargs) # Remove unchecked checkboxes because HTML is weird like that for key in d.keys(): if d[key] == False: del d[key] href = Href('/panoramix/explore/{self.datasource.type}/' '{self.datasource.id}/'.format(**locals())) return href(d)
def slice_url(self): """Defines the url to access the slice""" try: slice_params = json.loads(self.params) except Exception as e: logging.exception(e) slice_params = {} slice_params['slice_id'] = self.id slice_params['slice_name'] = self.slice_name from werkzeug.urls import Href href = Href("/caravel/explore/{self.datasource_type}/" "{self.datasource_id}/".format(self=self)) return href(slice_params)
def test_build_lots_and_categories_link_tree_with_lot_and_filter_and_same_named_filter_in_tree(self): url = "/g-cloud/search?lot=cloud-software&checkboxTreeExample=option+2.1&parentCategory=option+2" self.category_filter_group['filters'] += [ { 'label': 'Option 7', 'name': 'checkboxTreeExample', 'id': 'checkboxTreeExample-option-7', 'value': 'option 7', 'children': [ { 'label': 'Option 2.1', 'name': 'checkboxTreeExample', 'id': 'checkboxTreeExample-option-2.1', 'value': 'option 2.1' }, { 'label': 'Option 7.1', 'name': 'checkboxTreeExample', 'id': 'checkboxTreeExample-option-7.1', 'value': 'option 7.1' } ] } ] with self.app.test_request_context(url): tree = build_lots_and_categories_link_tree( self.framework, self.framework['lots'], self.category_filter_group.copy(), flask.request, flask.request.args, g9_builder, 'services', 'g-cloud-9', Href(flask.url_for('.search_services')), self.search_api_client ) assert tree == self.tree_with_cloud_software__option_2__option_2_1__selected assert( [(child['id'], child['selected']) for child in tree[0]['children'][0]['children'][0]['children']] == [('checkboxTreeExample-option-2.1', True), ('checkboxTreeExample-option-2.2', False)] ) tree_child_value_lists = [[child.get('id') for child in node.get('children', [])] for node in tree] tree_child_values = [item for sublist in tree_child_value_lists for item in sublist] assert 'checkboxTreeExample-option-7.1' not in tree_child_values assert tree_child_values.count('checkboxTreeExample-option-2.1') == 1
def test_href(): """Test the Href class""" x = Href('http://www.example.com/') assert x('foo') == 'http://www.example.com/foo' assert x.foo('bar') == 'http://www.example.com/foo/bar' assert x.foo('bar', x=42) == 'http://www.example.com/foo/bar?x=42' assert x.foo('bar', class_=42) == 'http://www.example.com/foo/bar?class=42' assert x.foo('bar', {'class': 42}) == 'http://www.example.com/foo/bar?class=42' assert_raises(AttributeError, lambda: x.__blah__) x = Href('blah') assert x.foo('bar') == 'blah/foo/bar' assert_raises(TypeError, x.foo, {"foo": 23}, x=42) x = Href('') assert x('foo') == 'foo'
def get_url(self, **kwargs): """Returns the URL for the viz""" d = self.orig_form_data.copy() if 'json' in d: del d['json'] if 'action' in d: del d['action'] d.update(kwargs) # Remove unchecked checkboxes because HTML is weird like that for key in d.keys(): if d[key] is False: del d[key] href = Href('/caravel/explore/{self.datasource.type}/' '{self.datasource.id}/'.format(**locals())) return href(d)
def paged_response(query, args, serialize_fn, maximum_limit=None): """ Paginate a query and generate a response object. :param query: A SQLAlchemy query object :param serialize_fn: A callable responsible for serializing the response :param args: Parsed request arguments :param maximum_limit: The maximum page size allowed """ # We don't use the paginate() method from flask-sqlalchemy because it # does not allow the more flexible limit/offset api. limit = args['limit'] offset = args['offset'] total = query.count() href = Href(request.base_url) if offset > 0 and offset >= total: # This is the behavior of flask-sqlalchemy pagination, but we could instead # return an empty list. abort(404) if maximum_limit and limit > maximum_limit: # throw a validation error if the limit is larger than the maximum limit = validate.Range(min=1, max=maximum_limit)(limit) response = jsonify(serialize_fn(query.limit(limit).offset(offset))) response.headers['Resource-Count'] = total # generate link header links = [] first_page = dict(args, limit=limit, offset=0) links.append('<%s>; rel="first"' % href(**first_page)) last_page = dict(args, limit=limit, offset=max(0, limit * ((total - 1) // limit))) links.append('<%s>; rel="last"' % href(**last_page)) if offset - limit >= 0: previous_page = dict(args, limit=limit, offset=(offset - limit)) links.append('<%s>; rel="prev"' % href(**previous_page)) if offset + limit < total: next_page = dict(args, limit=limit, offset=(offset + limit)) links.append('<%s>; rel="next"' % href(**next_page)) response.headers['Link'] = ', '.join(links) return response
def query_update(**kwargs): """Update the query string with new values. This is useful, for example, for updating the pagination for a search query. :param kwargs: items to add to the query :return: path with updated query """ q = request.args.copy() # can't use update since that appends values to the multi-dict instead of replacing for key, value in kwargs.items(): q[key] = value return Href(request.path)(q)
def to_url(self, url=None, replace=False, **kwargs): '''Serialize the query into an URL''' params = copy.deepcopy(self.kwargs) if kwargs: params.pop('page', None) for key, value in kwargs.items(): if not replace and key in params: if not isinstance(params[key], (list, tuple)): params[key] = [params[key], value] else: params[key].append(value) else: params[key] = value params.pop('facets', None) # Always true when used href = Href(url or request.base_url) return href(params)
def test_build_lots_and_categories_link_tree_with_no_categories_or_filters(self): url = "/g-cloud/search" with self.app.test_request_context(url): tree = build_lots_and_categories_link_tree( self.framework, self.framework['lots'], self.category_filter_group, flask.request, flask.request.args, g9_builder, 'services', 'g-cloud-9', Href(flask.url_for('.search_services')), self.search_api_client ) assert tree == [ { 'children': [ { 'label': 'Cloud hosting', 'link': '/g-cloud/search?lot=cloud-hosting', 'name': 'lot', 'service_count': 500, 'value': 'cloud-hosting' }, { 'label': 'Cloud software', 'link': '/g-cloud/search?lot=cloud-software', 'name': 'lot', 'service_count': 500, 'value': 'cloud-software' }, { 'label': 'Cloud support', 'link': '/g-cloud/search?lot=cloud-support', 'name': 'lot', 'service_count': 500, 'value': 'cloud-support' } ], 'label': 'All categories', 'link': '/g-cloud/search', 'selected': True } ]
def pins(): """Renders the /pins page template. Returns: TYPE: Description """ username = current_user.username limit = request.args.get('limit', 10) page = int(request.args.get('page', 1)) offset = (page - 1) * limit href = Href(url_for('pins')) next_url = href({'page': page + 1}) return render_template( 'pins.html', username=username, next_url=next_url, pins=Pin.query_all().fetch(limit, offset=offset))
def test_top_level_category_selection(self): url = "/g-cloud/search?q=&lot=cloud-software&otherfilter=somevalue&filterExample=option+1" \ "&checkboxTreeExample=option+1&page=2" with self.app.test_request_context(url): selection = build_lots_and_categories_link_tree( self.framework, self.framework['lots'], self.category_filter_group, flask.request, flask.request.args, g9_builder, 'services', 'g-cloud-9', Href(flask.url_for('.search_services')), self.search_api_client ) assert len(selection) == 3 # all -> software -> option1 tree_root = selection[0] assert tree_root.get('label') == 'All categories' lot_filters = tree_root['children'] selected_lot = next(f for f in lot_filters if f['selected']) assert selected_lot.get('label') == 'Cloud software' # there should be a link to the lot without a category, as a category has been selected within it assert 'lot=cloud-software' in selected_lot['link'] assert 'checkboxTreeExample' not in selected_lot['link'] # check that we have links in place to a search with the relevant category filter applied, # except to the currently-selected category category_filters = selected_lot['children'] assert 'link' not in category_filters[0] assert not category_filters[0].get('children') assert 'checkboxTreeExample=option+2' in category_filters[1]['link'] sub_category_filters = category_filters[1]['children'] assert 'checkboxTreeExample=option+2.1' in sub_category_filters[0]['link'] assert 'checkboxTreeExample=option+2.2' in sub_category_filters[1]['link'] # ...and also that each link preserves only the values of filters that are valid for the target... for f in itertools.chain(category_filters, sub_category_filters): if f.get('link'): assert 'filterExample=option+1' in f['link'] assert 'otherfilter=somevalue' not in f['link'] assert 'page=' not in f['link']
def to_url(self, url=None, replace=False, **kwargs): '''Serialize the query into an URL''' params = copy.deepcopy(self.filter_values) if self._query: params['q'] = self._query if self.page_size != DEFAULT_PAGE_SIZE: params['page_size'] = self.page_size if kwargs: for key, value in kwargs.items(): if not replace and key in params: if not isinstance(params[key], (list, tuple)): params[key] = [params[key], value] else: params[key].append(value) else: params[key] = value else: params['page'] = self.page href = Href(url or request.base_url) return href(params)
def get_channel_pins(channel_id, token): """Fetches all pinned messages from a particular channel. Args: channel_id (str): Slack channel ID. token (str): Slack user token. Returns: str: Response as a JSON string. Raises: Exception: Description """ href = Href('https://slack.com/api/pins.list') url = href({'channel': channel_id, 'token': token}) result = urlfetch.fetch(url) if result.status_code == 200: return json.loads(result.content) raise Exception('resultz {} {}'.format(result.status_code, result.content))
def test_build_lots_and_categories_link_tree_with_lot_and_filter_selected(self, url): with self.app.test_request_context(url): tree = build_lots_and_categories_link_tree( self.framework, self.framework['lots'], self.category_filter_group.copy(), flask.request, flask.request.args, g9_builder, 'services', 'g-cloud-9', Href(flask.url_for('.search_services')), self.search_api_client ) assert tree == self.tree_with_cloud_software__option_2__option_2_1__selected # Assert, as per the url, that only our specified filter is 'selected' in the output assert( [(child['id'], child['selected']) for child in tree[0]['children'][0]['children'][0]['children']] == [('checkboxTreeExample-option-2.1', True), ('checkboxTreeExample-option-2.2', False)] )
def test_getting_cached_images_instead_computing(self): env = api.Environment(self.registry.test_cr, self.uid, {}) record = env.ref("product.product_product_9").product_tmpl_id href = Href("/web/image") field = "image_1920" model = record._name width = 300 height = 300 url = href({ "model": model, "id": record.id, "field": field, "width": width, "height": height, }) self.authenticate("demo", "demo") response = self.url_open(url, timeout=30) self.assertEqual(response.status_code, 200) # Attachments must be created during the execution of requests that are written above. attachment = (env["ir.attachment"].sudo().search([ ("res_field", "=", field), ("res_model", "=", model), ("res_id", "=", record.id), ])) attachment.ensure_one() resized = attachment.get_resized_from_cache(width, height, crop=False) resized_attachment = env["ir.attachment"].browse( resized.resized_attachment_id.id) resized_attachment.check("read")
def current_url(root_only=False, host_only=False, strip_querystring=False, strip_host=False, https=None, environ=None, qs_replace=None, qs_update=None): """ Returns strings based on the current URL. Assume a request with path: /news/list?param=foo to an application mounted at: http://localhost:8080/script Then: :param root_only: set `True` if you only want the root URL. http://localhost:8080/script/ :param host_only: set `True` if you only want the scheme, host, & port. http://localhost:8080/ :param strip_querystring: set to `True` if you don't want the querystring. http://localhost:8080/script/news/list :param strip_host: set to `True` you want to remove the scheme, host, & port: /script/news/list?param=foo :param https: None = use schem of current environ; True = force https scheme; False = force http scheme. Has no effect if strip_host = True. :param qs_update: a dict of key/value pairs that will be used to replace or add values to the current query string arguments. :param qs_replace: a dict of key/value pairs that will be used to replace values of the current query string. Unlike qs_update, if a key is not present in the currenty query string, it will not be added to the returned url. :param environ: the WSGI environment to get the current URL from. If not given, the environement from the current request will be used. This is mostly for use in our unit tests and probably wouldn't have much application in normal use. """ retval = '' if environ: ro = BaseRequest(environ, shallow=True) else: ro = rg.request if qs_replace or qs_update: strip_querystring = True if root_only: retval = ro.url_root elif host_only: retval = ro.host_url else: if strip_querystring: retval = ro.base_url else: retval = ro.url if strip_host: retval = retval.replace(ro.host_url.rstrip('/'), '', 1) if not strip_host and https is not None: if https and retval.startswith('http://'): retval = retval.replace('http://', 'https://', 1) elif not https and retval.startswith('https://'): retval = retval.replace('https://', 'http://', 1) if qs_update or qs_replace: href = Href(retval, sort=True) args = MultiDict(ro.args) if qs_update: # convert to md first so that if we have lists in the kwargs, they # are converted appropriately qs_update = MultiDict(qs_update) for key, value_list in qs_update.lists(): # multidicts extend, not replace, so we need # to get rid of the key first try: del args[key] except KeyError: pass args.setlistdefault(key, []).extend(value_list) if qs_replace: # convert to md first so that if we have lists in the kwargs, they # are converted appropriately qs_replace = MultiDict(qs_replace) for key, value_list in qs_replace.lists(): # multidicts extend, not replace, so we need # to get rid of the key first try: del args[key] args.setlistdefault(key, []).extend(value_list) except KeyError: pass return href(args) return retval
def url_for(self, endpoint, **values): result = url_for(endpoint, **values) href = Href(result) # pass along any query parameters # this is kind of hacky as it replaces any existing parameters return href(request.args)