From 80fada26cd3c2e5f2f560f7952a797d555b83fae Mon Sep 17 00:00:00 2001 From: Carine Dengler Date: Wed, 23 Nov 2022 17:49:27 +0100 Subject: [PATCH] WIP: add collection (related to #14) --- app/api/collection/facade.py | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/app/api/collection/facade.py b/app/api/collection/facade.py index 07328a49..f721ad34 100644 --- a/app/api/collection/facade.py +++ b/app/api/collection/facade.py @@ -1,3 +1,5 @@ +import datetime + from app.api.abstract_facade import JSONAPIAbstractChangeloggedFacade from app.api.user.facade import UserFacade from app.models import Collection @@ -68,6 +70,16 @@ def get_children_resources(self, rel_facade=None): @property def resource(self): + if not self.obj.documents_including_children: + date_min = datetime.datetime.now().strftime("%Y-%m-%d") + date_max = date_min + else: + creation = [ + doc.creation for doc in self.obj.documents_including_children + if doc.creation + ] + date_min = min(creation) + date_max = max(creation) resource = { **self.resource_identifier, @@ -76,8 +88,8 @@ def resource(self): "path": [c.title for c in self.obj.parents] + [self.obj.title], "description": self.obj.description, "nb_docs": len(self.obj.documents_including_children), - "date_min": min([doc.creation for doc in self.obj.documents_including_children if doc.creation]), - "date_max": max([doc.creation for doc in self.obj.documents_including_children if doc.creation]) + "date_min": date_min, + "date_max": date_max }, "meta": self.meta, "links": { @@ -151,3 +163,12 @@ def remove_from_index(self, propagate): if data["payload"]["id"] != self.id and data["payload"]["type"] != self.TYPE: data["payload"]["collections"] = [l for l in data["payload"]["collections"] if l.get("id") != self.id] SearchIndexManager.add_to_index(index=data["index"], id=data["id"], payload=data["payload"]) + + @staticmethod + def create_resource(model, obj_id, attributes, related_resources): + return JSONAPIAbstractChangeloggedFacade.create_resource( + model, + obj_id, + attributes, + related_resources + )