openwisp/openwisp-network-topology

[change] Remove redundant code for checking view permissions

ManishShah120 opened this issue · 1 comments

class HasGetMethodPermission(BasePermission):
def has_permission(self, request, view):
return self.check_permission(request)
def has_object_permission(self, request, view, obj):
return self.check_permission(request)
def check_permission(self, request):
user = request.user
app_label = Topology._meta.app_label.lower()
model = Topology._meta.object_name.lower()
change_perm = f'{app_label}.change_{model}'
view_perm = f'{app_label}.view_{model}'
if user.is_authenticated:
if user.is_superuser or request.method != 'GET':
return True
if request.method == 'GET' and (
user.has_permission(change_perm) or user.has_permission(view_perm)
):
return True
return False

I guess, this portion of code does the same thing as what we did recently in this PR openwisp/openwisp-users#251

So, will it not better if we simply import it from there?

class HasGetMethodPermission(BasePermission):
def has_permission(self, request, view):
return self.check_permission(request)
def has_object_permission(self, request, view, obj):
return self.check_permission(request)
def check_permission(self, request):
user = request.user
app_label = Topology._meta.app_label.lower()
model = Topology._meta.object_name.lower()
change_perm = f'{app_label}.change_{model}'
view_perm = f'{app_label}.view_{model}'
if user.is_authenticated:
if user.is_superuser or request.method != 'GET':
return True
if request.method == 'GET' and (
user.has_permission(change_perm) or user.has_permission(view_perm)
):
return True
return False

I guess, this portion of code does the same thing as what we did recently in this PR openwisp/openwisp-users#251

So, will it not better if we simply import it from there?

This code was added before we introduced openwisp/openwisp-users#251 so yes, it will have to be updated.