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 create(identifier, git_name, git_mail, agent='cmdln'): """Creates new Entity, writes files, performs initial commit @param identifier: Identifier @param git_name: str @param git_mail: str @param agent: str @returns: exit,status int,str """ collection = identifier.collection().object() if not collection: raise Exception('Parent collection for %s does not exist.' % identifier) exit, status = commands.entity_create( user_name=git_name, user_mail=git_mail, collection=collection, eidentifier=identifier, updated_files=[collection.json_path_rel, collection.ead_path_rel], agent=agent) if exit: raise Exception('Could not create new Entity: %s, %s' % (exit, status)) # load Entity object, inherit values from parent, write back to file entity = Entity.from_identifier(identifier) 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) return exit, status
def create(identifier, git_name, git_mail, agent='cmdln'): """Creates new File (metadata only), writes files, performs initial commit @param identifier: Identifier @param git_name: str @param git_mail: str @param agent: str @returns: exit,status int,str """ parent = identifier.parent().object() if not parent: raise Exception('Parent for %s does not exist.' % identifier) file_ = File.new(identifier) file_.write_json() entity_file_edit(request, collection, file_, git_name, git_mail) # load Entity object, inherit values from parent, write back to file entity = parent 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) 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 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 files_dedupe( request, repo, org, cid, eid ): 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']) entity = Entity.from_request(request) collection = entity.collection() if collection.locked(): messages.error(request, WEBUI_MESSAGES['VIEWS_COLL_LOCKED'].format(collection.id)) return HttpResponseRedirect(collection.absolute_url()) duplicate_masters = entity.detect_file_duplicates('master') duplicate_mezzanines = entity.detect_file_duplicates('mezzanine') duplicates = duplicate_masters + duplicate_mezzanines if request.method == 'POST': form = RmDuplicatesForm(request.POST) if form.is_valid() and form.cleaned_data.get('confirmed',None) \ and (form.cleaned_data['confirmed'] == True): # remove duplicates entity.rm_file_duplicates() # update metadata files entity.write_json() entity.write_mets() updated_files = [entity.json_path, entity.mets_path,] success_msg = WEBUI_MESSAGES['VIEWS_ENT_UPDATED'] exit,status = commands.entity_update( git_name, git_mail, collection, entity, updated_files, agent=settings.AGENT ) collection.cache_delete() if exit: messages.error(request, WEBUI_MESSAGES['ERROR'].format(status)) else: # update search index try: entity.post_json(settings.DOCSTORE_HOSTS, settings.DOCSTORE_INDEX) except ConnectionError: logger.error('Could not post to Elasticsearch.') gitstatus_update.apply_async((collection.path,), countdown=2) # positive feedback messages.success(request, success_msg) return HttpResponseRedirect(entity.absolute_url()) else: data = {} form = RmDuplicatesForm() return render_to_response( 'webui/entities/files-dedupe.html', {'collection': collection, 'entity': entity, 'duplicates': duplicates, 'form': form,}, context_instance=RequestContext(request, processors=[]) )
def files_dedupe( request, eid ): git_name,git_mail = enforce_git_credentials(request) entity = Entity.from_identifier(Identifier(eid)) check_object(entity, request) collection = entity.collection() check_parent(collection) duplicate_masters = entity.detect_file_duplicates('master') duplicate_mezzanines = entity.detect_file_duplicates('mezzanine') duplicates = duplicate_masters + duplicate_mezzanines if request.method == 'POST': form = RmDuplicatesForm(request.POST) if form.is_valid() and form.cleaned_data.get('confirmed',None) \ and (form.cleaned_data['confirmed'] == True): # remove duplicates entity.rm_file_duplicates() # update metadata files entity.write_json() entity.write_mets() updated_files = [entity.json_path, entity.mets_path,] success_msg = WEBUI_MESSAGES['VIEWS_ENT_UPDATED'] exit,status = commands.entity_update( git_name, git_mail, collection, entity, updated_files, agent=settings.AGENT ) collection.cache_delete() if exit: messages.error(request, WEBUI_MESSAGES['ERROR'].format(status)) else: # update search index try: entity.post_json() except ConnectionError: logger.error('Could not post to Elasticsearch.') dvcs_tasks.gitstatus_update.apply_async( (collection.path,), countdown=2 ) # positive feedback messages.success(request, success_msg) return HttpResponseRedirect(entity.absolute_url()) else: data = {} form = RmDuplicatesForm() return render(request, 'webui/entities/files-dedupe.html', { 'collection': collection, 'entity': entity, 'duplicates': duplicates, 'form': form, })
def edit_json( request, repo, org, cid, eid ): """ NOTE: will permit editing even if entity is locked! (which you need to do sometimes). """ entity = Entity.from_request(request) collection = entity.collection() #if collection.locked(): # messages.error(request, WEBUI_MESSAGES['VIEWS_COLL_LOCKED'].format(collection.id)) # return HttpResponseRedirect(entity.absolute_url()) #collection.repo_fetch() #if collection.repo_behind(): # messages.error(request, WEBUI_MESSAGES['VIEWS_COLL_BEHIND'].format(collection.id)) # return HttpResponseRedirect(entity.absolute_url()) #if entity.locked(): # messages.error(request, WEBUI_MESSAGES['VIEWS_ENT_LOCKED']) # return HttpResponseRedirect(entity.absolute_url()) # if request.method == 'POST': form = JSONForm(request.POST) if form.is_valid(): git_name = request.session.get('git_name') git_mail = request.session.get('git_mail') if git_name and git_mail: json_text = form.cleaned_data['json'] fileio.write_text(json_text, entity.json_path) exit,status = commands.entity_update( git_name, git_mail, collection, entity, [entity.json_path], agent=settings.AGENT ) collection.cache_delete() if exit: messages.error(request, WEBUI_MESSAGES['ERROR'].format(status)) else: gitstatus_update.apply_async((collection.path,), countdown=2) messages.success(request, WEBUI_MESSAGES['VIEWS_ENT_UPDATED']) return HttpResponseRedirect(entity.absolute_url()) else: messages.error(request, WEBUI_MESSAGES['LOGIN_REQUIRED']) else: form = JSONForm({'json': entity.dump_json(),}) return render_to_response( 'webui/entities/edit-raw.html', {'entity': entity, 'form': form,}, context_instance=RequestContext(request, processors=[]) )
def save(self, git_name, git_mail, agent, collection=None, cleaned_data={}, commit=True): """Writes specified Entity metadata, stages, and commits. Updates .children and .file_groups if parent is another Entity. Returns exit code, status message, and list of updated files. Files list is for use by e.g. batch operations that want to commit all modified files in one operation rather than piecemeal. @param git_name: str @param git_mail: str @param agent: str @param collection: Collection @param cleaned_data: dict Form data (all fields required) @param commit: boolean @returns: exit,status,updated_files (int,str,list) """ if not collection: collection = self.identifier.collection().object() parent = self.identifier.parent().object() if cleaned_data: self.form_post(cleaned_data) self.children(force_read=True) self.write_json() self.write_xml() updated_files = [ self.json_path, self.mets_path, self.changelog_path, ] if parent and isinstance(parent, Entity): # update parent .children and .file_groups parent.children(force_read=True) parent.write_json() updated_files.append(parent.json_path) inheritables = self.selected_inheritables(cleaned_data) modified_ids,modified_files = self.update_inheritables(inheritables, cleaned_data) if modified_files: updated_files = updated_files + modified_files exit,status = commands.entity_update( git_name, git_mail, collection, self, updated_files, agent, commit ) return exit,status,updated_files
def save(self, git_name, git_mail, agent, collection=None, parent=None, inheritables=[], commit=True): """Writes File metadata, stages, and commits. Updates .children if parent is (almost certainly) an Entity. Returns exit code, status message, and list of *updated* files. Updated files list is for use by e.g. batch operations that want to commit all modified files in one operation rather than piecemeal. IMPORTANT: This list only includes METADATA files, NOT binaries. @param git_name: str @param git_mail: str @param agent: str @param collection: Collection @param parent: Entity or Segment @param inheritables: list of selected inheritable fields @param commit: boolean @returns: exit,status,updated_files (int,str,list) """ if not collection: collection = self.identifier.collection().object() if not parent: parent = self.identifier.parent().object() self.write_json() # list of files to stage updated_files = [ self.json_path, ] if parent and (parent.identifier.model in ['entity', 'segment']): # update parent.children parent.children(force_read=True) parent.write_json() updated_files.append(parent.json_path) updated_files.append(parent.changelog_path) # files have no child object inheritors exit, status = commands.entity_update(git_name, git_mail, collection, parent, updated_files, agent, commit) return exit, status, updated_files
def save(self, git_name, git_mail, agent, collection=None, inheritables=[], commit=True): """Writes specified Entity metadata, stages, and commits. Updates .children and .file_groups if parent is another Entity. Returns exit code, status message, and list of updated files. Files list is for use by e.g. batch operations that want to commit all modified files in one operation rather than piecemeal. @param git_name: str @param git_mail: str @param agent: str @param collection: Collection @param inheritables: list of selected inheritable fields @param commit: boolean @returns: exit,status,updated_files (int,str,list) """ if not collection: collection = self.identifier.collection().object() parent = self.identifier.parent().object() self.children(force_read=True) self.write_json() self.write_xml() updated_files = [ self.json_path, self.mets_path, self.changelog_path, ] if parent and isinstance(parent, Entity): # update parent .children and .file_groups parent.children(force_read=True) parent.write_json() updated_files.append(parent.json_path) # propagate inheritable changes to child objects modified_ids,modified_files = self.update_inheritables(inheritables) if modified_files: updated_files = updated_files + modified_files exit,status = commands.entity_update( git_name, git_mail, collection, self, updated_files, agent, commit ) return exit,status,updated_files
def save(self, git_name, git_mail, agent, collection=None, parent=None, cleaned_data={}, commit=True): """Writes File metadata, stages, and commits. Updates .children and .file_groups if parent is (almost certainly) an Entity. Returns exit code, status message, and list of updated files. Files list is for use by e.g. batch operations that want to commit all modified files in one operation rather than piecemeal. @param git_name: str @param git_mail: str @param agent: str @param collection: Collection @param parent: Entity or Segment @param cleaned_data: dict Form data (all fields required) @param commit: boolean @returns: exit,status,updated_files (int,str,list) """ if not collection: collection = self.identifier.collection().object() if not parent: parent = self.identifier.parent().object() if cleaned_data: self.form_post(cleaned_data) self.write_json() updated_files = [ self.json_path, ] if parent and (parent.identifier.model in ['entity', 'segment']): # update parent .children and .file_groups parent.children(force_read=True) parent.write_json() updated_files.append(parent.json_path) exit, status = commands.entity_update(git_name, git_mail, collection, parent, updated_files, agent, commit) return exit, status, updated_files
def save(self, git_name, git_mail, agent, collection=None, parent=None, inheritables=[], commit=True): """Writes File metadata, stages, and commits. Updates .children and .file_groups if parent is (almost certainly) an Entity. Returns exit code, status message, and list of updated files. Files list is for use by e.g. batch operations that want to commit all modified files in one operation rather than piecemeal. @param git_name: str @param git_mail: str @param agent: str @param collection: Collection @param parent: Entity or Segment @param inheritables: list of selected inheritable fields @param commit: boolean @returns: exit,status,updated_files (int,str,list) """ if not collection: collection = self.identifier.collection().object() if not parent: parent = self.identifier.parent().object() self.write_json() updated_files = [ self.json_path, ] if parent and (parent.identifier.model in ['entity','segment']): # update parent .children and .file_groups parent.children(force_read=True) parent.write_json() updated_files.append(parent.json_path) updated_files.append(parent.changelog_path) # files have no child object inheritors exit,status = commands.entity_update( git_name, git_mail, collection, parent, updated_files, agent, commit ) return exit,status,updated_files
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 new(identifier, git_name, git_mail, agent='cmdln'): """Creates new File (metadata only!), writes to filesystem, performs initial commit @param identifier: Identifier @param git_name: str @param git_mail: str @param agent: str @returns: exit,status int,str """ parent = identifier.parent().object() if not parent: raise Exception('Parent for %s does not exist.' % identifier) file_ = File.create(identifier.path_abs(), identifier) file_.write_json() entity_file_edit(request, collection, file_, git_name, git_mail) exit, status = commands.entity_create( git_name, git_mail, collection, entity.identifier, [collection.json_path_rel, collection.ead_path_rel], [config.TEMPLATE_EJSON, config.TEMPLATE_METS], agent=agent) if exit: raise Exception('Could not create new Entity: %s, %s' % (exit, status)) # load Entity object, inherit values from parent, write back to file entity = Identifier(identifier).object() 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) return exit, status
def new(identifier, git_name, git_mail, agent='cmdln'): """Creates new Entity, writes to filesystem, does initial commit. @param identifier: Identifier @param git_name: str @param git_mail: str @param agent: str @returns: exit,status int,str """ collection = identifier.collection().object() if not collection: raise Exception('Parent collection for %s does not exist.' % identifier) entity = Entity.create(identifier) fileio.write_text( entity.dump_json(template=True), config.TEMPLATE_EJSON ) exit,status = commands.entity_create( git_name, git_mail, collection, entity.identifier, [collection.json_path_rel, collection.ead_path_rel], [config.TEMPLATE_EJSON, config.TEMPLATE_METS], agent=agent ) if exit: raise Exception('Could not create new Entity: %s, %s' % (exit, status)) # load Entity object, inherit values from parent, write back to file entity = Entity.from_identifier(identifier) 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 ) 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
def new(identifier, git_name, git_mail, agent='cmdln'): """Creates new File (metadata only!), writes to filesystem, performs initial commit @param identifier: Identifier @param git_name: str @param git_mail: str @param agent: str @returns: exit,status int,str """ parent = identifier.parent().object() if not parent: raise Exception('Parent for %s does not exist.' % identifier) file_ = File.create(identifier) file_.write_json() entity_file_edit(request, collection, file_, git_name, git_mail) exit,status = commands.entity_create( git_name, git_mail, collection, entity.identifier, [collection.json_path_rel, collection.ead_path_rel], [config.TEMPLATE_EJSON, config.TEMPLATE_METS], agent=agent ) if exit: raise Exception('Could not create new Entity: %s, %s' % (exit, status)) # load Entity object, inherit values from parent, write back to file entity = Identifier(identifier).object() 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 ) return exit,status
def new(identifier, git_name, git_mail, agent='cmdln'): """Creates new Entity, writes to filesystem, does initial commit. @param identifier: Identifier @param git_name: str @param git_mail: str @param agent: str @returns: exit,status int,str """ collection = identifier.collection().object() if not collection: raise Exception('Parent collection for %s does not exist.' % identifier) entity = Entity.create(identifier.path_abs(), identifier) fileio.write_text( entity.dump_json(template=True), config.TEMPLATE_EJSON ) exit,status = commands.entity_create( git_name, git_mail, collection, entity.identifier, [collection.json_path_rel, collection.ead_path_rel], [config.TEMPLATE_EJSON, config.TEMPLATE_METS], agent=agent ) if exit: raise Exception('Could not create new Entity: %s, %s' % (exit, status)) # load Entity object, inherit values from parent, write back to file entity = Entity.from_identifier(identifier) 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 ) return exit,status
def new( request, repo, org, cid ): """Gets new EID from workbench, creates new entity record. If it messes up, goes back to collection. """ 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']) collection = Collection.from_request(request) if collection.locked(): messages.error(request, WEBUI_MESSAGES['VIEWS_COLL_LOCKED'].format(collection.id)) return HttpResponseRedirect(collection.absolute_url()) collection.repo_fetch() if collection.repo_behind(): messages.error(request, WEBUI_MESSAGES['VIEWS_COLL_BEHIND'].format(collection.id)) return HttpResponseRedirect(collection.absolute_url()) ic = idservice.IDServiceClient() # resume session auth_status,auth_reason = ic.resume(request.session['idservice_token']) if auth_status != 200: request.session['idservice_username'] = None request.session['idservice_token'] = None messages.warning( request, 'Session resume failed: %s %s (%s)' % ( auth_status,auth_reason,settings.IDSERVICE_API_BASE ) ) return HttpResponseRedirect(collection.absolute_url()) # get new entity ID http_status,http_reason,new_entity_id = ic.next_object_id( collection.identifier, 'entity' ) if http_status not in [200,201]: err = '%s %s' % (http_status, http_reason) msg = WEBUI_MESSAGES['VIEWS_ENT_ERR_NO_IDS'] % (settings.IDSERVICE_API_BASE, err) logger.error(msg) messages.error(request, msg) return HttpResponseRedirect(collection.absolute_url()) eidentifier = Identifier(id=new_entity_id) # create new entity entity_path = eidentifier.path_abs() # write entity.json template to entity location fileio.write_text( Entity(entity_path).dump_json(template=True), settings.TEMPLATE_EJSON ) # commit files 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=settings.AGENT ) # load Entity object, inherit values from parent, write back to file entity = Entity.from_identifier(eidentifier) 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=settings.AGENT ) collection.cache_delete() if exit: logger.error(exit) logger.error(status) messages.error(request, WEBUI_MESSAGES['ERROR'].format(status)) else: # update search index try: entity.post_json(settings.DOCSTORE_HOSTS, settings.DOCSTORE_INDEX) except ConnectionError: logger.error('Could not post to Elasticsearch.') gitstatus_update.apply_async((collection.path,), countdown=2) # positive feedback return HttpResponseRedirect(reverse('webui-entity-edit', args=entity.idparts)) # something happened... logger.error('Could not create new entity!') messages.error(request, WEBUI_MESSAGES['VIEWS_ENT_ERR_CREATE']) return HttpResponseRedirect(collection.absolute_url())
def edit_old( request, repo, org, cid, eid, role, sha1 ): """Edit file metadata """ 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']) collection = Collection.from_json(Collection.collection_path(request,repo,org,cid)) entity = Entity.from_json(Entity.entity_path(request,repo,org,cid,eid)) f = entity.file(repo, org, cid, eid, role, sha1) if collection.locked(): messages.error(request, WEBUI_MESSAGES['VIEWS_COLL_LOCKED'].format(collection.id)) return HttpResponseRedirect( f.url() ) collection.repo_fetch() if collection.repo_behind(): messages.error(request, WEBUI_MESSAGES['VIEWS_COLL_BEHIND'].format(collection.id)) return HttpResponseRedirect( reverse('webui-entity', args=[repo,org,cid,eid]) ) if entity.locked(): messages.error(request, WEBUI_MESSAGES['VIEWS_FILES_PARENT_LOCKED']) return HttpResponseRedirect( f.url() ) if request.method == 'POST': form = EditFileForm(request.POST, request.FILES) if form.is_valid(): #f.status = form.cleaned_data['status'] #f.public = form.cleaned_data['public'] f.sort = form.cleaned_data['sort'] f.label = form.cleaned_data['label'] f.xmp = form.cleaned_data['xmp'] result = entity.file(repo, org, cid, eid, role, sha1, f) if result in ['added','updated']: entity.dump_json() entity.dump_mets() exit,status = commands.entity_update(git_name, git_mail, entity.parent_path, entity.id, [entity.json_path, entity.mets_path,], agent=settings.AGENT) if exit: messages.error(request, WEBUI_MESSAGES['ERROR'].format(status)) else: messages.success(request, WEBUI_MESSAGES['VIEWS_FILES_UPDATED']) return HttpResponseRedirect( reverse('webui-file', args=[repo,org,cid,eid,role,sha1]) ) # something went wrong assert False else: data = { #'status': f.status, #'public': f.public, 'sort': f.sort, 'label': f.label, 'xmp': f.xmp, } form = EditFileForm(data) return render_to_response( 'webui/files/edit.html', {'repo': entity.repo, 'org': entity.org, 'cid': entity.cid, 'eid': entity.eid, 'role': file_.role, 'sha1': file_.sha1, 'collection_uid': collection.id, 'collection': collection, 'entity': entity, 'file': f, 'form': form,}, context_instance=RequestContext(request, processors=[]) )
def import_entities(csv_path, collection_path, git_name, git_mail): """ @param csv_path: Absolute path to CSV data file. @param collection_path: Absolute path to collection repo. @param git_name: Username for use in changelog, git log @param git_mail: User email address for use in changelog, git log """ rows = read_csv(csv_path) headers = rows[0] rows = rows[1:] headers = replace_variant_headers("entity", headers) field_names = FIELD_NAMES["entity"] nonrequired_fields = REQUIRED_FIELDS_EXCEPTIONS["entity"] required_fields = get_required_fields(ENTITY_FIELDS, nonrequired_fields) rows = replace_variant_cv_field_values("entity", headers, rows) invalid_headers("entity", headers, field_names, nonrequired_fields) all_rows_valid("entity", headers, required_fields, rows) if True: collection = Collection.from_json(collection_path) print(collection) # -------------------------------------------------- def prep_creators(data): return [x.strip() for x in data.strip().split(";") if x] def prep_language(data): """language can be 'eng', 'eng;jpn', 'eng:English', 'jpn:Japanese' """ y = [] for x in data.strip().split(";"): if ":" in x: y.append(x.strip().split(":")[0]) else: y.append(x.strip()) return y def prep_topics(data): return [x.strip() for x in data.strip().split(";") if x] def prep_persons(data): return [x.strip() for x in data.strip().split(";") if x] def prep_facility(data): return [x.strip() for x in data.strip().split(";") if x] # -------------------------------------------------- print("Data file looks ok") started = datetime.now() print("%s starting import" % dtfmt(started)) print("") for n, row in enumerate(rows): rowstarted = datetime.now() rowd = make_row_dict(headers, row) # create new entity entity_id = rowd["id"] entity_path = os.path.join(collection_path, COLLECTION_FILES_PREFIX, entity_id) # write entity.json template to entity location fileio.write_text(Entity(entity_path).dump_json(template=True), TEMPLATE_EJSON) # commit files exit, status = commands.entity_create( git_name, git_mail, collection.path, entity_id, [collection.json_path_rel, collection.ead_path_rel], [TEMPLATE_EJSON, TEMPLATE_METS], agent=AGENT, ) # reload newly-created Entity object entity = Entity.from_json(entity_path) # preppers rowd["creators"] = prep_creators(rowd["creators"]) rowd["language"] = prep_language(rowd["language"]) rowd["topics"] = prep_topics(rowd["topics"]) rowd["persons"] = prep_persons(rowd["persons"]) rowd["facility"] = prep_facility(rowd["facility"]) # insert values from CSV for key in rowd.keys(): setattr(entity, key, rowd[key]) entity.record_created = datetime.now() entity.record_lastmod = datetime.now() # write back to file entity.write_json() updated_files = [entity.json_path] exit, status = commands.entity_update( git_name, git_mail, entity.parent_path, entity.id, updated_files, agent=AGENT ) rowfinished = datetime.now() rowelapsed = rowfinished - rowstarted print("%s %s/%s %s (%s)" % (dtfmt(rowfinished), n + 1, len(rows), entity.id, rowelapsed)) finished = datetime.now() elapsed = finished - started print("") print("%s done (%s rows)" % (dtfmt(finished), len(rows))) print("%s elapsed" % elapsed) print("")