Mercurial > public > sg101
comparison gpp/downloads/models.py @ 204:b4305e18d3af
Resolve ticket #74. Add user badges. Some extra credit was done here: also refactored how pending news, links, and downloads are handled.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Sat, 01 May 2010 21:53:59 +0000 |
parents | 341759e1cda1 |
children | 272d3a8c98e8 |
comparison
equal
deleted
inserted
replaced
203:40e5903903e1 | 204:b4305e18d3af |
---|---|
43 def get_query_set(self): | 43 def get_query_set(self): |
44 return super(PublicDownloadManager, self).get_query_set().filter( | 44 return super(PublicDownloadManager, self).get_query_set().filter( |
45 is_public=True).select_related() | 45 is_public=True).select_related() |
46 | 46 |
47 | 47 |
48 class Download(models.Model): | 48 class DownloadBase(models.Model): |
49 """Model to represent a download.""" | 49 """Abstract model to collect common download fields.""" |
50 title = models.CharField(max_length=128) | 50 title = models.CharField(max_length=128) |
51 category = models.ForeignKey(Category) | 51 category = models.ForeignKey(Category) |
52 description = models.TextField() | 52 description = models.TextField() |
53 html = models.TextField(blank=True) | 53 html = models.TextField(blank=True) |
54 file = models.FileField(upload_to=download_path) | 54 file = models.FileField(upload_to=download_path) |
55 user = models.ForeignKey(User) | 55 user = models.ForeignKey(User) |
56 date_added = models.DateTimeField(auto_now_add=True) | 56 date_added = models.DateTimeField() |
57 ip_address = models.IPAddressField('IP Address') | 57 ip_address = models.IPAddressField('IP Address') |
58 | |
59 class Meta: | |
60 abstract = True | |
61 | |
62 def size(self): | |
63 return filesizeformat(self.file.size) | |
64 | |
65 | |
66 class PendingDownload(DownloadBase): | |
67 """This model represents pending downloads created by users. These pending | |
68 downloads must be approved by an admin before they turn into "real" | |
69 Downloads and are visible on site. | |
70 """ | |
71 class Meta: | |
72 ordering = ('date_added', ) | |
73 | |
74 def __unicode__(self): | |
75 return self.title | |
76 | |
77 def save(self, *args, **kwargs): | |
78 if not self.pk: | |
79 self.date_added = datetime.datetime.now() | |
80 self.html = site_markup(self.description) | |
81 super(PendingDownload, self).save(*args, **kwargs) | |
82 | |
83 | |
84 class Download(DownloadBase): | |
85 """Model to represent a download.""" | |
58 hits = models.IntegerField(default=0) | 86 hits = models.IntegerField(default=0) |
59 average_score = models.FloatField(default=0.0) | 87 average_score = models.FloatField(default=0.0) |
60 total_votes = models.IntegerField(default=0) | 88 total_votes = models.IntegerField(default=0) |
61 is_public = models.BooleanField(default=False, db_index=True) | 89 is_public = models.BooleanField(default=False, db_index=True) |
62 | 90 |
80 total_score = self.average_score * self.total_votes | 108 total_score = self.average_score * self.total_votes |
81 total_score += vote_value | 109 total_score += vote_value |
82 self.total_votes += 1 | 110 self.total_votes += 1 |
83 self.average_score = total_score / self.total_votes | 111 self.average_score = total_score / self.total_votes |
84 return self.average_score | 112 return self.average_score |
85 | |
86 def size(self): | |
87 return filesizeformat(self.file.size) | |
88 | 113 |
89 | 114 |
90 class AllowedExtensionManager(models.Manager): | 115 class AllowedExtensionManager(models.Manager): |
91 def get_extension_list(self): | 116 def get_extension_list(self): |
92 return self.values_list('extension', flat=True) | 117 return self.values_list('extension', flat=True) |