Ejemplo n.º 1
0
    def updatetool(self,
                   tool,
                   full_sync_flag="false",
                   directory_to_import_from=None):
        """Start Tool Update """
        tooldata = tool.get("tool_data")
        localTool = self.toolDB.get_tool_by_name(tooldata["name"])
        ToolHelperService.check_if_tool_data_is_valid(tooldata, localTool)
        tool_id = str(localTool.get("_id"))
        try:
            if tooldata.get("operation").lower() == "update":
                ToolHelperService.add_update_tool(tooldata, tool_id,
                                                  self.logo_path,
                                                  directory_to_import_from,
                                                  self.full_logo_path)
            versions = tooldata.get("versions")
            for record in versions:
                if record.get("operation") not in [
                        "delete", "update", "insert"
                ]:
                    continue
                VersionsData = record
                localVersion = self.versionsDB.get_version_by_tool_id_name_and_number(
                    tool_id, VersionsData["version_name"],
                    VersionsData["version_number"])
                if localVersion:
                    version_id = str(localVersion["_id"])
                if record.get("operation").lower(
                ) == "delete" and full_sync_flag == "true":
                    SyncHelperService.delete_version_and_related_builds(
                        version_id)
                else:
                    # HANDLE VERSION
                    # WE SEE THAT THE VERSION HAS TO BE BE UPDATED OR INSERTED

                    # IF ITS A EXISTING VERSION WE WILL ALREADY HAVE VERSION_ID
                    if record.get("operation").lower() == "update":
                        ToolHelperService.add_update_version(
                            VersionsData, tool_id, version_id, False)
                    # IF ITS A NEW VERSION
                    if record.get("operation").lower() == "insert":
                        version_id = ToolHelperService.add_update_version(
                            VersionsData, tool_id, None, False)

                    # HANLDE BUILD
                    if VersionsData.get('build') is not None and len(
                            VersionsData.get('build')) > 0:
                        builds_handled = [
                        ]  # WE need to deactivate all other builds
                        for build in VersionsData.get('build'):
                            BuildHelperService.add_update_build(
                                build, version_id,
                                join(
                                    directory_to_import_from,
                                    os.path.join(
                                        "artifacts",
                                        VersionsData["repository_to_use"])))
                            builds_handled.append(build["build_number"])
                        # SUPPOSE THE ACCOUNT SENDS 2 BUILDS THAT ARE  ACTIVE THEY WILL BE HANDLED
                        # BUT ALL OTHER BUILDS SHOULD BE MADE INACTIVE IN LOCAL
                        for build in self.buildsDB.get_all_builds(version_id):
                            if build["build_number"] not in builds_handled:
                                build_id = build.get("_id")
                                build["_id"] = {}
                                build["_id"]["oid"] = str(build_id)
                                build["status"] = "0"
                                self.buildsDB.update_build(build)

                    # HANLDE DOCUMENT
                    if VersionsData.get('document') is not None \
                            and len(VersionsData.get('document')) > 0:
                        HelperServices.add_update_documents(
                            VersionsData['document']['documents'], version_id)

                    # HANLDE DEPLOYMENT FIELDS
                    if VersionsData.get('deployment_field') is not None \
                            and len(VersionsData.get('deployment_field')) > 0:
                        HelperServices.add_update_deployment_fields(
                            VersionsData['deployment_field']['fields'],
                            version_id)

                    # HANLDE MEDIA FILES
                    if VersionsData.get('media_file') is not None \
                            and len(VersionsData.get('media_file')) > 0:
                        HelperServices.add_update_media_files(
                            VersionsData['media_file']['media_files'],
                            version_id, directory_to_import_from,
                            self.full_media_files_path, self.media_files_path)
            return {
                "result": "success",
                "message": tooldata["name"] + " was updated"
            }
        except Exception as e_value:  # catch *all* exceptions
            traceback.print_exc()
            return {"result": "failed", "message": str(e_value)}
Ejemplo n.º 2
0
 def addtool(self,
             tool,
             full_sync_flag="false",
             directory_to_import_from=None):
     """Start Tool Addition"""
     # MAINTAINING ARRAY TO MEMORISE INSERTED IDS
     inserted_tools_list = []
     inserted_build_list = []
     inserted_versions_list = []
     inserted_deployment_fields_list = []
     inserted_media_files_list = []
     inserted_documents_list = []
     tooldata = tool.get("tool_data")
     ToolHelperService.check_if_tool_exists(tooldata)
     try:
         tooldata = tool.get("tool_data")
         tool_inserted = ToolHelperService.add_update_tool(
             tool.get("tool_data"), None, self.logo_path,
             directory_to_import_from, self.full_logo_path)
         inserted_tools_list.append(tool_inserted)
         versions = tooldata.get("versions")
         if versions is None:
             raise Exception("versions is missing from tool_data")
         for VersionsData in versions:
             Versionresult = ToolHelperService.add_update_version(
                 VersionsData, tool_inserted, None, False)
             inserted_versions_list.append(Versionresult)
             # preparing version data
             # preparing DeploymentFields data
             if VersionsData.get("deployment_field") is not None and len(
                     VersionsData.get("deployment_field")) > 0:
                 inserted_deployment_fields_list.append(
                     HelperServices.add_update_deployment_fields(
                         VersionsData.get("deployment_field")["fields"],
                         Versionresult))
             if VersionsData.get('media_file') is not None \
                     and len(VersionsData.get('media_file')) > 0:
                 inserted_media_files_list.append(
                     HelperServices.add_update_media_files(
                         VersionsData.get('media_file')['media_files'],
                         Versionresult, directory_to_import_from,
                         self.full_media_files_path, self.media_files_path))
             # preparing Document data
             if VersionsData.get("document") is not None and len(
                     VersionsData.get("document")) > 0:
                 inserted_documents_list.append(
                     HelperServices.add_update_documents(
                         VersionsData.get("document")["documents"],
                         Versionresult))
             # preparing Build data
             if VersionsData.get("build") is not None and len(
                     VersionsData.get("build")):
                 for build in VersionsData.get("build"):
                     inserted_build_list.append(
                         BuildHelperService.add_update_build(
                             build, Versionresult,
                             join(
                                 directory_to_import_from,
                                 os.path.join(
                                     "artifacts",
                                     VersionsData["repository_to_use"]))))
         return {
             "result": "success",
             "message": tooldata["name"] + " was inserted"
         }
     except Exception as e_value:  # catch *all* exceptions
         traceback.print_exc()
         for rec in inserted_deployment_fields_list:
             self.deploymentFieldsDB.DeleteDeploymentFields(rec)
         for rec in inserted_media_files_list:
             self.mediaFilesDB.delete_media_file(rec)
         for rec in inserted_documents_list:
             self.documentsDB.DeleteDocuments(rec)
         for rec in inserted_versions_list:
             self.versionsDB.delete_version(rec)
         for rec in inserted_tools_list:
             self.toolDB.delete_tool(rec)
         for rec in inserted_build_list:
             self.buildsDB.delete_build(rec)
         return {"result": "failed", "message": str(e_value)}