~widelands-dev/widelands-website/trunk

147 by Holger Rapp
Added a first version of a screenshot application
1
from django.db import models
2
3
from django.template.defaultfilters import slugify
350 by Holger Rapp
Added support for PIL. Fixed some whitespace. Fixed tests for wlmaps.
4
from PIL import Image
404.2.1 by franku
first try on Django1.8
5
from PIL.Image import core as _imaging
147 by Holger Rapp
Added a first version of a screenshot application
6
from cStringIO import StringIO
7
from django.core.files.uploadedfile import SimpleUploadedFile, UploadedFile
152 by Holger Rapp
Fixed some bugs with wlscreen app
8
from django.core.files.storage import FileSystemStorage
247.1.1 by Holger Rapp
Model.save() should always take args and kwargs and pass them one. Whitespace fixes. Removed dependency on Site being configured on initial run of syncdb
9
import os
152 by Holger Rapp
Fixed some bugs with wlscreen app
10
from settings import THUMBNAIL_SIZE, MEDIA_ROOT
147 by Holger Rapp
Added a first version of a screenshot application
11
152 by Holger Rapp
Fixed some bugs with wlscreen app
12
# Taken from django snippet 976
13
class OverwriteStorage(FileSystemStorage):
14
    def get_available_name(self, name):
15
        """
16
        Returns a filename that's free on the target storage system, and
17
        available for new content to be written to.
18
        """
19
        # If the filename already exists, remove it as if it was a true file system
20
        if self.exists(name):
21
            os.remove(os.path.join(MEDIA_ROOT, name))
22
        return name
147 by Holger Rapp
Added a first version of a screenshot application
23
24
# Create your models here.
25
class Category(models.Model):
148 by Holger Rapp
Small fixes to screenshot application
26
    name = models.CharField(max_length=255)
27
    slug = models.SlugField(max_length=255, unique=True, blank = True)
247.1.1 by Holger Rapp
Model.save() should always take args and kwargs and pass them one. Whitespace fixes. Removed dependency on Site being configured on initial run of syncdb
28
147 by Holger Rapp
Added a first version of a screenshot application
29
    def save(self, *args, **kwargs):
30
        if not self.slug:
31
            self.slug = slugify(self.name)
247.1.1 by Holger Rapp
Model.save() should always take args and kwargs and pass them one. Whitespace fixes. Removed dependency on Site being configured on initial run of syncdb
32
147 by Holger Rapp
Added a first version of a screenshot application
33
        return super(Category,self).save(*args,**kwargs)
247.1.1 by Holger Rapp
Model.save() should always take args and kwargs and pass them one. Whitespace fixes. Removed dependency on Site being configured on initial run of syncdb
34
147 by Holger Rapp
Added a first version of a screenshot application
35
    @models.permalink
36
    def get_absolute_url(self):
37
        return ("wlscreens_category", None, { "category_slug": self.slug } )
38
39
    def __unicode__(self):
40
        return u"%s" % self.name
41
42
class Screenshot(models.Model):
148 by Holger Rapp
Small fixes to screenshot application
43
    name = models.CharField(max_length=255)
247.1.1 by Holger Rapp
Model.save() should always take args and kwargs and pass them one. Whitespace fixes. Removed dependency on Site being configured on initial run of syncdb
44
147 by Holger Rapp
Added a first version of a screenshot application
45
    screenshot = models.ImageField(
46
        upload_to= lambda i,n: "wlscreens/screens/%s/%s.%s" % (i.category,i.name,n.rsplit(".",1)[-1].lower()),
152 by Holger Rapp
Fixed some bugs with wlscreen app
47
        storage = OverwriteStorage(),
147 by Holger Rapp
Added a first version of a screenshot application
48
    )
49
    thumbnail = models.ImageField(
50
        upload_to= lambda i,n: "wlscreens/thumbs/%s/%s.png" % (i.category,i.name),
51
        editable = False,
152 by Holger Rapp
Fixed some bugs with wlscreen app
52
        storage = OverwriteStorage(),
147 by Holger Rapp
Added a first version of a screenshot application
53
    )
54
    comment = models.TextField( null = True, blank = True )
55
    category = models.ForeignKey( Category, related_name = "screenshots" )
247.1.1 by Holger Rapp
Model.save() should always take args and kwargs and pass them one. Whitespace fixes. Removed dependency on Site being configured on initial run of syncdb
56
147 by Holger Rapp
Added a first version of a screenshot application
57
    class Meta:
58
        unique_together = ("name","category")
247.1.1 by Holger Rapp
Model.save() should always take args and kwargs and pass them one. Whitespace fixes. Removed dependency on Site being configured on initial run of syncdb
59
147 by Holger Rapp
Added a first version of a screenshot application
60
    def save(self, *args, **kwargs):
152 by Holger Rapp
Fixed some bugs with wlscreen app
61
        # Open original screenshot which we want to thumbnail using PIL's Image
62
        # object
63
        image = Image.open(self.screenshot)
247.1.1 by Holger Rapp
Model.save() should always take args and kwargs and pass them one. Whitespace fixes. Removed dependency on Site being configured on initial run of syncdb
64
152 by Holger Rapp
Fixed some bugs with wlscreen app
65
        # Convert to RGB if necessary
66
        if image.mode not in ('L', 'RGB'):
67
            image = image.convert('RGB')
247.1.1 by Holger Rapp
Model.save() should always take args and kwargs and pass them one. Whitespace fixes. Removed dependency on Site being configured on initial run of syncdb
68
152 by Holger Rapp
Fixed some bugs with wlscreen app
69
        image.thumbnail(THUMBNAIL_SIZE, Image.ANTIALIAS)
247.1.1 by Holger Rapp
Model.save() should always take args and kwargs and pass them one. Whitespace fixes. Removed dependency on Site being configured on initial run of syncdb
70
152 by Holger Rapp
Fixed some bugs with wlscreen app
71
        # Save the thumbnail
72
        temp_handle = StringIO()
73
        image.save(temp_handle, 'png')
74
        temp_handle.seek(0)
247.1.1 by Holger Rapp
Model.save() should always take args and kwargs and pass them one. Whitespace fixes. Removed dependency on Site being configured on initial run of syncdb
75
152 by Holger Rapp
Fixed some bugs with wlscreen app
76
        # Save to the thumbnail field
77
        suf = SimpleUploadedFile(os.path.split(self.screenshot.name)[-1],
78
                temp_handle.read(), content_type='image/png')
79
        self.thumbnail.save(suf.name+'.png', suf, save=False)
247.1.1 by Holger Rapp
Model.save() should always take args and kwargs and pass them one. Whitespace fixes. Removed dependency on Site being configured on initial run of syncdb
80
152 by Holger Rapp
Fixed some bugs with wlscreen app
81
        # Save this photo instance
82
        super(Screenshot, self).save(*args,**kwargs)
147 by Holger Rapp
Added a first version of a screenshot application
83
84
    def __unicode__(self):
85
        return u"%s:%s" % (self.category.name,self.name)
86
247.1.1 by Holger Rapp
Model.save() should always take args and kwargs and pass them one. Whitespace fixes. Removed dependency on Site being configured on initial run of syncdb
87