Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Backport][Fixes #7644] Api - filter favorite in /resources endpoint doesn't return all favorites item #7646

Merged
merged 11 commits into from
Jun 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion geonode/base/api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ def to_representation(self, instance):
)
if not request.user.is_anonymous:
favorite = Favorite.objects.filter(user=request.user, object_id=instance.pk).count()
data['favourite'] = favorite > 0
data['favorite'] = favorite > 0
return data


Expand Down
7 changes: 6 additions & 1 deletion geonode/favorite/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#
#########################################################################

from geonode.base.models import ResourceBase
from django.conf import settings
from django.contrib.auth import get_user_model
from django.contrib.contenttypes.models import ContentType
Expand Down Expand Up @@ -79,7 +80,11 @@ def bulk_favorite_objects(self, user):
return favs

def create_favorite(self, content_object, user):
content_type = ContentType.objects.get_for_model(type(content_object))
if type(content_object) == ResourceBase:
content_type = ContentType.objects.get(model=content_object.resource_type)
else:
content_type = ContentType.objects.get_for_model(type(content_object))

favorite, _ = self.get_or_create(
user=user,
content_type=content_type,
Expand Down
23 changes: 23 additions & 0 deletions geonode/favorite/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
#
#########################################################################

from geonode.base.models import ResourceBase
from geonode.base.populate_test_data import create_single_layer
from geonode.tests.base import GeoNodeBaseTestSupport

import json
Expand Down Expand Up @@ -90,6 +92,27 @@ def test_favorite(self):
self.assertEqual(len(bulk_favorites["map"]), 0)
self.assertEqual(len(bulk_favorites["user"]), 0)

def test_given_resource_base_object_will_assign_subtype_as_content_type(self):
test_user = get_user_model().objects.first()

'''
If the input object is a ResourceBase, in favorite content type, should be saved he
subtype content type (Doc, Layer, Map or GeoApp)
'''
create_single_layer('foo_layer')
resource = ResourceBase.objects.get(title='foo_layer')
created_fav = Favorite.objects.create_favorite(resource, test_user)
self.assertEqual('layer', created_fav.content_type.model)

'''
If the input object is a subtype, should save the relative content type
'''
test_document_1 = Document.objects.first()
Favorite.objects.create_favorite(test_document_1, test_user)
fav = Favorite.objects.last()
ct = ContentType.objects.get_for_model(test_document_1)
self.assertEqual(fav.content_type, ct)

# tests of view methods.
def test_create_favorite_view(self):
"""
Expand Down