annotate apps/bundle/models.py @ 177:86129d185ddb

Add versioning to bundles Some other bundle-related changes were made, including: * Editing the snippetform CSS and HTML to allow bundle/form.djhtml to be reused for editing * Changing {% block title %} to {% block section %} in the base template for bundles to allow for more flexibility when creating breadcrumbs * Saved common URL patterns in variables in bundle/urls.py * Renamed explore.html to explore.djhtml for consistency You should now be able to upload new versions as well as view the files (or a particular file) for a bundle at a specific version. Coming soon: the ability to add a timestamp and a comment for each new uploaded version (if this feature is desirable).
author dellsystem <ilostwaldo@gmail.com>
date Sat, 20 Oct 2012 23:28:50 -0400
parents c042d26e6936
children b711f0087709
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
151
c7be7def8b57 Bundles! (basic functionality)
dellsystem <ilostwaldo@gmail.com>
parents: 29
diff changeset
1 import os
c7be7def8b57 Bundles! (basic functionality)
dellsystem <ilostwaldo@gmail.com>
parents: 29
diff changeset
2
2
3cef0d445036 Start actual app structure
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
diff changeset
3 from django.db import models
3
9a4429652662 Define models
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 2
diff changeset
4 from django.contrib.auth.models import User
171
a57d15b044a7 Add help_text to name field for bundles
dellsystem <ilostwaldo@gmail.com>
parents: 151
diff changeset
5 from django.utils.translation import ugettext_lazy as _
151
c7be7def8b57 Bundles! (basic functionality)
dellsystem <ilostwaldo@gmail.com>
parents: 29
diff changeset
6 from pygments import lexers, highlight, formatters, util
c7be7def8b57 Bundles! (basic functionality)
dellsystem <ilostwaldo@gmail.com>
parents: 29
diff changeset
7 from mptt.models import MPTTModel, TreeForeignKey
c7be7def8b57 Bundles! (basic functionality)
dellsystem <ilostwaldo@gmail.com>
parents: 29
diff changeset
8 from sizefield.models import FileSizeField
c7be7def8b57 Bundles! (basic functionality)
dellsystem <ilostwaldo@gmail.com>
parents: 29
diff changeset
9
c7be7def8b57 Bundles! (basic functionality)
dellsystem <ilostwaldo@gmail.com>
parents: 29
diff changeset
10 from apps.free_license.models import FreeLicense
c7be7def8b57 Bundles! (basic functionality)
dellsystem <ilostwaldo@gmail.com>
parents: 29
diff changeset
11 from apps.snippet.highlight import NakedHtmlFormatter
c7be7def8b57 Bundles! (basic functionality)
dellsystem <ilostwaldo@gmail.com>
parents: 29
diff changeset
12
2
3cef0d445036 Start actual app structure
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
diff changeset
13
3
9a4429652662 Define models
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 2
diff changeset
14 class Bundle(models.Model):
151
c7be7def8b57 Bundles! (basic functionality)
dellsystem <ilostwaldo@gmail.com>
parents: 29
diff changeset
15 class Meta:
c7be7def8b57 Bundles! (basic functionality)
dellsystem <ilostwaldo@gmail.com>
parents: 29
diff changeset
16 # Every user must pick unique names for their bundles
c7be7def8b57 Bundles! (basic functionality)
dellsystem <ilostwaldo@gmail.com>
parents: 29
diff changeset
17 unique_together = ('uploader','name')
c7be7def8b57 Bundles! (basic functionality)
dellsystem <ilostwaldo@gmail.com>
parents: 29
diff changeset
18 ordering = ['-pub_date']
c7be7def8b57 Bundles! (basic functionality)
dellsystem <ilostwaldo@gmail.com>
parents: 29
diff changeset
19
171
a57d15b044a7 Add help_text to name field for bundles
dellsystem <ilostwaldo@gmail.com>
parents: 151
diff changeset
20 name = models.SlugField(help_text=_("Your bundle will be accessible " +
a57d15b044a7 Add help_text to name field for bundles
dellsystem <ilostwaldo@gmail.com>
parents: 151
diff changeset
21 "from a URL that uses the name you enter here, so choose wisely. " +
a57d15b044a7 Add help_text to name field for bundles
dellsystem <ilostwaldo@gmail.com>
parents: 151
diff changeset
22 "Acceptable characters: alphanumeric characters, hyphens, and " +
a57d15b044a7 Add help_text to name field for bundles
dellsystem <ilostwaldo@gmail.com>
parents: 151
diff changeset
23 "underscores."))
3
9a4429652662 Define models
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 2
diff changeset
24 uploader = models.ForeignKey(User)
176
c042d26e6936 Make Bundle description optional (SCHEMA CHANGE)
dellsystem <ilostwaldo@gmail.com>
parents: 171
diff changeset
25 description = models.TextField(max_length=32728, blank=True, null=True)
151
c7be7def8b57 Bundles! (basic functionality)
dellsystem <ilostwaldo@gmail.com>
parents: 29
diff changeset
26 free_license = models.ForeignKey(FreeLicense, default=1)
c7be7def8b57 Bundles! (basic functionality)
dellsystem <ilostwaldo@gmail.com>
parents: 29
diff changeset
27 pub_date = models.DateTimeField('date uploaded', auto_now_add=True)
c7be7def8b57 Bundles! (basic functionality)
dellsystem <ilostwaldo@gmail.com>
parents: 29
diff changeset
28 mod_date = models.DateTimeField('date last modified', auto_now=True)
c7be7def8b57 Bundles! (basic functionality)
dellsystem <ilostwaldo@gmail.com>
parents: 29
diff changeset
29 done_uploading = models.BooleanField(default=False)
c7be7def8b57 Bundles! (basic functionality)
dellsystem <ilostwaldo@gmail.com>
parents: 29
diff changeset
30 file_name = models.CharField(max_length=256) # the uploaded file
177
86129d185ddb Add versioning to bundles
dellsystem <ilostwaldo@gmail.com>
parents: 176
diff changeset
31 latest_version = models.IntegerField(default=1)
6
c83460992637 Elaborate on stub bundle template
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 4
diff changeset
32
3
9a4429652662 Define models
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 2
diff changeset
33 def __unicode__(self):
9a4429652662 Define models
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 2
diff changeset
34 return self.name
9a4429652662 Define models
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 2
diff changeset
35
151
c7be7def8b57 Bundles! (basic functionality)
dellsystem <ilostwaldo@gmail.com>
parents: 29
diff changeset
36 @models.permalink
c7be7def8b57 Bundles! (basic functionality)
dellsystem <ilostwaldo@gmail.com>
parents: 29
diff changeset
37 def get_absolute_url(self):
c7be7def8b57 Bundles! (basic functionality)
dellsystem <ilostwaldo@gmail.com>
parents: 29
diff changeset
38 return ('bundle_details', [self.uploader.username, self.name])
c7be7def8b57 Bundles! (basic functionality)
dellsystem <ilostwaldo@gmail.com>
parents: 29
diff changeset
39
c7be7def8b57 Bundles! (basic functionality)
dellsystem <ilostwaldo@gmail.com>
parents: 29
diff changeset
40 def get_temp_path(self):
177
86129d185ddb Add versioning to bundles
dellsystem <ilostwaldo@gmail.com>
parents: 176
diff changeset
41 """
86129d185ddb Add versioning to bundles
dellsystem <ilostwaldo@gmail.com>
parents: 176
diff changeset
42 Returns the path to where the file is initially uploaded
86129d185ddb Add versioning to bundles
dellsystem <ilostwaldo@gmail.com>
parents: 176
diff changeset
43 """
86129d185ddb Add versioning to bundles
dellsystem <ilostwaldo@gmail.com>
parents: 176
diff changeset
44 return os.path.join('tmp', 'bundles',
86129d185ddb Add versioning to bundles
dellsystem <ilostwaldo@gmail.com>
parents: 176
diff changeset
45 "%d_%d" % (self.id, self.latest_version))
151
c7be7def8b57 Bundles! (basic functionality)
dellsystem <ilostwaldo@gmail.com>
parents: 29
diff changeset
46
c7be7def8b57 Bundles! (basic functionality)
dellsystem <ilostwaldo@gmail.com>
parents: 29
diff changeset
47
c7be7def8b57 Bundles! (basic functionality)
dellsystem <ilostwaldo@gmail.com>
parents: 29
diff changeset
48 class BundleFile(MPTTModel):
c7be7def8b57 Bundles! (basic functionality)
dellsystem <ilostwaldo@gmail.com>
parents: 29
diff changeset
49 bundle = models.ForeignKey(Bundle)
177
86129d185ddb Add versioning to bundles
dellsystem <ilostwaldo@gmail.com>
parents: 176
diff changeset
50 version = models.IntegerField()
151
c7be7def8b57 Bundles! (basic functionality)
dellsystem <ilostwaldo@gmail.com>
parents: 29
diff changeset
51 parent = TreeForeignKey('self', null=True, blank=True,
c7be7def8b57 Bundles! (basic functionality)
dellsystem <ilostwaldo@gmail.com>
parents: 29
diff changeset
52 related_name='children')
3
9a4429652662 Define models
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 2
diff changeset
53 name = models.CharField(max_length=256)
151
c7be7def8b57 Bundles! (basic functionality)
dellsystem <ilostwaldo@gmail.com>
parents: 29
diff changeset
54 is_dir = models.BooleanField()
c7be7def8b57 Bundles! (basic functionality)
dellsystem <ilostwaldo@gmail.com>
parents: 29
diff changeset
55 code = models.TextField(null=True, blank=True)
c7be7def8b57 Bundles! (basic functionality)
dellsystem <ilostwaldo@gmail.com>
parents: 29
diff changeset
56 full_path = models.CharField(max_length=256)
c7be7def8b57 Bundles! (basic functionality)
dellsystem <ilostwaldo@gmail.com>
parents: 29
diff changeset
57 file_size = FileSizeField(default=0) # for directories
c7be7def8b57 Bundles! (basic functionality)
dellsystem <ilostwaldo@gmail.com>
parents: 29
diff changeset
58
3
9a4429652662 Define models
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 2
diff changeset
59 def __unicode__(self):
9a4429652662 Define models
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 2
diff changeset
60 return self.name
9a4429652662 Define models
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 2
diff changeset
61
151
c7be7def8b57 Bundles! (basic functionality)
dellsystem <ilostwaldo@gmail.com>
parents: 29
diff changeset
62 def get_path(self):
c7be7def8b57 Bundles! (basic functionality)
dellsystem <ilostwaldo@gmail.com>
parents: 29
diff changeset
63 if self.parent:
c7be7def8b57 Bundles! (basic functionality)
dellsystem <ilostwaldo@gmail.com>
parents: 29
diff changeset
64 return os.path.join(self.parent.get_path(), self.name)
c7be7def8b57 Bundles! (basic functionality)
dellsystem <ilostwaldo@gmail.com>
parents: 29
diff changeset
65 else:
c7be7def8b57 Bundles! (basic functionality)
dellsystem <ilostwaldo@gmail.com>
parents: 29
diff changeset
66 return self.name
c7be7def8b57 Bundles! (basic functionality)
dellsystem <ilostwaldo@gmail.com>
parents: 29
diff changeset
67
c7be7def8b57 Bundles! (basic functionality)
dellsystem <ilostwaldo@gmail.com>
parents: 29
diff changeset
68 def get_lines(self):
c7be7def8b57 Bundles! (basic functionality)
dellsystem <ilostwaldo@gmail.com>
parents: 29
diff changeset
69 return self.code.splitlines()
c7be7def8b57 Bundles! (basic functionality)
dellsystem <ilostwaldo@gmail.com>
parents: 29
diff changeset
70
c7be7def8b57 Bundles! (basic functionality)
dellsystem <ilostwaldo@gmail.com>
parents: 29
diff changeset
71 def save_file_contents(self, file, original_filename=None):
c7be7def8b57 Bundles! (basic functionality)
dellsystem <ilostwaldo@gmail.com>
parents: 29
diff changeset
72 code = file.read()
c7be7def8b57 Bundles! (basic functionality)
dellsystem <ilostwaldo@gmail.com>
parents: 29
diff changeset
73
c7be7def8b57 Bundles! (basic functionality)
dellsystem <ilostwaldo@gmail.com>
parents: 29
diff changeset
74 if original_filename is not None:
c7be7def8b57 Bundles! (basic functionality)
dellsystem <ilostwaldo@gmail.com>
parents: 29
diff changeset
75 filename = original_filename
c7be7def8b57 Bundles! (basic functionality)
dellsystem <ilostwaldo@gmail.com>
parents: 29
diff changeset
76 else:
c7be7def8b57 Bundles! (basic functionality)
dellsystem <ilostwaldo@gmail.com>
parents: 29
diff changeset
77 filename = file.name
c7be7def8b57 Bundles! (basic functionality)
dellsystem <ilostwaldo@gmail.com>
parents: 29
diff changeset
78
c7be7def8b57 Bundles! (basic functionality)
dellsystem <ilostwaldo@gmail.com>
parents: 29
diff changeset
79 try:
c7be7def8b57 Bundles! (basic functionality)
dellsystem <ilostwaldo@gmail.com>
parents: 29
diff changeset
80 lexer = lexers.get_lexer_for_filename(filename)
c7be7def8b57 Bundles! (basic functionality)
dellsystem <ilostwaldo@gmail.com>
parents: 29
diff changeset
81 print "lexer is:"
c7be7def8b57 Bundles! (basic functionality)
dellsystem <ilostwaldo@gmail.com>
parents: 29
diff changeset
82 print lexer
c7be7def8b57 Bundles! (basic functionality)
dellsystem <ilostwaldo@gmail.com>
parents: 29
diff changeset
83 except util.ClassNotFound:
c7be7def8b57 Bundles! (basic functionality)
dellsystem <ilostwaldo@gmail.com>
parents: 29
diff changeset
84 print "can't guess the lexer"
c7be7def8b57 Bundles! (basic functionality)
dellsystem <ilostwaldo@gmail.com>
parents: 29
diff changeset
85 lexer = lexers.TextLexer()
c7be7def8b57 Bundles! (basic functionality)
dellsystem <ilostwaldo@gmail.com>
parents: 29
diff changeset
86
c7be7def8b57 Bundles! (basic functionality)
dellsystem <ilostwaldo@gmail.com>
parents: 29
diff changeset
87 self.code = highlight(code, lexer, NakedHtmlFormatter())
c7be7def8b57 Bundles! (basic functionality)
dellsystem <ilostwaldo@gmail.com>
parents: 29
diff changeset
88 self.save()
c7be7def8b57 Bundles! (basic functionality)
dellsystem <ilostwaldo@gmail.com>
parents: 29
diff changeset
89
c7be7def8b57 Bundles! (basic functionality)
dellsystem <ilostwaldo@gmail.com>
parents: 29
diff changeset
90 @models.permalink
c7be7def8b57 Bundles! (basic functionality)
dellsystem <ilostwaldo@gmail.com>
parents: 29
diff changeset
91 def get_absolute_url(self):
c7be7def8b57 Bundles! (basic functionality)
dellsystem <ilostwaldo@gmail.com>
parents: 29
diff changeset
92 return ('bundlefile_details', [
c7be7def8b57 Bundles! (basic functionality)
dellsystem <ilostwaldo@gmail.com>
parents: 29
diff changeset
93 self.bundle.uploader.username,
c7be7def8b57 Bundles! (basic functionality)
dellsystem <ilostwaldo@gmail.com>
parents: 29
diff changeset
94 self.bundle.name,
177
86129d185ddb Add versioning to bundles
dellsystem <ilostwaldo@gmail.com>
parents: 176
diff changeset
95 self.version,
151
c7be7def8b57 Bundles! (basic functionality)
dellsystem <ilostwaldo@gmail.com>
parents: 29
diff changeset
96 self.get_path()
c7be7def8b57 Bundles! (basic functionality)
dellsystem <ilostwaldo@gmail.com>
parents: 29
diff changeset
97 ])