Skip to content

Commit

Permalink
fix: improve Robotoff edit message
Browse files Browse the repository at this point in the history
say when the edit was triggered by an anonymous vote
  • Loading branch information
raphael0202 committed Apr 17, 2023
1 parent 64f13eb commit 4b5f230
Show file tree
Hide file tree
Showing 7 changed files with 181 additions and 88 deletions.
7 changes: 4 additions & 3 deletions robotoff/app/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,9 +301,10 @@ def on_post(self, req: falcon.Request, resp: falcon.Response):

device_id = device_id_from_request(req)
logger.info(
"New annotation received from "
f"{auth.get_username() if auth else 'unknown annotator'} "
f"(annotation: {annotation}, insight: {insight_id})"
"New annotation received from %s (annotation: %s, insight: %s)",
auth.get_username() if auth else "unknown annotator",
annotation,
insight_id,
)

annotation_result = save_annotation(
Expand Down
11 changes: 7 additions & 4 deletions robotoff/app/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
)
from robotoff.off import OFFAuthentication
from robotoff.taxonomy import match_taxonomized_value
from robotoff.types import ServerType
from robotoff.types import InsightAnnotation, ServerType
from robotoff.utils import get_logger
from robotoff.utils.text import get_tag

Expand Down Expand Up @@ -346,7 +346,7 @@ def get_image_predictions(

def save_annotation(
insight_id: str,
annotation: int,
annotation: InsightAnnotation,
device_id: str,
update: bool = True,
data: Optional[dict] = None,
Expand Down Expand Up @@ -374,7 +374,8 @@ def save_annotation(
:param update: If True, perform the update on Product Opener if annotation=1,
otherwise only save the annotation (default: True)
:param data: Optional additional data, required for some insight types
:param auth: User authentication data
:param auth: User authentication data, it is expected to be None if
`trusted_annotator=False` (=anonymous vote)
:param trusted_annotator: Defines whether the given annotation comes from
an authoritative source (e.g. a trusted user), ot whether the annotation
should be subject to the voting system.
Expand Down Expand Up @@ -448,7 +449,9 @@ def save_annotation(
if not verified:
return SAVED_ANNOTATION_VOTE_RESULT

result = annotate(insight, annotation, update, data=data, auth=auth)
result = annotate(
insight, annotation, update, data=data, auth=auth, is_vote=not trusted_annotator
)
username = auth.get_username() if auth else "unknown annotator"
events.event_processor.send_async(
"question_answered",
Expand Down
53 changes: 47 additions & 6 deletions robotoff/insights/annotate.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
update_quantity,
)
from robotoff.products import get_image_id, get_product
from robotoff.types import InsightType
from robotoff.types import InsightAnnotation, InsightType
from robotoff.utils import get_logger

logger = get_logger(__name__)
Expand Down Expand Up @@ -108,14 +108,30 @@ class InsightAnnotator(metaclass=abc.ABCMeta):
def annotate(
cls,
insight: ProductInsight,
annotation: int,
annotation: InsightAnnotation,
update: bool = True,
data: Optional[dict] = None,
auth: Optional[OFFAuthentication] = None,
is_vote: bool = False,
) -> AnnotationResult:
"""Annotate an insight: save the annotation in DB and send the update
to Product Opener if `update=True`.
:param insight: the insight to annotate
:param annotation: the annotation as an integer, either -1, 0 or 1
:param update: if True, a write query is sent to Product Opener with
the update, defaults to True
:param data: additional data sent by the client, defaults to None
:param auth: user authentication data, should be None if the
annotation was triggered by an anonymous vote (in which case
`is_vote=True`) or if the insight is applied automatically.
:param is_vote: True if the annotation was triggered by an anonymous
vote, defaults to False
:return: the result of the annotation process
"""
with db.atomic() as tx:
try:
return cls._annotate(insight, annotation, update, data, auth)
return cls._annotate(insight, annotation, update, data, auth, is_vote)
except HTTPError as e:
if e.response.status_code >= 500:
logger.info(
Expand All @@ -136,10 +152,11 @@ def annotate(
def _annotate(
cls,
insight: ProductInsight,
annotation: int,
annotation: InsightAnnotation,
update: bool = True,
data: Optional[dict] = None,
auth: Optional[OFFAuthentication] = None,
is_vote: bool = False,
) -> AnnotationResult:
if cls.is_data_required() and data is None:
return DATA_REQUIRED_RESULT
Expand All @@ -156,7 +173,7 @@ def _annotate(
# Save insight before processing the annotation
insight.save()
annotation_result = cls.process_annotation(
insight, data=data, auth=auth
insight, data=data, auth=auth, is_vote=is_vote
) # calls the process_annotation function of the class corresponding to the current insight type
else:
annotation_result = SAVED_ANNOTATION_RESULT
Expand All @@ -180,6 +197,7 @@ def process_annotation(
insight: ProductInsight,
data: Optional[dict] = None,
auth: Optional[OFFAuthentication] = None,
is_vote: bool = False,
) -> AnnotationResult:
pass

Expand All @@ -195,6 +213,7 @@ def process_annotation(
insight: ProductInsight,
data: Optional[dict] = None,
auth: Optional[OFFAuthentication] = None,
is_vote: bool = False,
) -> AnnotationResult:
emb_code: str = insight.value

Expand All @@ -219,6 +238,7 @@ def process_annotation(
emb_codes,
insight_id=insight.id,
auth=auth,
is_vote=is_vote,
)
return UPDATED_ANNOTATION_RESULT

Expand All @@ -241,6 +261,7 @@ def process_annotation(
insight: ProductInsight,
data: Optional[dict] = None,
auth: Optional[OFFAuthentication] = None,
is_vote: bool = False,
) -> AnnotationResult:
product_id = insight.get_product_id()
product = get_product(product_id, ["labels_tags"])
Expand All @@ -258,6 +279,7 @@ def process_annotation(
insight.value_tag,
insight_id=insight.id,
auth=auth,
is_vote=is_vote,
)
return UPDATED_ANNOTATION_RESULT

Expand All @@ -269,6 +291,7 @@ def process_annotation(
insight: ProductInsight,
data: Optional[dict] = None,
auth: Optional[OFFAuthentication] = None,
is_vote: bool = False,
) -> AnnotationResult:
product_id = insight.get_product_id()
lang = insight.data["lang"]
Expand Down Expand Up @@ -299,6 +322,7 @@ def process_annotation(
lang=lang,
insight_id=insight.id,
auth=auth,
is_vote=is_vote,
)
return UPDATED_ANNOTATION_RESULT

Expand All @@ -310,6 +334,7 @@ def process_annotation(
insight: ProductInsight,
data: Optional[dict] = None,
auth: Optional[OFFAuthentication] = None,
is_vote: bool = False,
) -> AnnotationResult:
product_id = insight.get_product_id()
product = get_product(product_id, ["categories_tags"])
Expand All @@ -328,6 +353,7 @@ def process_annotation(
category_tag,
insight_id=insight.id,
auth=auth,
is_vote=is_vote,
)
return UPDATED_ANNOTATION_RESULT

Expand All @@ -339,6 +365,7 @@ def process_annotation(
insight: ProductInsight,
data: Optional[dict] = None,
auth: Optional[OFFAuthentication] = None,
is_vote: bool = False,
) -> AnnotationResult:
product_id = insight.get_product_id()
product = get_product(product_id, ["quantity"])
Expand All @@ -356,6 +383,7 @@ def process_annotation(
insight.value,
insight_id=insight.id,
auth=auth,
is_vote=is_vote,
)
return UPDATED_ANNOTATION_RESULT

Expand All @@ -367,6 +395,7 @@ def process_annotation(
insight: ProductInsight,
data: Optional[dict] = None,
auth: Optional[OFFAuthentication] = None,
is_vote: bool = False,
) -> AnnotationResult:
product_id = insight.get_product_id()
product = get_product(product_id, ["expiration_date"])
Expand All @@ -384,6 +413,7 @@ def process_annotation(
insight.value,
insight_id=insight.id,
auth=auth,
is_vote=is_vote,
)
return UPDATED_ANNOTATION_RESULT

Expand All @@ -395,6 +425,7 @@ def process_annotation(
insight: ProductInsight,
data: Optional[dict] = None,
auth: Optional[OFFAuthentication] = None,
is_vote: bool = False,
) -> AnnotationResult:
product_id = insight.get_product_id()
product = get_product(product_id, ["brands_tags"])
Expand All @@ -407,6 +438,7 @@ def process_annotation(
insight.value,
insight_id=insight.id,
auth=auth,
is_vote=is_vote,
)

return UPDATED_ANNOTATION_RESULT
Expand All @@ -419,6 +451,7 @@ def process_annotation(
insight: ProductInsight,
data: Optional[dict] = None,
auth: Optional[OFFAuthentication] = None,
is_vote: bool = False,
) -> AnnotationResult:
product_id = insight.get_product_id()
product = get_product(product_id, ["stores_tags"])
Expand All @@ -436,6 +469,7 @@ def process_annotation(
insight.value,
insight_id=insight.id,
auth=auth,
is_vote=is_vote,
)
return UPDATED_ANNOTATION_RESULT

Expand All @@ -447,6 +481,7 @@ def process_annotation(
insight: ProductInsight,
data: Optional[dict] = None,
auth: Optional[OFFAuthentication] = None,
is_vote: bool = False,
) -> AnnotationResult:
product_id = insight.get_product_id()
product = get_product(product_id, ["code"])
Expand All @@ -459,6 +494,7 @@ def process_annotation(
insight.data["element"],
insight_id=insight.id,
auth=auth,
is_vote=is_vote,
)
return UPDATED_ANNOTATION_RESULT

Expand All @@ -470,6 +506,7 @@ def process_annotation(
insight: ProductInsight,
data: Optional[dict] = None,
auth: Optional[OFFAuthentication] = None,
is_vote: bool = False,
) -> AnnotationResult:
product_id = insight.get_product_id()
product = get_product(product_id, ["code"])
Expand All @@ -492,6 +529,7 @@ def process_annotation(
image_key=image_key,
rotate=insight.data.get("rotation"),
auth=auth,
is_vote=is_vote,
)
return UPDATED_ANNOTATION_RESULT

Expand All @@ -503,6 +541,7 @@ def process_annotation(
insight: ProductInsight,
data: Optional[dict] = None,
auth: Optional[OFFAuthentication] = None,
is_vote: bool = False,
) -> AnnotationResult:
insight.data["annotation"] = data
insight.save()
Expand All @@ -529,17 +568,19 @@ def is_data_required(self):

def annotate(
insight: ProductInsight,
annotation: int,
annotation: InsightAnnotation,
update: bool = True,
data: Optional[dict] = None,
auth: Optional[OFFAuthentication] = None,
is_vote: bool = False,
) -> AnnotationResult:
return ANNOTATOR_MAPPING[insight.type].annotate(
insight=insight,
annotation=annotation,
update=update,
data=data,
auth=auth,
is_vote=is_vote,
)


Expand Down
Loading

0 comments on commit 4b5f230

Please sign in to comment.