def delete(self, pk): index = get_object_or_404(Index, Index.name == pk) with database.atomic(): ndocs = (IndexDocument.delete().where( IndexDocument.index == index).execute()) index.delete_instance() logger.info('Deleted index "%s" and unlinked %s associated documents.', index.name, ndocs) return jsonify({'success': True})
def create(self): data = validator.parse_post(['name']) with database.atomic(): try: index = Index.create(name=data['name']) except IntegrityError: error('"%s" already exists.' % data['name']) else: logger.info('Created new index "%s"' % index.name) return self.detail(index.name)
def delete(self, pk): document = self._get_document(pk) with database.atomic(): (IndexDocument.delete().where( IndexDocument.document == document).execute()) (Attachment.delete().where( Attachment.document == document).execute()) Metadata.delete().where(Metadata.document == document).execute() document.delete_instance() logger.info('Deleted document with id = %s', document.get_id()) return jsonify({'success': True})
def update(self, pk): index = get_object_or_404(Index, Index.name == pk) data = validator.parse_post(['name']) index.name = data['name'] with database.atomic(): try: index.save() except IntegrityError: error('"%s" is already in use.' % index.name) else: logger.info('Updated index "%s"' % index.name) return self.detail(index.name)
def populate(self): k1 = ['k1-1', 'k1-2'] k2 = ['k2-1', 'k2-2'] k3 = ['k3-1', 'k3-2'] messages = [ 'foo bar baz', 'nuggie zaizee', 'huey mickey', 'googie', ] with database.atomic(): for i in range(100): self.index.index( content='testing %s' % i, test='true', k1=k1[i % 2], k2=k2[i % 2], k3=k3[i % 2], idx='%02d' % i, idx10=i % 10, name=messages[i % 4], )
def update(self, pk): document = self._get_document(pk) data = validator.parse_post( [], ['content', 'identifier', 'index', 'indexes', 'metadata']) save_document = False if data.get('content'): document.content = data['content'] save_document = True if data.get('identifier'): document.identifier = data['identifier'] save_document = True if save_document: document.save() logger.info('Updated document with id = %s', document.get_id()) if 'metadata' in data: del document.metadata if data['metadata']: document.metadata = data['metadata'] if len(request.files): self.attach_files(document) indexes = validator.validate_indexes(data, required=False) if indexes is not None: with database.atomic(): (IndexDocument.delete().where( IndexDocument.document == document).execute()) if indexes: IndexDocument.insert_many([{ 'index': index, 'document': document } for index in indexes]).execute() return self.detail(document.get_id())