gremmie@1: """ gremmie@1: Models for the downloads application. gremmie@1: """ gremmie@1: import os gremmie@1: gremmie@1: import datetime gremmie@1: from django.db import models gremmie@1: from django.contrib.auth.models import User gremmie@1: from django.template.defaultfilters import filesizeformat gremmie@1: bgneal@128: from core.markup import site_markup bgneal@124: gremmie@1: gremmie@1: class Category(models.Model): gremmie@1: """Downloads belong to categories.""" gremmie@1: title = models.CharField(max_length=64) gremmie@1: description = models.TextField(blank=True) gremmie@1: gremmie@1: class Meta: gremmie@1: verbose_name_plural = 'Categories' gremmie@1: ordering = ('title', ) gremmie@1: gremmie@1: def __unicode__(self): gremmie@1: return self.title gremmie@1: gremmie@1: def num_downloads(self): gremmie@1: return Download.public_objects.filter(category=self.pk).count() gremmie@1: gremmie@1: gremmie@1: def download_path(instance, filename): gremmie@1: """ gremmie@1: Creates a path for a download. Uses the current date to avoid filename gremmie@1: clashes. Uses the current microsecond also to make the directory name gremmie@1: harder to guess. gremmie@1: """ gremmie@1: now = datetime.datetime.now() gremmie@1: parts = ['downloads'] gremmie@1: parts.extend([str(p) for p in (now.year, now.month, now.day, now.microsecond)]) gremmie@1: parts.append(filename) gremmie@1: return os.path.join(*parts) gremmie@1: gremmie@1: gremmie@1: class PublicDownloadManager(models.Manager): gremmie@1: """The manager for all public downloads.""" gremmie@1: def get_query_set(self): gremmie@1: return super(PublicDownloadManager, self).get_query_set().filter(is_public=True) gremmie@1: gremmie@1: gremmie@1: class Download(models.Model): gremmie@1: """Model to represent a download.""" gremmie@1: title = models.CharField(max_length=128) gremmie@1: category = models.ForeignKey(Category) gremmie@1: description = models.TextField() gremmie@1: html = models.TextField(blank=True) gremmie@1: file = models.FileField(upload_to=download_path) gremmie@1: user = models.ForeignKey(User) gremmie@1: date_added = models.DateTimeField(auto_now_add=True) gremmie@1: ip_address = models.IPAddressField('IP Address') gremmie@1: hits = models.IntegerField(default=0) gremmie@1: average_score = models.FloatField(default=0.0) gremmie@1: total_votes = models.IntegerField(default=0) gremmie@1: is_public = models.BooleanField(default=False, db_index=True) gremmie@1: gremmie@1: # Managers: gremmie@1: objects = models.Manager() gremmie@1: public_objects = PublicDownloadManager() gremmie@1: gremmie@1: def __unicode__(self): gremmie@1: return self.title gremmie@1: bgneal@14: @models.permalink bgneal@14: def get_absolute_url(self): bgneal@23: return ('downloads-details', [str(self.id)]) bgneal@14: bgneal@182: def save(self, *args, **kwargs): bgneal@128: self.html = site_markup(self.description) bgneal@182: super(Download, self).save(*args, **kwargs) gremmie@1: gremmie@1: def vote(self, vote_value): gremmie@1: """receives a vote_value and updates internal score accordingly""" gremmie@1: total_score = self.average_score * self.total_votes gremmie@1: total_score += vote_value gremmie@1: self.total_votes += 1 gremmie@1: self.average_score = total_score / self.total_votes gremmie@1: return self.average_score gremmie@1: gremmie@1: def size(self): gremmie@1: return filesizeformat(self.file.size) gremmie@1: gremmie@1: gremmie@1: class AllowedExtensionManager(models.Manager): gremmie@1: def get_extension_list(self): gremmie@1: return self.values_list('extension', flat=True) gremmie@1: gremmie@1: gremmie@1: class AllowedExtension(models.Model): gremmie@1: """Model to represent the list of allowed file extensions.""" gremmie@1: extension = models.CharField(max_length=8) gremmie@1: gremmie@1: objects = AllowedExtensionManager() gremmie@1: gremmie@1: def __unicode__(self): gremmie@1: return self.extension gremmie@1: gremmie@1: class Meta: gremmie@1: ordering = ('extension', ) gremmie@1: gremmie@1: gremmie@1: class VoteRecord(models.Model): gremmie@1: """Model to record the date that a user voted on a download.""" gremmie@1: download = models.ForeignKey(Download) gremmie@1: user = models.ForeignKey(User) gremmie@1: vote_date = models.DateTimeField(auto_now_add=True) gremmie@1: gremmie@1: def __unicode__(self): gremmie@1: return "%s voted on '%s' on %s" % ( gremmie@1: self.user.username, gremmie@1: self.download.title, gremmie@1: self.vote_date.strftime('%b %d, %Y %H:%M:%S')) gremmie@1: gremmie@1: class Meta: gremmie@1: ordering = ('-vote_date', )