[extensions-web] Don't use the filename as a path component



commit a07c3578f9b7f4a779677eaae275f238ac680b0a
Author: Jasper St. Pierre <jstpierre mecheye net>
Date:   Mon Oct 31 13:27:18 2011 -0400

    Don't use the filename as a path component
    
    Rather than using the uploaded filename as a path component, allow (for now)
    one icon and screenshot per extension, marked by the PK. Doing it the way
    we are now is dangerous and stupid.

 .../migrations/0009_icon_and_screenshot_names.py   |  112 ++++++++++++++++++++
 sweettooth/extensions/models.py                    |    8 +-
 2 files changed, 116 insertions(+), 4 deletions(-)
---
diff --git a/sweettooth/extensions/migrations/0009_icon_and_screenshot_names.py b/sweettooth/extensions/migrations/0009_icon_and_screenshot_names.py
new file mode 100644
index 0000000..1c18a82
--- /dev/null
+++ b/sweettooth/extensions/migrations/0009_icon_and_screenshot_names.py
@@ -0,0 +1,112 @@
+# encoding: utf-8
+import os
+import os.path
+import datetime
+import shutil
+from south.db import db
+from south.v2 import DataMigration
+from django.conf import settings
+from django.db import models
+
+class Migration(DataMigration):
+
+    def forwards(self, orm):
+        "Write your forwards methods here."
+        try:
+            os.makedirs(os.path.join(settings.MEDIA_ROOT, 'screenshots'))
+        except os.error:
+            pass
+
+        try:
+            os.makedirs(os.path.join(settings.MEDIA_ROOT, 'icons'))
+        except os.error:
+            pass
+
+        for ext in orm.Extension.objects.all():
+            if ext.screenshot:
+                new_screenshot = os.path.join("screenshots", "screenshot_%d.png" % (ext.pk,))
+                os.rename(ext.screenshot.url, os.path.join(settings.MEDIA_ROOT, new_screenshot))
+                ext.screenshot = new_screenshot
+
+            new_icon = os.path.join("icons", "icon_%d.png" % (ext.pk,))
+            if ext.icon.path == orm.Extension._meta.get_field_by_name('icon')[0].default:
+                # Don't try to move the default
+                continue
+            shutil.copy(ext.icon.path, os.path.join(settings.MEDIA_ROOT, new_icon))
+            ext.icon = new_icon
+            ext.save()
+
+    def backwards(self, orm):
+        "Write your backwards methods here."
+
+
+    models = {
+        'auth.group': {
+            'Meta': {'object_name': 'Group'},
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'name': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '80'}),
+            'permissions': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Permission']", 'symmetrical': 'False', 'blank': 'True'})
+        },
+        'auth.permission': {
+            'Meta': {'ordering': "('content_type__app_label', 'content_type__model', 'codename')", 'unique_together': "(('content_type', 'codename'),)", 'object_name': 'Permission'},
+            'codename': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
+            'content_type': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['contenttypes.ContentType']"}),
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'name': ('django.db.models.fields.CharField', [], {'max_length': '50'})
+        },
+        'auth.user': {
+            'Meta': {'object_name': 'User'},
+            'date_joined': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}),
+            'email': ('django.db.models.fields.EmailField', [], {'max_length': '75', 'blank': 'True'}),
+            'first_name': ('django.db.models.fields.CharField', [], {'max_length': '30', 'blank': 'True'}),
+            'groups': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Group']", 'symmetrical': 'False', 'blank': 'True'}),
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'is_active': ('django.db.models.fields.BooleanField', [], {'default': 'True'}),
+            'is_staff': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
+            'is_superuser': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
+            'last_login': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}),
+            'last_name': ('django.db.models.fields.CharField', [], {'max_length': '30', 'blank': 'True'}),
+            'password': ('django.db.models.fields.CharField', [], {'max_length': '128'}),
+            'user_permissions': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Permission']", 'symmetrical': 'False', 'blank': 'True'}),
+            'username': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '30'})
+        },
+        'contenttypes.contenttype': {
+            'Meta': {'ordering': "('name',)", 'unique_together': "(('app_label', 'model'),)", 'object_name': 'ContentType', 'db_table': "'django_content_type'"},
+            'app_label': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'model': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
+            'name': ('django.db.models.fields.CharField', [], {'max_length': '100'})
+        },
+        'extensions.extension': {
+            'Meta': {'object_name': 'Extension'},
+            'created': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}),
+            'creator': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['auth.User']"}),
+            'description': ('django.db.models.fields.TextField', [], {}),
+            'icon': ('django.db.models.fields.files.ImageField', [], {'default': "'/static/images/plugin.png'", 'max_length': '100', 'blank': 'True'}),
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'name': ('django.db.models.fields.CharField', [], {'max_length': '200'}),
+            'screenshot': ('sorl.thumbnail.fields.ImageField', [], {'max_length': '100', 'blank': 'True'}),
+            'slug': ('autoslug.fields.AutoSlugField', [], {'unique_with': '()', 'max_length': '50', 'populate_from': 'None', 'db_index': 'True'}),
+            'url': ('django.db.models.fields.URLField', [], {'max_length': '200'}),
+            'uuid': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '200', 'db_index': 'True'})
+        },
+        'extensions.extensionversion': {
+            'Meta': {'unique_together': "(('extension', 'version'),)", 'object_name': 'ExtensionVersion'},
+            'extension': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'versions'", 'to': "orm['extensions.Extension']"}),
+            'extra_json_fields': ('django.db.models.fields.TextField', [], {}),
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'shell_versions': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['extensions.ShellVersion']", 'symmetrical': 'False'}),
+            'source': ('django.db.models.fields.files.FileField', [], {'max_length': '223'}),
+            'status': ('django.db.models.fields.PositiveIntegerField', [], {}),
+            'version': ('django.db.models.fields.IntegerField', [], {'default': '0'})
+        },
+        'extensions.shellversion': {
+            'Meta': {'object_name': 'ShellVersion'},
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'major': ('django.db.models.fields.PositiveIntegerField', [], {}),
+            'minor': ('django.db.models.fields.PositiveIntegerField', [], {}),
+            'point': ('django.db.models.fields.IntegerField', [], {})
+        }
+    }
+
+    complete_apps = ['extensions']
diff --git a/sweettooth/extensions/models.py b/sweettooth/extensions/models.py
index 5456dbe..d04260f 100644
--- a/sweettooth/extensions/models.py
+++ b/sweettooth/extensions/models.py
@@ -59,13 +59,13 @@ class Extension(models.Model):
             ("can-modify-data", "Can modify extension data"),
         )
 
-    def make_screenshot_filename(self, filename):
-        return os.path.join(self.uuid, filename)
+    def make_screenshot_filename(self, filename=None):
+        return "screenshots/screenshot_%d.png" % (self.pk,)
 
     screenshot = thumbnail.ImageField(upload_to=make_screenshot_filename, blank=True)
 
-    def make_icon_filename(self, filename):
-        return os.path.join(self.uuid, "icons/", filename)
+    def make_icon_filename(self, filename=None):
+        return "icons/icon_%d.png" % (self.pk,)
 
     icon = models.ImageField(upload_to=make_icon_filename, blank=True, default="/static/images/plugin.png")
 



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]