Example #1
0
 def get_search_results(self, request, query):
     if settings.SHOOP_GUIDE_FETCH_RESULTS:
         try:
             response = requests.get(
                 settings.SHOOP_GUIDE_API_URL,
                 timeout=settings.SHOOP_GUIDE_TIMEOUT_LIMIT,
                 params={"q": query})
             json = response.json()
             results = json["results"]["hits"]["hits"]
             for result in results:
                 title = result["fields"]["title"][0]
                 link = result["fields"]["link"] + ".html"
                 url = manipulate_query_string(link, highlight=query)
                 yield SearchResult(
                     text=_("Guide: %s") % title,
                     url=url,
                     is_action=True,
                     relevance=0,
                     target="_blank",
                 )
         except RequestException:
             # Catch timeout or network errors so as not to break the search
             pass
     else:
         url = manipulate_query_string(settings.SHOOP_GUIDE_LINK_URL,
                                       q=query)
         yield SearchResult(text=_("Search guide for: \"%s\"") % query,
                            url=url,
                            is_action=True,
                            relevance=0,
                            target="_blank")
Example #2
0
def test_qs_manipulation():
    url = "http://example.com/"
    assert manipulate_query_string(url) == url  # Noop works
    hello_url = manipulate_query_string(url, q="hello", w="wello")  # Adding works
    assert "q=hello" in hello_url
    assert "w=wello" in hello_url
    unhello_url = manipulate_query_string(hello_url, q=None)  # Removal works
    assert "w=wello" in unhello_url
Example #3
0
def get_config(context):
    request = context["request"]
    url_name = None
    if getattr(request, "resolver_match", None):
        # This does not exist when testing views directly
        url_name = request.resolver_match.url_name

    qs = {"context": url_name}
    return {
        "searchUrl": manipulate_query_string(reverse("shoop_admin:search"), **qs),
        "menuUrl": manipulate_query_string(reverse("shoop_admin:menu"), **qs),
        "browserUrls": get_browser_urls(),
        "csrf": get_token(request)
    }
Example #4
0
def get_config(context):
    request = context["request"]
    url_name = None
    if getattr(request, "resolver_match", None):
        # This does not exist when testing views directly
        url_name = request.resolver_match.url_name

    qs = {"context": url_name}
    return {
        "searchUrl": manipulate_query_string(reverse("shoop_admin:search"), **qs),
        "menuUrl": manipulate_query_string(reverse("shoop_admin:menu"), **qs),
        "browserUrls": get_browser_urls(),
        "csrf": get_token(request)
    }
Example #5
0
 def form_valid(self, form):
     file = form.cleaned_data["file"]
     if not file.name.endswith(".zip"):
         raise Problem("Only ZIP files are supported")
     # TODO: Maybe verify the file before saving?
     filename = "shoop-addon-%s-%s" % (uuid.uuid4(), os.path.basename(file.name))
     with open(os.path.join(tempfile.gettempdir(), filename), "wb") as outf:
         shutil.copyfileobj(file, outf)
     return HttpResponseRedirect(manipulate_query_string(reverse("shoop_admin:addon.upload_confirm"), file=filename))
Example #6
0
def get_config(context):
    request = context["request"]
    url_name = None
    if getattr(request, "resolver_match", None):
        # This does not exist when testing views directly
        url_name = request.resolver_match.url_name

    try:
        media_browse_url = reverse("shoop_admin:media.browse")
    except NoReverseMatch:  # This may occur when the media module is not available.
        media_browse_url = None

    qs = {"context": url_name}
    return {
        "searchUrl": manipulate_query_string(reverse("shoop_admin:search"), **qs),
        "menuUrl": manipulate_query_string(reverse("shoop_admin:menu"), **qs),
        "mediaBrowserUrl": media_browse_url,
        "csrf": get_token(request)
    }
Example #7
0
 def form_valid(self, form):
     file = form.cleaned_data["file"]
     if not file.name.endswith(".zip"):
         raise Problem("Only ZIP files are supported")
     # TODO: Maybe verify the file before saving?
     filename = "shoop-addon-%s-%s" % (uuid.uuid4(),
                                       os.path.basename(file.name))
     with open(os.path.join(tempfile.gettempdir(), filename), "wb") as outf:
         shutil.copyfileobj(file, outf)
     return HttpResponseRedirect(
         manipulate_query_string(
             reverse("shoop_admin:addon.upload_confirm"), file=filename))
Example #8
0
    def get_search_results(self, request, query):
        minimum_query_length = 3
        skus_seen = set()
        if len(query) >= minimum_query_length:
            pk_counter = Counter()
            pk_counter.update(Product.objects.filter(sku__startswith=query).values_list("pk", flat=True))
            name_q = Q()
            for part in split_query(query, minimum_query_length):
                name_q &= Q(name__icontains=part)
            pk_counter.update(
                Product._parler_meta.root_model.objects.filter(name_q).values_list("master_id", flat=True)
            )
            pks = [pk for (pk, count) in pk_counter.most_common(10)]
            for product in Product.objects.filter(pk__in=pks):
                relevance = 100 - pk_counter.get(product.pk, 0)
                skus_seen.add(product.sku.lower())
                yield SearchResult(
                    text=force_text(product),
                    url=get_model_url(product),
                    category=_("Products"),
                    relevance=relevance
                )

        if len(query) >= minimum_query_length:
            url = reverse("shoop_admin:product.new")
            if " " in query:
                yield SearchResult(
                    text=_("Create Product Called \"%s\"") % query,
                    url=manipulate_query_string(url, name=query),
                    is_action=True
                )
            else:
                if query.lower() not in skus_seen:
                    yield SearchResult(
                        text=_("Create Product with SKU \"%s\"") % query,
                        url=manipulate_query_string(url, sku=query),
                        is_action=True
                    )
Example #9
0
 def get_search_results(self, request, query):
     if settings.SHOOP_GUIDE_FETCH_RESULTS:
         try:
             response = requests.get(
                 settings.SHOOP_GUIDE_API_URL, timeout=settings.SHOOP_GUIDE_TIMEOUT_LIMIT, params={"q": query}
             )
             json = response.json()
             results = json["results"]["hits"]["hits"]
             for result in results:
                 title = result["fields"]["title"][0]
                 link = result["fields"]["link"] + ".html"
                 url = manipulate_query_string(link, highlight=query)
                 yield SearchResult(
                     text=_("Guide: %s") % title, url=url, is_action=True, relevance=0, target="_blank"
                 )
         except RequestException:
             # Catch timeout or network errors so as not to break the search
             pass
     else:
         url = manipulate_query_string(settings.SHOOP_GUIDE_LINK_URL, q=query)
         yield SearchResult(
             text=_('Search guide for: "%s"') % query, url=url, is_action=True, relevance=0, target="_blank"
         )