def get_template(self, public_id: int) -> DocapiTemplate: try: result = self.dbm.find_one(collection=DocapiTemplate.COLLECTION, public_id=public_id) except (DocapiTemplateManagerGetError, Exception) as err: LOGGER.error(err) raise err return DocapiTemplate(**result)
def get_template_list(params: CollectionParameters): template_manager = DocapiTemplateManager( database_manager=current_app.database_manager) body = request.method == 'HEAD' try: iteration_result: IterationResult[ DocapiTemplate] = template_manager.get_templates( filter=params.filter, limit=params.limit, skip=params.skip, sort=params.sort, order=params.order) types = [ DocapiTemplate.to_json(type) for type in iteration_result.results ] api_response = GetMultiResponse(types, total=iteration_result.total, params=params, url=request.url, model=DocapiTemplate.MODEL, body=body) except ManagerIterationError as err: return abort(400, err.message) except ManagerGetError as err: return abort(404, err.message) return api_response.make_response()
def get_all_templates(self): template_list = [] for template in self.dbm.find_all(collection=DocapiTemplate.COLLECTION): try: template_list.append(DocapiTemplate(**template)) except CMDBError: continue return template_list
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 get_template_by_name(self, **requirements) -> DocapiTemplate: try: templates = self._get_many(collection=DocapiTemplate.COLLECTION, limit=1, **requirements) if len(templates) > 0: return DocapiTemplate(**templates[0]) else: raise DocapiTemplateManagerGetError(err='More than 1 type matches this requirement') except (CMDBError, Exception) as e: raise DocapiTemplateManagerGetError(err=e)
def get_templates_by(self, **requirements): try: ack = [] templates = self._get_many(collection=DocapiTemplate.COLLECTION, limit=0, **requirements) for template in templates: ack.append(DocapiTemplate(**template)) return ack except (CMDBError, Exception) as e: raise DocapiTemplateManagerGetError(err=e)
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
def update_template(request_user: UserModel): from bson import json_util add_data_dump = json.dumps(request.json) new_tpl_data = None try: new_tpl_data = json.loads(add_data_dump, object_hook=json_util.object_hook) except TypeError as err: LOGGER.warning(err) abort(400) try: update_tpl_instance = DocapiTemplate(**new_tpl_data) except CMDBError: return abort(400) try: docapi_tpl_manager.update_template(update_tpl_instance, request_user) except DocapiTemplateManagerUpdateError: return abort(500) resp = make_response(update_tpl_instance) return resp
def add_template(request_user: UserModel): from bson import json_util add_data_dump = json.dumps(request.json) try: new_tpl_data = json.loads(add_data_dump, object_hook=json_util.object_hook) new_tpl_data['public_id'] = docapi_tpl_manager.get_new_id() new_tpl_data['author_id'] = request_user.get_public_id() except TypeError as err: LOGGER.warning(err) abort(400) try: template_instance = DocapiTemplate(**new_tpl_data) except CMDBError as err: LOGGER.debug(err) return abort(400) try: ack = docapi_tpl_manager.insert_template(template_instance) except DocapiTemplateManagerInsertError: return abort(500) resp = make_response(ack) return resp