view apps/bundle/views.py @ 177:86129d185ddb

Add versioning to bundles Some other bundle-related changes were made, including: * Editing the snippetform CSS and HTML to allow bundle/form.djhtml to be reused for editing * Changing {% block title %} to {% block section %} in the base template for bundles to allow for more flexibility when creating breadcrumbs * Saved common URL patterns in variables in bundle/urls.py * Renamed explore.html to explore.djhtml for consistency You should now be able to upload new versions as well as view the files (or a particular file) for a bundle at a specific version. Coming soon: the ability to add a timestamp and a comment for each new uploaded version (if this feature is desirable).
author dellsystem <ilostwaldo@gmail.com>
date Sat, 20 Oct 2012 23:28:50 -0400
parents 3be23e2e8fe7
children cdcbfaa65cfe
line wrap: on
line source

from __future__ import with_statement

import os

from django.shortcuts import get_object_or_404, render, redirect
from django.contrib.auth.decorators import login_required
from django.http import HttpResponse

from apps.bundle.models import Bundle, BundleFile
from apps.bundle.forms import BundleForm, BundleEditForm
from apps.bundle.tasks import handle_bundle_upload
from apps.pygments_style.models import PygmentsStyle


def detail(request, user, bundle, file=None, version=0):
    bundle = get_object_or_404(Bundle, uploader__username=user, name=bundle)
    # If the version is not set, use the latest version
    version = int(version) or bundle.latest_version
    files = bundle.bundlefile_set.filter(version=version)

    if request.user.is_authenticated():
        pygments_style = request.user.get_profile().pygments_style
    else:
        pygments_style = PygmentsStyle.objects.get(pk=1)

    context = {
        'default_style': pygments_style,
        'pygments_styles': PygmentsStyle.objects.all(),
        'bundle': bundle,
        'files': files,
        'file': file,
        'previous_versions': xrange(1, bundle.latest_version + 1),
        'this_version': version,
    }

    return render(request, 'bundle/bundle.djhtml', context)


def file_detail(request, user, bundle, version, path):
    print version
    bundle_file = get_object_or_404(BundleFile, bundle__uploader__username=user,
        bundle__name=bundle, full_path=path, is_dir=False, version=version)

    return detail(request, user, bundle, file=bundle_file, version=version)


@login_required
def index(request):
    if request.method == 'POST':
        post_data = request.POST.copy()
        bundle = Bundle(uploader=request.user)
        form = BundleForm(post_data, request.FILES, instance=bundle)

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

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

            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)


def explore(request):
    context = {
        'recent_bundles': Bundle.objects.all()[:20]
    }

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


@login_required
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.get_temp_path()

                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)