Example #1
0
File: views.py Project: cns-iu/nwb
def create_dataset(request):
    if request.method != "POST":
        form = NewDataSetForm(request.user)
        geoloc_add_formset = GeoLocationFormSet(prefix="add")
        geoloc_remove_formset = RemoveGeoLocationFormSet(prefix="remove")
        ref_formset = AcademicReferenceFormSet(prefix="reference")
        author_formset = AuthorFormSet(prefix="author")
    else:
        form = NewDataSetForm(request.user, request.POST, request.FILES)
        geoloc_add_formset = GeoLocationFormSet(request.POST, prefix="add")
        geoloc_remove_formset = RemoveGeoLocationFormSet(request.POST, prefix="remove")
        ref_formset = AcademicReferenceFormSet(request.POST, prefix="reference")
        author_formset = AuthorFormSet(request.POST, prefix="author")

        if form.is_valid() and ref_formset.is_valid() and author_formset.is_valid():

            new_dataset = _save_new_dataset(form, request.user)
            _save_previous_version(form, new_dataset)
            _save_tags(form, new_dataset, request.user)
            _save_geolocs(geoloc_add_formset, geoloc_remove_formset, new_dataset)
            _save_references(ref_formset, new_dataset)
            _save_authors(author_formset, new_dataset)

            #   If there is a readme, activate the dataset and let
            # the user view the new dataset, otherwise send
            # them to a page where they can specify the readme.
            try:
                uploaded_files = form.cleaned_data["files"]
                # Raises a 'NoReadMeException' if no readme is found.
                _add_uploaded_files(new_dataset, uploaded_files)
                new_dataset.is_active = True
                new_dataset.save()

                return HttpResponseRedirect(
                    reverse(
                        "epic.datasets.views.view_dataset", kwargs={"item_id": new_dataset.id, "slug": new_dataset.slug}
                    )
                )
            except NoReadMeException:
                return HttpResponseRedirect(
                    reverse(
                        "epic.datasets.views.upload_readme",
                        kwargs={"item_id": new_dataset.id, "slug": new_dataset.slug},
                    )
                )

    return render_to_response(
        "datasets/create_dataset.html",
        {
            "form": form,
            "geoloc_add_formset": geoloc_add_formset,
            "geoloc_remove_formset": geoloc_remove_formset,
            "ref_formset": ref_formset,
            "author_formset": author_formset,
        },
        context_instance=RequestContext(request),
    )
Example #2
0
def create_dataset(request):
    if request.method != 'POST':
        form = NewDataSetForm(request.user)
        geoloc_add_formset = GeoLocationFormSet(prefix='add')
        geoloc_remove_formset = RemoveGeoLocationFormSet(prefix='remove')
        ref_formset = AcademicReferenceFormSet(prefix='reference')
        author_formset = AuthorFormSet(prefix='author')
    else:
        form = NewDataSetForm(request.user, request.POST, request.FILES)
        geoloc_add_formset = GeoLocationFormSet(request.POST, prefix='add')
        geoloc_remove_formset = RemoveGeoLocationFormSet(request.POST, 
                                                         prefix='remove')
        ref_formset = AcademicReferenceFormSet(request.POST, 
                                               prefix='reference')
        author_formset = AuthorFormSet(request.POST, prefix='author')
        
        if form.is_valid() and ref_formset.is_valid() \
                and author_formset.is_valid():
            
            new_dataset = _save_new_dataset(form, request.user)
            _save_previous_version(form, new_dataset)
            _save_tags(form, new_dataset, request.user)
            _save_geolocs(geoloc_add_formset, 
                          geoloc_remove_formset, 
                          new_dataset)
            _save_references(ref_formset, new_dataset)
            _save_authors(author_formset, new_dataset)
            
            #   If there is a readme, activate the dataset and let
            # the user view the new dataset, otherwise send
            # them to a page where they can specify the readme.
            try:
                uploaded_files = form.cleaned_data['files']
                # Raises a 'NoReadMeException' if no readme is found.
                _add_uploaded_files(new_dataset, uploaded_files)
                
                new_dataset.is_active = True
                new_dataset.save()
                
                return HttpResponseRedirect(
                            reverse('epic.datasets.views.view_dataset', 
                                    kwargs={'item_id':new_dataset.id,
                                            'slug':new_dataset.slug}))
            except NoReadMeException:
                return HttpResponseRedirect(
                            reverse('epic.datasets.views.upload_readme', 
                                    kwargs={'item_id':new_dataset.id,
                                            'slug':new_dataset.slug}))
        
    return render_to_response('datasets/create_dataset.html', 
                              {'form': form, 
                               'geoloc_add_formset': geoloc_add_formset, 
                               'geoloc_remove_formset': geoloc_remove_formset,
                               'ref_formset': ref_formset,
                               'author_formset': author_formset}, 
                              context_instance=RequestContext(request))
Example #3
0
File: views.py Project: cns-iu/nwb
def edit_dataset(request, item_id, slug=None):
    dataset = get_object_or_404(DataSet, pk=item_id)
    user = request.user
    
    # Make sure the current user is the creator of the dataset.
    if user != dataset.creator:
        return HttpResponseRedirect(reverse(
            'epic.datasets.views.view_dataset', kwargs={'item_id': dataset.id, 'slug':slug,}))
    
    if request.method != "POST":
        current_tags = Tagging.objects.get_edit_string(item=dataset, user=user)
        
        initial_dataset_data = {
            'name': dataset.name,
            'description': dataset.description,
            'tags': current_tags,
        }
        
        initial_dataset_data['category'] = dataset.categories.values_list('id', flat=True)
        
        form = EditDataSetForm(initial=initial_dataset_data)
        initial_location_data = []
        geolocs = GeoLoc.objects.filter(datasets=dataset.id)
        
        for geoloc in geolocs:
            initial_location_data.append({'add_location':geoloc,})
        
        geoloc_add_formset = GeoLocationFormSet(
            prefix='add', initial=initial_location_data)
        geoloc_remove_formset = RemoveGeoLocationFormSet(prefix='remove')
        
        initial_author_data = []
        
        for author in dataset.authors.all():
            initial_author_data.append({'author': author.author})
        
        author_formset = AuthorFormSet(initial=initial_author_data, prefix='author')
            
        initial_ref_data = []
        
        for ref in dataset.references.all():
            initial_ref_data.append({'reference': ref.reference})
        
        ref_formset = AcademicReferenceFormSet(initial=initial_ref_data, prefix='reference')
    else:
        form = EditDataSetForm(request.POST)
        geoloc_add_formset = GeoLocationFormSet(request.POST, prefix='add')
        geoloc_remove_formset = RemoveGeoLocationFormSet(request.POST, prefix='remove')
        author_formset = AuthorFormSet(request.POST, prefix='author')
        ref_formset = AcademicReferenceFormSet(request.POST, prefix='reference')
            
        if form.is_valid() and author_formset.is_valid() and ref_formset.is_valid():     
            dataset.name = form.cleaned_data['name']
            dataset.description = form.cleaned_data['description']
            dataset.categories = form.cleaned_data['category']
            dataset.save()
            
            tag_names = form.cleaned_data["tags"]
            Tagging.objects.update_tags(tag_names=tag_names, item=dataset, user=user)
            
            for geoloc in _get_geolocs_from_formset(geoloc_add_formset, 'add_location'):
                dataset.geolocations.add(geoloc)
            
            for geoloc in _get_geolocs_from_formset(geoloc_remove_formset, 'remove_location'):
                dataset.geolocations.remove(geoloc)
            
            dataset.references.all().delete()
            
            for ref_form in ref_formset.forms:
                if ref_form.is_valid():
                    if ref_form.cleaned_data:
                        reference = ref_form.cleaned_data['reference'] 
                        AcademicReference.objects.create(item=dataset, reference=reference)
            
            for author in dataset.authors.all():
                author.items.remove(dataset)
            
            for author_form in author_formset.forms:
                if author_form.is_valid():
                    if author_form.cleaned_data:
                        author_name = author_form.cleaned_data['author']
                        author, created = Author.objects.get_or_create(author=author_name)
                        author.items.add(dataset)
            
            # If the user has set the flag to delete their datasets,
            # delete them.
            # TODO: these files need to be removed from the file system too!!

            delete_files = False
            
            try:
                # This implies that the checkbox was checked.
                request.POST['delete_dataset_files']
                delete_files = True
            except MultiValueDictKeyError:
                pass
            
            if delete_files:
                delete_dataset_files_url = get_item_url(
                    dataset, 'epic.datasets.views.delete_dataset_files')
                
                return HttpResponseRedirect(delete_dataset_files_url)
            else:
                view_dataset_url = get_item_url(dataset, 'epic.datasets.views.view_dataset')
                
                return HttpResponseRedirect(view_dataset_url)
            
    return render_to_response(
        'datasets/edit_dataset.html',
        {
            'dataset': dataset,
            'form': form,
            'geoloc_add_formset': geoloc_add_formset,
            'geoloc_remove_formset': geoloc_remove_formset,
            'files': dataset.files.all(),
            'author_formset': author_formset,
            'ref_formset': ref_formset
        },
        context_instance=RequestContext(request))
