From 1a51d00cd4b434d236457e35abf67f255875c1e2 Mon Sep 17 00:00:00 2001 From: Your Name Date: Sat, 14 Jan 2023 09:02:53 -0500 Subject: [PATCH] add objective collection to timeline --- common/static/lib/css/collection.css | 13 +++++++++++++ common/templates/partial/_sidebar.html | 14 -------------- journal/models.py | 18 ++++++++++++++---- journal/views.py | 8 ++++++-- social/models.py | 8 ++++++++ 5 files changed, 41 insertions(+), 20 deletions(-) diff --git a/common/static/lib/css/collection.css b/common/static/lib/css/collection.css index c9524c16..4db399a8 100644 --- a/common/static/lib/css/collection.css +++ b/common/static/lib/css/collection.css @@ -181,3 +181,16 @@ div.jsoneditor-menu { vertical-align: middle; text-align: center; } + +progress { + border-radius: 7px; + width: 80%; + height: 10px; + box-shadow: 1px 1px 4px rgba( 0, 0, 0, 0.2 ); +} +::-webkit-progress-bar { + background-color: #ccc; +} +::-webkit-progress-value { + background-color: #00a1cc; +} diff --git a/common/templates/partial/_sidebar.html b/common/templates/partial/_sidebar.html index cd05dc5c..85a04aa2 100644 --- a/common/templates/partial/_sidebar.html +++ b/common/templates/partial/_sidebar.html @@ -47,20 +47,6 @@ {% if user.featured_collections.all %}
-
{% trans '当前目标' %} diff --git a/journal/models.py b/journal/models.py index 50baa15a..5e1e8461 100644 --- a/journal/models.py +++ b/journal/models.py @@ -711,7 +711,9 @@ class Collection(List): if len(items) == 0: return 0 shelf = user.shelf_manager.shelf_list["complete"] - return shelf.members.all().filter(item_id__in=items).count() * 100 / len(items) + return round( + shelf.members.all().filter(item_id__in=items).count() * 100 / len(items) + ) def save(self, *args, **kwargs): if getattr(self, "catalog_item", None) is None: @@ -727,14 +729,22 @@ class Collection(List): super().save(*args, **kwargs) -class FeaturedCollection(models.Model): +class FeaturedCollection(Piece): owner = models.ForeignKey(User, on_delete=models.CASCADE) - collection = models.ForeignKey(Collection, on_delete=models.CASCADE) + target = models.ForeignKey(Collection, on_delete=models.CASCADE) created_time = models.DateTimeField(auto_now_add=True) edited_time = models.DateTimeField(auto_now=True) class Meta: - unique_together = [["owner", "collection"]] + unique_together = [["owner", "target"]] + + @property + def visibility(self): + return self.target.visibility + + @cached_property + def progress(self): + return self.target.get_progress_for_user(self.owner) """ diff --git a/journal/views.py b/journal/views.py index ffd5ed4e..0256e9f7 100644 --- a/journal/views.py +++ b/journal/views.py @@ -221,7 +221,7 @@ def collection_add_featured(request, collection_uuid): collection = get_object_or_404(Collection, uid=base62.decode(collection_uuid)) if not collection.is_visible_to(request.user): raise PermissionDenied() - request.user.featured_collections.add(collection) + FeaturedCollection.objects.update_or_create(owner=request.user, target=collection) return HttpResponseRedirect(request.META.get("HTTP_REFERER")) @@ -231,7 +231,11 @@ def collection_remove_featured(request, collection_uuid): collection = get_object_or_404(Collection, uid=base62.decode(collection_uuid)) if not collection.is_visible_to(request.user): raise PermissionDenied() - request.user.featured_collections.remove(collection) + fc = FeaturedCollection.objects.filter( + owner=request.user, target=collection + ).first() + if fc: + fc.delete() return HttpResponseRedirect(request.META.get("HTTP_REFERER")) diff --git a/social/models.py b/social/models.py index a5efd739..6a011582 100644 --- a/social/models.py +++ b/social/models.py @@ -27,6 +27,7 @@ class ActivityTemplate(models.TextChoices): ReviewItem = "review_item" CreateCollection = "create_collection" LikeCollection = "like_collection" + FeatureCollection = "feature_collection" class LocalActivity(models.Model, UserOwnedObjectMixin): @@ -78,6 +79,7 @@ class DataSignalManager: @staticmethod def save_handler(sender, instance, created, **kwargs): processor_class = DataSignalManager.processors.get(instance.__class__) + if processor_class: processor = processor_class(instance) if created: @@ -173,3 +175,9 @@ class LikeCollectionProcessor(DefaultActivityProcessor): def updated(self): if isinstance(self.action_object.target, Collection): super().update() + + +@DataSignalManager.register +class FeaturedCollectionProcessor(DefaultActivityProcessor): + model = FeaturedCollection + template = ActivityTemplate.FeatureCollection