changeset 211:2a234e11185c

Merge in Wendy's changes
author Jordi Gutiérrez Hermoso <jordigh@octave.org>
date Wed, 21 Aug 2013 14:43:48 -0400
parents 95edf106bdef (diff) a22259c9862e (current diff)
children b3c3d940cc87
files apps/bundle/views.py templates/bundle/bundle.djhtml
diffstat 5 files changed, 39 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/apps/bundle/models.py	Wed Aug 21 03:52:06 2013 +0500
+++ b/apps/bundle/models.py	Wed Aug 21 14:43:48 2013 -0400
@@ -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	Wed Aug 21 03:52:06 2013 +0500
+++ b/apps/bundle/tasks.py	Wed Aug 21 14:43:48 2013 -0400
@@ -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,11 +72,11 @@
     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
+        # Save the new file name (needed for the BundleVersion)
+
+        new_file_name = os.path.basename(new_path)
         # Treat it as an archive. Rename it to that, then extract
         os.rename(file, new_path)
 
@@ -87,7 +87,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
@@ -96,7 +95,11 @@
             version=bundle.latest_version)
         bundle_file.save_file_contents(open(file, 'rt'),
             original_filename=bundle.file_name)
+        new_file_name = os.path.basename(file)
 
-    print "Done uploading!"
+    # Create the new BundleVersion
+    BundleVersion.objects.create(bundle=bundle, file_name=new_file_name,
+        version=bundle.latest_version)
+
     bundle.done_uploading = True
     bundle.save()
--- a/apps/bundle/urls.py	Wed Aug 21 03:52:06 2013 +0500
+++ b/apps/bundle/urls.py	Wed Aug 21 14:43:48 2013 -0400
@@ -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	Wed Aug 21 03:52:06 2013 +0500
+++ b/apps/bundle/views.py	Wed Aug 21 14:43:48 2013 -0400
@@ -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
@@ -38,7 +39,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)
 
@@ -124,3 +124,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	Wed Aug 21 03:52:06 2013 +0500
+++ b/templates/bundle/bundle.djhtml	Wed Aug 21 14:43:48 2013 -0400
@@ -100,7 +100,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>