changeset 209:4033ebe1867f

Add ability to download files This makes use of a new model (BundleVersion) to keep track of the locations on disk of the original uploads for each version. This will require some manual processing to get it working for existing bundles, since the information needed isn't being stored at the moment.
author dellsystem <ilostwaldo@gmail.com>
date Sun, 17 Feb 2013 14:57:39 -0500
parents 1eb652aa501a
children 95edf106bdef
files apps/bundle/models.py apps/bundle/tasks.py apps/bundle/urls.py apps/bundle/views.py templates/bundle/bundle.djhtml
diffstat 5 files changed, 36 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/apps/bundle/models.py	Sat Oct 27 16:26:54 2012 -0400
+++ b/apps/bundle/models.py	Sun Feb 17 14:57:39 2013 -0500
@@ -53,6 +53,20 @@
             "%d_%d" % (self.id, self.latest_version))
 
 
+class BundleVersion(models.Model):
+    class Meta:
+        unique_together = ('bundle', 'version')
+    """
+    Needed to allow users to download the originally-uploaded files
+    """
+    bundle = models.ForeignKey(Bundle)
+    version = models.IntegerField()
+    file_name = models.CharField(max_length=256)
+
+    def __unicode__(self):
+        return self.file_name
+
+
 class BundleFile(MPTTModel):
     bundle = models.ForeignKey(Bundle)
     version = models.IntegerField()
--- a/apps/bundle/tasks.py	Sat Oct 27 16:26:54 2012 -0400
+++ b/apps/bundle/tasks.py	Sun Feb 17 14:57:39 2013 -0500
@@ -7,7 +7,7 @@
 import magic
 from celery import task
 
-from apps.bundle.models import Bundle, BundleFile
+from apps.bundle.models import Bundle, BundleFile, BundleVersion
 
 
 mimetypes.add_type('application/x-gzip', '.tgz')
@@ -72,14 +72,16 @@
     mime_type = magic.from_file(file, mime=True)
     extension = mimetypes.guess_extension(mime_type)
 
-    print "mime type: %s" % mime_type
-    print "extension: %s" % extension
-
     if extension in archive_extensions:
         new_path = file + extension
         # Treat it as an archive. Rename it to that, then extract
         os.rename(file, new_path)
 
+        # Create the new BundleVersion
+        new_file_name = os.path.basename(new_path)
+        BundleVersion.objects.create(bundle=bundle, file_name=new_file_name,
+            version=bundle.latest_version)
+
         try:
             # Extract it to a directory, same path as the filename
             archive.extract(new_path, to_path=file)
@@ -87,7 +89,6 @@
             # Now go through the extracted files, make BundleFiles from them
             process_files_in_dir(bundle, file, None)
         except archive.ArchiveException:
-            print "Archive exception"
             pass
     elif mime_type.startswith('text/'):
         # Should be a plain text file - create a CodeFile for it
@@ -97,6 +98,5 @@
         bundle_file.save_file_contents(open(file, 'rt'),
             original_filename=bundle.file_name)
 
-    print "Done uploading!"
     bundle.done_uploading = True
     bundle.save()
--- a/apps/bundle/urls.py	Sat Oct 27 16:26:54 2012 -0400
+++ b/apps/bundle/urls.py	Sun Feb 17 14:57:39 2013 -0500
@@ -10,6 +10,8 @@
     url(BUNDLE_PATTERN + '/' + VERSION_PATTERN + '/?$', 'detail',
         name='bundle_version'),
     url(BUNDLE_PATTERN + '/edit', 'edit', name='bundle_edit'),
+    url(BUNDLE_PATTERN + '/' + VERSION_PATTERN + '/download/?$',
+        'download', name='bundle_download'),
     url(BUNDLE_PATTERN + '/' + VERSION_PATTERN + '/(?P<path>.+)/?$',
         'file_detail', name='bundlefile_details'),
     url(r'^$', 'index', name='bundle_new'),
--- a/apps/bundle/views.py	Sat Oct 27 16:26:54 2012 -0400
+++ b/apps/bundle/views.py	Sun Feb 17 14:57:39 2013 -0500
@@ -5,8 +5,9 @@
 from django.shortcuts import get_object_or_404, render, redirect
 from django.contrib.auth.decorators import login_required
 from django.http import HttpResponse
+from django.views.static import serve
 
-from apps.bundle.models import Bundle, BundleFile
+from apps.bundle.models import Bundle, BundleFile, BundleVersion
 from apps.bundle.forms import BundleForm, BundleEditForm
 from apps.bundle.tasks import handle_bundle_upload
 from apps.pygments_style.models import PygmentsStyle
@@ -37,7 +38,6 @@
 
 
 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)
 
@@ -123,3 +123,14 @@
     }
 
     return render(request, "bundle/edit.djhtml", context)
+
+
+def download(request, user, bundle, version):
+    bundle = get_object_or_404(Bundle, uploader__username=user, name=bundle)
+    version = int(version)
+
+    # Look for the BundleVersion with this version
+    bundle_version = get_object_or_404(BundleVersion, bundle=bundle,
+        version=version)
+    return serve(request, bundle_version.file_name, os.path.join('tmp',
+        'bundles'))
--- a/templates/bundle/bundle.djhtml	Sat Oct 27 16:26:54 2012 -0400
+++ b/templates/bundle/bundle.djhtml	Sun Feb 17 14:57:39 2013 -0500
@@ -98,7 +98,7 @@
                 {% if version == this_version %}<strong>{% endif %}
                 Version {{ version }}
                 {% if version == this_version %}</strong>{% endif %}
-            </a>
+            </a> :: <a href="{% url bundle_download bundle.uploader.username bundle.name version %}">Download &raquo;</a>
         </li>
     {% endfor %}
     </ul>