annotate gpp/downloads/models.py @ 210:77b606e0ad5f

Fix #78: improve the download upload_to path.
author Brian Neal <bgneal@gmail.com>
date Fri, 07 May 2010 02:56:49 +0000
parents 272d3a8c98e8
children 8d13baeaa5c1
rev   line source
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@128 11 from core.markup import site_markup
bgneal@124 12
gremmie@1 13
gremmie@1 14 class Category(models.Model):
gremmie@1 15 """Downloads belong to categories."""
gremmie@1 16 title = models.CharField(max_length=64)
gremmie@1 17 description = models.TextField(blank=True)
bgneal@192 18 count = models.IntegerField(default=0, 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
gremmie@1 28 def download_path(instance, filename):
gremmie@1 29 """
gremmie@1 30 Creates a path for a download. Uses the current date to avoid filename
gremmie@1 31 clashes. Uses the current microsecond also to make the directory name
gremmie@1 32 harder to guess.
gremmie@1 33 """
gremmie@1 34 now = datetime.datetime.now()
gremmie@1 35 parts = ['downloads']
bgneal@210 36 parts.extend([str(p) for p in (now.year, now.month, now.day)])
bgneal@210 37 parts.append(hex((now.hour * 3600 + now.minute * 60 + now.second) * 1000 + (
bgneal@210 38 now.microsecond / 1000))[2:])
gremmie@1 39 parts.append(filename)
gremmie@1 40 return os.path.join(*parts)
gremmie@1 41
gremmie@1 42
gremmie@1 43 class PublicDownloadManager(models.Manager):
gremmie@1 44 """The manager for all public downloads."""
gremmie@1 45 def get_query_set(self):
bgneal@190 46 return super(PublicDownloadManager, self).get_query_set().filter(
bgneal@190 47 is_public=True).select_related()
gremmie@1 48
gremmie@1 49
bgneal@204 50 class DownloadBase(models.Model):
bgneal@204 51 """Abstract model to collect common download fields."""
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)
bgneal@204 58 date_added = models.DateTimeField()
gremmie@1 59 ip_address = models.IPAddressField('IP Address')
bgneal@204 60
bgneal@204 61 class Meta:
bgneal@204 62 abstract = True
bgneal@204 63
bgneal@204 64 def size(self):
bgneal@204 65 return filesizeformat(self.file.size)
bgneal@204 66
bgneal@204 67
bgneal@204 68 class PendingDownload(DownloadBase):
bgneal@204 69 """This model represents pending downloads created by users. These pending
bgneal@204 70 downloads must be approved by an admin before they turn into "real"
bgneal@204 71 Downloads and are visible on site.
bgneal@204 72 """
bgneal@204 73 class Meta:
bgneal@204 74 ordering = ('date_added', )
bgneal@204 75
bgneal@204 76 def __unicode__(self):
bgneal@204 77 return self.title
bgneal@204 78
bgneal@204 79 def save(self, *args, **kwargs):
bgneal@204 80 if not self.pk:
bgneal@204 81 self.date_added = datetime.datetime.now()
bgneal@204 82 self.html = site_markup(self.description)
bgneal@204 83 super(PendingDownload, self).save(*args, **kwargs)
bgneal@204 84
bgneal@204 85
bgneal@204 86 class Download(DownloadBase):
bgneal@204 87 """Model to represent a download."""
gremmie@1 88 hits = models.IntegerField(default=0)
gremmie@1 89 average_score = models.FloatField(default=0.0)
gremmie@1 90 total_votes = models.IntegerField(default=0)
gremmie@1 91 is_public = models.BooleanField(default=False, db_index=True)
gremmie@1 92
gremmie@1 93 # Managers:
gremmie@1 94 objects = models.Manager()
gremmie@1 95 public_objects = PublicDownloadManager()
gremmie@1 96
gremmie@1 97 def __unicode__(self):
gremmie@1 98 return self.title
gremmie@1 99
bgneal@14 100 @models.permalink
bgneal@14 101 def get_absolute_url(self):
bgneal@23 102 return ('downloads-details', [str(self.id)])
bgneal@14 103
bgneal@182 104 def save(self, *args, **kwargs):
bgneal@128 105 self.html = site_markup(self.description)
bgneal@182 106 super(Download, self).save(*args, **kwargs)
gremmie@1 107
gremmie@1 108 def vote(self, vote_value):
gremmie@1 109 """receives a vote_value and updates internal score accordingly"""
gremmie@1 110 total_score = self.average_score * self.total_votes
gremmie@1 111 total_score += vote_value
gremmie@1 112 self.total_votes += 1
gremmie@1 113 self.average_score = total_score / self.total_votes
gremmie@1 114 return self.average_score
gremmie@1 115
gremmie@1 116
gremmie@1 117 class AllowedExtensionManager(models.Manager):
gremmie@1 118 def get_extension_list(self):
gremmie@1 119 return self.values_list('extension', flat=True)
gremmie@1 120
gremmie@1 121
gremmie@1 122 class AllowedExtension(models.Model):
gremmie@1 123 """Model to represent the list of allowed file extensions."""
bgneal@206 124 extension = models.CharField(max_length=8, help_text="e.g. .txt")
gremmie@1 125
gremmie@1 126 objects = AllowedExtensionManager()
gremmie@1 127
gremmie@1 128 def __unicode__(self):
gremmie@1 129 return self.extension
gremmie@1 130
gremmie@1 131 class Meta:
gremmie@1 132 ordering = ('extension', )
gremmie@1 133
gremmie@1 134
gremmie@1 135 class VoteRecord(models.Model):
gremmie@1 136 """Model to record the date that a user voted on a download."""
gremmie@1 137 download = models.ForeignKey(Download)
gremmie@1 138 user = models.ForeignKey(User)
gremmie@1 139 vote_date = models.DateTimeField(auto_now_add=True)
gremmie@1 140
gremmie@1 141 def __unicode__(self):
bgneal@192 142 return u"%s voted on '%s' on %s" % (
gremmie@1 143 self.user.username,
gremmie@1 144 self.download.title,
gremmie@1 145 self.vote_date.strftime('%b %d, %Y %H:%M:%S'))
gremmie@1 146
gremmie@1 147 class Meta:
gremmie@1 148 ordering = ('-vote_date', )