def test_commodity_code_html(self): code = _commodity_code_html("4104115900") self.assertEqual( code, '<span class="app-commodity-code app-hierarchy-tree__commodity-code" aria-label="Commodity code"><span ' 'class="app-commodity-code__highlight app-commodity-code__highlight--1">41</span><span class="app-commodity-code__highlight app-commodity-code__highlight--2">04</span><span class="app-commodity-code__highlight app-commodity-code__highlight--3">11</span><span class="app-commodity-code__highlight app-commodity-code__highlight--4">59</span><span class="app-commodity-code__highlight app-commodity-code__highlight--5"><span class="app-commodity-code__is-blank">00</span></span></span>', # noqa: E501 )
def _construct_result(item): """Constructs a dictionary which is mostly compatible with ElasticSearch Hit and can be used in it's place in templates """ result = { "description": item.description, "id": item.goods_nomenclature_sid, "commodity_code": item.commodity_code, "commodity_code_html": _commodity_code_html(item, ignore_duplicate=False), } return result
def test_commodity_code_html_for_chapter(self): html = _commodity_code_html(self.chapter.chapter_code) code_html = """<span class="app-commodity-code app-hierarchy-tree__commodity-code" aria-label="Commodity code"><span class="app-commodity-code__highlight app-commodity-code__highlight--1">01</span><span class="app-commodity-code__highlight app-commodity-code__highlight--2"><span class="app-commodity-code__is-blank">00</span></span><span class="app-commodity-code__highlight app-commodity-code__highlight--3"><span class="app-commodity-code__is-blank">00</span></span><span class="app-commodity-code__highlight app-commodity-code__highlight--4"><span class="app-commodity-code__is-blank">00</span></span><span class="app-commodity-code__highlight app-commodity-code__highlight--5"><span class="app-commodity-code__is-blank">00</span></span></span>""" # noqa: E501 self.assertInHTML(code_html, html)
def get(self, request, *args, **kwargs): self.initial = self.get_initial() country_code = kwargs["country_code"] if not "origin_country" not in request.session: request.session["origin_country"] = country_code form = self.form_class(request.GET) context = self.get_context_data(kwargs={"country_code": country_code}) context["title_suffix"] = "" if form.is_valid(): form_data = form.cleaned_data page = int(form_data.get("page")) if form_data.get("page") else 1 normalised_q = helpers.normalise_commodity_code(form_data.get("q")) if normalised_q.isdigit(): response = helpers.search_by_code(code=normalised_q) hits = [hit for hit in response.scan()] if hits: hit = hits[0] item = helpers.get_object_from_hit(hit) hit.meta["index"] = helpers.get_alias_from_hit(hit) country_code = form_data.get("country") try: return redirect(item.get_detail_url(country_code)) except helpers.ObjectNotFoundFromHit: # response for no results found for commodity code context["message"] = "nothing found for that number" context["results"] = [] context["title_suffix"] = self.EMPTY_RESULTS_SUFFIX return self.render_to_response(context) else: context["results"] = [] context["title_suffix"] = self.EMPTY_RESULTS_SUFFIX return self.render_to_response(context) else: term_search_context = helpers.search_by_term( form_data=form_data) context.update(term_search_context) hits = term_search_context["_all_results"] # since we are supplying hits the function will not hit ES again grouped_search_context = helpers.group_search_by_term( form_data, hits=hits) context["group_result_count"] = grouped_search_context[ "group_result_count"] curr_url_items = dict((x, y) for x, y in request.GET.items()) next_urls_items = curr_url_items.copy() next_urls_items["page"] = str(int(next_urls_items["page"]) + 1) prev_url_items = curr_url_items.copy() prev_url_items["page"] = str(int(prev_url_items["page"]) - 1) context["next_url"] = "?{0}".format(urlencode(next_urls_items)) context["previous_url"] = "?{0}".format( urlencode(prev_url_items)) context["current_page"] = page context["results_per_page"] = settings.RESULTS_PER_PAGE context["page_total"] = len(context["results"]) if not context["results"]: context["title_suffix"] = self.EMPTY_RESULTS_SUFFIX search_term = form_data.get("q") total_results = context["total_results"] logger.info( f"Performed search for {search_term}", extra={ "search_term": search_term, "search_count": total_results }, ) track_event( request, "search", f"products results ({country_code})", label=search_term, value=total_results, ) for hit in context["results"]: if isinstance(hit["commodity_code"], str): try: item = helpers.get_object_from_hit(hit) except helpers.ObjectNotFoundFromHit: item = None hit.meta["index"] = helpers.get_alias_from_hit(hit) hit["commodity_code_html"] = _commodity_code_html(item) return self.render_to_response(context) else: if "q" in request.GET.keys() and form.has_error("q"): context["form_q_error"] = True error_data = form.errors.as_data() context["form_q_validation_message"] = error_data["q"][0] return self.render_to_response(context)