you need this line:
xmlsec.tree.add_ids(template, ["ID"])
place it before signature_node = xmlsec.tree.find_node(template, xmlsec.constants.NodeSignature) and it will work.