Commit 71bfd38a by Damien Moulard

Merge branch '4982-debinomisation-si-titulaire-parti-e' into 'dev_cooperatic'

extract core of method delete_pair and call it after set_cooperative_state('gone')

See merge request !234
parents b76bd834 7e7468ec
Pipeline #2872 failed with stage
in 1 minute 4 seconds
...@@ -552,6 +552,13 @@ def delete_shift_template_registration(request): ...@@ -552,6 +552,13 @@ def delete_shift_template_registration(request):
if permanent_unsuscribe is True: if permanent_unsuscribe is True:
res["set_done"] = cm.set_cooperative_state("gone") res["set_done"] = cm.set_cooperative_state("gone")
if res["set_done"]:
""" Delete pair(s?) of partner if it is a parent to improve statistics (#4810) """
api = OdooAPI()
associated_members = api.search_read('res.partner', [['parent_id', '=', partner_id]], ['id'])
for am in associated_members:
data = {"child": {"id": am["id"]}, "gone": ["parent", "child"]}
delete_pair_core(data)
except Exception as e: except Exception as e:
res["error"] = str(e) res["error"] = str(e)
...@@ -895,14 +902,38 @@ def delete_pair(request): ...@@ -895,14 +902,38 @@ def delete_pair(request):
return HttpResponse(template.render(context, request)) return HttpResponse(template.render(context, request))
elif request.method == 'POST': elif request.method == 'POST':
if CagetteUser.are_credentials_ok(request): if CagetteUser.are_credentials_ok(request):
api = OdooAPI()
data = json.loads(request.body.decode()) data = json.loads(request.body.decode())
delete_pair_core(data)
response = JsonResponse({"message": "Succesfuly unpaired members"}, status=200)
else:
response = JsonResponse({"message": "Unauthorized"}, status=403)
return response
else:
return JsonResponse({"message": "Method Not Allowed"}, status=405)
def delete_pair_core(data):
"""
Core of delete_pair
argument example :
{
"child": {
"id": "1620"
},
"gone": [
"parent",
"child"
]
}
"""
api = OdooAPI()
child_id = int(data['child']['id']) child_id = int(data['child']['id'])
child = api.search_read('res.partner', [['id', '=', child_id]], ['email', 'id', 'parent_id'])[0] child = api.search_read('res.partner', [['id', '=', child_id]], ['email', 'id', 'parent_id'])[0]
child_accounts = api.search_read('res.partner', [['email', '=', child['email']]], ['id', 'email']) child_accounts = api.search_read('res.partner', [['email', '=', child['email']]], ['id', 'email'])
prev_child = [x['id'] for x in child_accounts if x['id'] != child_id] prev_child = [x['id'] for x in child_accounts if x['id'] != child_id]
parent = api.search_read('res.partner', [['id', '=', child['parent_id'][0]]], ['cooperative_state'])[0] parent = api.search_read('res.partner', [['id', '=', child['parent_id'][0]]], ['cooperative_state'])[0]
api.update('res.partner', [child_id], {"parent_id": False, "is_associated_people": False, "active": False, "is_former_associated_people": True}) api.update('res.partner', [child_id], {"parent_id": False, "is_associated_people": False, "active": False,
"is_former_associated_people": True})
child_update_fields = {'cooperative_state': "unsubscribed", "is_former_associated_people": True} child_update_fields = {'cooperative_state': "unsubscribed", "is_former_associated_people": True}
if 'gone' in data and 'child' in data['gone']: if 'gone' in data and 'child' in data['gone']:
child_update_fields['cooperative_state'] = "gone" child_update_fields['cooperative_state'] = "gone"
...@@ -910,11 +941,3 @@ def delete_pair(request): ...@@ -910,11 +941,3 @@ def delete_pair(request):
api.update("res.partner", [id], child_update_fields) api.update("res.partner", [id], child_update_fields)
if 'gone' in data and 'parent' in data['gone']: if 'gone' in data and 'parent' in data['gone']:
api.update("res.partner", [parent['id']], {'cooperative_state': "gone", "is_former_associated_people": True}) api.update("res.partner", [parent['id']], {'cooperative_state': "gone", "is_former_associated_people": True})
response = JsonResponse({"message": "Succesfuly unpaired members"}, status=200)
else:
response = JsonResponse({"message": "Unauthorized"}, status=403)
return response
else:
return JsonResponse({"message": "Method Not Allowed"}, status=405)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment