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 delete( request, repo, org, cid, eid, role, sha1 ): try: file_ = DDRFile.from_request(request) entity = file_.parent() collection = file_.collection() except: raise Http404 if entity.locked(): messages.error(request, WEBUI_MESSAGES['VIEWS_ENT_LOCKED']) return HttpResponseRedirect(entity.absolute_url()) if collection.locked(): messages.error(request, WEBUI_MESSAGES['VIEWS_COLL_LOCKED'].format(collection.id)) return HttpResponseRedirect(entity.absolute_url()) 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']) # if request.method == 'POST': form = DeleteFileForm(request.POST) if form.is_valid() and form.cleaned_data['confirmed']: entity_delete_file(request, git_name, git_mail, collection, entity, file_, settings.AGENT) return HttpResponseRedirect(collection.absolute_url()) else: form = DeleteFileForm() return render_to_response( 'webui/files/delete.html', {'file': file_, 'role': role, 'form': form, }, context_instance=RequestContext(request, processors=[]) )
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 new_access( request, repo, org, cid, eid, role, sha1 ): """Generate a new access file for the specified file. NOTE: There is no GET for this view. GET requests will redirect to entity. """ 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']) file_ = DDRFile.from_request(request) entity = file_.parent() collection = file_.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 = NewAccessFileForm(request.POST) if form.is_valid(): src_path = form.cleaned_data['path'] # start tasks result = entity_add_access.apply_async( (git_name, 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': datetime.now().strftime(settings.TIMESTAMP_FORMAT),} 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 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 edit( request, repo, org, cid, eid, role, sha1 ): 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']) file_ = DDRFile.from_request(request) module = file_.identifier.fields_module() entity = file_.parent() collection = file_.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()) file_.model_def_commits() file_.model_def_fields() # if request.method == 'POST': form = DDRForm(request.POST, fields=module.FIELDS) if form.is_valid(): file_.form_post(form) file_.write_json() # commit files, delete cache, update search index, update git status entity_file_edit(request, collection, file_, git_name, git_mail) return HttpResponseRedirect( file_.absolute_url() ) else: form = DDRForm(file_.form_prep(), fields=module.FIELDS) return render_to_response( 'webui/files/edit-json.html', {'collection': collection, 'entity': entity, 'role': role, 'file': file_, 'form': form, }, context_instance=RequestContext(request, processors=[]) )
def detail( request, repo, org, cid, eid, role, sha1 ): """Add file to entity. """ file_ = DDRFile.from_request(request) entity = file_.parent() collection = file_.collection() file_.model_def_commits() file_.model_def_fields() formdata = {'path':file_.path_rel} return render_to_response( 'webui/files/detail.html', {'collection': collection, 'entity': entity, 'role': role, 'file': file_, 'new_access_url': file_.new_access_url, 'new_access_form': NewAccessFileForm(formdata),}, context_instance=RequestContext(request, processors=[]) )
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, })
def file_json( request, repo, org, cid, eid, role, sha1 ): file_ = DDRFile.from_request(request) if file_.json_path and os.path.exists(file_.json_path): return HttpResponse(file_.dump_json(), content_type="application/json") messages.success(request, 'no JSON file. sorry.') return HttpResponseRedirect(file_.absolute_url())