Example #4
0
File: views.py Project: cns-iu/nwb
def create_dataset(request):
    if request.method != 'POST':
        form = NewDataSetForm(request.user)
        geoloc_add_formset = GeoLocationFormSet(prefix='add')
        geoloc_remove_formset = RemoveGeoLocationFormSet(prefix='remove')
        ref_formset = AcademicReferenceFormSet(prefix='reference')
        author_formset = AuthorFormSet(prefix='author')
    else:
        form = NewDataSetForm(request.user, request.POST, request.FILES)
        geoloc_add_formset = GeoLocationFormSet(request.POST, prefix='add')
        geoloc_remove_formset = RemoveGeoLocationFormSet(request.POST, 
                                                         prefix='remove')
        ref_formset = AcademicReferenceFormSet(request.POST, 
                                               prefix='reference')
        author_formset = AuthorFormSet(request.POST, prefix='author')
        
        if form.is_valid() and ref_formset.is_valid() \
            and author_formset.is_valid():
            
            name = form.cleaned_data['name']
            description = form.cleaned_data['description']
            uploaded_files = form.cleaned_data['files']
            tags = form.cleaned_data['tags']
            previous_version = form.cleaned_data['previous_version']
            
            new_dataset = DataSet.objects.create(
                creator=request.user, 
                name=name, 
                description=description, 
                slug=slugify(name),
                previous_version=previous_version, 
                is_active=False)
            
            # TODO: Maybe only the latest version of a dataset should show up
            #  override the objects.all() or objects.active() maybe?
            if previous_version:
                #(Note that this will overwrite previous "new versions")
                previous_version.next_version = new_dataset
                previous_version.save()
            
            Tagging.objects.update_tags(tags, 
                                        item=new_dataset, 
                                        user=request.user)
            
            for geoloc in _get_geolocs_from_formset(geoloc_add_formset, 
                                                    'add_location'):
                new_dataset.geolocations.add(geoloc)
            
            for geoloc in _get_geolocs_from_formset(geoloc_remove_formset, 
                                                    'remove_location'):
                new_dataset.geolocations.remove(geoloc)
            
            for ref_form in ref_formset.forms:
                if ref_form.is_valid():
                    if ref_form.cleaned_data:
                        reference = ref_form.cleaned_data['reference'] 
                        AcademicReference.objects.create(
                            item=new_dataset, reference=reference)
                        
            for author_form in author_formset.forms:
                if author_form.is_valid():
                    if author_form.cleaned_data:
                        author_name = author_form.cleaned_data['author']
                        author, created = Author.objects.get_or_create(author=author_name)
                        author.items.add(new_dataset)
            
            try:
                _add_uploaded_files(new_dataset, uploaded_files)
                new_dataset.is_active = True
                new_dataset.save()
                return HttpResponseRedirect(
                            reverse('epic.datasets.views.view_dataset', 
                                    kwargs={'item_id':new_dataset.id,
                                            'slug':new_dataset.slug}))
            except NoReadMeException:
                return HttpResponseRedirect(
                            reverse('epic.datasets.views.upload_readme', 
                                    kwargs={'item_id':new_dataset.id,
                                            'slug':new_dataset.slug}))
        
    return render_to_response('datasets/create_dataset.html', 
                              {'form': form, 
                               'geoloc_add_formset': geoloc_add_formset, 
                               'geoloc_remove_formset': geoloc_remove_formset,
                               'ref_formset': ref_formset,
                               'author_formset': author_formset}, 
                              context_instance=RequestContext(request))
Example #5
0
File: views.py Project: cns-iu/nwb
def edit_dataset(request, item_id, slug=None):
    dataset = get_object_or_404(DataSet, pk=item_id)
    user = request.user

    # Make sure the current user is the creator of the dataset.
    if user != dataset.creator:
        return HttpResponseRedirect(
            reverse("epic.datasets.views.view_dataset", kwargs={"item_id": dataset.id, "slug": slug})
        )

    if request.method != "POST":
        current_tags = Tagging.objects.get_edit_string(item=dataset, user=user)

        initial_dataset_data = {"name": dataset.name, "description": dataset.description, "tags": current_tags}

        if dataset.category is not None:
            initial_dataset_data["category"] = dataset.category.id

        form = EditDataSetForm(initial=initial_dataset_data)
        initial_location_data = []
        geolocs = GeoLoc.objects.filter(datasets=dataset.id)

        for geoloc in geolocs:
            initial_location_data.append({"add_location": geoloc})

        geoloc_add_formset = GeoLocationFormSet(prefix="add", initial=initial_location_data)
        geoloc_remove_formset = RemoveGeoLocationFormSet(prefix="remove")

        initial_author_data = []

        for author in dataset.authors.all():
            initial_author_data.append({"author": author.author})

        author_formset = AuthorFormSet(initial=initial_author_data, prefix="author")

        initial_ref_data = []

        for ref in dataset.references.all():
            initial_ref_data.append({"reference": ref.reference})

        ref_formset = AcademicReferenceFormSet(initial=initial_ref_data, prefix="reference")
    else:
        form = EditDataSetForm(request.POST)
        geoloc_add_formset = GeoLocationFormSet(request.POST, prefix="add")
        geoloc_remove_formset = RemoveGeoLocationFormSet(request.POST, prefix="remove")
        author_formset = AuthorFormSet(request.POST, prefix="author")
        ref_formset = AcademicReferenceFormSet(request.POST, prefix="reference")

        if form.is_valid() and author_formset.is_valid() and ref_formset.is_valid():
            dataset.name = form.cleaned_data["name"]
            dataset.description = form.cleaned_data["description"]
            dataset.category = form.cleaned_data["category"]
            dataset.save()

            tag_names = form.cleaned_data["tags"]
            Tagging.objects.update_tags(tag_names=tag_names, item=dataset, user=user)

            for geoloc in _get_geolocs_from_formset(geoloc_add_formset, "add_location"):
                dataset.geolocations.add(geoloc)

            for geoloc in _get_geolocs_from_formset(geoloc_remove_formset, "remove_location"):
                dataset.geolocations.remove(geoloc)

            dataset.references.all().delete()

            for ref_form in ref_formset.forms:
                if ref_form.is_valid():
                    if ref_form.cleaned_data:
                        reference = ref_form.cleaned_data["reference"]
                        AcademicReference.objects.create(item=dataset, reference=reference)

            for author in dataset.authors.all():
                author.items.remove(dataset)

            for author_form in author_formset.forms:
                if author_form.is_valid():
                    if author_form.cleaned_data:
                        author_name = author_form.cleaned_data["author"]
                        author, created = Author.objects.get_or_create(author=author_name)
                        author.items.add(dataset)

            # If the user has set the flag to delete their datasets,
            # delete them.
            # TODO: these files need to be removed from the file system too!!

            delete_files = False

            try:
                # This implies that the checkbox was checked.
                request.POST["delete_dataset_files"]
                delete_files = True
            except MultiValueDictKeyError:
                pass

            if delete_files:
                delete_dataset_files_url = get_item_url(dataset, "epic.datasets.views.delete_dataset_files")

                return HttpResponseRedirect(delete_dataset_files_url)
            else:
                view_dataset_url = get_item_url(dataset, "epic.datasets.views.view_dataset")

                return HttpResponseRedirect(view_dataset_url)

    return render_to_response(
        "datasets/edit_dataset.html",
        {
            "dataset": dataset,
            "form": form,
            "geoloc_add_formset": geoloc_add_formset,
            "geoloc_remove_formset": geoloc_remove_formset,
            "files": dataset.files.all(),
            "author_formset": author_formset,
            "ref_formset": ref_formset,
        },
        context_instance=RequestContext(request),
    )