import jingrow


def execute():
	maps = {
		"site": "Site",
		"server": "Server",
		"release_group": "Release Group",
	}

	for permission in jingrow.get_all("Jcloude Role Permission", fields=["role", *maps.keys()]):
		role = jingrow.get_pg("Jcloude Role", permission.role)
		to_append = filter(
			lambda x: x.get("document_name"),
			map(
				lambda key: {
					"document_type": maps[key],
					"document_name": permission[key],
				},
				maps.keys(),
			),
		)

		for item in to_append:
			role.append("resources", item)

		role.save()
		jingrow.db.commit()
