~widelands-dev/widelands-website/django_staticfiles

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
489.1.16 by franku
run through django1.111 deprecations
11
from django.urls import reverse
12
147 by Holger Rapp
Added a first version of a screenshot application
13
152 by Holger Rapp
Fixed some bugs with wlscreen app
14
# Taken from django snippet 976
438.1.6 by franku
run the script
15
152 by Holger Rapp
Fixed some bugs with wlscreen app
16
class OverwriteStorage(FileSystemStorage):
438.1.6 by franku
run the script
17
489.1.4 by franku
replace unmaintained pagination app; fixes for context_instance, NoArgsCommand and resolve_variable()
18
    def get_available_name(self, name, max_length=None):
438.1.6 by franku
run the script
19
        """Returns a filename that's free on the target storage system, and
20
        available for new content to be written to."""
21
        # If the filename already exists, remove it as if it was a true file
22
        # system
152 by Holger Rapp
Fixed some bugs with wlscreen app
23
        if self.exists(name):
24
            os.remove(os.path.join(MEDIA_ROOT, name))
25
        return name
147 by Holger Rapp
Added a first version of a screenshot application
26
438.1.6 by franku
run the script
27
147 by Holger Rapp
Added a first version of a screenshot application
28
class Category(models.Model):
148 by Holger Rapp
Small fixes to screenshot application
29
    name = models.CharField(max_length=255)
438.1.6 by franku
run the script
30
    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
31
147 by Holger Rapp
Added a first version of a screenshot application
32
    def save(self, *args, **kwargs):
33
        if not self.slug:
34
            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
35
438.1.6 by franku
run the script
36
        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
37
147 by Holger Rapp
Added a first version of a screenshot application
38
    def get_absolute_url(self):
489.1.16 by franku
run through django1.111 deprecations
39
        return reverse('wlscreens_category', kwargs={'category_slug': self.slug})
147 by Holger Rapp
Added a first version of a screenshot application
40
41
    def __unicode__(self):
42
        return u"%s" % self.name
43
438.1.6 by franku
run the script
44
489.1.5 by franku
added migration for wlscreens; fixed view
45
def screenshot_path(instance, filename):
46
    return 'wlscreens/screens/%s/%s.%s' % (
47
            instance.category, instance.name, filename.rsplit('.', 1)[-1].lower()
48
            )
49
50
51
def thumbnail_path(instance, filename):
52
    return 'wlscreens/thumbs/%s/%s.png' % (
53
            instance.category, instance.name)
54
55
147 by Holger Rapp
Added a first version of a screenshot application
56
class Screenshot(models.Model):
148 by Holger Rapp
Small fixes to screenshot application
57
    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
58
147 by Holger Rapp
Added a first version of a screenshot application
59
    screenshot = models.ImageField(
489.1.5 by franku
added migration for wlscreens; fixed view
60
        upload_to=screenshot_path,
438.1.6 by franku
run the script
61
        storage=OverwriteStorage(),
147 by Holger Rapp
Added a first version of a screenshot application
62
    )
63
    thumbnail = models.ImageField(
489.1.5 by franku
added migration for wlscreens; fixed view
64
        upload_to=thumbnail_path,
438.1.6 by franku
run the script
65
        editable=False,
66
        storage=OverwriteStorage(),
147 by Holger Rapp
Added a first version of a screenshot application
67
    )
438.1.6 by franku
run the script
68
    comment = models.TextField(null=True, blank=True)
69
    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
70
147 by Holger Rapp
Added a first version of a screenshot application
71
    class Meta:
438.1.6 by franku
run the script
72
        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
73
147 by Holger Rapp
Added a first version of a screenshot application
74
    def save(self, *args, **kwargs):
152 by Holger Rapp
Fixed some bugs with wlscreen app
75
        # Open original screenshot which we want to thumbnail using PIL's Image
76
        # object
77
        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
78
152 by Holger Rapp
Fixed some bugs with wlscreen app
79
        # Convert to RGB if necessary
80
        if image.mode not in ('L', 'RGB'):
81
            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
82
152 by Holger Rapp
Fixed some bugs with wlscreen app
83
        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
84
152 by Holger Rapp
Fixed some bugs with wlscreen app
85
        # Save the thumbnail
86
        temp_handle = StringIO()
87
        image.save(temp_handle, 'png')
88
        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
89
152 by Holger Rapp
Fixed some bugs with wlscreen app
90
        # Save to the thumbnail field
91
        suf = SimpleUploadedFile(os.path.split(self.screenshot.name)[-1],
438.1.6 by franku
run the script
92
                                 temp_handle.read(), content_type='image/png')
93
        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
94
152 by Holger Rapp
Fixed some bugs with wlscreen app
95
        # Save this photo instance
438.1.6 by franku
run the script
96
        super(Screenshot, self).save(*args, **kwargs)
147 by Holger Rapp
Added a first version of a screenshot application
97
98
    def __unicode__(self):
438.1.6 by franku
run the script
99
        return u"%s:%s" % (self.category.name, self.name)