summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorÉtienne Loks <etienne.loks@iggdrasil.net>2019-07-12 17:47:56 +0200
committerÉtienne Loks <etienne.loks@iggdrasil.net>2019-07-12 17:47:56 +0200
commit7185d81691ede9520cecdd2a2e8dcac372aaac6c (patch)
treeed184fa6e4ea7b90928beb85f7b33588e9a33d40
parent3b3f4bb167226810a4d323488071bba22ae27621 (diff)
downloadIshtar-7185d81691ede9520cecdd2a2e8dcac372aaac6c.tar.bz2
Ishtar-7185d81691ede9520cecdd2a2e8dcac372aaac6c.zip
Base public serializer
-rw-r--r--archaeological_context_records/models.py14
-rw-r--r--archaeological_finds/models_finds.py42
-rw-r--r--archaeological_operations/models.py33
-rw-r--r--example_project/settings.py2
-rwxr-xr-xinstall/ishtar-install2
-rw-r--r--ishtar_common/models.py49
-rw-r--r--ishtar_common/serializers.py6
-rw-r--r--requirements.txt2
8 files changed, 148 insertions, 2 deletions
diff --git a/archaeological_context_records/models.py b/archaeological_context_records/models.py
index 3052844b4..2d60ba4e2 100644
--- a/archaeological_context_records/models.py
+++ b/archaeological_context_records/models.py
@@ -552,6 +552,20 @@ class ContextRecord(BulkUpdatedItem, DocumentItem, BaseHistorizedItem,
def __str__(self):
return self.short_label or ""
+ def public_representation(self):
+ dct = super(ContextRecord, self).public_representation()
+ dct.update({
+ "operation": self.operation.public_representation(),
+ "site": self.archaeological_site and
+ self.archaeological_site.public_representation(),
+ "parcel": str(self.parcel),
+ "town": self.town.label_with_areas(),
+ "label": self.label,
+ "description": self.description,
+ "comment": self.comment
+ })
+ return dct
+
def get_values(self, prefix='', no_values=False, no_base_finds=True):
values = super(ContextRecord, self).get_values(prefix=prefix,
no_values=no_values)
diff --git a/archaeological_finds/models_finds.py b/archaeological_finds/models_finds.py
index d56789d3a..8a942e306 100644
--- a/archaeological_finds/models_finds.py
+++ b/archaeological_finds/models_finds.py
@@ -334,6 +334,19 @@ class BaseFind(BulkUpdatedItem, BaseHistorizedItem, GeoItem, OwnPerms,
def natural_key(self):
return (self.external_id, )
+ def public_representation(self):
+ dct = super(BaseFind, self).public_representation()
+ dct.update({
+ "context-record": self.context_record.public_representation(),
+ "description": self.description,
+ "comment": self.comment,
+ "discovery-date": self.discovery_date and
+ self.discovery_date.strftime("%Y/%m/%d"),
+ "discovery-date-taq": self.discovery_date_taq,
+ "discovery-date-tpq": self.discovery_date_tpq,
+ })
+ return dct
+
def get_values(self, prefix='', no_values=False, no_find=False):
values = super(BaseFind, self).get_values(prefix=prefix,
no_values=no_values)
@@ -1638,6 +1651,35 @@ class Find(BulkUpdatedItem, ValueGetter, DocumentItem, BaseHistorizedItem,
def show_url(self):
return reverse('show-find', args=[self.pk, ''])
+ def public_representation(self):
+ dct = super(Find, self).public_representation()
+ dct.update({
+ "denomination": self.denomination,
+ "free-id": self.label,
+ "description": self.description,
+ "materials": [str(mt) for mt in self.material_types.all()],
+ "material-comment": self.material_comment,
+ "object-types": [str(ot) for ot in self.object_types.all()],
+ "find-number": self.find_number,
+ "decoration": self.decoration,
+ "inscription": self.inscription,
+ "manufacturing-place":self.manufacturing_place,
+ "comment": self.comment,
+ "length": self.length,
+ "width": self.width,
+ "height": self.height,
+ "thickness": self.thickness,
+ "diameter": self.diameter,
+ "circumference": self.circumference,
+ "volume": self.volume,
+ "weight": self.weight,
+ "datings": [str(dating) for dating in self.datings.all()],
+ "base-finds": [bf.public_representation()
+ for bf in self.base_finds.all()]
+ })
+ # images
+ return dct
+
@property
def name(self):
return u" - ".join([base_find.name
diff --git a/archaeological_operations/models.py b/archaeological_operations/models.py
index a6e55a59e..336a9ca74 100644
--- a/archaeological_operations/models.py
+++ b/archaeological_operations/models.py
@@ -342,6 +342,24 @@ class ArchaeologicalSite(DocumentItem, BaseHistorizedItem, QRCodeItem,
def short_class_name(self):
return _("SITE")
+ def public_representation(self):
+ dct = super(ArchaeologicalSite, self).public_representation()
+ dct.update({
+ "reference": self.reference,
+ "name": self.name,
+ "materials": [str(p) for p in self.periods.all()],
+ "remains": [str(r) for r in self.remains.all()],
+ "towns": [t.label_with_areas() for t in self.towns.all()],
+ "comment": self.comment,
+ "locality": self.locality_ngi or self.locality_cadastral,
+ })
+ profile = get_current_profile()
+ if profile.underwater():
+ dct["shipwreck-name"] = self.shipwreck_name
+ dct["sinking-date"] = self.sinking_date
+ dct["discovery-area"] = self.discovery_area
+ return dct
+
@property
def finds(self):
from archaeological_finds.models import Find
@@ -1068,6 +1086,21 @@ class Operation(ClosedItem, DocumentItem, BaseHistorizedItem, QRCodeItem,
]
return values
+ def public_representation(self):
+ dct = super(Operation, self).public_representation()
+ dct.update({
+ "year": self.year,
+ "common-name": self.common_name,
+ "operation-type": self.operation_type and str(self.operation_type),
+ "remains": [str(r) for r in self.remains.all()],
+ "periods": [str(p) for p in self.periods.all()],
+ "excavation-start-date": self.start_date,
+ "excavation-end-date": self.excavation_end_date,
+ "address": self.address,
+ "comment": self.comment,
+ })
+ return dct
+
@classmethod
def _get_department_code(cls, value):
if not settings.ISHTAR_DPTS:
diff --git a/example_project/settings.py b/example_project/settings.py
index 15ed14559..3897cbb21 100644
--- a/example_project/settings.py
+++ b/example_project/settings.py
@@ -267,6 +267,8 @@ ISHTAR_DOC_TYPES = {u"undefined": u"Undefined"}
ISHTAR_SEARCH_LANGUAGE = "french"
+ISHTAR_SECURE = True
+
ISHTAR_DPTS = []
MAX_ATTEMPTS = 1 # django background tasks
diff --git a/install/ishtar-install b/install/ishtar-install
index 82e31d002..050e340a8 100755
--- a/install/ishtar-install
+++ b/install/ishtar-install
@@ -468,7 +468,7 @@ EOF
( set -x; $sh_c 'sleep 3; apt-get install -y -q \
libpangocairo-1.0-0 python3-requests \
python3-bs4 python3-cffi pandoc libjs-jquery graphviz \
- python3-xmltodict \
+ python3-xmltodict python3-djangorestframework \
python3-tidylib python3-lxml python3-imaging python3-html5lib \
python3-psycopg2 python3-gdal gettext memcached \
python3-memcache python3-dbf python3-markdown \
diff --git a/ishtar_common/models.py b/ishtar_common/models.py
index f39cdabe8..dc7c219ac 100644
--- a/ishtar_common/models.py
+++ b/ishtar_common/models.py
@@ -1755,6 +1755,16 @@ class QRCodeItem(models.Model, ImageContainerModel):
class DocumentItem(object):
+ def public_representation(self):
+ images = []
+ if getattr(self, "main_image", None):
+ images.append(self.main_image.public_representation())
+ images += [
+ image.public_representation()
+ for image in self.images_without_main_image().all()
+ ]
+ return {"images": images}
+
@property
def images(self):
if not hasattr(self, 'documents'):
@@ -2058,6 +2068,9 @@ class BaseHistorizedItem(StatisticItem, TemplateItem, FullSearch, Imported,
def merge(self, item, keep_old=False):
merge_model_objects(self, item, keep_old=keep_old)
+ def public_representation(self):
+ return {}
+
def duplicate(self, user=None, data=None):
model = self.__class__
new = model.objects.get(pk=self.pk)
@@ -4720,6 +4733,12 @@ class Author(FullSearch):
list(self.findsource_related.all()) + \
list(self.contextrecordsource_related.all())
+ def public_representation(self):
+ return {
+ "type": str(self.author_type),
+ "person": str(self.person)
+ }
+
post_save.connect(cached_label_changed, sender=Author)
@@ -5028,6 +5047,36 @@ class Document(BaseHistorizedItem, OwnPerms, ImageModel, ValueGetter, MainItem):
self.index)
"""
+ def public_representation(self):
+ site = Site.objects.get_current()
+ if settings.ISHTAR_SECURE:
+ scheme = "https"
+ else:
+ scheme = "http"
+ base_url = scheme + "://" + site.domain + "/"
+ image = None
+ if self.image:
+ image = self.image.url
+ if not image.startswith("http"):
+ if not image.startswith("/"):
+ image = "/" + image
+ image = base_url + image
+ thumbnail = None
+ if self.thumbnail:
+ thumbnail = self.thumbnail.url
+ if not thumbnail.startswith("http"):
+ if not thumbnail.startswith("/"):
+ thumbnail = "/" + thumbnail
+ thumbnail = base_url + thumbnail
+ return {
+ "title": self.title,
+ "reference": self.reference,
+ "type": self.source_type,
+ "authors": [a.public_representation() for a in self.authors.all()],
+ "image": image,
+ "thumbnail": thumbnail,
+ }
+
@property
def image_path(self):
if not self.image:
diff --git a/ishtar_common/serializers.py b/ishtar_common/serializers.py
new file mode 100644
index 000000000..9789bd9f6
--- /dev/null
+++ b/ishtar_common/serializers.py
@@ -0,0 +1,6 @@
+from rest_framework import serializers
+
+
+class PublicFindSerializer(serializers.BaseSerializer):
+ def to_representation(self, obj):
+ return obj.public_representation()
diff --git a/requirements.txt b/requirements.txt
index 9f93e818b..bd90c1fb2 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -15,7 +15,7 @@ requests==2.12
dbf==0.96.003
python-memcached==1.57
# celery==4.2.1 ## not mandatory
-
+djangorestframework==3.4
pytidylib==0.3.2
lxml==3.4.0