def add_doc(request, dataset_name, datafile_id=None): dataset = get_object_or_404(Dataset, name=dataset_name) if not user_can_edit_dataset(request.user, dataset): return HttpResponseForbidden() datafile = get_object_or_404(Datafile, id=datafile_id) \ if datafile_id else None form = f.FileForm(request.POST or None, instance=datafile) _set_flow_state(request) if request.method == 'POST': if form.is_valid(): data = dict(**form.cleaned_data) if datafile: form.save() else: if not _file_already_added(dataset, data['name'], data['url']): data['dataset'] = dataset data['is_documentation'] = True obj = Datafile.objects.create(**data) obj.save() return HttpResponseRedirect( reverse('dataset_documents', args=[dataset_name]) ) return render(request, "datasets/edit_adddoc.html", { 'is_first_file': len(dataset.files.filter(is_documentation=True)) == 0, 'form': form, 'dataset': dataset, 'datafile_id': datafile_id or '', })
def publish_dataset(request, dataset_name): dataset = get_object_or_404(Dataset, name=dataset_name) if not user_can_edit_dataset(request.user, dataset): return HttpResponseForbidden() return _publish_dataset( request, dataset, 'checking', )
def documents(request, dataset_name): dataset = get_object_or_404(Dataset, name=dataset_name) if not user_can_edit_dataset(request.user, dataset): return HttpResponseForbidden() _set_flow_state(request) files = [f for f in dataset.files.all() if f.is_documentation] return render(request, "datasets/show_docs.html", { 'dataset': dataset, 'files': files, })
def confirm_delete_dataset(request, dataset_name): dataset = get_object_or_404(Dataset, name=dataset_name) if not user_can_edit_dataset(request.user, dataset): return HttpResponseForbidden() if dataset.published and not request.user.is_staff: return HttpResponseForbidden() return _publish_dataset( request, dataset, 'editing', deleting=True )
def files(request, dataset_name): dataset = get_object_or_404(Dataset, name=dataset_name) url = _frequency_addfile_viewname(dataset) if not user_can_edit_dataset(request.user, dataset): return HttpResponseForbidden() _set_flow_state(request) files = [f for f in dataset.files.all() if not f.is_documentation] return render(request, "datasets/show_files.html", { 'addfile_viewname': url, 'dataset': dataset, 'files': files })
def location(request, dataset_name): dataset = get_object_or_404(Dataset, name=dataset_name) if not user_can_edit_dataset(request.user, dataset): return HttpResponseForbidden() _set_flow_state(request) form = f.LocationForm(request.POST or None, instance=dataset) if request.method == 'POST': if form.is_valid(): obj = form.save() return _redirect_to(request, 'dataset_frequency', [obj.name]) return render(request, "datasets/edit_location.html", { 'form': form, 'dataset': dataset, })
def _addfile(request, dataset_name, form_class, template, datafile_id=None): ''' Handler function for all of the 'period' datafile additions that vary only by template and form class ''' dataset = get_object_or_404(Dataset, name=dataset_name) if not user_can_edit_dataset(request.user, dataset): return HttpResponseForbidden() datafile = get_object_or_404(Datafile, id=datafile_id) \ if datafile_id else None form = form_class(request.POST or None, instance=datafile) _set_flow_state(request) if request.method == 'POST': if form.is_valid(): data = dict(**form.cleaned_data) if datafile: data['dataset'] = dataset for k, v in data.items(): setattr(datafile, k, v) datafile.save() else: if not _file_already_added(dataset, data['name'], data['url']): data['dataset'] = dataset obj = Datafile.objects.create(**data) obj.save() return HttpResponseRedirect( reverse('dataset_files', args=[dataset.name]) ) return render(request, "datasets/edit_addfile_{}.html".format(template), { 'is_first_file': len(dataset.files.all()) == 0, 'form': form, 'dataset': dataset, 'datafile_id': datafile.id if datafile else '', })
def confirmdeletefile(request, dataset_name, datafile_id): dataset = get_object_or_404(Dataset, name=dataset_name) datafile = get_object_or_404(Datafile, id=datafile_id) \ if datafile_id else None url = _frequency_addfile_viewname(dataset) template = 'datasets/show_docs.html' if datafile.is_documentation \ else 'datasets/show_files.html' if not user_can_edit_dataset(request.user, dataset): return HttpResponseForbidden() _set_flow_state(request) files = [f for f in dataset.files.all() if not f.is_documentation] return render(request, template, { 'addfile_viewname': url, 'dataset': dataset, 'files': files, 'file_to_delete_id': datafile_id, 'file_to_delete_title': datafile.name, })
def delete_dataset(request, dataset_name): dataset = get_object_or_404(Dataset, name=dataset_name) if not user_can_edit_dataset(request.user, dataset): return HttpResponseForbidden() if dataset.published and not request.user.is_staff: return HttpResponseForbidden() audit_log( 'delete-dataset', '{} deleted "{}"'.format(request.user.username, dataset.title), data={ 'dataset_name': dataset.name, 'dataset_title': dataset.title, 'user': request.user.username }, external_key=dataset.name ) unindex_dataset(dataset) dataset.delete() msg = '<h1 class="heading-medium">' + \ (_('The dataset ‘%(title)s’ has been deleted') % {'title': dataset.title}) + '</h1>' messages.add_message( request, messages.INFO, msg, extra_tags='confirm-delete-box' ) return HttpResponseRedirect( reverse('manage_my_data') )
def test_user_has_no_access(self): can = user_can_edit_dataset(self.naughty_user, self.dataset) assert can == False, can
def test_user_has_access(self): can = user_can_edit_dataset(self.test_user, self.dataset) assert can, can