def update_template(self, data: (dict, DocapiTemplate), request_user: UserModel) -> str: """ Update new DocapiTemplat Object Args: data: init data request_user: current user, to detect who triggered event Returns: Public ID of the DocapiTemplate in database """ if isinstance(data, dict): update_object = DocapiTemplate(**data) elif isinstance(data, DocapiTemplate): update_object = data else: raise DocapiTemplateManagerUpdateError( f'Could not update template with ID: {data.get_public_id()}') update_object.last_execute_date = datetime.utcnow() ack = self._update(collection=DocapiTemplate.COLLECTION, public_id=update_object.get_public_id(), data=update_object.to_database()) if self._event_queue: event = Event( "cmdb.docapi.updated", { "id": update_object.get_public_id(), "active": update_object.get_active(), "user_id": request_user.get_public_id() }) self._event_queue.put(event) return ack.acknowledged
def insert_template(self, data: (DocapiTemplate, dict)) -> int: """ Insert new DocapiTemplate Object Args: data: init data Returns: Public ID of the new DocapiTemplate in database """ if isinstance(data, dict): try: new_object = DocapiTemplate(**data) except CMDBError as e: raise DocapiTemplateManagerInsertError(e) elif isinstance(data, DocapiTemplate): new_object = data try: ack = self.dbm.insert(collection=DocapiTemplate.COLLECTION, data=new_object.to_database()) if self._event_queue: event = Event( "cmdb.docapi.added", { "id": new_object.get_public_id(), "active": new_object.get_active(), "user_id": new_object.get_author_id() }) self._event_queue.put(event) except CMDBError as e: raise DocapiTemplateManagerInsertError(e) return ack