+ def create_admin_project(self):
+ """
+ Creates a new project 'admin' into database if it doesn't exist. Useful for initialization.
+ :return: _id identity of the 'admin' project
+ """
+
+ # projects = self.db.get_one("projects", fail_on_empty=False, fail_on_more=False)
+ project_desc = {"name": "admin"}
+ projects = self.backend.get_project_list(project_desc)
+ if projects:
+ return projects[0]["_id"]
+ now = time()
+ project_desc["_id"] = str(uuid4())
+ project_desc["_admin"] = {"created": now, "modified": now}
+ pid = self.backend.create_project(project_desc)
+ self.logger.info("Project '{}' created at database".format(project_desc["name"]))
+ return pid
+
+ def create_admin_user(self, project_id):
+ """
+ Creates a new user admin/admin into database if database is empty. Useful for initialization
+ :return: _id identity of the inserted data, or None
+ """
+ # users = self.db.get_one("users", fail_on_empty=False, fail_on_more=False)
+ users = self.backend.get_user_list()
+ if users:
+ return None
+ # user_desc = {"username": "admin", "password": "admin", "projects": [project_id]}
+ now = time()
+ user_desc = {"username": "admin", "password": "admin", "_admin": {"created": now, "modified": now}}
+ if project_id:
+ pid = project_id
+ else:
+ # proj = self.db.get_one("projects", {"name": "admin"}, fail_on_empty=False, fail_on_more=False)
+ proj = self.backend.get_project_list({"name": "admin"})
+ pid = proj[0]["_id"] if proj else None
+ # role = self.db.get_one("roles", {"name": "system_admin"}, fail_on_empty=False, fail_on_more=False)
+ roles = self.backend.get_role_list({"name": "system_admin"})
+ if pid and roles:
+ user_desc["project_role_mappings"] = [{"project": pid, "role": roles[0]["_id"]}]
+ uid = self.backend.create_user(user_desc)
+ self.logger.info("User '{}' created at database".format(user_desc["username"]))
+ return uid
+