def delete_file( git_name, git_mail, collection_path, entity_id, file_basename, agent='' ): """ @param collection_path: string @param entity_id: string @param file_basename: string @param git_name: Username of git committer. @param git_mail: Email of git committer. @param agent: (optional) Name of software making the change. """ logger.debug('delete_file(%s,%s,%s,%s,%s,%s)' % (git_name, git_mail, collection_path, entity_id, file_basename, agent)) gitstatus.lock(settings.MEDIA_BASE, 'delete_file') file_id = os.path.splitext(file_basename)[0] file_ = DDRFile.from_identifier(Identifier(file_id)) entity = Entity.from_identifier(Identifier(entity_id)) collection = Collection.from_identifier(Identifier(path=collection_path)) logger.debug('delete from repository') rm_files,updated_files = entity.prep_rm_file(file_) status,message = commands.file_destroy( git_name, git_mail, collection, entity, rm_files, updated_files, agent ) logger.debug('delete from search index') try: docstore.delete(settings.DOCSTORE_HOSTS, settings.DOCSTORE_INDEX, file_.id) except ConnectionError: logger.error('Could not delete document from Elasticsearch.') return status,message,collection_path,file_basename
def delete( request, fid ): enforce_git_credentials(request) file_ = DDRFile.from_identifier(Identifier(fid)) check_file(file_) entity = file_.parent() collection = file_.collection() check_parents(entity, collection) # if request.method == 'POST': form = DeleteFileForm(request.POST) if form.is_valid() and form.cleaned_data['confirmed']: file_tasks.delete( request, request.session['git_name'], request.session['git_mail'], collection, entity, file_, settings.AGENT ) return HttpResponseRedirect(collection.absolute_url()) else: form = DeleteFileForm() return render(request, 'webui/files/delete.html', { 'file': file_, 'role': file_.identifier.parts['role'], 'form': form, })
def new_access( request, fid ): """Generate a new access file for the specified file. NOTE: There is no GET for this view. GET requests will redirect to entity. """ enforce_git_credentials(request) file_ = DDRFile.from_identifier(Identifier(fid)) check_file(file_) entity = file_.parent() collection = file_.collection() check_parents(entity, collection) # if request.method == 'POST': form = NewAccessFileForm(request.POST) if form.is_valid(): src_path = form.cleaned_data['path'] # start tasks result = file_tasks.add_access.apply_async( ( request.session['git_name'], request.session['git_mail'], entity, file_ ), countdown=2 ) result_dict = result.__dict__ log = addfile_logger(entity.identifier) log.ok('START task_id %s' % result.task_id) log.ok('ddrlocal.webui.file.new_access') log.ok('Locking %s' % entity.id) # lock entity lockstatus = entity.lock(result.task_id) if lockstatus == 'ok': log.ok( 'locked') else: log.not_ok( lockstatus) # add celery task_id to session celery_tasks = request.session.get(settings.CELERY_TASKS_SESSION_KEY, {}) # IMPORTANT: 'action' *must* match a message in webui.tasks.TASK_STATUS_MESSAGES. task = {'task_id': result.task_id, 'action': 'webui-file-new-access', 'filename': os.path.basename(src_path), 'file_url': file_.absolute_url(), 'entity_id': entity.id, 'start': converters.datetime_to_text(datetime.now(settings.TZ)),} celery_tasks[result.task_id] = task #del request.session[settings.CELERY_TASKS_SESSION_KEY] request.session[settings.CELERY_TASKS_SESSION_KEY] = celery_tasks # feedback #messages.success(request, WEBUI_MESSAGES['VIEWS_FILES_NEWACCESS'] % os.path.basename(src_path)) # redirect to entity return HttpResponseRedirect(entity.absolute_url())
def file_edit(collection_path, file_id, git_name, git_mail): """The time-consuming parts of file-edit. @param collection_path: str Absolute path to collection @param file_id: str @param git_name: Username of git committer. @param git_mail: Email of git committer. """ logger.debug('file_edit(%s,%s,%s,%s)' % (git_name, git_mail, collection_path, file_id)) fidentifier = Identifier(id=file_id) file_ = DDRFile.from_identifier(fidentifier) gitstatus.lock(settings.MEDIA_BASE, 'file_edit') exit,status = file_.save(git_name, git_mail) gitstatus_update.apply_async((collection_path,), countdown=2) return status,collection_path,file_id
def set_signature( request, fid ): """Make file the signature of the specified entity or collection. """ enforce_git_credentials(request) file_ = DDRFile.from_identifier(Identifier(fid)) check_file(file_) entity = file_.parent() collection = file_.collection() check_parents(entity, collection) # if request.method == 'POST': parent_id = request.POST.get('object_id') if parent_id in [entity.id, collection.id]: file_tasks.signature( request, parent_id=parent_id, file_id=fid, git_name=request.session['git_name'], git_mail=request.session['git_mail'], ) return HttpResponseRedirect( file_.absolute_url() )
def edit( request, fid ): enforce_git_credentials(request) file_ = DDRFile.from_identifier(Identifier(fid)) check_file(file_) entity = file_.parent() collection = file_.collection() check_parents(entity, collection) file_.model_def_commits() file_.model_def_fields() module = file_.identifier.fields_module() # if request.method == 'POST': form = DDRForm(request.POST, fields=module.FIELDS) if form.is_valid(): file_.form_post(form.cleaned_data) # write these so we see a change on refresh # will be rewritten in file_.save() file_.write_json() # commit files, delete cache, update search index, update git status file_tasks.edit( request, collection, file_, form.cleaned_data, request.session['git_name'], request.session['git_mail'], ) return HttpResponseRedirect( file_.absolute_url() ) else: form = DDRForm(file_.form_prep(), fields=module.FIELDS) return render(request, 'webui/files/edit-json.html', { 'collection': collection, 'entity': entity, 'role': file_.identifier.parts['role'], 'file': file_, 'form': form, })
def detail( request, fid ): """Add file to entity. """ file_ = DDRFile.from_identifier(Identifier(fid)) check_file(file_) entity = file_.parent() collection = file_.collection() check_parents(entity, collection, check_locks=0, fetch=0) file_.model_def_commits() file_.model_def_fields() formdata = {'path':file_.path_rel} if settings.GIT_ANNEX_WHEREIS: annex_whereis = annex_whereis_file(repository(collection.path_abs), file_) else: annex_whereis = {} return render(request, 'webui/files/detail.html', { 'collection': collection, 'entity': entity, 'role': file_.identifier.parts['role'], 'file': file_, 'new_access_url': file_.new_access_url, 'new_access_form': NewAccessFileForm(formdata), 'annex_whereis': annex_whereis, })