comparison downloads/models.py @ 581:ee87ea74d46b

For Django 1.4, rearranged project structure for new manage.py.
author Brian Neal <bgneal@gmail.com>
date Sat, 05 May 2012 17:10:48 -0500
parents gpp/downloads/models.py@639cfdf59167
children 15dbe0ccda95
comparison
equal deleted inserted replaced
580:c525f3e0b5d0 581:ee87ea74d46b
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.defaultfilters import filesizeformat
10
11 from core.markup import site_markup
12
13
14 class Category(models.Model):
15 """Downloads belong to categories."""
16 title = models.CharField(max_length=64)
17 slug = models.SlugField(max_length=64)
18 description = models.TextField(blank=True)
19 count = models.IntegerField(default=0, blank=True)
20
21 class Meta:
22 verbose_name_plural = 'Categories'
23 ordering = ('title', )
24
25 def __unicode__(self):
26 return self.title
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)])
38 parts.append(hex((now.hour * 3600 + now.minute * 60 + now.second) * 1000 + (
39 now.microsecond / 1000))[2:])
40 parts.append(filename)
41 return os.path.join(*parts)
42
43
44 class PublicDownloadManager(models.Manager):
45 """The manager for all public downloads."""
46 def get_query_set(self):
47 return super(PublicDownloadManager, self).get_query_set().filter(
48 is_public=True).select_related()
49
50
51 class DownloadBase(models.Model):
52 """Abstract model to collect common download fields."""
53 title = models.CharField(max_length=128)
54 category = models.ForeignKey(Category)
55 description = models.TextField()
56 html = models.TextField(blank=True)
57 file = models.FileField(upload_to=download_path)
58 user = models.ForeignKey(User)
59 date_added = models.DateTimeField(db_index=True)
60 ip_address = models.IPAddressField('IP Address')
61 update_date = models.DateTimeField(db_index=True, blank=True)
62
63 class Meta:
64 abstract = True
65
66 def size(self):
67 return filesizeformat(self.file.size)
68
69
70 class PendingDownload(DownloadBase):
71 """This model represents pending downloads created by users. These pending
72 downloads must be approved by an admin before they turn into "real"
73 Downloads and are visible on site.
74 """
75 class Meta:
76 ordering = ('date_added', )
77
78 def __unicode__(self):
79 return self.title
80
81 def save(self, *args, **kwargs):
82 if not self.pk:
83 self.date_added = datetime.datetime.now()
84 self.update_date = self.date_added
85 else:
86 self.update_date = datetime.datetime.now()
87
88 self.html = site_markup(self.description)
89 super(PendingDownload, self).save(*args, **kwargs)
90
91
92 class Download(DownloadBase):
93 """Model to represent a download."""
94 hits = models.IntegerField(default=0)
95 average_score = models.FloatField(default=0.0)
96 total_votes = models.IntegerField(default=0)
97 is_public = models.BooleanField(default=False, db_index=True)
98
99 # Managers:
100 objects = models.Manager()
101 public_objects = PublicDownloadManager()
102
103 def __unicode__(self):
104 return self.title
105
106 @models.permalink
107 def get_absolute_url(self):
108 return ('downloads-details', [str(self.id)])
109
110 def save(self, *args, **kwargs):
111 if not self.pk:
112 self.date_added = datetime.datetime.now()
113 self.update_date = self.date_added
114 else:
115 self.update_date = datetime.datetime.now()
116
117 self.html = site_markup(self.description)
118 super(Download, self).save(*args, **kwargs)
119
120 def vote(self, vote_value):
121 """receives a vote_value and updates internal score accordingly"""
122 total_score = self.average_score * self.total_votes
123 total_score += vote_value
124 self.total_votes += 1
125 self.average_score = total_score / self.total_votes
126 return self.average_score
127
128 def search_title(self):
129 return self.title
130
131 def search_summary(self):
132 return self.description
133
134
135 class AllowedExtensionManager(models.Manager):
136 def get_extension_list(self):
137 return self.values_list('extension', flat=True)
138
139
140 class AllowedExtension(models.Model):
141 """Model to represent the list of allowed file extensions."""
142 extension = models.CharField(max_length=8, help_text="e.g. .txt")
143
144 objects = AllowedExtensionManager()
145
146 def __unicode__(self):
147 return self.extension
148
149 class Meta:
150 ordering = ('extension', )
151
152
153 class VoteRecord(models.Model):
154 """Model to record the date that a user voted on a download."""
155 download = models.ForeignKey(Download)
156 user = models.ForeignKey(User)
157 vote_date = models.DateTimeField(auto_now_add=True)
158
159 def __unicode__(self):
160 return u"%s voted on '%s' on %s" % (
161 self.user.username,
162 self.download.title,
163 self.vote_date.strftime('%b %d, %Y %H:%M:%S'))
164
165 class Meta:
166 ordering = ('-vote_date', )