Пример #1
0
def upload_html(request, project_slug):
    proj = get_object_or_404(Project.objects.all(), slug=project_slug)
    FormClass = build_upload_html_form(proj)
    if request.method == "POST":
        form = FormClass(request.POST, request.FILES, request=request)
        if form.is_valid():
            file = request.FILES["content"]
            version_slug = form.cleaned_data["version"]
            version = proj.versions.get(slug=version_slug)
            # Copy file
            dest_dir = os.path.join(settings.UPLOAD_ROOT, proj.slug)
            if not os.path.exists(dest_dir):
                os.makedirs(dest_dir)
            dest_file = os.path.join(dest_dir, file.name)
            destination = open(dest_file, "wb+")
            for chunk in file.chunks():
                destination.write(chunk)
            destination.close()

            # Mark version active.
            version.active = True
            version.uploaded = True
            version.built = False
            version.save()

            # Extract file into the correct place.
            html_path = proj.rtd_build_path(version.slug)
            unzip_files(dest_file, html_path)
            return HttpResponseRedirect(proj.get_absolute_url())
    else:
        form = FormClass(request=request)
    return render_to_response(
        "projects/upload_html.html", {"form": form, "project": proj}, context_instance=RequestContext(request)
    )
Пример #2
0
def upload_html(request, project_slug):
    proj = get_object_or_404(Project.objects.all(), slug=project_slug)
    FormClass = build_upload_html_form(proj)
    if request.method == 'POST':
        form = FormClass(request.POST, request.FILES, request=request)
        if form.is_valid():
            file = request.FILES['content']
            version_slug = form.cleaned_data['version']
            version = proj.versions.get(slug=version_slug)
            #Copy file
            dest_dir = os.path.join(settings.UPLOAD_ROOT, proj.slug)
            if not os.path.exists(dest_dir):
                os.makedirs(dest_dir)
            dest_file = os.path.join(dest_dir, file.name)
            destination = open(dest_file, 'wb+')
            for chunk in file.chunks():
                destination.write(chunk)
            destination.close()

            #Mark version active.
            version.active = True
            version.uploaded = True
            version.built = False
            version.save()

            #Extract file into the correct place.
            html_path = proj.rtd_build_path(version.slug)
            unzip_files(dest_file, html_path)
            return HttpResponseRedirect(proj.get_absolute_url())
    else:
        form = FormClass(request=request)
    return render_to_response('projects/upload_html.html', {
        'form': form,
        'project': proj
    },
                              context_instance=RequestContext(request))