Exemplo n.º 1
0
 def get_search_results(self, request, query):
     if settings.SHUUP_GUIDE_FETCH_RESULTS:
         try:
             response = requests.get(
                 settings.SHUUP_GUIDE_API_URL,
                 timeout=settings.SHUUP_GUIDE_TIMEOUT_LIMIT,
                 params={"q": query})
             json = response.json()
             if "results" in 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.SHUUP_GUIDE_LINK_URL,
                                       q=query)
         yield SearchResult(text=_("Search guide for: \"%s\"") % query,
                            url=url,
                            is_action=True,
                            relevance=0,
                            target="_blank")
Exemplo n.º 2
0
 def get_search_results(self, request, query):
     if settings.SHUUP_GUIDE_FETCH_RESULTS:
         try:
             response = requests.get(
                 settings.SHUUP_GUIDE_API_URL, timeout=settings.SHUUP_GUIDE_TIMEOUT_LIMIT, params={"q": query}
             )
             json = response.json()
             if "results" in 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.SHUUP_GUIDE_LINK_URL, q=query)
         yield SearchResult(
             text=_("Search guide for: \"%s\"") % query,
             url=url,
             is_action=True,
             relevance=0,
             target="_blank"
         )
Exemplo n.º 3
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
Exemplo n.º 4
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
Exemplo n.º 5
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("shuup_admin:search"), **qs),
        "menuUrl": manipulate_query_string(reverse("shuup_admin:menu"), **qs),
        "browserUrls": get_browser_urls(),
        "csrf": get_token(request)
    }
Exemplo n.º 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

    qs = {"context": url_name}
    return {
        "searchUrl": manipulate_query_string(reverse("shuup_admin:search"),
                                             **qs),
        "menuUrl": manipulate_query_string(reverse("shuup_admin:menu"), **qs),
        "browserUrls": get_browser_urls(),
        "csrf": get_token(request)
    }
Exemplo n.º 7
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("shuup_admin:search"), **qs),
        "menuUrl": manipulate_query_string(reverse("shuup_admin:menu"), **qs),
        "browserUrls": get_browser_urls(request),
        "csrf": get_token(request),
        "docsPage": settings.SHUUP_ADMIN_MERCHANT_DOCS_PAGE,
        "menuOpen": is_menu_open(request),
        "settings": get_settings(request)
    }
Exemplo n.º 8
0
    def get_search_results(self, request, query):
        shop = request.shop
        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, shop_products__shop_id=shop.id):
                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, shop=request.shop),
                    category=_("Products"),
                    relevance=relevance,
                )

        if len(query) >= minimum_query_length:
            url = reverse("shuup_admin:shop_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,
                    )
Exemplo n.º 9
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 = "shuup-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("shuup_admin:addon.upload_confirm"), file=filename))
Exemplo n.º 10
0
    def get_search_results(self, request, query):
        shop = request.shop
        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, shop_products__shop_id=shop.id):
                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, shop=request.shop),
                    category=_("Products"),
                    relevance=relevance
                )

        if len(query) >= minimum_query_length:
            url = reverse("shuup_admin:shop_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
                    )
Exemplo n.º 11
0
 def form_valid(self, form):
     file = form.cleaned_data["file"]
     if not file.name.lower().endswith(".whl"):
         raise Problem(_("Only wheel files (`.whl`) are supported."))
     # TODO: Maybe verify the file before saving?
     tmp_dir = tempfile.mkdtemp(prefix="shuup")
     tmp_token = os.path.basename(tmp_dir)
     filename = os.path.basename(file.name)
     with open(os.path.join(tmp_dir, filename), "wb") as outf:
         shutil.copyfileobj(file, outf)
     return HttpResponseRedirect(
         manipulate_query_string(reverse("shuup_admin:addon.upload_confirm"), file=filename, token=tmp_token)
     )
Exemplo n.º 12
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 = "shuup-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("shuup_admin:addon.upload_confirm"),
             file=filename
         )
     )
Exemplo n.º 13
0
 def form_valid(self, form):
     file = form.cleaned_data["file"]
     if not file.name.lower().endswith(".whl"):
         raise Problem(_("Only wheel files are supported"))
     # TODO: Maybe verify the file before saving?
     tmp_dir = tempfile.mkdtemp(prefix='shuup')
     tmp_token = os.path.basename(tmp_dir)
     filename = os.path.basename(file.name)
     with open(os.path.join(tmp_dir, filename), "wb") as outf:
         shutil.copyfileobj(file, outf)
     return HttpResponseRedirect(
         manipulate_query_string(
             reverse("shuup_admin:addon.upload_confirm"),
             file=filename,
             token=tmp_token
         )
     )