def show(self, id): """Return a corpus. :URL: ``GET /corpora/id`` :param str id: the ``id`` value of the corpus to be returned. :returns: a corpus model object. """ corpus = h.eagerload_corpus(Session.query(Corpus)).get(id) if corpus: return corpus else: response.status_int = 404 return {'error': 'There is no corpus with id %s' % id}
def delete(self, id): """Delete an existing corpus and return it. :URL: ``DELETE /corpora/id`` :param str id: the ``id`` value of the corpus to be deleted. :returns: the deleted corpus model. """ corpus = h.eagerload_corpus(Session.query(Corpus)).get(id) if corpus: corpus_dict = corpus.get_dict() backup_corpus(corpus_dict) Session.delete(corpus) Session.commit() remove_corpus_directory(corpus) return corpus_dict else: response.status_int = 404 return {'error': 'There is no corpus with id %s' % id}
def index(self): """Get all corpus resources. :URL: ``GET /corpora`` with optional query string parameters for ordering and pagination. :returns: a list of all corpus resources. .. note:: See :func:`utils.add_order_by` and :func:`utils.add_pagination` for the query string parameters that effect ordering and pagination. """ try: query = h.eagerload_corpus(Session.query(Corpus)) query = h.add_order_by(query, dict(request.GET), self.query_builder_for_ordering) return h.add_pagination(query, dict(request.GET)) except Invalid, e: response.status_int = 400 return {'errors': e.unpack_errors()}
def edit(self, id): """Return a corpus and the data needed to update it. :URL: ``GET /corpora/edit`` :param str id: the ``id`` value of the corpus that will be updated. :returns: a dictionary of the form:: {"corpus": {...}, "data": {...}} where the value of the ``corpus`` key is a dictionary representation of the corpus and the value of the ``data`` key is an empty dictionary. """ corpus = h.eagerload_corpus(Session.query(Corpus)).get(id) if corpus: return {'data': get_data_for_new_edit(request.GET), 'corpus': corpus} else: response.status_int = 404 return {'error': 'There is no corpus with id %s' % id}
def update(self, id): """Update a corpus and return it. :URL: ``PUT /corpora/id`` :Request body: JSON object representing the corpus with updated attribute values. :param str id: the ``id`` value of the corpus to be updated. :returns: the updated corpus model. """ corpus = h.eagerload_corpus(Session.query(Corpus)).get(int(id)) if corpus: try: schema = CorpusSchema() values = json.loads(unicode(request.body, request.charset)) state = h.get_state_object(values) state.id = id state.config = config data = schema.to_python(values, state) corpus_dict = corpus.get_dict() corpus = update_corpus(corpus, data) # corpus will be False if there are no changes (cf. update_corpus). if corpus: backup_corpus(corpus_dict) Session.add(corpus) Session.commit() return corpus else: response.status_int = 400 return { 'error': u'The update request failed because the submitted data were not new.' } except h.JSONDecodeError: response.status_int = 400 return h.JSONDecodeErrorResponse except Invalid, e: response.status_int = 400 return {'errors': e.unpack_errors()}
def edit(self, id): """Return a corpus and the data needed to update it. :URL: ``GET /corpora/edit`` :param str id: the ``id`` value of the corpus that will be updated. :returns: a dictionary of the form:: {"corpus": {...}, "data": {...}} where the value of the ``corpus`` key is a dictionary representation of the corpus and the value of the ``data`` key is an empty dictionary. """ corpus = h.eagerload_corpus(Session.query(Corpus)).get(id) if corpus: return { 'data': get_data_for_new_edit(request.GET), 'corpus': corpus } else: response.status_int = 404 return {'error': 'There is no corpus with id %s' % id}
def update(self, id): """Update a corpus and return it. :URL: ``PUT /corpora/id`` :Request body: JSON object representing the corpus with updated attribute values. :param str id: the ``id`` value of the corpus to be updated. :returns: the updated corpus model. """ corpus = h.eagerload_corpus(Session.query(Corpus)).get(int(id)) if corpus: try: schema = CorpusSchema() values = json.loads(unicode(request.body, request.charset)) state = h.get_state_object(values) state.id = id state.config = config data = schema.to_python(values, state) corpus_dict = corpus.get_dict() corpus = update_corpus(corpus, data) # corpus will be False if there are no changes (cf. update_corpus). if corpus: backup_corpus(corpus_dict) Session.add(corpus) Session.commit() return corpus else: response.status_int = 400 return {'error': u'The update request failed because the submitted data were not new.'} except h.JSONDecodeError: response.status_int = 400 return h.JSONDecodeErrorResponse except Invalid, e: response.status_int = 400 return {'errors': e.unpack_errors()}