summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--archaeological_context_records/models.py10
-rw-r--r--archaeological_finds/models_finds.py12
-rw-r--r--archaeological_finds/models_treatments.py13
-rw-r--r--archaeological_operations/models.py5
-rw-r--r--ishtar_common/models.py41
-rw-r--r--ishtar_common/models_imports.py10
6 files changed, 67 insertions, 24 deletions
diff --git a/archaeological_context_records/models.py b/archaeological_context_records/models.py
index d71864302..70858ea31 100644
--- a/archaeological_context_records/models.py
+++ b/archaeological_context_records/models.py
@@ -26,6 +26,7 @@ from django.db import connection, transaction
from django.db.models import Q
from django.db.models.signals import post_delete, post_save
from django.utils.translation import ugettext_lazy as _, ugettext, pgettext
+from django.utils.text import slugify
from ishtar_common.utils import cached_label_changed
@@ -215,7 +216,6 @@ class ContextRecord(BulkUpdatedItem, BaseHistorizedItem, ImageModel, OwnPerms,
'related_context_records': 'detailled_related_context_records'
}
}
- IMAGE_PREFIX = 'context_records/'
# search parameters
EXTRA_REQUEST_KEYS = {
@@ -447,6 +447,13 @@ class ContextRecord(BulkUpdatedItem, BaseHistorizedItem, ImageModel, OwnPerms,
context_record_id=self.pk)
return True
+ def _get_base_image_path(self):
+ ope = self.operation
+ return u"operation/{}/{}/{}/{}".format(
+ ope.year, ope.reference, self.SLUG,
+ slugify(self.label or u"00")
+ )
+
@property
def reference(self):
if not self.operation:
@@ -691,3 +698,4 @@ class ContextRecordSource(Source):
Q(context_record__operation__collaborators__pk=
user.ishtaruser.person.pk)) \
& Q(context_record__operation__end_date__isnull=True)
+
diff --git a/archaeological_finds/models_finds.py b/archaeological_finds/models_finds.py
index 12f78e64d..71b57849e 100644
--- a/archaeological_finds/models_finds.py
+++ b/archaeological_finds/models_finds.py
@@ -25,6 +25,7 @@ from django.core.urlresolvers import reverse
from django.db import connection, transaction
from django.db.models import Max, Q
from django.db.models.signals import m2m_changed, post_save, post_delete
+from django.utils.text import slugify
from django.utils.translation import ugettext_lazy as _, ugettext
from ishtar_common.data_importer import post_importer_action, ImporterError
@@ -707,7 +708,6 @@ class Find(BulkUpdatedItem, ValueGetter, BaseHistorizedItem, ImageModel,
cached_label = models.TextField(_(u"Cached name"), null=True, blank=True)
history = HistoricalRecords()
BASKET_MODEL = FindBasket
- IMAGE_PREFIX = 'finds/'
class Meta:
verbose_name = _(u"Find")
@@ -777,6 +777,16 @@ class Find(BulkUpdatedItem, ValueGetter, BaseHistorizedItem, ImageModel,
return "00"
return bf.short_id()
+ def _get_base_image_path(self):
+ bf = None
+ if self.id:
+ bf = self.get_first_base_find()
+ if not bf:
+ return u"detached/{}".format(self.SLUG)
+ ope = bf.context_record.operation
+ return u"operation/{}/{}/{}/{}".format(
+ ope.year, ope.reference, self.SLUG, slugify(self.reference))
+
@property
def administrative_index(self):
bf = self.get_first_base_find()
diff --git a/archaeological_finds/models_treatments.py b/archaeological_finds/models_treatments.py
index 048d3064d..866e218aa 100644
--- a/archaeological_finds/models_treatments.py
+++ b/archaeological_finds/models_treatments.py
@@ -92,7 +92,6 @@ class Treatment(DashboardFormItem, ValueGetter, BaseHistorizedItem,
"treatment_types__label": _(u"Type"),
"treatment_state__label": _(u"State"),
}
- IMAGE_PREFIX = 'treatment'
# extra keys than can be passed to save method
EXTRA_SAVED_KEYS = ('items', 'user')
SLUG = 'treatment'
@@ -202,6 +201,10 @@ class Treatment(DashboardFormItem, ValueGetter, BaseHistorizedItem,
getattr(self, k)]
return u'{} | {}'.format(u"-".join(items), self.treatment_types_lbl())
+ def _get_base_image_path(self,):
+ return u"treatment/{}/{}".format(
+ self.year, self.index)
+
def treatment_types_lbl(self):
"""
Treatment types label
@@ -631,6 +634,10 @@ class TreatmentSource(Source):
def owner(self):
return self.treatment
+ def _get_base_image_path(self):
+ return u"treatment/{}/{}/source".format(
+ self.treatment.year, self.treatment.index)
+
class TreatmentFileSource(Source):
treatment_file = models.ForeignKey(
@@ -660,3 +667,7 @@ class TreatmentFileSource(Source):
@property
def owner(self):
return self.treatment_file
+
+ def _get_base_image_path(self):
+ return u"treatmentfile/{}/{}/source".format(
+ self.treatment_file.year, self.treatment_file.index)
diff --git a/archaeological_operations/models.py b/archaeological_operations/models.py
index 5a90ccac5..1f4108c28 100644
--- a/archaeological_operations/models.py
+++ b/archaeological_operations/models.py
@@ -179,7 +179,6 @@ class Operation(ClosedItem, BaseHistorizedItem, ImageModel, OwnPerms,
SHOW_URL = 'show-operation'
TABLE_COLS = ['year', 'towns', 'common_name', 'operation_type',
'start_date', 'excavation_end_date', 'remains']
- IMAGE_PREFIX = 'operations/'
SLUG = 'operation'
# search parameters
@@ -479,6 +478,10 @@ class Operation(ClosedItem, BaseHistorizedItem, ImageModel, OwnPerms,
self.context_record.model.cached_label_bulk_update(operation_id=self.pk)
return True
+ def _get_base_image_path(self):
+ return u"operation/{}/{}".format(
+ self.year, self.reference)
+
def get_town_label(self):
lbl = unicode(_('Intercommunal'))
if self.towns.count() == 1:
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))
diff --git a/ishtar_common/models_imports.py b/ishtar_common/models_imports.py
index cfc8857de..d638c76f1 100644
--- a/ishtar_common/models_imports.py
+++ b/ishtar_common/models_imports.py
@@ -724,21 +724,21 @@ class Import(models.Model):
blank=True, null=True)
importer_type = models.ForeignKey(ImporterType)
imported_file = models.FileField(
- _(u"Imported file"), upload_to="upload/imports/", max_length=220)
+ _(u"Imported file"), upload_to="upload/imports/%Y/%m/", max_length=220)
imported_images = models.FileField(
- _(u"Associated images (zip file)"), upload_to="upload/imports/",
+ _(u"Associated images (zip file)"), upload_to="upload/imports/%Y/%m/",
blank=True, null=True, max_length=220)
encoding = models.CharField(_(u"Encoding"), choices=ENCODINGS,
default=u'utf-8', max_length=15)
skip_lines = models.IntegerField(_(u"Skip lines"), default=1)
error_file = models.FileField(_(u"Error file"),
- upload_to="upload/imports/",
+ upload_to="upload/imports/%Y/%m/",
blank=True, null=True, max_length=255)
result_file = models.FileField(_(u"Result file"),
- upload_to="upload/imports/",
+ upload_to="upload/imports/%Y/%m/",
blank=True, null=True, max_length=255)
match_file = models.FileField(_(u"Match file"),
- upload_to="upload/imports/",
+ upload_to="upload/imports/%Y/%m/",
blank=True, null=True, max_length=255)
state = models.CharField(_(u"State"), max_length=2, choices=IMPORT_STATE,
default=u'C')