Features: 1) Update resolve_feedback method to retrieve Feedback object based on order_product; 2) Enhance do_feedback method to handle existing feedback with better validation and support for returning feedback deletion count.

Fixes: 1) Fix handling of feedback deletion to return actual delete count rather than `None`.

Extra: 1) Refactor `do_feedback` method for clarity and improved logic flow; 2) Minor updates to error messages for consistency and accuracy.
This commit is contained in:
Egor Pavlovich Gorbunov 2025-09-18 14:44:52 +03:00
parent 17a2b01894
commit 79714750fa
2 changed files with 17 additions and 10 deletions

View file

@ -416,7 +416,7 @@ class OrderProductType(DjangoObjectType):
description = _("order products")
def resolve_feedback(self: OrderProduct, _info):
return self.feedback
return Feedback.objects.get(order_product=self)
def resolve_attributes(self, _info):
return camelize(self.attributes)

View file

@ -1944,19 +1944,26 @@ class OrderProduct(ExportModelOperationsMixin("order_product"), NiceModel): # t
return self.download.url
return ""
def do_feedback(self, rating=10, comment="", action="add") -> Optional["Feedback"]:
def do_feedback(self, rating=10, comment="", action="add") -> Optional["Feedback"] | int:
if not self.order:
raise ValueError(_("order product must have an order"))
if action not in ["add", "remove"]:
raise ValueError(_(f"wrong action specified for feedback: {action}"))
if action == "remove" and self.feedback:
self.feedback.delete()
return None
if action == "add" and not self.feedback:
feedback_qs = Feedback.objects.filter(order_product=self)
feedback_exists = feedback_qs.exists()
if action == "remove":
if feedback_exists:
return feedback_qs.delete()[0]
if action == "add":
if not feedback_exists:
if self.order.status not in ["MOMENTAL", "PENDING", "FAILED"]:
return Feedback.objects.create(rating=rating, comment=comment, order_product=self)
else:
raise ValueError(_("you cannot feedback an order which is not received"))
return None