[extensions-web] Add a downloads field, and keep track of downloads



commit 3a4fb8860bb128d085a90a9bad7d419f998d472d
Author: Jasper St. Pierre <jstpierre mecheye net>
Date:   Thu Dec 15 17:43:56 2011 -0500

    Add a downloads field, and keep track of downloads

 .../management/commands/populatedownloads.py       |   21 +++++
 .../0010_auto__add_field_extension_downloads.py    |   91 ++++++++++++++++++++
 sweettooth/extensions/models.py                    |    1 +
 sweettooth/extensions/views.py                     |    4 +
 4 files changed, 117 insertions(+), 0 deletions(-)
---
diff --git a/sweettooth/extensions/management/commands/populatedownloads.py b/sweettooth/extensions/management/commands/populatedownloads.py
new file mode 100644
index 0000000..0e86dc0
--- /dev/null
+++ b/sweettooth/extensions/management/commands/populatedownloads.py
@@ -0,0 +1,21 @@
+
+from django.core.management.base import BaseCommand, CommandError
+from extensions.models import Extension
+
+class Command(BaseCommand):
+    args = 'downnload_data [download_data ...]'
+    help = 'Populates the downloads field from a special whitespace-separated format, easy to generate with some shell scripts'
+
+    def handle(self, *filenames, **options):
+        for filename in filenames:
+            with open(filename, 'r') as download_data:
+                for line in download_data:
+                    downloads, uuid = line.split()
+
+                    try:
+                        ext = Extension.objects.get(uuid=uuid)
+                    except Extension.DoesNotExist:
+                        print "Skipping", uuid
+                    else:
+                        ext.downloads = int(downloads, 10)
+                        ext.save()
diff --git a/sweettooth/extensions/migrations/0010_auto__add_field_extension_downloads.py b/sweettooth/extensions/migrations/0010_auto__add_field_extension_downloads.py
new file mode 100644
index 0000000..e884d90
--- /dev/null
+++ b/sweettooth/extensions/migrations/0010_auto__add_field_extension_downloads.py
@@ -0,0 +1,91 @@
+# encoding: utf-8
+import datetime
+from south.db import db
+from south.v2 import SchemaMigration
+from django.db import models
+
+class Migration(SchemaMigration):
+
+    def forwards(self, orm):
+        
+        # Adding field 'Extension.downloads'
+        db.add_column('extensions_extension', 'downloads', self.gf('django.db.models.fields.PositiveIntegerField')(default=0), keep_default=False)
+
+
+    def backwards(self, orm):
+        
+        # Deleting field 'Extension.downloads'
+        db.delete_column('extensions_extension', 'downloads')
+
+
+    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', [], {'blank': 'True'}),
+            'downloads': ('django.db.models.fields.PositiveIntegerField', [], {'default': '0'}),
+            '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', 'blank': 'True'}),
+            '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 66422fe..6931fc8 100644
--- a/sweettooth/extensions/models.py
+++ b/sweettooth/extensions/models.py
@@ -71,6 +71,7 @@ class Extension(models.Model):
     description = models.TextField(blank=True)
     url = models.URLField(verify_exists=False, blank=True)
     created = models.DateTimeField(auto_now_add=True)
+    downloads = models.PositiveIntegerField(default=0)
 
     class Meta:
         permissions = (
diff --git a/sweettooth/extensions/views.py b/sweettooth/extensions/views.py
index 95f2c5c..b45847e 100644
--- a/sweettooth/extensions/views.py
+++ b/sweettooth/extensions/views.py
@@ -26,6 +26,7 @@ def shell_download(request, uuid):
 
     else:
         version = get_object_or_404(models.ExtensionVersion, pk=pk)
+        extension = version.extension
 
         if version.extension.uuid != uuid:
             raise Http404()
@@ -33,6 +34,9 @@ def shell_download(request, uuid):
     if version.status != models.STATUS_ACTIVE:
         return HttpResponseForbidden()
 
+    extension.downloads += 1
+    extension.save()
+
     return redirect(version.source.url)
 
 @ajax_view



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