Mercurial > agora-palash
diff apps/bundle/views.py @ 151:c7be7def8b57
Bundles! (basic functionality)
Changes made in this commit:
* Added new dependencies (see pip-requirements)
* Added new dependency and setup information to README
* Deleted the included mptt app (in apps/mptt) in favour of just adding
the dependency to pip-requirements (makes it easier to update, etc)
* Changed the import convention to use
`from apps.bundle.models import Bundle`
rather than
`from agora.apps.bundle.models import Bundle`
because Celery was having problems with the latter style. Everything
should still work.
* Moved the syntax-highlighting and related code for snippets into
separate HTML files so that they can be used by the bundle app
And, of course, the ability to upload bundles.
But wait! There's more! Changes still to come, for only $19.95 a month:
* Bundle versioning
* Automatic license integration (i.e. adding headers to files)
* The ability to download bundles (zip, tar, etc)
* Rating bundles
* And much, much more!
Batteries not included.
author | dellsystem <ilostwaldo@gmail.com> |
---|---|
date | Mon, 15 Oct 2012 00:52:00 -0400 |
parents | d5ebcf4a249f |
children | d31b236ed5cf |
line wrap: on
line diff
--- a/apps/bundle/views.py Mon Oct 15 00:27:41 2012 -0400 +++ b/apps/bundle/views.py Mon Oct 15 00:52:00 2012 -0400 @@ -1,27 +1,77 @@ -from django.shortcuts import get_object_or_404 -from agora.apps.bundle.models import * -from django.views.generic.simple import direct_to_template +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 -def detail(request, user, bundle): - b = get_object_or_404(Bundle, uploader__username=user, name=bundle) - f = BundleFile.objects.filter(bundle=b) +from apps.bundle.models import Bundle, BundleFile +from apps.bundle.forms import BundleForm +from apps.bundle.tasks import handle_bundle_upload +from apps.pygments_style.models import PygmentsStyle + + +def detail(request, user, bundle, file=None): + bundle = get_object_or_404(Bundle, uploader__username=user, name=bundle) + files = bundle.bundlefile_set.all() + + if request.user.is_authenticated(): + pygments_style = request.user.get_profile().pygments_style + else: + pygments_style = PygmentsStyle.objects.get(pk=1) - return direct_to_template(request, 'bundle/bundle.djhtml', - { - 'bundle':b, - 'files': f, - }, - ) + context = { + 'default_style': pygments_style, + 'pygments_styles': PygmentsStyle.objects.all(), + 'bundle': bundle, + 'files': files, + 'file': file, + } + + return render(request, 'bundle/bundle.djhtml', context) + + +def file_detail(request, user, bundle, path): + bundle_file = get_object_or_404(BundleFile, bundle__uploader__username=user, + bundle__name=bundle, full_path=path, is_dir=False) + + return detail(request, user, bundle, file=bundle_file) +@login_required def index(request): - try: - b = Bundle.objects.all().order_by('pub_date')[:5] - except Bundle.DoesNotExist: - raise Http404 + 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.get_temp_path() + + with open(bundle_path, 'wb+') as destination: + for chunk in request.FILES.get('file', []): + destination.write(chunk) - return direct_to_template(request, 'bundle/index.djhtml', - { - 'bundles':b, - }, - ) + 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, "snippet/explore.html", context)