Commit 7e7468ec by Yvon

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

parent b76bd834
Pipeline #2870 failed with stage
in 1 minute 5 seconds
......@@ -552,6 +552,13 @@ def delete_shift_template_registration(request):
if permanent_unsuscribe is True:
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:
res["error"] = str(e)
......@@ -895,26 +902,42 @@ def delete_pair(request):
return HttpResponse(template.render(context, request))
elif request.method == 'POST':
if CagetteUser.are_credentials_ok(request):
api = OdooAPI()
data = json.loads(request.body.decode())
child_id = int(data['child']['id'])
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'])
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]
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}
if 'gone' in data and 'child' in data['gone']:
child_update_fields['cooperative_state'] = "gone"
for id in prev_child:
api.update("res.partner", [id], child_update_fields)
if 'gone' in data and 'parent' in data['gone']:
api.update("res.partner", [parent['id']], {'cooperative_state': "gone", "is_former_associated_people": True})
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 = 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'])
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]
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}
if 'gone' in data and 'child' in data['gone']:
child_update_fields['cooperative_state'] = "gone"
for id in prev_child:
api.update("res.partner", [id], child_update_fields)
if 'gone' in data and 'parent' in data['gone']:
api.update("res.partner", [parent['id']], {'cooperative_state': "gone", "is_former_associated_people": True})
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