Skip to content

Commit

Permalink
[Fixes #12594] Error when saing a new map
Browse files Browse the repository at this point in the history
  • Loading branch information
mattiagiupponi committed Sep 23, 2024
1 parent c3140e8 commit 19e3815
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 13 deletions.
18 changes: 5 additions & 13 deletions geonode/base/api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,6 @@
logger = logging.getLogger(__name__)


# fixing up the publishing option based on user permissions
SETTINGS_MAPPING = {"is_approved": "can_approve", "is_published": "can_publish", "featured": "can_feature"}


def user_serializer():
import geonode.people.api.serializers as ser

Expand Down Expand Up @@ -741,11 +737,9 @@ def to_internal_value(self, data):

def update(self, instance, validated_data):
user = self.context["request"].user
for field, user_action in SETTINGS_MAPPING.items():
if not getattr(user, user_action)(instance) and field in validated_data:
# in case the user does not have the perms to do the action
# we reset the default values
validated_data.pop(field, None)
for field in user.APPROVAL_STATUS_FIELDS:
if not user.can_change_resource_field(instance, field):
validated_data.pop(field)
return super().update(instance, validated_data)

def save(self, **kwargs):
Expand All @@ -766,10 +760,8 @@ def save(self, **kwargs):
instance.set_bbox_polygon(coords, srid)

user = self.context["request"].user
for field, user_action in SETTINGS_MAPPING.items():
if not getattr(user, user_action)(instance):
# in case the user does not have the perms to do the action
# we reset the default values
for field in user.APPROVAL_STATUS_FIELDS:
if not user.can_change_resource_field(instance, field):
logger.debug("User can perform the action, the default value is set")
setattr(user, field, getattr(ResourceBase, field).field.default)
return instance
Expand Down
12 changes: 12 additions & 0 deletions geonode/people/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,9 @@ def class_name(value):
objects = ProfileUserManager()
USERNAME_FIELD = "username"

# fixing up the publishing option based on user permissions
APPROVAL_STATUS_FIELDS = ["is_approved", "is_published", "featured"]

def group_list_public(self):
return GroupProfile.objects.exclude(access="private").filter(groupmember__user=self)

Expand Down Expand Up @@ -261,6 +264,15 @@ def send_mail(self, template_prefix, context):
if self.email:
get_adapter().send_mail(template_prefix, self.email, context)

def can_change_resource_field(self, resource, field):
match field:
case "is_approved":
return self.can_approve(resource)
case "is_published":
return self.can_publish(resource)
case "featured":
return self.can_feature(resource)

def can_approve(self, resource):
return can_approve(self, resource)

Expand Down

0 comments on commit 19e3815

Please sign in to comment.