当前位置: 首页>>代码示例>>Python>>正文


Python URL.append_query_param方法代码示例

本文整理汇总了Python中purl.URL.append_query_param方法的典型用法代码示例。如果您正苦于以下问题:Python URL.append_query_param方法的具体用法?Python URL.append_query_param怎么用?Python URL.append_query_param使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在purl.URL的用法示例。


在下文中一共展示了URL.append_query_param方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: UnicodeTests

# 需要导入模块: from purl import URL [as 别名]
# 或者: from purl.URL import append_query_param [as 别名]
class UnicodeTests(TestCase):

    def setUp(self):
        self.base = URL('http://127.0.0.1/')
        self.text = u'ć'
        self.bytes = self.text.encode('utf8')

    def test_set_unicode_query_param_value(self):
        url = self.base.query_param('q', self.text)
        self.assertEqual(self.text, url.query_param('q'))

    def test_set_bytestring_query_param_value(self):
        url = self.base.query_param('q', self.bytes)
        self.assertEqual(self.text, url.query_param('q'))

    def test_set_unicode_query_param_key(self):
        url = self.base.query_param(self.text, 'value')
        self.assertEqual('value', url.query_param(self.text))

    def test_set_bytestring_query_param_key(self):
        url = self.base.query_param(self.bytes, 'value')
        self.assertEqual('value', url.query_param(self.text))

    def test_append_unicode_query_param(self):
        url = self.base.append_query_param('q', self.text)
        self.assertEqual(self.text, url.query_param('q'))

    def test_append_bytestring_query_param(self):
        url = self.base.append_query_param('q', self.bytes)
        self.assertEqual(self.text, url.query_param('q'))

    def test_set_unicode_query_params(self):
        url = self.base.query_params({'q': self.text})
        self.assertEqual(self.text, url.query_param('q'))

    def test_set_bytestring_query_params(self):
        url = self.base.query_params({'q': self.bytes})
        self.assertEqual(self.text, url.query_param('q'))

    def test_add_unicode_path_segment(self):
        url = self.base.add_path_segment(self.text)
        self.assertEqual(self.text, url.path_segment(0))

    def test_add_bytestring_path_segment(self):
        url = self.base.add_path_segment(self.bytes)
        self.assertEqual(self.text, url.path_segment(0))

    def test_add_bytestring_path_segment(self):
        url = self.base.add_path_segment(self.bytes)
        self.assertEqual(self.text, url.path_segment(0))

    def test_add_unicode_fragment(self):
        url = self.base.fragment(self.text)
        self.assertEqual(self.text, url.fragment())
开发者ID:suquant,项目名称:purl,代码行数:56,代码来源:tests.py

示例2: select

# 需要导入模块: from purl import URL [as 别名]
# 或者: from purl.URL import append_query_param [as 别名]
 def select(self, url):
     print "SELECTED!!"
     url = URL(url)
     field_name = '%s_exact' % self.facet_name
     url = url.append_query_param('selected_facets', '%s:%s' % (field_name, self.key))
     self.selected = True
     return url
开发者ID:bryandmc,项目名称:saleor,代码行数:9,代码来源:facet.py

示例3: facet_data

# 需要导入模块: from purl import URL [as 别名]
# 或者: from purl.URL import append_query_param [as 别名]
def facet_data(request, form, results):  # noqa (too complex (10))
    """
    Convert Haystack's facet data into a more useful datastructure that
    templates can use without having to manually construct URLs
    """
    facet_data = {}
    if not results:
        return facet_data

    base_url = URL(request.get_full_path())
    facet_counts = results.facet_counts()

    # Field facets
    valid_facets = [f for f in form.selected_facets if ':' in f]
    selected = dict(
        map(lambda x: x.split(':', 1), valid_facets))
    for key, facet in settings.OSCAR_SEARCH_FACETS['fields'].items():
        facet_data[key] = {
            'name': facet['name'],
            'results': []}
        for name, count in facet_counts['fields'][key]:
            # Ignore zero-count facets for field
            if count == 0:
                continue
            field_filter = '%s_exact' % facet['field']
            datum = {
                'name': name,
                'count': count}
            if selected.get(field_filter, None) == name:
                # This filter is selected - build the 'deselect' URL
                datum['selected'] = True
                url = base_url.remove_query_param(
                    'selected_facets', '%s:%s' % (
                        field_filter, name))
                # Don't carry through pagination params
                if url.has_query_param('page'):
                    url = url.remove_query_param('page')
                datum['deselect_url'] = url.as_string()
            else:
                # This filter is not selected - built the 'select' URL
                datum['selected'] = False
                url = base_url.append_query_param(
                    'selected_facets', '%s:%s' % (
                        field_filter, name))
                # Don't carry through pagination params
                if url.has_query_param('page'):
                    url = url.remove_query_param('page')
                datum['select_url'] = url.as_string()
            facet_data[key]['results'].append(datum)

    # Query facets
    for key, facet in settings.OSCAR_SEARCH_FACETS['queries'].items():
        facet_data[key] = {
            'name': facet['name'],
            'results': []}
        for name, query in facet['queries']:
            field_filter = '%s_exact' % facet['field']
            match = '%s_exact:%s' % (facet['field'], query)
            if match not in facet_counts['queries']:
                datum = {
                    'name': name,
                    'count': 0,
                }
            else:
                datum = {
                    'name': name,
                    'count': facet_counts['queries'][match],
                }
                if selected.get(field_filter, None) == query:
                    # Selected
                    datum['selected'] = True
                    url = base_url.remove_query_param(
                        'selected_facets', match)
                    datum['deselect_url'] = url.as_string()
                else:
                    datum['selected'] = False
                    url = base_url.append_query_param(
                        'selected_facets', match)
                    datum['select_url'] = url.as_string()
                facet_data[key]['results'].append(datum)

    return facet_data
开发者ID:HiddenData,项目名称:django-oscar,代码行数:84,代码来源:facets.py

示例4: extra_context

# 需要导入模块: from purl import URL [as 别名]
# 或者: from purl.URL import append_query_param [as 别名]
    def extra_context(self):
        extra = super(FacetedSearchView, self).extra_context()

        if 'fields'  not in extra['facets']:
            # Looks like Solr is not responding correctly
            return extra

        # Convert facet data into a more useful datastructure

        # Field facets
        facet_data = {}
        base_url = URL(self.request.get_full_path())
        selected = dict(
            map(lambda x: x.split(':'), self.form.selected_facets))
        for field, facets in extra['facets']['fields'].items():
            facet_data[field] = []
            for name, count in facets:
                # Ignore zero-count facets for field
                if count == 0:
                    continue
                field_filter = '%s_exact' % field
                datum = {
                    'name': name,
                    'count': count}
                if selected.get(field_filter, None) == name:
                    # This filter is selected - build the 'deselect' URL
                    datum['selected'] = True
                    url = base_url.remove_query_param(
                        'selected_facets', '%s:%s' % (
                            field_filter, name))
                    datum['deselect_url'] = url.as_string()
                else:
                    # This filter is not selected - built the 'select' URL
                    datum['selected'] = False
                    url = base_url.append_query_param(
                        'selected_facets', '%s:%s' % (
                            field_filter, name))
                    datum['select_url'] = url.as_string()
                facet_data[field].append(datum)

        # Query facets
        for key, facet in settings.OSCAR_SEARCH_FACETS['queries'].items():
            facet_data[key] = []
            for name, query in facet['queries']:
                field_filter = '%s_exact' % facet['field']
                match = '%s_exact:%s' % (facet['field'], query)
                if not match in extra['facets']['queries']:
                    datum = {
                        'name': name,
                        'count': 0,
                    }
                else:
                    datum = {
                        'name': name,
                        'count': extra['facets']['queries'][match],
                    }
                    if selected.get(field_filter, None) == query:
                        # Selected
                        datum['selected'] = True
                        url = base_url.remove_query_param(
                            'selected_facets', match)
                        datum['deselect_url'] = url.as_string()
                    else:
                        datum['selected'] = False
                        url = base_url.append_query_param(
                            'selected_facets', match)
                        datum['select_url'] = url.as_string()
                    facet_data[key].append(datum)

        extra['facet_data'] = facet_data

        return extra
开发者ID:AndreaCrotti,项目名称:django-oscar,代码行数:74,代码来源:views.py

示例5: facet_data

# 需要导入模块: from purl import URL [as 别名]
# 或者: from purl.URL import append_query_param [as 别名]
def facet_data(request, form, results):
    """
    Convert Haystack's facet data into a more useful datastructure that
    templates can use without having to manually construct URLs
    """
    facet_data = {}
    base_url = URL(request.get_full_path())
    facet_counts = results.facet_counts()

    # Field facets
    selected = dict(
        map(lambda x: x.split(':'), form.selected_facets))
    for field, facets in facet_counts['fields'].items():
        facet_data[field] = []
        for name, count in facets:
            # Ignore zero-count facets for field
            if count == 0:
                continue
            field_filter = '%s_exact' % field
            datum = {
                'name': name,
                'count': count}
            if selected.get(field_filter, None) == name:
                # This filter is selected - build the 'deselect' URL
                datum['selected'] = True
                url = base_url.remove_query_param(
                    'selected_facets', '%s:%s' % (
                        field_filter, name))
                datum['deselect_url'] = url.as_string()
            else:
                # This filter is not selected - built the 'select' URL
                datum['selected'] = False
                url = base_url.append_query_param(
                    'selected_facets', '%s:%s' % (
                        field_filter, name))
                datum['select_url'] = url.as_string()
            facet_data[field].append(datum)

    # Query facets
    for key, facet in settings.OSCAR_SEARCH_FACETS['queries'].items():
        facet_data[key] = []
        for name, query in facet_counts['queries'].items():
            field_filter = '%s_exact' % facet['field']
            match = '%s_exact:%s' % (facet['field'], query)
            if match not in facet_counts['queries']:
                datum = {
                    'name': name,
                    'count': 0,
                }
            else:
                datum = {
                    'name': name,
                    'count': facet_counts['queries'][match],
                }
                if selected.get(field_filter, None) == query:
                    # Selected
                    datum['selected'] = True
                    url = base_url.remove_query_param(
                        'selected_facets', match)
                    datum['deselect_url'] = url.as_string()
                else:
                    datum['selected'] = False
                    url = base_url.append_query_param(
                        'selected_facets', match)
                    datum['select_url'] = url.as_string()
                facet_data[key].append(datum)

    return facet_data
开发者ID:ButchershopCreative,项目名称:django-oscar,代码行数:70,代码来源:facets.py

示例6: FacetMunger

# 需要导入模块: from purl import URL [as 别名]
# 或者: from purl.URL import append_query_param [as 别名]
class FacetMunger(object):

    def __init__(self, path, selected_multi_facets, facet_counts):
        self.base_url = URL(path)
        self.selected_facets = selected_multi_facets
        self.facet_counts = facet_counts

    def facet_data(self):
        facet_data = OrderedDict()
        # Haystack can return an empty dict for facet_counts when e.g. Solr
        # isn't running. Skip facet munging in that case.
        if self.facet_counts:
            self.munge_field_facets(facet_data)
            self.munge_query_facets(facet_data)
        return facet_data

    def munge_field_facets(self, clean_data):
        for key, facet in settings.GRAVYBOAT_SEARCH_FACETS['fields'].items():
            self.munge_field_facet(key, facet, clean_data)

    def munge_field_facet(self, key, facet, clean_data):
        clean_data[key] = {
            'name': facet['name'],
            'results': []}
        for field_value, count in self.facet_counts['fields'][key]:
            field_name = '%s_exact' % facet['field']
            is_faceted_already = field_name in self.selected_facets
            datum = {
                'name': field_value,
                'count': count,
                # We don't show facet counts if a this field is already being
                # faceted (as we don't know them)
                'show_count': not is_faceted_already,
                'disabled': count == 0 and not is_faceted_already,
                'selected': False
            }
            if field_value in self.selected_facets.get(field_name, []):
                # This filter is selected - build the 'deselect' URL
                datum['selected'] = True
                url = self.base_url.remove_query_param(
                    'selected_facets', '%s:%s' % (
                        field_name, field_value))
                datum['deselect_url'] = self.strip_pagination(url)
            else:
                # This filter is not selected - built the 'select' URL
                url = self.base_url.append_query_param(
                    'selected_facets', '%s:%s' % (
                        field_name, field_value))
                datum['select_url'] = self.strip_pagination(url)

            clean_data[key]['results'].append(datum)

    def munge_query_facets(self, clean_data):
        for key, facet in settings.GRAVYBOAT_SEARCH_FACETS['queries'].items():
            self.munge_query_facet(key, facet, clean_data)

    def munge_query_facet(self, key, facet, clean_data):
        clean_data[key] = {
            'name': facet['name'],
            'results': []}
        # Loop over the queries in GRAVYBOAT_SEARCH_FACETS rather than the returned
        # facet information from the search backend.
        for field_value, query in facet['queries']:
            field_name = '%s_exact' % facet['field']
            is_faceted_already = field_name in self.selected_facets

            match = '%s:%s' % (field_name, query)
            if match not in self.facet_counts['queries']:
                # This query was not returned
                datum = {
                    'name': field_value,
                    'count': 0,
                    'show_count': True,
                    'disabled': True,
                }
            else:
                count = self.facet_counts['queries'][match]
                datum = {
                    'name': field_value,
                    'count': count,
                    'show_count': not is_faceted_already,
                    'disabled': count == 0 and not is_faceted_already,
                    'selected': False,
                }
                if query in self.selected_facets.get(field_name, []):
                    # Selected
                    datum['selected'] = True
                    datum['show_count'] = True
                    url = self.base_url.remove_query_param(
                        'selected_facets', match)
                    datum['deselect_url'] = self.strip_pagination(url)
                else:
                    url = self.base_url.append_query_param(
                        'selected_facets', match)
                    datum['select_url'] = self.strip_pagination(url)
            clean_data[key]['results'].append(datum)

    def strip_pagination(self, url):
        if url.has_query_param('page'):
            url = url.remove_query_param('page')
#.........这里部分代码省略.........
开发者ID:quakig,项目名称:gravyboat,代码行数:103,代码来源:facets.py

示例7: test_append_to_nonexistant_list

# 需要导入模块: from purl import URL [as 别名]
# 或者: from purl.URL import append_query_param [as 别名]
 def test_append_to_nonexistant_list(self):
     base = URL('http://127.0.0.1/?q=a&q=b')
     url = base.append_query_param('p', 'c')
     values = url.query_param('p', as_list=True)
     self.assertEqual(['c'], values)
开发者ID:st4lk,项目名称:purl,代码行数:7,代码来源:tests.py


注:本文中的purl.URL.append_query_param方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。