~widelands-dev/widelands-website/django_staticfiles

« back to all changes in this revision

Viewing changes to wlscreens/models.py

  • Committer: franku
  • Date: 2016-04-18 13:29:23 UTC
  • mto: This revision was merged to the branch mainline in revision 409.
  • Revision ID: somal@arcor.de-20160418132923-bfzkb5mvdr7l8mz4
added migrations to each app

Show diffs side-by-side

added added

removed removed

Lines of Context:
8
8
from django.core.files.storage import FileSystemStorage
9
9
import os
10
10
from settings import THUMBNAIL_SIZE, MEDIA_ROOT
11
 
from django.urls import reverse
12
 
 
13
11
 
14
12
# Taken from django snippet 976
15
 
 
16
13
class OverwriteStorage(FileSystemStorage):
17
 
 
18
 
    def get_available_name(self, name, max_length=None):
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
 
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
23
20
        if self.exists(name):
24
21
            os.remove(os.path.join(MEDIA_ROOT, name))
25
22
        return name
26
23
 
27
 
 
 
24
# Create your models here.
28
25
class Category(models.Model):
29
26
    name = models.CharField(max_length=255)
30
 
    slug = models.SlugField(max_length=255, unique=True, blank=True)
 
27
    slug = models.SlugField(max_length=255, unique=True, blank = True)
31
28
 
32
29
    def save(self, *args, **kwargs):
33
30
        if not self.slug:
34
31
            self.slug = slugify(self.name)
35
32
 
36
 
        return super(Category, self).save(*args, **kwargs)
 
33
        return super(Category,self).save(*args,**kwargs)
37
34
 
 
35
    @models.permalink
38
36
    def get_absolute_url(self):
39
 
        return reverse('wlscreens_category', kwargs={'category_slug': self.slug})
 
37
        return ("wlscreens_category", None, { "category_slug": self.slug } )
40
38
 
41
39
    def __unicode__(self):
42
40
        return u"%s" % self.name
43
41
 
44
 
 
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
 
 
56
42
class Screenshot(models.Model):
57
43
    name = models.CharField(max_length=255)
58
44
 
59
45
    screenshot = models.ImageField(
60
 
        upload_to=screenshot_path,
61
 
        storage=OverwriteStorage(),
 
46
        upload_to= lambda i,n: "wlscreens/screens/%s/%s.%s" % (i.category,i.name,n.rsplit(".",1)[-1].lower()),
 
47
        storage = OverwriteStorage(),
62
48
    )
63
49
    thumbnail = models.ImageField(
64
 
        upload_to=thumbnail_path,
65
 
        editable=False,
66
 
        storage=OverwriteStorage(),
 
50
        upload_to= lambda i,n: "wlscreens/thumbs/%s/%s.png" % (i.category,i.name),
 
51
        editable = False,
 
52
        storage = OverwriteStorage(),
67
53
    )
68
 
    comment = models.TextField(null=True, blank=True)
69
 
    category = models.ForeignKey(Category, related_name='screenshots')
 
54
    comment = models.TextField( null = True, blank = True )
 
55
    category = models.ForeignKey( Category, related_name = "screenshots" )
70
56
 
71
57
    class Meta:
72
 
        unique_together = ('name', 'category')
 
58
        unique_together = ("name","category")
73
59
 
74
60
    def save(self, *args, **kwargs):
75
61
        # Open original screenshot which we want to thumbnail using PIL's Image
89
75
 
90
76
        # Save to the thumbnail field
91
77
        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)
 
78
                temp_handle.read(), content_type='image/png')
 
79
        self.thumbnail.save(suf.name+'.png', suf, save=False)
94
80
 
95
81
        # Save this photo instance
96
 
        super(Screenshot, self).save(*args, **kwargs)
 
82
        super(Screenshot, self).save(*args,**kwargs)
97
83
 
98
84
    def __unicode__(self):
99
 
        return u"%s:%s" % (self.category.name, self.name)
 
85
        return u"%s:%s" % (self.category.name,self.name)
 
86
 
 
87