def batch_update(self):

        response = self._response.copy()

        try:
            for params in json.loads(self.request.params['dataset']):
                filters = (MediaItemPage.id == params['id'],)
                values = dict(weight=params['weight'])
                MediaItemPage.search(self.session,
                                     filters=filters,
                                     return_query=True).update(values)

        except KeyError as e:
            self.log.exception('Not needed param in the request.')
            self.session.rollback()
            self.request.response.status = 400
            response['msg'] = self.request.translate(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("MediaItemPage updated.")

        finally:
            return response
    def batch_delete(self):

        response = self._response.copy()

        try:
            for params in json.loads(self.request.params['dataset']):
                MediaItemPage.remove(self.session, params['id'])

        except KeyError as e:
            self.log.exception('Not param in the request.')
            self.session.rollback()
            self.request.response.status = 400
            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("MediaItemPage found.")

        finally:
            return response
    def delete(self):

        response = self._response.copy()

        try:
            id_ = self.request.matchdict['id']
            MediaItemPage.remove(self.session, id_)

        except KeyError as e:
            self.log.exception('Not ID param in the request.')
            self.session.rollback()
            self.request.response.status = 400
            response['msg'] = self.request.translate("Missing parameter: 'id'.")

        except NoResultFound as e:
            msg = "No MediaItemPage found: %s" % id_
            self.log.exception(msg)
            self.session.rollback()
            self.request.response.status = 404
            response['msg'] = self.request.translate(msg)

        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'] = [id_]
            response['dataset_length'] = len(response['dataset'])
            response['msg'] = self.request.translate("MediaItemPage found.")

        finally:
            return response
    def update(self):

        response = self._response.copy()

        try:
            id_ = self.request.matchdict['id']
            # Convert JSON request param into dictionary.
            params = json.loads(self.request.params['dataset'])
            item = MediaItemPage.get(self.session, id_)
            item.weight = params['weight']
            translation = params['translations'][0]
            info = MediaItemPageInfo.get(self.session, translation['id'])
            info.label = translation['label']
            info.title = info.label
            info.url_part = urlify(info.title)
            info.content = translation['content']

        except KeyError as e:
            self.log.exception('Not needed param in the request.')
            self.session.rollback()
            self.request.response.status = 400
            response['msg'] = self.request.translate(str(e))

        except NoResultFound as e:
            msg = "No MediaItemPage found: %s" % id_
            self.log.exception(msg)
            self.session.rollback()
            self.request.response.status = 404
            response['msg'] = self.request.translate(msg)

        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("MediaItemPage updated.")

        finally:
            return response