summaryrefslogtreecommitdiff
path: root/ishtar_common/models.py
diff options
context:
space:
mode:
authorÉtienne Loks <etienne.loks@iggdrasil.net>2018-06-01 15:50:52 +0200
committerÉtienne Loks <etienne.loks@iggdrasil.net>2018-06-12 09:57:24 +0200
commit572b9047260322ed19cc9f674657b8be8d62c052 (patch)
tree0ed9392291c50980c7dbff14c9a05edc601076de /ishtar_common/models.py
parent66a376e081e335cf94ecf6fd41e5fe13a5445c57 (diff)
downloadIshtar-572b9047260322ed19cc9f674657b8be8d62c052.tar.bz2
Ishtar-572b9047260322ed19cc9f674657b8be8d62c052.zip
Adapt forms, wizards, views, urls for new management of documents (refs #4107)
Diffstat (limited to 'ishtar_common/models.py')
-rw-r--r--ishtar_common/models.py106
1 files changed, 58 insertions, 48 deletions
diff --git a/ishtar_common/models.py b/ishtar_common/models.py
index ef4ae63e2..995a2c136 100644
--- a/ishtar_common/models.py
+++ b/ishtar_common/models.py
@@ -51,7 +51,7 @@ from django.db.utils import DatabaseError
from django.template.defaultfilters import slugify
from django.utils.functional import lazy
from django.utils.safestring import SafeUnicode, mark_safe
-from django.utils.translation import ugettext_lazy as _
+from django.utils.translation import ugettext_lazy as _, ugettext
from secretary import Renderer as SecretaryRenderer
from simple_history.models import HistoricalRecords as BaseHistoricalRecords
from unidecode import unidecode
@@ -1244,7 +1244,14 @@ class FixAssociated(object):
setattr(item, subkey, new_value)
-class BaseHistorizedItem(FullSearch, Imported, JsonData, FixAssociated):
+class DocumentItem(object):
+ @property
+ def images(self):
+ return self.documents.filter(image__isnull=False).exclude(image="")
+
+
+class BaseHistorizedItem(FullSearch, Imported, JsonData, FixAssociated,
+ DocumentItem):
"""
Historized item with external ID management.
All historized items are searcheable and have a data json field
@@ -3079,7 +3086,18 @@ post_save.connect(post_save_cache, sender=LicenseType)
post_delete.connect(post_save_cache, sender=LicenseType)
-class BaseSource(OwnPerms, ImageModel, FullSearch):
+class Document(OwnPerms, ImageModel, FullSearch):
+ # order is important: put the image in the first match found
+ # other will be symbolic links
+ RELATED_MODELS = [
+ 'treatment_files', 'treatments', 'finds', 'context_records',
+ 'operations', 'sites', 'warehouses',
+ ]
+
+ LINK_SPLIT = u"<||>"
+
+
+
title = models.TextField(_(u"Title"), blank=True, default='')
index = models.IntegerField(verbose_name=_(u"Index"), blank=True,
null=True)
@@ -3125,21 +3143,51 @@ class BaseSource(OwnPerms, ImageModel, FullSearch):
BASE_SEARCH_VECTORS = ['title', 'source_type__label', 'external_id',
'reference', 'description', 'comment',
'additional_information']
- PARENT_SEARCH_VECTORS = ['authors']
+ PARENT_SEARCH_VECTORS = ['authors', ]
class Meta:
- abstract = True
+ verbose_name = _(u"Document")
+ verbose_name_plural = _(u"Documents")
+ ordering = ('title',)
+ permissions = (
+ ("view_document",
+ ugettext(u"Can view all Documents")),
+ ("view_own_document",
+ ugettext(u"Can view own Document")),
+ ("add_own_document",
+ ugettext(u"Can add own Document")),
+ ("change_own_document",
+ ugettext(u"Can change own Document")),
+ ("delete_own_document",
+ ugettext(u"Can delete own Document")),
+ )
def __unicode__(self):
return self.title
+ """
+ @property
+ def code(self):
+ if not self.index:
+ return u"{}-".format(self.operation.code_patriarche or '')
+ return u"{}-{:04d}".format(self.operation.code_patriarche or '',
+ self.index)
+ """
+
+ @classmethod
+ def get_query_owns(cls, ishtaruser):
+ Operation = cls.operations.rel.related_model
+ ArchaeologicalSite = cls.sites.rel.related_model
+ q = cls._construct_query_own(
+ 'operations__', Operation._get_query_owns_dicts(ishtaruser)
+ ) | cls._construct_query_own(
+ 'sites__', ArchaeologicalSite._get_query_owns_dicts(ishtaruser)
+ )
+ return q
+
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))
@@ -3147,25 +3195,6 @@ class BaseSource(OwnPerms, ImageModel, FullSearch):
if getattr(self, attr)]
return slugify(u"-".join(values))
-
-class Document(BaseSource):
- pass
-
-
-class IshtarImage(BaseSource):
- # order is important: put the image in the first match found
- # other will be symbolic links
- RELATED_MODELS = [
- 'treatmentimage_set', 'findimage_set', 'contextrecordimage_set',
- 'operationimage_set', 'siteimage_set', 'warehouseimage_set',
- ]
- LINK_SPLIT = u"<||>"
-
- class Meta:
- verbose_name = _(u"Image")
- verbose_name_plural = _(u"Images")
- ordering = ('title',)
-
def _get_base_image_paths(self):
for related_model in self.RELATED_MODELS:
q = getattr(self, related_model).all()
@@ -3269,8 +3298,7 @@ class IshtarImage(BaseSource):
def save(self, *args, **kwargs):
no_path_change = 'no_path_change' in kwargs \
and kwargs.pop('no_path_change')
-
- super(IshtarImage, self).save(*args, **kwargs)
+ super(Document, self).save(*args, **kwargs)
if self.image and not no_path_change and \
not getattr(self, '_no_path_change', False):
@@ -3281,24 +3309,6 @@ class IshtarImage(BaseSource):
self.save(no_path_change=True)
-class ThroughImage(models.Model):
- image = models.ForeignKey(
- IshtarImage, on_delete=models.CASCADE)
- is_main = models.BooleanField(_(u"Main image"), default=False)
-
- class Meta:
- ordering = ('-is_main', 'image__name')
- abstract = True
-
- def save(self, force_insert=False, force_update=False, using=None,
- update_fields=None):
- super(ThroughImage, self).save(
- force_insert=force_insert, force_update=force_update, using=using,
- update_fields=update_fields)
- # force relocation of image file and creation of symlinks
- self.image.save()
-
-
class Arrondissement(models.Model):
name = models.CharField(u"Nom", max_length=30)
department = models.ForeignKey(Department, verbose_name=u"Département")