~widelands-dev/widelands-website/trunk

« back to all changes in this revision

Viewing changes to wlscreens/models.py

  • Committer: Timo Wingender
  • Date: 2010-09-28 21:26:23 UTC
  • mto: This revision was merged to the branch mainline in revision 218.
  • Revision ID: timo.wingender@gmx.de-20100928212623-awo0wh3hcaptc0oj
add field to choose between markdown and bbcode as markup

Show diffs side-by-side

added added

removed removed

Lines of Context:
5
5
from cStringIO import StringIO
6
6
from django.core.files.uploadedfile import SimpleUploadedFile, UploadedFile
7
7
from django.core.files.storage import FileSystemStorage
8
 
import os
 
8
import os  
9
9
from settings import THUMBNAIL_SIZE, MEDIA_ROOT
10
10
 
11
11
# Taken from django snippet 976
24
24
class Category(models.Model):
25
25
    name = models.CharField(max_length=255)
26
26
    slug = models.SlugField(max_length=255, unique=True, blank = True)
27
 
 
 
27
   
28
28
    def save(self, *args, **kwargs):
29
29
        if not self.slug:
30
30
            self.slug = slugify(self.name)
31
 
 
 
31
        
32
32
        return super(Category,self).save(*args,**kwargs)
33
 
 
 
33
    
34
34
    @models.permalink
35
35
    def get_absolute_url(self):
36
36
        return ("wlscreens_category", None, { "category_slug": self.slug } )
40
40
 
41
41
class Screenshot(models.Model):
42
42
    name = models.CharField(max_length=255)
43
 
 
 
43
    
44
44
    screenshot = models.ImageField(
45
45
        upload_to= lambda i,n: "wlscreens/screens/%s/%s.%s" % (i.category,i.name,n.rsplit(".",1)[-1].lower()),
46
46
        storage = OverwriteStorage(),
52
52
    )
53
53
    comment = models.TextField( null = True, blank = True )
54
54
    category = models.ForeignKey( Category, related_name = "screenshots" )
55
 
 
 
55
    
56
56
    class Meta:
57
57
        unique_together = ("name","category")
58
 
 
 
58
   
59
59
    def save(self, *args, **kwargs):
60
60
        # Open original screenshot which we want to thumbnail using PIL's Image
61
61
        # object
62
62
        image = Image.open(self.screenshot)
63
 
 
 
63
        
64
64
        # Convert to RGB if necessary
65
65
        if image.mode not in ('L', 'RGB'):
66
66
            image = image.convert('RGB')
67
 
 
 
67
        
68
68
        image.thumbnail(THUMBNAIL_SIZE, Image.ANTIALIAS)
69
 
 
 
69
        
70
70
        # Save the thumbnail
71
71
        temp_handle = StringIO()
72
72
        image.save(temp_handle, 'png')
73
73
        temp_handle.seek(0)
74
 
 
 
74
        
75
75
        # Save to the thumbnail field
76
76
        suf = SimpleUploadedFile(os.path.split(self.screenshot.name)[-1],
77
77
                temp_handle.read(), content_type='image/png')
78
78
        self.thumbnail.save(suf.name+'.png', suf, save=False)
79
 
 
 
79
  
80
80
        # Save this photo instance
81
81
        super(Screenshot, self).save(*args,**kwargs)
82
82
 
83
83
    def __unicode__(self):
84
84
        return u"%s:%s" % (self.category.name,self.name)
85
85
 
86
 
 
 
86