diff apps/bundle/models.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 6ba969517b9c
children a57d15b044a7
line wrap: on
line diff
--- a/apps/bundle/models.py	Mon Oct 15 00:27:41 2012 -0400
+++ b/apps/bundle/models.py	Mon Oct 15 00:52:00 2012 -0400
@@ -1,30 +1,86 @@
+import os
+
 from django.db import models
 from django.contrib.auth.models import User
-from agora.apps.free_license.models import FreeLicense
-from agora.apps.snippet.models import CodeLanguage
+from pygments import lexers, highlight, formatters, util
+from mptt.models import MPTTModel, TreeForeignKey
+from sizefield.models import FileSizeField
+
+from apps.free_license.models import FreeLicense
+from apps.snippet.highlight import NakedHtmlFormatter
+
 
 class Bundle(models.Model):
-    name = models.CharField(max_length=256)
+    class Meta:
+        # Every user must pick unique names for their bundles
+        unique_together = ('uploader','name')
+        ordering = ['-pub_date']
+
+    name = models.SlugField()
     uploader = models.ForeignKey(User)
     description = models.TextField(max_length=32728)
-    free_license = models.ForeignKey(FreeLicense)
-    pub_date = models.DateTimeField('date uploaded')
-    mod_date = models.DateTimeField('date last modified')
-
-    class Meta:
-        #Every user must pick unique names for their bundles
-        unique_together = ('uploader','name')
+    free_license = models.ForeignKey(FreeLicense, default=1)
+    pub_date = models.DateTimeField('date uploaded', auto_now_add=True)
+    mod_date = models.DateTimeField('date last modified', auto_now=True)
+    done_uploading = models.BooleanField(default=False)
+    file_name = models.CharField(max_length=256) # the uploaded file
 
     def __unicode__(self):
         return self.name
 
-class BundleFile(models.Model):
+    @models.permalink
+    def get_absolute_url(self):
+        return ('bundle_details', [self.uploader.username, self.name])
+
+    def get_temp_path(self):
+        return os.path.join('tmp', 'bundles', '%s' % self.id)
+
+
+class BundleFile(MPTTModel):
+    bundle = models.ForeignKey(Bundle)
+    parent = TreeForeignKey('self', null=True, blank=True,
+        related_name='children')
     name = models.CharField(max_length=256)
-    bundle = models.ForeignKey(Bundle)
-    bundle_file = models.FileField(upload_to='bundles/')
+    is_dir = models.BooleanField()
+    code = models.TextField(null=True, blank=True)
+    full_path = models.CharField(max_length=256)
+    file_size = FileSizeField(default=0) # for directories
+
     def __unicode__(self):
         return self.name
 
-class CodeFile(BundleFile):
-    code = models.TextField()
-    language = models.ForeignKey(CodeLanguage)
+    def get_path(self):
+        if self.parent:
+            return os.path.join(self.parent.get_path(), self.name)
+        else:
+            return self.name
+
+    def get_lines(self):
+        return self.code.splitlines()
+
+    def save_file_contents(self, file, original_filename=None):
+        code = file.read()
+
+        if original_filename is not None:
+            filename = original_filename
+        else:
+            filename = file.name
+
+        try:
+            lexer = lexers.get_lexer_for_filename(filename)
+            print "lexer is:"
+            print lexer
+        except util.ClassNotFound:
+            print "can't guess the lexer"
+            lexer = lexers.TextLexer()
+
+        self.code = highlight(code, lexer, NakedHtmlFormatter())
+        self.save()
+
+    @models.permalink
+    def get_absolute_url(self):
+        return ('bundlefile_details', [
+            self.bundle.uploader.username,
+            self.bundle.name,
+            self.get_path()
+        ])