29
29
name = models.CharField(max_length=255)
30
30
slug = models.SlugField(max_length=255, unique=True, blank=True)
32
35
def save(self, *args, **kwargs):
34
37
self.slug = slugify(self.name)
36
39
return super(Category, self).save(*args, **kwargs)
38
def get_absolute_url(self):
39
return reverse('wlscreens_category', kwargs={'category_slug': self.slug})
41
41
def __unicode__(self):
42
42
return u"%s" % self.name
66
66
storage=OverwriteStorage(),
68
comment = models.TextField(null=True, blank=True)
69
category = models.ForeignKey(Category, related_name='screenshots')
68
comment = models.TextField(
72
category = models.ForeignKey(
74
related_name='screenshots'
76
position = models.IntegerField(
80
help_text='The position inside the category',
72
84
unique_together = ('name', 'category')
85
ordering = ['-category__name', 'position']
74
87
def save(self, *args, **kwargs):
75
88
# Open original screenshot which we want to thumbnail using PIL's Image
77
image = Image.open(self.screenshot)
79
# Convert to RGB if necessary
80
if image.mode not in ('L', 'RGB'):
81
image = image.convert('RGB')
83
image.thumbnail(settings.THUMBNAIL_SIZE, Image.ANTIALIAS)
86
temp_handle = StringIO()
87
image.save(temp_handle, 'png')
90
# Save to the thumbnail field
91
suf = SimpleUploadedFile(os.path.split(self.screenshot.name)[-1],
92
temp_handle.read(), content_type='image/png')
93
self.thumbnail.save(suf.name + '.png', suf, save=False)
95
# Save this photo instance
96
super(Screenshot, self).save(*args, **kwargs)
91
image = Image.open(self.screenshot)
93
# Convert to RGB if necessary
94
if image.mode not in ('L', 'RGB'):
95
image = image.convert('RGB')
97
image.thumbnail(settings.THUMBNAIL_SIZE, Image.ANTIALIAS)
100
temp_handle = StringIO()
101
image.save(temp_handle, 'png')
104
# Save to the thumbnail field
105
suf = SimpleUploadedFile(os.path.split(self.screenshot.name)[-1],
106
temp_handle.read(), content_type='image/png')
107
self.thumbnail.save(suf.name + '.png', suf, save=False)
109
# Save this photo instance
110
super(Screenshot, self).save(*args, **kwargs)
112
# Likely we have a screenshot in the database which didn't exist
113
# on the filesystem at the given path. Ignore it.
98
117
def __unicode__(self):
99
118
return u"%s:%s" % (self.category.name, self.name)