gremmie@1
|
1 """
|
gremmie@1
|
2 Models for the downloads application.
|
gremmie@1
|
3 """
|
gremmie@1
|
4 import os
|
gremmie@1
|
5
|
gremmie@1
|
6 import datetime
|
gremmie@1
|
7 from django.db import models
|
gremmie@1
|
8 from django.contrib.auth.models import User
|
gremmie@1
|
9 from django.template.defaultfilters import filesizeformat
|
gremmie@1
|
10
|
bgneal@124
|
11 from core.markup import markdown
|
bgneal@124
|
12 from smiley import smilify
|
bgneal@124
|
13
|
gremmie@1
|
14
|
gremmie@1
|
15 class Category(models.Model):
|
gremmie@1
|
16 """Downloads belong to categories."""
|
gremmie@1
|
17 title = models.CharField(max_length=64)
|
gremmie@1
|
18 description = models.TextField(blank=True)
|
gremmie@1
|
19
|
gremmie@1
|
20 class Meta:
|
gremmie@1
|
21 verbose_name_plural = 'Categories'
|
gremmie@1
|
22 ordering = ('title', )
|
gremmie@1
|
23
|
gremmie@1
|
24 def __unicode__(self):
|
gremmie@1
|
25 return self.title
|
gremmie@1
|
26
|
gremmie@1
|
27 def num_downloads(self):
|
gremmie@1
|
28 return Download.public_objects.filter(category=self.pk).count()
|
gremmie@1
|
29
|
gremmie@1
|
30
|
gremmie@1
|
31 def download_path(instance, filename):
|
gremmie@1
|
32 """
|
gremmie@1
|
33 Creates a path for a download. Uses the current date to avoid filename
|
gremmie@1
|
34 clashes. Uses the current microsecond also to make the directory name
|
gremmie@1
|
35 harder to guess.
|
gremmie@1
|
36 """
|
gremmie@1
|
37 now = datetime.datetime.now()
|
gremmie@1
|
38 parts = ['downloads']
|
gremmie@1
|
39 parts.extend([str(p) for p in (now.year, now.month, now.day, now.microsecond)])
|
gremmie@1
|
40 parts.append(filename)
|
gremmie@1
|
41 return os.path.join(*parts)
|
gremmie@1
|
42
|
gremmie@1
|
43
|
gremmie@1
|
44 class PublicDownloadManager(models.Manager):
|
gremmie@1
|
45 """The manager for all public downloads."""
|
gremmie@1
|
46 def get_query_set(self):
|
gremmie@1
|
47 return super(PublicDownloadManager, self).get_query_set().filter(is_public=True)
|
gremmie@1
|
48
|
gremmie@1
|
49
|
gremmie@1
|
50 class Download(models.Model):
|
gremmie@1
|
51 """Model to represent a download."""
|
gremmie@1
|
52 title = models.CharField(max_length=128)
|
gremmie@1
|
53 category = models.ForeignKey(Category)
|
gremmie@1
|
54 description = models.TextField()
|
gremmie@1
|
55 html = models.TextField(blank=True)
|
gremmie@1
|
56 file = models.FileField(upload_to=download_path)
|
gremmie@1
|
57 user = models.ForeignKey(User)
|
gremmie@1
|
58 date_added = models.DateTimeField(auto_now_add=True)
|
gremmie@1
|
59 ip_address = models.IPAddressField('IP Address')
|
gremmie@1
|
60 hits = models.IntegerField(default=0)
|
gremmie@1
|
61 average_score = models.FloatField(default=0.0)
|
gremmie@1
|
62 total_votes = models.IntegerField(default=0)
|
gremmie@1
|
63 is_public = models.BooleanField(default=False, db_index=True)
|
gremmie@1
|
64
|
gremmie@1
|
65 # Managers:
|
gremmie@1
|
66 objects = models.Manager()
|
gremmie@1
|
67 public_objects = PublicDownloadManager()
|
gremmie@1
|
68
|
gremmie@1
|
69 def __unicode__(self):
|
gremmie@1
|
70 return self.title
|
gremmie@1
|
71
|
bgneal@14
|
72 @models.permalink
|
bgneal@14
|
73 def get_absolute_url(self):
|
bgneal@23
|
74 return ('downloads-details', [str(self.id)])
|
bgneal@14
|
75
|
gremmie@1
|
76 def save(self, force_insert=False, force_update=False):
|
bgneal@124
|
77 self.html = smilify(markdown(self.description))
|
gremmie@1
|
78 super(Download, self).save(force_insert, force_update)
|
gremmie@1
|
79
|
gremmie@1
|
80 def vote(self, vote_value):
|
gremmie@1
|
81 """receives a vote_value and updates internal score accordingly"""
|
gremmie@1
|
82 total_score = self.average_score * self.total_votes
|
gremmie@1
|
83 total_score += vote_value
|
gremmie@1
|
84 self.total_votes += 1
|
gremmie@1
|
85 self.average_score = total_score / self.total_votes
|
gremmie@1
|
86 return self.average_score
|
gremmie@1
|
87
|
gremmie@1
|
88 def size(self):
|
gremmie@1
|
89 return filesizeformat(self.file.size)
|
gremmie@1
|
90
|
gremmie@1
|
91
|
gremmie@1
|
92 class AllowedExtensionManager(models.Manager):
|
gremmie@1
|
93 def get_extension_list(self):
|
gremmie@1
|
94 return self.values_list('extension', flat=True)
|
gremmie@1
|
95
|
gremmie@1
|
96
|
gremmie@1
|
97 class AllowedExtension(models.Model):
|
gremmie@1
|
98 """Model to represent the list of allowed file extensions."""
|
gremmie@1
|
99 extension = models.CharField(max_length=8)
|
gremmie@1
|
100
|
gremmie@1
|
101 objects = AllowedExtensionManager()
|
gremmie@1
|
102
|
gremmie@1
|
103 def __unicode__(self):
|
gremmie@1
|
104 return self.extension
|
gremmie@1
|
105
|
gremmie@1
|
106 class Meta:
|
gremmie@1
|
107 ordering = ('extension', )
|
gremmie@1
|
108
|
gremmie@1
|
109
|
gremmie@1
|
110 class VoteRecord(models.Model):
|
gremmie@1
|
111 """Model to record the date that a user voted on a download."""
|
gremmie@1
|
112 download = models.ForeignKey(Download)
|
gremmie@1
|
113 user = models.ForeignKey(User)
|
gremmie@1
|
114 vote_date = models.DateTimeField(auto_now_add=True)
|
gremmie@1
|
115
|
gremmie@1
|
116 def __unicode__(self):
|
gremmie@1
|
117 return "%s voted on '%s' on %s" % (
|
gremmie@1
|
118 self.user.username,
|
gremmie@1
|
119 self.download.title,
|
gremmie@1
|
120 self.vote_date.strftime('%b %d, %Y %H:%M:%S'))
|
gremmie@1
|
121
|
gremmie@1
|
122 class Meta:
|
gremmie@1
|
123 ordering = ('-vote_date', )
|