Skip to content

[Housekeeping]: Address duplicate code as much as possible #67

Open
@ryanmerolle

Description

@ryanmerolle

Proposed Changes

Eliminate code duplication specifically in netbox_acls/api/serializers.py

 Clone found (python):
 - /github/workspace/netbox_acls/api/serializers.py [159:15 - 173:12] (14 lines, 93 tokens)
   /github/workspace/netbox_acls/api/serializers.py [80:13 - 94:12]

Clone found (python):
 - /github/workspace/netbox_acls/api/serializers.py [178:9 - 191:3] (13 lines, 95 tokens)
   /github/workspace/netbox_acls/api/serializers.py [98:9 - 111:83]

Clone found (python):
 - /github/workspace/netbox_acls/api/serializers.py [334:9 - 344:55] (10 lines, 83 tokens)
   /github/workspace/netbox_acls/api/serializers.py [260:9 - 271:3]

Clone found (python):
 - /github/workspace/netbox_acls/api/serializers.py [159:15 - 173:12] (14 lines, 93 tokens)
   /github/workspace/netbox_acls/api/serializers.py [80:13 - 94:12]

 159 │ 80 │ ,                                                                                                      
 160 │ 81 │         )                                                                                              
 161 │ 82 │                                                                                                        
 162 │ 83 │     @swagger_serializer_method(serializer_or_field=serializers.DictField)                              
 163 │ 84 │     def get_assigned_object(self, obj):                                                                
 164 │ 85 │         serializer = get_serializer_for_model(obj.assigned_object, prefix="Nested")                    
 165 │ 86 │         context = {"request": self.context["request"]}                                                 
 166 │ 87 │         return serializer(obj.assigned_object, context=context).data                                   
 167 │ 88 │                                                                                                        
 168 │ 89 │     def validate(self, data):                                                                          
 169 │ 90 │         """                                                                                            
 170 │ 91 │         Validate the AccessList django model model's inputs before allowing it to update the instance. 
 171 │ 92 │           - Check that the GFK object is valid.                                                        
 172 │ 93 │           - Check that the associated interface's parent host has the selected ACL defined.            
 173 │ 94 │         """                                                                                            

Clone found (python):
 - /github/workspace/netbox_acls/api/serializers.py [178:9 - 191:3] (13 lines, 95 tokens)
   /github/workspace/netbox_acls/api/serializers.py [98:9 - 111:83]

 178 │ 98  │ if "assigned_object_type" in data and "assigned_object_id" in data:                                                                   
 179 │ 99  │             try:                                                                                                                      
 180 │ 100 │                 assigned_object = data[  # noqa: F841                                                                                 
 181 │ 101 │                     "assigned_object_type"                                                                                            
 182 │ 102 │                 ].get_object_for_this_type(                                                                                           
 183 │ 103 │                     id=data["assigned_object_id"],                                                                                    
 184 │ 104 │                 )                                                                                                                     
 185 │ 105 │             except ObjectDoesNotExist:                                                                                                
 186 │ 106 │                 # Sets a standard error message for invalid GFK                                                                       
 187 │ 107 │                 error_message_invalid_gfk = f"Invalid assigned_object {data['assigned_object_type']} ID {data['assigned_object_id']}" 
 188 │ 108 │                 error_message["assigned_object_type"] = [error_message_invalid_gfk]                                                   
 189 │ 109 │                 error_message["assigned_object_id"] = [error_message_invalid_gfk]                                                     
 190 │ 110 │                                                                                                                                       
 191 │ 111 │         if                                                                                                                            

Clone found (python):
 - /github/workspace/netbox_acls/api/serializers.py [334:9 - 344:55] (10 lines, 83 tokens)
   /github/workspace/netbox_acls/api/serializers.py [260:9 - 271:3]

 334 │ 260 │ error_message = {}                                                        
 335 │ 261 │                                                                           
 336 │ 262 │         # Check if action set to remark, but no remark set.               
 337 │ 263 │         if data.get("action") == "remark" and data.get("remark") is None: 
 338 │ 264 │             error_message["remark"] = [error_message_no_remark]           
 339 │ 265 │         # Check if action set to remark, but source_prefix set.           
 340 │ 266 │         if data.get("source_prefix"):                                     
 341 │ 267 │             error_message["source_prefix"] = [                            
 342 │ 268 │                 error_message_action_remark_source_prefix_set,            
 343 │ 269 │             ]                                                             
 344 │ 270 │         # Check if action set to remark, but source_ports set.  

Justification

Eliminate duplicate code.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions