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:
parent
17a2b01894
commit
79714750fa
2 changed files with 17 additions and 10 deletions
|
|
@ -416,7 +416,7 @@ class OrderProductType(DjangoObjectType):
|
||||||
description = _("order products")
|
description = _("order products")
|
||||||
|
|
||||||
def resolve_feedback(self: OrderProduct, _info):
|
def resolve_feedback(self: OrderProduct, _info):
|
||||||
return self.feedback
|
return Feedback.objects.get(order_product=self)
|
||||||
|
|
||||||
def resolve_attributes(self, _info):
|
def resolve_attributes(self, _info):
|
||||||
return camelize(self.attributes)
|
return camelize(self.attributes)
|
||||||
|
|
|
||||||
|
|
@ -1944,19 +1944,26 @@ class OrderProduct(ExportModelOperationsMixin("order_product"), NiceModel): # t
|
||||||
return self.download.url
|
return self.download.url
|
||||||
return ""
|
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:
|
if not self.order:
|
||||||
raise ValueError(_("order product must have an order"))
|
raise ValueError(_("order product must have an order"))
|
||||||
if action not in ["add", "remove"]:
|
if action not in ["add", "remove"]:
|
||||||
raise ValueError(_(f"wrong action specified for feedback: {action}"))
|
raise ValueError(_(f"wrong action specified for feedback: {action}"))
|
||||||
if action == "remove" and self.feedback:
|
|
||||||
self.feedback.delete()
|
feedback_qs = Feedback.objects.filter(order_product=self)
|
||||||
return None
|
feedback_exists = feedback_qs.exists()
|
||||||
if action == "add" and not self.feedback:
|
|
||||||
if self.order.status not in ["MOMENTAL", "PENDING", "FAILED"]:
|
if action == "remove":
|
||||||
return Feedback.objects.create(rating=rating, comment=comment, order_product=self)
|
if feedback_exists:
|
||||||
else:
|
return feedback_qs.delete()[0]
|
||||||
raise ValueError(_("you cannot feedback an order which is not received"))
|
|
||||||
|
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
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue