def update(self): response = self._response.copy() try: id_ = self.request.matchdict.get("id") if not id_ is None: params = json.loads(self.request.params["dataset"]) item = Background.get(self.session, params["id"]) item.weight = params["weight"] else: for params in json.loads(self.request.params["dataset"]): item = Background.get(self.session, params["id"]) item.weight = params["weight"] except KeyError as e: self.log.exception("Bad request.") self.session.rollback() self.request.response.status = 400 response["success"] = False response["msg"] = str(e) except NoResultFound as e: self.log.exception("Cannot found Background.") self.session.rollback() self.request.response.status = 400 response["success"] = False response["msg"] = str(e) except TypeError as e: self.log.exception("Invalid name") self.request.response.status = 400 response["success"] = False response["msg"] = str(e) except Exception as e: self.log.exception("Unknown error.") self.session.rollback() self.request.response.status = 500 response["msg"] = str(e) else: self.session.commit() response["success"] = True response["dataset"] = [] response["dataset_length"] = len(response["dataset"]) response["msg"] = self.request.translate("Background was updated.") self.proxy.invalidate(url=item.url) finally: return response
def delete(self): response = self._response.copy() try: banner = Background.get(self.session, int(self.request.matchdict["id"])) banner.delete() except (KeyError, ValueError, NoResultFound) as e: self.log.exception("Cannot get the background to delete.") self.session.rollback() self.request.response.status = 400 response["success"] = False response["msg"] = str(e) except Exception as e: self.log.exception("Unknown error.") self.session.rollback() self.request.response.status = 500 response["msg"] = str(e) else: self.session.commit() response["success"] = True response["dataset"] = [banner.to_dict()] response["dataset_length"] = len(response["dataset"]) response["msg"] = self.request.translate("Background was deleted.") self.proxy.invalidate(url=banner.url) finally: return response