Skip to content
Merged
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: 2 additions & 0 deletions apps/system_manage/serializers/user_resource_permission.py
Original file line number Diff line number Diff line change
Expand Up @@ -384,8 +384,10 @@ def get_queryset(self, instance, is_x_pack_ee: bool):
})
role_name_and_type_query_set = QuerySet(model=get_dynamics_model({
'user_role_relation.workspace_id': models.CharField(),
'role_setting.type': models.CharField(),
})).filter(**{
"user_role_relation.workspace_id": self.data.get('workspace_id'),
"role_setting.type": "USER",
})
if role_name:
user_query_set = user_query_set.filter(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your current implementation looks mostly correct, but there are a few improvements to be made for clarity and potential future enhancements:

  1. Consistent Use of Model Class: Ensure that the get_dynamics_model function returns an actual model class instead of just its string representation.

  2. Type Checking: Consider adding type hints within the filter conditions to make it clear what types of values are expected.

  3. Code Readability: Use more descriptive variable names (e.g., role_related_filter_conditions, user_query_filters) to improve code readability.

Here's an optimized version with these considerations:

def get_queryset(self, instance, is_x_pack_ee: bool):
    # Define common query fields and their types
    common_fields = {
        'user_role_relation.workspace_id': models.CharField(),
        'role_setting.type': models.CharField(max_length=100),  # Assuming this field can have different types
    }
    
    # Get the actual Django model based on some logic (not shown here)
    dynamics_model = get_dynamics_model(common_fields.keys())
    
    # Initialize base queryset
    role_name_and_type_qs = QuerySet(
        model=dynamics_model,
        **common_fields
    )
    
    # Add additional filters
    role_related_filter_conditions = {
        'user_role_relation.workspace_id': self.data.get('workspace_id')
    }
    if 'role_setting' in self.data:
        role_related_filter_conditions['role_setting__type'] = "USER"
    
    # Apply all filters to the role-related queryset
    role_name_and_type_qs = role_name_and_type_qs.filter(**role_related_filter_conditions)
    
    try:
        workspace_id = self.data['workspace_id']
    
    except KeyError:
        return QuerySet.empty()

Key Changes:

  • Model Type Return Value: The get_dynamics_model function now returns a valid Django model instead of just a dictionary mapping keys to field types.
  • Field Definition: Added max_length attribute to models.CharField for better data validation.
  • Descriptive Variable Names: Used dynamics_model where appropriate and renamed variables for clarity.
  • Exception Handling: Updated exception handling to ensure safe access to form data attributes without causing unexpected errors.

These changes enhance both functionality and maintainability of the code.

Expand Down
Loading