Exemplo n.º 1
0
def index(request):
    if request.method == 'POST':
        post_data = request.POST.copy()
        post_data['uploader'] = request.user.id
        form = BundleForm(post_data, request.FILES)

        if form.is_valid():
            file = request.FILES.get('file')
            bundle = form.save()

            bundle.file_name = file.name
            bundle.save()
            bundle_path = bundle.mkdtemp()

            with open(bundle_path, 'wb+') as destination:
                for chunk in request.FILES.get('file', []):
                    destination.write(chunk)

            handle_bundle_upload.delay(bundle.id)

            return redirect(bundle)
    else:
        form = BundleForm()

    context = {
        'form': form,
        'bundles': Bundle.objects.order_by('-pub_date')[:5]
    }
    return render(request, 'bundle/index.djhtml', context)
Exemplo n.º 2
0
def edit(request, user, bundle):
    bundle = get_object_or_404(Bundle, name=bundle,
        uploader__username=request.user.username)

    # If the username specified in the URL is someone else's, show that page
    if user != request.user.username:
        # The bundle must exist, otherwise it would 404
        return redirect(bundle)

    if request.method == 'POST':
        form = BundleEditForm(request.POST, instance=bundle)

        if form.is_valid():
            form.save()

            file = request.FILES.get('file')
            if file is not None:
                bundle.done_uploading = False
                bundle.file_name = file.name
                bundle.latest_version += 1
                bundle.save()
                bundle_path = bundle.mkdtemp()

                with open(bundle_path, 'wb+') as destination:
                    for chunk in request.FILES.get('file', []):
                        destination.write(chunk)

                handle_bundle_upload.delay(bundle.id)
            return redirect(bundle)
    else:
        form = BundleEditForm(instance=bundle)

    context = {
        'bundle': bundle,
        'form': form,
    }

    return render(request, "bundle/edit.djhtml", context)