comparison gpp/downloads/models.py @ 1:dbd703f7d63a

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