original_docstring: Update method is used to promote or revoke moderator role
docstring: Update method is used to promote or revoke moderator role
docstring_tokens: Update method is used to promote or revoke moderator role
code: def update(self, request, pk=None):
          """Update method is used to promote or revoke moderator role"""
          user = User.objects.get(pk=pk)
          lti_context = get_current_lti_session(self.request)
          roles = self.request.data.get("roles", [])
          # Load current groups
          user_groups = lti_context.get_user_roles(user)
          group_moderator = lti_context.get_role_group(_FORUM_ROLE_MODERATOR)
  
          # Asked to revoke moderator,
          if _FORUM_ROLE_MODERATOR not in roles and _FORUM_ROLE_MODERATOR in user_groups:
              user.groups.remove(group_moderator)
              user.save()
          # Asked to become moderator, check user is not yet moderator and make sure user
          # belongs to this context by checking he has groups from this context.
          elif (
              _FORUM_ROLE_MODERATOR in roles
              and _FORUM_ROLE_MODERATOR not in user_groups
              and len(user_groups) > 0
          ):
              user.groups.add(group_moderator)
              user.save()
          else:
              logger.debug(
                  "Update moderator forbidden - Role asked %s - user groups %s",
                  roles,
                  user_groups,
              )
              raise PermissionDenied()
  
          return Response({"success": True}, status=status.HTTP_200_OK)
code_tokens: def update ( self , request , pk = None ) : user = User . objects . get ( pk = pk ) lti_context = get_current_lti_session ( self . request ) roles = self . request . data . get ( "roles" , [ ] ) user_groups = lti_context . get_user_roles ( user ) group_moderator = lti_context . get_role_group ( _FORUM_ROLE_MODERATOR ) if _FORUM_ROLE_MODERATOR not in roles and _FORUM_ROLE_MODERATOR in user_groups : user . groups . remove ( group_moderator ) user . save ( ) elif ( _FORUM_ROLE_MODERATOR in roles and _FORUM_ROLE_MODERATOR not in user_groups and len ( user_groups ) > 0 ) : user . groups . add ( group_moderator ) user . save ( ) else : logger . debug ( "Update moderator forbidden - Role asked %s - user groups %s" , roles , user_groups , ) raise PermissionDenied ( ) return Response ( { "success" : True } , status = status . HTTP_200_OK )
short_docstring: Update method is used to promote or revoke moderator role
short_docstring_tokens: Update method is used to promote or revoke moderator role
comment: """Update method is used to promote or revoke moderator role"""$SEP$# Load current groups$SEP$# Asked to revoke moderator,$SEP$# Asked to become moderator, check user is not yet moderator and make sure user$SEP$# belongs to this context by checking he has groups from this context.