models.py 5.51 KB
Newer Older
Christophe Siraut's avatar
Christophe Siraut committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
from django.db import models
from django.template.defaultfilters import slugify
from ckeditor.fields import RichTextField
from sorl.thumbnail.fields import ImageField

class Page(models.Model):
    title = models.CharField(max_length=80)
    text = RichTextField(max_length=4000,blank=True,null=True)
    parent = models.ForeignKey('self',blank=True,null=True)
    in_menu = models.BooleanField()
    weight = models.IntegerField(default=0,blank=True,null=True)
    date = models.DateField(auto_now=True)
    bgimage = models.ImageField(upload_to="images",blank=True)
    slug = models.CharField(max_length=80, editable=False)
    lnk = models.CharField(max_length=100, editable=False)

    def __unicode__(self):
        return self.title

    class Meta:
        ordering = ['weight']

    def save(self):
        #if not self.id:
        self.slug = slugify(self.title)
        #self.lnk = "/" + self.folder.slug + "/" + self.slug
        super(Page, self).save()

class Image(models.Model):
    page = models.ForeignKey(Page)
    #image = models.ImageField(upload_to="images")
    image = ImageField(upload_to="images")
    title = models.CharField(max_length=80,blank=True,null=True)
    text = RichTextField(max_length=800,blank=True,null=True)

    def url(self):
        return '/media/%s' % self.image

    def __unicode__(self):
        return self.title

    def save(self):
        if not self.title:
            self.title = slugify(str(self.image))
        super(Image, self).save()

class Video(models.Model):
    page = models.ForeignKey(Page)
    title = models.CharField(max_length=80)
    text = RichTextField(max_length=800,blank=True,null=True)

    def __unicode__(self):
        return self.title

class Agenda(models.Model):
    page = models.ForeignKey(Page, unique=True)
    text = RichTextField(max_length=2000,blank=True,null=True)

    def __unicode__(self):
        return "Agenda " + str(self.page)

class Press(models.Model):
    page = models.ForeignKey(Page)
    image = ImageField(upload_to="images",blank=True,null=True)
    text = RichTextField(max_length=2000,blank=True,null=True)

    def __unicode__(self):
        return "Press review" + str(self.page)

    class Meta:
        verbose_name = 'Press review'

class Reference(models.Model):
    page = models.ForeignKey(Page)
    text = RichTextField(max_length=2000,blank=True,null=True)

    def __unicode__(self):
        return "Reference " + str(self.page)

class File(models.Model):
    page = models.ForeignKey(Page)
    file = models.FileField(upload_to="files",blank=True,null=True)
    text = RichTextField(max_length=200,blank=True,null=True)

    def __unicode__(self):
        return self.title

    def save(self):
        if not self.title:
            self.title = slugify(str(self.file))
        super(File, self).save()


class ImagesUpload(models.Model):
    page = models.ForeignKey(Page)
    zip_file = models.FileField(upload_to="zip", help_text='Select a .zip file of images to upload')

    class Meta:
        verbose_name = 'imagepack'

    #def __unicode__(self):
    #    return str(self.zip_file)

    def save(self):
        super(ImagesUpload, self).save()
        page = self.process_zipfile()
        super(ImagesUpload, self).delete()
        return page

    def process_zipfile(self):
        import os
        import zipfile
        from cStringIO import StringIO
        from PIL import Image as PILImage
        from django.core.files.base import ContentFile
        if os.path.isfile(self.zip_file.path):
            # TODO: implement try-except here
            zip = zipfile.ZipFile(self.zip_file.path)
            bad_file = zip.testzip()
            if bad_file:
                raise Exception('"%s" in the .zip archive is corrupt.' % bad_file)
            count = 1
            page = self.page

            #f = open('/tmp/workfile', 'wa')
            #f.write(filename)
            #f.write(slug)

            for filename in sorted(zip.namelist()):
                if filename.startswith('__'): # do not process meta files
                    continue
                data = zip.read(filename)
                if len(data):
                    try:
                        # the following is taken from django.newforms.fields.ImageField:
                        #  load() is the only method that can spot a truncated JPEG,
                        #  but it cannot be called sanely after verify()
                        trial_image = PILImage.open(StringIO(data))
                        trial_image.load()
                        # verify() is the only method that can spot a corrupt PNG,
                        #  but it must be called immediately after the constructor
                        trial_image = PILImage.open(StringIO(data))
                        trial_image.verify()
                    except Exception:
                        # if a "bad" file is found we just skip it.
                        continue
                    #while 1:
                    #title = ' '.join([self.title, str(count)])
                    slug = slugify(filename)
                    try:
                        print slug
                        p = Image.objects.get(title=slug)
                    except:
                        image = Image(title=slug,page=self.page)
                        image.image.save(filename, ContentFile(data))
                        #image.save() # needed?
                        #page.photos.add(photo)
                        #count = count + 1
                        #break
                        count = count + 1
            zip.close()
            return page