Ejemplo n.º 1
0
    def on_get(self, req, resp, barcode):
        response = {}
        count: int = req.get_param_as_int('count', min=1) or 1
        lang: str = req.get_param('lang', default='en')

        keep_types = QuestionFormatterFactory.get_available_types()
        insights = list(
            get_insights(barcode=barcode, keep_types=keep_types, count=count))

        if not insights:
            response['questions'] = []
            response['status'] = "no_questions"
        else:
            questions: List[JSONType] = []

            for insight in insights:
                formatter_cls = QuestionFormatterFactory.get(insight.type)
                formatter: QuestionFormatter = formatter_cls(TRANSLATION_STORE)
                question = formatter.format_question(insight, lang)
                questions.append(question.serialize())

            response['questions'] = questions
            response['status'] = "found"

        resp.media = response
Ejemplo n.º 2
0
    def on_get(self, req, resp, barcode):
        response = {}
        count: int = req.get_param_as_int("count", min_value=1) or 1
        lang: str = req.get_param("lang", default="en")
        server_domain: Optional[str] = req.get_param("server_domain")

        keep_types = QuestionFormatterFactory.get_available_types()
        insights = list(
            get_insights(
                barcode=barcode,
                keep_types=keep_types,
                server_domain=server_domain,
                limit=count,
            ))

        if not insights:
            response["questions"] = []
            response["status"] = "no_questions"
        else:
            questions: List[JSONType] = []

            for insight in insights:
                formatter_cls = QuestionFormatterFactory.get(insight.type)
                formatter: QuestionFormatter = formatter_cls(TRANSLATION_STORE)
                question = formatter.format_question(insight, lang)
                questions.append(question.serialize())

            response["questions"] = questions
            response["status"] = "found"

        resp.media = response
Ejemplo n.º 3
0
    def on_get(self, req, resp, barcode):
        response = {}
        insights = [i.serialize() for i in get_insights(barcode=barcode)]

        if not insights:
            response['status'] = "no_insights"
        else:
            response['insights'] = insights
            response['status'] = "found"

        resp.media = response
Ejemplo n.º 4
0
    def on_get(self, req: falcon.Request, resp: falcon.Response, barcode: str):
        response = {}
        insights = [i.serialize() for i in get_insights(barcode=barcode)]

        if not insights:
            response['status'] = "no_insights"
        else:
            response['insights'] = insights
            response['status'] = "found"

        resp.media = response
Ejemplo n.º 5
0
    def on_get(self, req: falcon.Request, resp: falcon.Response, barcode: str):
        server_domain: Optional[str] = req.get_param("server_domain")
        response: JSONType = {}
        insights = [
            i.serialize() for i in get_insights(
                barcode=barcode, server_domain=server_domain, limit=None)
        ]

        if not insights:
            response["status"] = "no_insights"
        else:
            response["insights"] = insights
            response["status"] = "found"

        resp.media = response
Ejemplo n.º 6
0
    def on_get(self, req, resp):
        response = {}
        count: int = req.get_param_as_int('count', min=1) or 1
        lang: str = req.get_param('lang', default='en')
        keep_types: Optional[List[str]] = req.get_param_as_list(
            'insight_types', required=False)
        country: Optional[str] = req.get_param('country') or None
        brands = req.get_param_as_list('brands') or None

        if keep_types is None:
            keep_types = QuestionFormatterFactory.get_available_types()
        else:
            # Limit the number of types to prevent slow SQL queries
            keep_types = keep_types[:10]

        if brands is not None:
            # Limit the number of brands to prevent slow SQL queries
            brands = brands[:10]

        insights = list(
            get_insights(keep_types=keep_types,
                         count=count,
                         country=country,
                         brands=brands))

        if not insights:
            response['questions'] = []
            response['status'] = "no_questions"
        else:
            questions: List[JSONType] = []

            for insight in insights:
                formatter_cls = QuestionFormatterFactory.get(insight.type)

                if formatter_cls is None:
                    continue

                formatter: QuestionFormatter = formatter_cls(TRANSLATION_STORE)
                question = formatter.format_question(insight, lang)
                questions.append(question.serialize())

            response['questions'] = questions
            response['status'] = "found"

        resp.media = response
Ejemplo n.º 7
0
    def on_get(self, req: falcon.Request, resp: falcon.Response, barcode: str):
        response: JSONType = {}
        count: int = req.get_param_as_int("count", min_value=1) or 1
        lang: str = req.get_param("lang", default="en")
        # If the device_id is not provided as a request parameter, we use the
        # hash of the IPs as a backup.
        device_id = device_id_from_request(req)
        server_domain: Optional[str] = req.get_param("server_domain")

        auth: Optional[OFFAuthentication] = parse_auth(req)

        keep_types = QuestionFormatterFactory.get_default_types()

        insights = list(
            get_insights(
                barcode=barcode,
                keep_types=keep_types,
                server_domain=server_domain,
                limit=count,
                order_by="n_votes",
                avoid_voted_on=_get_skip_voted_on(auth, device_id),
            )
        )

        if not insights:
            response["questions"] = []
            response["status"] = "no_questions"
        else:
            questions: List[JSONType] = []

            for insight in insights:
                formatter_cls = QuestionFormatterFactory.get(insight.type)
                formatter: QuestionFormatter = formatter_cls(TRANSLATION_STORE)
                question = formatter.format_question(insight, lang)
                questions.append(question.serialize())

            response["questions"] = questions
            response["status"] = "found"

        resp.media = response
Ejemplo n.º 8
0
    def on_get(self, req: falcon.Request, resp: falcon.Response):
        keep_types: Optional[List[str]] = req.get_param_as_list(
            "insight_types", required=False
        )

        if keep_types is not None:
            keep_types = keep_types[:10]

        barcode = req.get_param("barcode")
        annotated = req.get_param_as_bool("annotated", blank_as_true=False)
        value_tag = req.get_param("value_tag")

        insights_iter = get_insights(
            barcode=barcode,
            keep_types=keep_types,
            annotated=annotated,
            value_tag=value_tag,
            limit=None,
        )

        writer = None

        with tempfile.TemporaryFile("w+", newline="") as temp_f:
            for insight in insights_iter:
                serial = orjson.loads(orjson.dumps(insight.to_dict()))

                if writer is None:
                    writer = csv.DictWriter(temp_f, fieldnames=serial.keys())
                    writer.writeheader()

                writer.writerow(serial)

            temp_f.seek(0)
            content = temp_f.read()

        if content:
            resp.content_type = "text/csv"
            resp.body = content
        else:
            resp.status = falcon.HTTP_204
Ejemplo n.º 9
0
    def on_get(self, req, resp):
        insight_type: Optional[str] = req.get_param("type")
        country: Optional[str] = req.get_param("country")
        value_tag: Optional[str] = req.get_param("value_tag")
        server_domain: Optional[str] = req.get_param("server_domain")

        insights = list(
            get_insights(
                keep_types=[insight_type],
                country=country,
                value_tag=value_tag,
                order_by="random",
                server_domain=server_domain,
                limit=1,
            ))

        response = {}
        if not insights:
            response["status"] = "no_insights"
        else:
            response["insight"] = insights[0].serialize()
            response["status"] = "found"

        resp.media = response
Ejemplo n.º 10
0
    def on_get(self, req: falcon.Request, resp: falcon.Response):
        insight_type: Optional[str] = req.get_param("type")
        country: Optional[str] = req.get_param("country")
        value_tag: Optional[str] = req.get_param("value_tag")
        server_domain: Optional[str] = req.get_param("server_domain")
        count: int = req.get_param_as_int("count",
                                          default=1,
                                          min_value=1,
                                          max_value=50)

        keep_types = [insight_type] if insight_type else None
        insights: List[ProductInsight] = list(
            get_insights(
                keep_types=keep_types,
                country=country,
                value_tag=value_tag,
                order_by="random",
                server_domain=server_domain,
                limit=count,
            ))

        resp.media = {
            "insights": [insight.serialize() for insight in insights]
        }
Ejemplo n.º 11
0
def dump_insights():
    logger.info("Dumping insights...")
    insights_iter = get_insights(as_dict=True, annotated=None, limit=None)
    insights_iter = transform_insight_iter(insights_iter)
    dumped = dump_jsonl(settings.INSIGHT_DUMP_PATH, insights_iter)
    logger.info("Dump finished, {} insights dumped".format(dumped))