summaryrefslogtreecommitdiff
path: root/ishtar_common
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 /ishtar_common
parent3b3f4bb167226810a4d323488071bba22ae27621 (diff)
downloadIshtar-7185d81691ede9520cecdd2a2e8dcac372aaac6c.tar.bz2
Ishtar-7185d81691ede9520cecdd2a2e8dcac372aaac6c.zip
Base public serializer
Diffstat (limited to 'ishtar_common')
-rw-r--r--ishtar_common/models.py49
-rw-r--r--ishtar_common/serializers.py6
2 files changed, 55 insertions, 0 deletions
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()