diff options
Diffstat (limited to 'ishtar_common/models.py')
| -rw-r--r-- | ishtar_common/models.py | 41 | 
1 files changed, 26 insertions, 15 deletions
| diff --git a/ishtar_common/models.py b/ishtar_common/models.py index 89b35d445..0c06f0b4d 100644 --- a/ishtar_common/models.py +++ b/ishtar_common/models.py @@ -765,10 +765,14 @@ class ItemKey(models.Model):          return self.key +def get_image_path(instance, filename): +    return instance._get_image_path(filename) + +  class ImageModel(models.Model): -    image = models.ImageField(upload_to="upload/", blank=True, null=True, +    image = models.ImageField(upload_to=get_image_path, blank=True, null=True,                                max_length=255) -    thumbnail = models.ImageField(upload_to='upload/thumbs/', blank=True, +    thumbnail = models.ImageField(upload_to=get_image_path, blank=True,                                    null=True, max_length=255)      IMAGE_MAX_SIZE = settings.IMAGE_MAX_SIZE      THUMB_MAX_SIZE = settings.THUMB_MAX_SIZE @@ -777,15 +781,11 @@ class ImageModel(models.Model):      class Meta:          abstract = True -    def __init__(self, *args, **kwargs): -        super(ImageModel, self).__init__(*args, **kwargs) -        image = self._meta.get_field("image") -        IMAGE_PREFIX = self.IMAGE_PREFIX -        if not IMAGE_PREFIX.endswith('/'): -            IMAGE_PREFIX += u'/' -        image.upload_to = IMAGE_PREFIX -        thumbnail = self._meta.get_field("thumbnail") -        thumbnail.upload_to = IMAGE_PREFIX + "thumbs/" +    def _get_image_path(self, filename): +        return u"{}/{}".format(self._get_base_image_path(), filename) + +    def _get_base_image_path(self): +        return u"upload"      def has_changed(self, field):          if not self.pk: @@ -832,8 +832,12 @@ class ImageModel(models.Model):                          pass                  # save the thumbnail +                splited = filename.split('.') +                thumb_filename = u"{}-thumb.{}".format( +                    u".".join(splited[:-1]), splited[-1] +                )                  self.thumbnail.save( -                    '_%s' % filename, +                    thumb_filename,                      self.create_thumb(image, self.THUMB_MAX_SIZE),                      save=False)              except IOError: @@ -1570,7 +1574,8 @@ class DocumentTemplate(models.Model):      name = models.CharField(_(u"Name"), max_length=100)      slug = models.SlugField(_(u"Slug"), blank=True, null=True, max_length=100,                              unique=True) -    template = models.FileField(_(u"Template"), upload_to="upload/templates/") +    template = models.FileField( +        _(u"Template"), upload_to="templates/%Y/")      associated_object_name = models.CharField(          _(u"Associated object"), max_length=100, choices=CLASSNAMES)      available = models.BooleanField(_(u"Available"), default=True) @@ -1621,7 +1626,7 @@ class DocumentTemplate(models.Model):          from old.ooo_replace import ooo_replace          from archaeological_operations.models import AdministrativeAct -        old_dir = settings.MEDIA_ROOT + "/upload/templates/v1/" +        old_dir = settings.MEDIA_ROOT + "/templates/v1/"          if not os.path.exists(old_dir):              os.makedirs(old_dir)          shutil.copy(settings.MEDIA_ROOT + self.template.name, old_dir) @@ -2372,7 +2377,6 @@ class Source(OwnPerms, ImageModel, models.Model):      duplicate = models.BooleanField(_(u"Has a duplicate"), default=False)      TABLE_COLS = ['title', 'source_type', 'authors', 'associated_url']      COL_LINK = ['associated_url'] -    IMAGE_PREFIX = 'sources'      class Meta:          abstract = True @@ -2380,6 +2384,13 @@ class Source(OwnPerms, ImageModel, models.Model):      def __unicode__(self):          return self.title +    def get_associated_operation(self): +        raise NotImplementedError() + +    def _get_base_image_path(self): +        base = self.owner._get_base_image_path() +        return u"{}/sources".format(base) +      @property      def associated_filename(self):          values = [unicode(getattr(self, attr)) | 
