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

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

Open
ryanmerolle opened this issue Oct 6, 2022 · 2 comments · May be fixed by #122
Open

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

ryanmerolle opened this issue Oct 6, 2022 · 2 comments · May be fixed by #122
Assignees
Labels
help wanted Extra attention is needed housekeeping

Comments

@ryanmerolle
Copy link
Contributor

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.

@ryanmerolle ryanmerolle added help wanted Extra attention is needed housekeeping labels Oct 6, 2022
@abhi1693
Copy link
Member

The GFK validation can be removed from the code here once netbox-community/netbox#10221 is fixed.

@ryanmerolle ryanmerolle self-assigned this Jan 21, 2023
@ryanmerolle ryanmerolle changed the title [Housekeeping]: JSCPD duplicate code due to abstract class usage [Housekeeping]: Adderess duplicate code as much as possible Jan 26, 2023
@ryanmerolle ryanmerolle linked a pull request Jan 26, 2023 that will close this issue
2 tasks
@ryanmerolle ryanmerolle changed the title [Housekeeping]: Adderess duplicate code as much as possible [Housekeeping]: Address duplicate code as much as possible Jan 26, 2023
@ryanmerolle
Copy link
Contributor Author

Waiting on netbox-community/netbox#11550

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed housekeeping
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants