comparison 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
comparison
equal deleted inserted replaced
176:c042d26e6936 177:86129d185ddb
5 from django.shortcuts import get_object_or_404, render, redirect 5 from django.shortcuts import get_object_or_404, render, redirect
6 from django.contrib.auth.decorators import login_required 6 from django.contrib.auth.decorators import login_required
7 from django.http import HttpResponse 7 from django.http import HttpResponse
8 8
9 from apps.bundle.models import Bundle, BundleFile 9 from apps.bundle.models import Bundle, BundleFile
10 from apps.bundle.forms import BundleForm 10 from apps.bundle.forms import BundleForm, BundleEditForm
11 from apps.bundle.tasks import handle_bundle_upload 11 from apps.bundle.tasks import handle_bundle_upload
12 from apps.pygments_style.models import PygmentsStyle 12 from apps.pygments_style.models import PygmentsStyle
13 13
14 14
15 def detail(request, user, bundle, file=None): 15 def detail(request, user, bundle, file=None, version=0):
16 bundle = get_object_or_404(Bundle, uploader__username=user, name=bundle) 16 bundle = get_object_or_404(Bundle, uploader__username=user, name=bundle)
17 files = bundle.bundlefile_set.all() 17 # If the version is not set, use the latest version
18 version = int(version) or bundle.latest_version
19 files = bundle.bundlefile_set.filter(version=version)
18 20
19 if request.user.is_authenticated(): 21 if request.user.is_authenticated():
20 pygments_style = request.user.get_profile().pygments_style 22 pygments_style = request.user.get_profile().pygments_style
21 else: 23 else:
22 pygments_style = PygmentsStyle.objects.get(pk=1) 24 pygments_style = PygmentsStyle.objects.get(pk=1)
25 'default_style': pygments_style, 27 'default_style': pygments_style,
26 'pygments_styles': PygmentsStyle.objects.all(), 28 'pygments_styles': PygmentsStyle.objects.all(),
27 'bundle': bundle, 29 'bundle': bundle,
28 'files': files, 30 'files': files,
29 'file': file, 31 'file': file,
32 'previous_versions': xrange(1, bundle.latest_version + 1),
33 'this_version': version,
30 } 34 }
31 35
32 return render(request, 'bundle/bundle.djhtml', context) 36 return render(request, 'bundle/bundle.djhtml', context)
33 37
34 38
35 def file_detail(request, user, bundle, path): 39 def file_detail(request, user, bundle, version, path):
40 print version
36 bundle_file = get_object_or_404(BundleFile, bundle__uploader__username=user, 41 bundle_file = get_object_or_404(BundleFile, bundle__uploader__username=user,
37 bundle__name=bundle, full_path=path, is_dir=False) 42 bundle__name=bundle, full_path=path, is_dir=False, version=version)
38 43
39 return detail(request, user, bundle, file=bundle_file) 44 return detail(request, user, bundle, file=bundle_file, version=version)
45
40 46
41 @login_required 47 @login_required
42 def index(request): 48 def index(request):
43 if request.method == 'POST': 49 if request.method == 'POST':
44 post_data = request.POST.copy() 50 post_data = request.POST.copy()
45 post_data['uploader'] = request.user.id 51 bundle = Bundle(uploader=request.user)
46 form = BundleForm(post_data, 52 form = BundleForm(post_data, request.FILES, instance=bundle)
47 request.FILES)
48 53
49 if form.is_valid(): 54 if form.is_valid():
50 file = request.FILES.get('file') 55 file = request.FILES.get('file')
51 bundle = form.save() 56 form.save()
52 57
53 bundle.file_name = file.name 58 bundle.file_name = file.name
54 bundle.save() 59 bundle.save()
55 bundle_path = bundle.get_temp_path() 60 bundle_path = bundle.get_temp_path()
56 61
74 def explore(request): 79 def explore(request):
75 context = { 80 context = {
76 'recent_bundles': Bundle.objects.all()[:20] 81 'recent_bundles': Bundle.objects.all()[:20]
77 } 82 }
78 83
79 return render(request, "bundle/explore.html", context) 84 return render(request, "bundle/explore.djhtml", context)
85
86
87 @login_required
88 def edit(request, user, bundle):
89 bundle = get_object_or_404(Bundle, name=bundle,
90 uploader__username=request.user.username)
91
92 # If the username specified in the URL is someone else's, show that page
93 if user != request.user.username:
94 # The bundle must exist, otherwise it would 404
95 return redirect(bundle)
96
97 if request.method == 'POST':
98 form = BundleEditForm(request.POST, instance=bundle)
99
100 if form.is_valid():
101 form.save()
102
103 file = request.FILES.get('file')
104 if file is not None:
105 bundle.done_uploading = False
106 bundle.file_name = file.name
107 bundle.latest_version += 1
108 bundle.save()
109 bundle_path = bundle.get_temp_path()
110
111 with open(bundle_path, 'wb+') as destination:
112 for chunk in request.FILES.get('file', []):
113 destination.write(chunk)
114
115 handle_bundle_upload.delay(bundle.id)
116 return redirect(bundle)
117 else:
118 form = BundleEditForm(instance=bundle)
119
120 context = {
121 'bundle': bundle,
122 'form': form,
123 }
124
125 return render(request, "bundle/edit.djhtml", context)