Beispiel #1
0
def upload_readme(request, item_id, slug):
    dataset = get_object_or_404(DataSet, pk=item_id)
    user = request.user
    if user != dataset.creator:
        return HttpResponseRedirect(reverse('epic.datasets.views.view_dataset', 
                                            kwargs={'item_id':dataset.id,
                                                    'slug':dataset.slug}))
    if request.method != 'POST':
        form = UploadReadMeForm()
    else:
        form = UploadReadMeForm(request.POST, request.FILES)
        
        if form.is_valid():
            readme_file = form.cleaned_data['readme']
            if _is_readme_filename(readme_file.name):
                datasetreadmefile = ReadMeFile(parent_dataset=dataset, file_contents=readme_file)
                datasetreadmefile.file_contents.save('readme.txt', readme_file, save=True)
                datasetreadmefile.save()
                
                dataset.is_active = True
                dataset.save()
                
                return HttpResponseRedirect(reverse('epic.datasets.views.view_dataset', 
                                                    kwargs={'item_id':dataset.id,
                                                            'slug':dataset.slug}))
            else:
                msg = u"The readme '%(readme)s' is not a valid readme.txt file." % {'readme': readme_file.name,}
                form._errors['readme'] = ErrorList([msg])
        
    return render_to_response('datasets/upload_readme.html',
                              {'form': form, 'dataset': dataset},
                              context_instance=RequestContext(request))
Beispiel #2
0
def _add_uploaded_files(dataset, uploaded_files):
    readme_file = _get_readme(uploaded_files)
    
    if readme_file:
        datasetreadmefile = ReadMeFile(parent_dataset=dataset, file_contents=readme_file)
        datasetreadmefile.file_contents.save('readme.txt', readme_file, save=True)
        datasetreadmefile.save()
    else:
        pass
    
#    # TODO: Maybe if the readme was a top level file that was uploaded, it should be pulled out...
#    if readme_file in uploaded_files:
#        uploaded_files.remove(readme_file)
    
    for uploaded_file in uploaded_files:
        new_datasetfile = DataSetFile(parent_dataset=dataset, file_contents=uploaded_file)
        new_datasetfile.file_contents.save(uploaded_file.name, uploaded_file, save=True)
        new_datasetfile.save()
        
    return readme_file