def save( self, git_name, git_mail ): """Perform file-save functions. Commit files, delete cache, update search index. These steps are to be called asynchronously from tasks.file_edit. @param collection: Collection @param file_id: str @param git_name: str @param git_mail: str """ collection = self.collection() entity = self.parent() exit,status = commands.entity_update( git_name, git_mail, collection, entity, [self.json_path], agent=settings.AGENT) collection.cache_delete() with open(self.json_path, 'r') as f: document = json.loads(f.read()) try: docstore.post(settings.DOCSTORE_HOSTS, settings.DOCSTORE_INDEX, document) except ConnectionError: logger.error('Could not post to Elasticsearch.') return exit,status
def save( self, git_name, git_mail ): """Perform file-save functions. Commit files, delete cache, update search index. These steps are to be called asynchronously from tasks.file_edit. @param collection: Collection @param file_id: str @param git_name: str @param git_mail: str """ collection = Collection.from_json(self.collection_path) entity_id = models.make_object_id( 'entity', self.repo, self.org, self.cid, self.eid) exit,status = commands.entity_update( git_name, git_mail, collection.path, entity_id, [self.json_path], agent=settings.AGENT) collection.cache_delete() with open(self.json_path, 'r') as f: document = json.loads(f.read()) docstore.post(settings.DOCSTORE_HOSTS, settings.DOCSTORE_INDEX, document) return exit,status
def save_part2( self, collection, updated_files, form_data, git_name, git_mail ): """Save entity part 2: the slow parts Commit files, delete cache, update search index. These steps are slow, should be called from tasks.entity_edit @param updated_files: list of paths @param collection: Collection @param git_name: str @param git_mail: str """ inheritables = self.selected_inheritables(form_data) modified_ids,modified_files = self.update_inheritables(inheritables, form_data) if modified_files: updated_files = updated_files + modified_files exit,status = commands.entity_update( git_name, git_mail, collection, self, updated_files, agent=settings.AGENT) collection.cache_delete() with open(self.json_path, 'r') as f: document = json.loads(f.read()) try: docstore.post(settings.DOCSTORE_HOSTS, settings.DOCSTORE_INDEX, document) except ConnectionError: logger.error('Could not post to Elasticsearch.') return exit,status
def create(collection_path, git_name, git_mail): """create new entity given an entity ID TODO remove write and commit, just create object """ # write collection.json template to collection location and commit fileio.write_text(Collection(collection_path).dump_json(template=True), settings.TEMPLATE_CJSON) templates = [settings.TEMPLATE_CJSON, settings.TEMPLATE_EAD] agent = settings.AGENT cidentifier = Identifier(collection_path) exit,status = commands.create( git_name, git_mail, cidentifier, templates, agent ) collection = Collection.from_identifier(cidentifier) # [delete cache], update search index #collection.cache_delete() with open(collection.json_path, 'r') as f: document = json.loads(f.read()) try: docstore.post(settings.DOCSTORE_HOSTS, settings.DOCSTORE_INDEX, document) except ConnectionError: logger.error('Could not post to Elasticsearch.') return collection
def create(collection, entity_id, git_name, git_mail, agent=settings.AGENT): """create new entity given an entity ID """ repo,org,cid,eid = entity_id.split('-') entity_path = Entity.entity_path(None, repo, org, cid, eid) # write entity.json template to entity location and commit Entity(entity_path).dump_json(path=settings.TEMPLATE_EJSON, template=True) exit,status = commands.entity_create( git_name, git_mail, collection.path, entity_id, [collection.json_path_rel, collection.ead_path_rel], [settings.TEMPLATE_EJSON, settings.TEMPLATE_METS], agent=agent) # load new entity, inherit values from parent, write and commit entity = Entity.from_json(entity_path) entity.inherit(collection) entity.dump_json() updated_files = [entity.json_path] exit,status = commands.entity_update( git_name, git_mail, collection.path, entity.id, updated_files, agent=agent) # delete cache, update search index collection.cache_delete() with open(entity.json_path, 'r') as f: document = json.loads(f.read()) docstore.post(settings.DOCSTORE_HOSTS, settings.DOCSTORE_INDEX, document) return entity
def collection_sync( git_name, git_mail, collection_path ): """Synchronizes collection repo with workbench server. @param src_path: Absolute path to collection repo. @param git_name: Username of git committer. @param git_mail: Email of git committer. @return collection_path: Absolute path to collection. """ gitstatus.lock(settings.MEDIA_BASE, 'collection_sync') exit,status = sync(git_name, git_mail, collection_path) # update search index path = os.path.join(collection_path, 'collection.json') with open(path, 'r') as f: document = json.loads(f.read()) docstore.post(settings.DOCSTORE_HOSTS, settings.DOCSTORE_INDEX, document) return collection_path
def new( request, repo, org ): """Gets new CID from workbench, creates new collection record. If it messes up, goes back to collection list. """ git_name = request.session.get('git_name') git_mail = request.session.get('git_mail') if not (git_name and git_mail): messages.error(request, WEBUI_MESSAGES['LOGIN_REQUIRED']) # get new collection ID try: collection_ids = api.collections_next(request, repo, org, 1) except Exception as e: logger.error('Could not get new collecion ID!') logger.error(str(e.args)) messages.error(request, WEBUI_MESSAGES['VIEWS_COLL_ERR_NO_IDS']) messages.error(request, e) return HttpResponseRedirect(reverse('webui-collections')) cid = int(collection_ids[-1].split('-')[2]) # create the new collection repo collection_path = Collection.collection_path(request,repo,org,cid) # collection.json template Collection(collection_path).dump_json(path=settings.TEMPLATE_CJSON, template=True) exit,status = commands.create(git_name, git_mail, collection_path, [settings.TEMPLATE_CJSON, settings.TEMPLATE_EAD], agent=settings.AGENT) if exit: logger.error(exit) logger.error(status) messages.error(request, WEBUI_MESSAGES['ERROR'].format(status)) else: # update search index json_path = os.path.join(collection_path, 'collection.json') with open(json_path, 'r') as f: document = json.loads(f.read()) docstore.post(settings.DOCSTORE_HOSTS, settings.DOCSTORE_INDEX, document) gitstatus_update.apply_async((collection_path,), countdown=2) # positive feedback return HttpResponseRedirect( reverse('webui-collection-edit', args=[repo,org,cid]) ) # something happened... logger.error('Could not create new collecion!') messages.error(request, WEBUI_MESSAGES['VIEWS_COLL_ERR_CREATE']) return HttpResponseRedirect(reverse('webui-collections'))
def save( self, updated_files, git_name, git_mail ): """Perform file-save functions. Commit files, delete cache, update search index. These steps are to be called asynchronously from tasks.collection_edit. @param collection: Collection @param updated_files: list @param git_name: str @param git_mail: str """ exit,status = commands.update( git_name, git_mail, self.path, updated_files, agent=settings.AGENT) self.cache_delete() with open(self.json_path, 'r') as f: document = json.loads(f.read()) docstore.post(settings.DOCSTORE_HOSTS, settings.DOCSTORE_INDEX, document) return exit,status
def create(collection_path, git_name, git_mail): """create new entity given an entity ID """ # write collection.json template to collection location and commit Collection(collection_path).dump_json(path=settings.TEMPLATE_CJSON, template=True) templates = [settings.TEMPLATE_CJSON, settings.TEMPLATE_EAD] agent = settings.AGENT exit,status = commands.create( git_name, git_mail, collection_path, templates, agent) collection = Collection.from_json(collection_path) # [delete cache], update search index #collection.cache_delete() with open(collection.json_path, 'r') as f: document = json.loads(f.read()) docstore.post(settings.DOCSTORE_HOSTS, settings.DOCSTORE_INDEX, document) return collection
def save_part2( self, updated_files, collection, git_name, git_mail ): """Save entity part 2: the slow parts Commit files, delete cache, update search index. These steps are slow, should be called from tasks.entity_edit @param updated_files: list of paths @param collection: Collection @param git_name: str @param git_mail: str """ exit,status = commands.entity_update( git_name, git_mail, collection.path, self.id, updated_files, agent=settings.AGENT) collection.cache_delete() with open(self.json_path, 'r') as f: document = json.loads(f.read()) docstore.post(settings.DOCSTORE_HOSTS, settings.DOCSTORE_INDEX, document) return exit,status
def create(collection, entity_id, git_name, git_mail, agent=settings.AGENT): """create new entity given an entity ID TODO remove write and commit, just create object """ eidentifier = Identifier(id=entity_id) entity_path = eidentifier.path_abs() # write entity.json template to entity location and commit fileio.write_text(Entity(entity_path).dump_json(template=True), settings.TEMPLATE_EJSON) exit,status = commands.entity_create( git_name, git_mail, collection, eidentifier, [collection.json_path_rel, collection.ead_path_rel], [settings.TEMPLATE_EJSON, settings.TEMPLATE_METS], agent=agent) # load new entity, inherit values from parent, write and commit entity = Entity.from_json(entity_path) entity.inherit(collection) entity.write_json() updated_files = [entity.json_path] exit,status = commands.entity_update( git_name, git_mail, collection, entity, updated_files, agent=agent) # delete cache, update search index collection.cache_delete() with open(entity.json_path, 'r') as f: document = json.loads(f.read()) try: docstore.post(settings.DOCSTORE_HOSTS, settings.DOCSTORE_INDEX, document) except ConnectionError: logger.error('Could not post to Elasticsearch.') return entity