~widelands-dev/widelands-website/trunk

« back to all changes in this revision

Viewing changes to wlscreens/models.py

  • Committer: franku
  • Date: 2019-04-10 16:18:40 UTC
  • mfrom: (531.1.5 screens_ordering)
  • Revision ID: somal@arcor.de-20190410161840-29hlbwn3qsbctaa4
allow ordering of screenshots by adding a new column to the tables screenshots; reworked the corresponding admin pages

Show diffs side-by-side

added added

removed removed

Lines of Context:
29
29
    name = models.CharField(max_length=255)
30
30
    slug = models.SlugField(max_length=255, unique=True, blank=True)
31
31
 
 
32
    class Meta:
 
33
        ordering = ['-name']
 
34
 
32
35
    def save(self, *args, **kwargs):
33
36
        if not self.slug:
34
37
            self.slug = slugify(self.name)
35
38
 
36
39
        return super(Category, self).save(*args, **kwargs)
37
40
 
38
 
    def get_absolute_url(self):
39
 
        return reverse('wlscreens_category', kwargs={'category_slug': self.slug})
40
 
 
41
41
    def __unicode__(self):
42
42
        return u"%s" % self.name
43
43
 
65
65
        editable=False,
66
66
        storage=OverwriteStorage(),
67
67
    )
68
 
    comment = models.TextField(null=True, blank=True)
69
 
    category = models.ForeignKey(Category, related_name='screenshots')
 
68
    comment = models.TextField(
 
69
        null=True,
 
70
        blank=True
 
71
    )
 
72
    category = models.ForeignKey(
 
73
        Category,
 
74
        related_name='screenshots'
 
75
    )
 
76
    position = models.IntegerField(
 
77
        null=True,
 
78
        blank=True,
 
79
        default=0,
 
80
        help_text='The position inside the category',
 
81
    )
70
82
 
71
83
    class Meta:
72
84
        unique_together = ('name', 'category')
 
85
        ordering = ['-category__name', 'position']
73
86
 
74
87
    def save(self, *args, **kwargs):
75
88
        # Open original screenshot which we want to thumbnail using PIL's Image
76
89
        # object
77
 
        image = Image.open(self.screenshot)
78
 
 
79
 
        # Convert to RGB if necessary
80
 
        if image.mode not in ('L', 'RGB'):
81
 
            image = image.convert('RGB')
82
 
 
83
 
        image.thumbnail(settings.THUMBNAIL_SIZE, Image.ANTIALIAS)
84
 
 
85
 
        # Save the thumbnail
86
 
        temp_handle = StringIO()
87
 
        image.save(temp_handle, 'png')
88
 
        temp_handle.seek(0)
89
 
 
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)
94
 
 
95
 
        # Save this photo instance
96
 
        super(Screenshot, self).save(*args, **kwargs)
 
90
        try:
 
91
            image = Image.open(self.screenshot)
 
92
    
 
93
            # Convert to RGB if necessary
 
94
            if image.mode not in ('L', 'RGB'):
 
95
                image = image.convert('RGB')
 
96
    
 
97
            image.thumbnail(settings.THUMBNAIL_SIZE, Image.ANTIALIAS)
 
98
    
 
99
            # Save the thumbnail
 
100
            temp_handle = StringIO()
 
101
            image.save(temp_handle, 'png')
 
102
            temp_handle.seek(0)
 
103
    
 
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)
 
108
    
 
109
            # Save this photo instance
 
110
            super(Screenshot, self).save(*args, **kwargs)
 
111
        except IOError:
 
112
            # Likely we have a screenshot in the database which didn't exist
 
113
            # on the filesystem at the given path. Ignore it.            
 
114
            pass
 
115
 
97
116
 
98
117
    def __unicode__(self):
99
118
        return u"%s:%s" % (self.category.name, self.name)