Skip to content

Commit

Permalink
[12.0][IMP] base_tier_validation_forward, allow backward tier
Browse files Browse the repository at this point in the history
  • Loading branch information
AdriaGForgeFlow authored and HviorForgeFlow committed Nov 8, 2022
1 parent 609ad1d commit a560533
Show file tree
Hide file tree
Showing 7 changed files with 165 additions and 13 deletions.
5 changes: 5 additions & 0 deletions base_tier_validation_forward/models/tier_definition.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,8 @@ class TierDefinition(models.Model):
default=False,
help="Allow option to 'Forward' to different person.",
)
backward = fields.Boolean(
string="Backward",
help="If the forwarded step is approved, "
"auto forward back again to finish the step.",
)
5 changes: 5 additions & 0 deletions base_tier_validation_forward/models/tier_review.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ class TierReview(models.Model):
approve_sequence = fields.Boolean(
compute="_compute_definition_data", store=True, readonly=False,
)
origin_id = fields.Many2one(
comodel_name="tier.review",
copy=False,
help="Reference to origin tier review that createก this tier.",
)

@api.depends(
"definition_id.name",
Expand Down
39 changes: 38 additions & 1 deletion base_tier_validation_forward/models/tier_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class TierValidation(models.AbstractModel):
_inherit = "tier.validation"

can_forward = fields.Boolean(compute="_compute_can_forward")
can_backward = fields.Boolean(compute="_compute_can_backward")

def _compute_can_forward(self):
for rec in self:
Expand All @@ -18,6 +19,16 @@ def _compute_can_forward(self):
definitions = reviews.mapped("definition_id")
rec.can_forward = True in definitions.mapped("has_forward")

def _compute_can_backward(self):
for rec in self:
if not rec.can_review:
rec.can_backward = False
continue
sequences = self._get_sequences_to_approve(self.env.user)
reviews = rec.review_ids.filtered(lambda l: l.sequence in sequences)
definitions = reviews.mapped("definition_id")
rec.can_backward = True in definitions.mapped("backward")

@api.model
def _calc_reviews_validated(self, reviews):
"""Override for different validation policy."""
Expand Down Expand Up @@ -73,7 +84,7 @@ def _notify_forwarded_reviews(self):
post = "message_post"
if hasattr(self, post):
# Notify state change
getattr(self, post)(
getattr(self.sudo(), post)(
subtype=self._get_forwarded_notification_subtype(),
body=self._notify_forwarded_reviews_body(),
)
Expand All @@ -88,3 +99,29 @@ def _notify_forwarded_reviews_body(self):
"A review was forwarded from {} {}".format(self.env.user.name, comment)
)
return _("A review was forwarded by %s.") % (self.env.user.name)

def _validate_tier(self, tiers=False):
self.ensure_one()
self._create_backward(tiers)
super()._validate_tier(tiers=tiers)

def _create_backward(self, tiers):
""" Find the forward tier that require to backward """
tier_reviews = tiers or self.review_ids
to_backward_reviews = tier_reviews.filtered(
lambda r: r.status == "pending"
and (self.env.user in r.reviewer_ids)
and r.origin_id.definition_id.has_forward # Forward
and r.origin_id.definition_id.backward # To Backward
)
for review in to_backward_reviews:
review.origin_id.copy(
{
"sequence": round(review.sequence + 0.1, 2),
"done_by": False,
"reviewed_date": False,
"status": "pending",
"comment": False,
"origin_id": False,
}
)
68 changes: 68 additions & 0 deletions base_tier_validation_forward/tests/test_tier_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,71 @@ def test_01_forward_tier(self):
wiz = wizard.save()
wiz.add_comment()
self.assertTrue(record.validated)

def test_02_forward_tier_backward(self):
# Create new test record
test_record = self.test_model.create({"test_field": 2.5})
# Create tier definitions
self.tier_def_obj.create(
{
"model_id": self.tester_model.id,
"review_type": "individual",
"reviewer_id": self.test_user_2.id,
"definition_domain": "[('test_field', '>', 1.0)]",
"approve_sequence": True,
"has_forward": True,
"backward": True,
}
)
# Request validation
review = test_record.with_user(self.test_user_2.id).request_validation()
self.assertTrue(review)
record = test_record.with_user(self.test_user_1.id)
record.invalidate_cache()
record.validate_tier()
self.assertFalse(record.can_forward)
self.assertFalse(record.can_backward)
# User 2 forward to user 1
record = test_record.with_user(self.test_user_2.id)
record.invalidate_cache()
self.assertTrue(record.can_forward)
self.assertTrue(record.can_backward)
res = record.forward_tier()
ctx = res.get("context")
wizard = Form(
self.env["tier.validation.forward.wizard"]
.with_user(self.test_user_2.id)
.with_context(ctx)
)
wizard.forward_reviewer_id = self.test_user_1
wizard.forward_description = "Please review again"
wizard.backward = True
wiz = wizard.save()
wiz.add_forward()
# Newly created forwarded review will have no definition and will have origin
record = test_record.with_user(self.test_user_2.id)
record.invalidate_cache()
self.assertTrue(record.review_ids.filtered(lambda l: not l.definition_id))
self.assertTrue(record.review_ids.filtered(lambda l: l.origin_id))
# User 1 validate
res = record.with_user(self.test_user_1.id).validate_tier()
ctx = res.get("context")
wizard = Form(
self.env["comment.wizard"].with_user(self.test_user_1.id).with_context(ctx)
)
wizard.comment = "Forward tier is reviewed"
wiz = wizard.save()
wiz.add_comment()
self.assertFalse(record.validated)
# Newly created review back to the original user
record = test_record.with_user(self.test_user_2.id)
record.invalidate_cache()
# There are now two tier reviews for user 2
self.assertEqual(
len(record.review_ids.filtered(
lambda l: l.reviewer_id == self.test_user_2)),
2
)
# User 2 does final validation
record.validate_tier()
self.assertTrue(record.validated)
15 changes: 14 additions & 1 deletion base_tier_validation_forward/views/tier_definition_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,20 @@
<field name="inherit_id" ref="base_tier_validation.tier_definition_view_form" />
<field name="arch" type="xml">
<field name="approve_sequence" position="after">
<field name="has_forward" />
<label for="has_forward" />
<div name="has_forward_div" class="o_row">
<field name="has_forward" />
</div>
<label for="backward" invisible="1" />
<div
name="backward_div"
class="o_row"
attrs="{'invisible': [('has_forward', '=', False)]}"
title="If the forwarded step is approved, auto forward back again to finish the step."
>
<field name="backward" />
<span>Allow backward after the forward step is validated</span>
</div>
</field>
</field>
</record>
Expand Down
44 changes: 33 additions & 11 deletions base_tier_validation_forward/wizard/forward_wizard.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,37 @@ class ValidationForwardWizard(models.TransientModel):
forward_description = fields.Char()
has_comment = fields.Boolean(string="Allow Comment", default=True)
approve_sequence = fields.Boolean(string="Approve by sequence", default=True,)
can_backward = fields.Boolean(
string="Can ask for review",
compute="_compute_can_backward"
)
backward = fields.Boolean(
string="Ask for review",
default=False
)

def _compute_can_backward(self):
self.ensure_one()
record = self.env[self.res_model].browse(self.res_id)
self.can_backward = record.can_backward

def _get_tier_review_data(self, rec, prev_review):
data = {
"name": self.forward_description,
"model": rec._name,
"res_id": rec.id,
"sequence": round(prev_review.sequence + 0.1, 2),
"requested_by": self.env.uid,
"review_type": "individual",
"reviewer_id": self.forward_reviewer_id.id,
"has_comment": self.has_comment,
"approve_sequence": self.approve_sequence,
}
if self.backward:
data.update({
"origin_id": prev_review.id,
})
return data

def add_forward(self):
""" Add extra step, with specific reviewer """
Expand All @@ -27,18 +58,9 @@ def add_forward(self):
{"comment": _(">> %s") % self.forward_reviewer_id.display_name}
)
prev_reviews = prev_comment.add_comment()
prev_review = prev_reviews.sorted("sequence")[-1:] # Get max sequence
self.env["tier.review"].create(
{
"name": self.forward_description,
"model": rec._name,
"res_id": rec.id,
"sequence": max(prev_reviews.mapped("sequence")) + 0.1,
"requested_by": self.env.uid,
"review_type": "individual",
"reviewer_id": self.forward_reviewer_id.id,
"has_comment": self.has_comment,
"approve_sequence": self.approve_sequence,
}
self._get_tier_review_data(rec, prev_review)
)
rec.invalidate_cache()
rec.review_ids._compute_can_review()
2 changes: 2 additions & 0 deletions base_tier_validation_forward/wizard/forward_wizard_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@
<group>
<field name="forward_reviewer_id" />
<field name="forward_description" required="1" />
<field name="backward" />
</group>
<group>
<field name="has_comment" invisible="1" />
<field name="can_backward" invisible="1" />
<field name="approve_sequence" invisible="1" />
</group>
</group>
Expand Down

0 comments on commit a560533

Please sign in to comment.