diff --git a/api/models/album_thing.py b/api/models/album_thing.py index 1d54b77fd1..dcd8162b92 100644 --- a/api/models/album_thing.py +++ b/api/models/album_thing.py @@ -36,8 +36,6 @@ class Meta: ] def save(self, *args, **kwargs): - # Update default cover photos whenever the AlbumThing is saved - self.update_default_cover_photo() super().save(*args, **kwargs) def update_default_cover_photo(self): @@ -53,6 +51,7 @@ def update_photo_count(sender, instance, action, reverse, model, pk_set, **kwarg count = instance.photos.filter(hidden=False).count() instance.photo_count = count instance.save(update_fields=["photo_count"]) + instance.update_default_cover_photo() def get_album_thing(title, owner, thing_type=None): diff --git a/api/models/photo.py b/api/models/photo.py index 8a886d1c30..97ed46ba0b 100644 --- a/api/models/photo.py +++ b/api/models/photo.py @@ -262,19 +262,21 @@ def _save_captions(self, commit=True, caption=None): def _recreate_search_captions(self): search_captions = "" - places365_captions = self.captions_json.get("places365", {}) - attributes = places365_captions.get("attributes", []) - search_captions += " ".join(attributes) + " " + if self.captions_json: + places365_captions = self.captions_json.get("places365", {}) - categories = places365_captions.get("categories", []) - search_captions += " ".join(categories) + " " + attributes = places365_captions.get("attributes", []) + search_captions += " ".join(attributes) + " " - environment = places365_captions.get("environment", "") - search_captions += environment + " " + categories = places365_captions.get("categories", []) + search_captions += " ".join(categories) + " " - user_caption = self.captions_json.get("user_caption", "") - search_captions += user_caption + " " + environment = places365_captions.get("environment", "") + search_captions += environment + " " + + user_caption = self.captions_json.get("user_caption", "") + search_captions += user_caption + " " for face in api.models.face.Face.objects.filter(photo=self).all(): search_captions += face.person.name + " " @@ -287,6 +289,7 @@ def _recreate_search_captions(self): if self.camera: search_captions += self.camera + " " + if self.lens: search_captions += self.lens + " " @@ -310,7 +313,7 @@ def _generate_captions(self, commit): self.captions_json["places365"] = res_places365 self._recreate_search_captions() - # Remove from all places365_attribute and places365_category albums + for album_thing in api.models.album_thing.AlbumThing.objects.filter( Q(photos__in=[self.image_hash]) & ( @@ -321,6 +324,7 @@ def _generate_captions(self, commit): ).all(): album_thing.photos.remove(self) album_thing.save() + if "attributes" in res_places365: for attribute in res_places365["attributes"]: album_thing = api.models.album_thing.get_album_thing( @@ -330,6 +334,7 @@ def _generate_captions(self, commit): ) album_thing.photos.add(self) album_thing.save() + if "categories" in res_places365: for category in res_places365["categories"]: album_thing = api.models.album_thing.get_album_thing(