Ejemplo n.º 1
0
def index(request, page='0', size='20'):
    """Index view, displaying and processing a form."""
    search = SearchForm(request.GET)
    if search.is_valid():
        orig_pattern = search.cleaned_data['search']
        alt_pattern = None if orig_pattern.find('*') > -1 else ('*%s*' % orig_pattern)
        alt_url = reverse('pythonnest.views.index')

        patterns = orig_pattern.split()
        sub_query = None
        for pattern in patterns:
            sub_query = prepare_query(sub_query, '', 'normalized_name', pattern, global_and=True)
        query = Package.objects.filter(sub_query).distinct()
        page_int = int(page)
        page_size = int(size)
        packages = list(query.select_related()[page_int * page_size:(page_int + 1) * page_size])
        total = query.count()
        page_count = math.ceil(total / page_size)
        page_index = page_int + 1
        template_values = {'results': packages, 'title': _('PythonNest'), 'alt_pattern': alt_pattern,
                           'page_count': page_count, 'page_index': page_index, 'alt_url': alt_url,
                           'previous_page': None if page_int <= 0 else page_int - 1, 'pattern': orig_pattern,
                           'next_page': None if page_index >= page_count else page_index, }
        return render_to_response('search_result.html', template_values, RequestContext(request))
    base_url = settings.HOST
    use_ssl = base_url.startswith('https://')
    template_values = {'base_url': base_url, 'use_ssl': use_ssl, }
    template_values.update(csrf(request))  # prevents cross-domain requests
    return render_to_response('index.html', template_values, RequestContext(request))
Ejemplo n.º 2
0
def search(spec, operator="and"):
    """Search the package database using the indicated search spec. """
    sub_query = None
    for key in (
        "name",
        "home_page",
        "license",
        "summary",
        "download_url",
        "author",
        "author_email",
        "maintainer",
        "maintainer_email",
    ):
        if key not in spec:
            continue
        sub_query = prepare_query(sub_query, "package__", key, spec[key], global_and=(operator == "and"))
    for key in ("version", "description", "keywords", "platform"):
        if key not in spec:
            continue
        sub_query = prepare_query(sub_query, "", key, spec[key], global_and=(operator == "and"))
    query = Release.objects.filter(sub_query).select_related()
    return [{"name": ans.name, "version": ans.version, "summary": ans.summary, "_pypi_ordering": 0} for ans in query]