Skip to content

Commit

Permalink
[change] More requested changes
Browse files Browse the repository at this point in the history
  • Loading branch information
purhan committed Jan 7, 2021
1 parent a4fb43f commit f88f335
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 34 deletions.
17 changes: 7 additions & 10 deletions openwisp_ipam/api/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,6 @@ def get_organization_queryset(self, qs):


class AuthorizeCSVImport:
def post(self, request):
self.assert_organization_permissions(request)

def assert_organization_permissions(self, request):
if request.user.is_superuser:
return
Expand Down Expand Up @@ -108,10 +105,10 @@ def filter_fields(self):
self.fields[field].queryset = self.fields[field].queryset.filter(
pk__in=organization_filter
)
else:
try:
self.fields[field].queryset = self.fields[field].queryset.filter(
organization__in=organization_filter
)
except AttributeError:
pass
continue
try:
self.fields[field].queryset = self.fields[field].queryset.filter(
organization__in=organization_filter
)
except AttributeError:
pass
37 changes: 13 additions & 24 deletions openwisp_ipam/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,20 +47,6 @@ def get_parent_queryset(self):
return qs


class ImportSubnetCSVMixin(AuthorizeCSVOrgManaged):
def get_csv_organization(self):
data = self.subnet_model._get_csv_reader(
self, deepcopy(self.request.FILES['csvfile'])
)
org = Organization.objects.get(name=list(data)[2][0].strip())
return org


class SerializerContextMixin:
def get_serializer_context(self):
return {'request': self.request}


class ListViewPagination(pagination.PageNumberPagination):
page_size = 10
page_size_query_param = 'page_size'
Expand Down Expand Up @@ -176,9 +162,7 @@ def get(self, request, *args, **kwargs):
return Response(subnet.get_next_available_ip())


class IpAddressListCreateView(
IpAddressOrgMixin, ListCreateAPIView, SerializerContextMixin
):
class IpAddressListCreateView(IpAddressOrgMixin, ListCreateAPIView):
queryset = IpAddress.objects.none()
subnet_model = Subnet
serializer_class = IpAddressSerializer
Expand All @@ -192,17 +176,15 @@ def get_queryset(self):
return subnet.ipaddress_set.all().order_by('ip_address')


class SubnetListCreateView(
FilterByOrganizationManaged, ListCreateAPIView, SerializerContextMixin
):
class SubnetListCreateView(FilterByOrganizationManaged, ListCreateAPIView):
serializer_class = SubnetSerializer
authentication_classes = (BearerAuthentication, SessionAuthentication)
permission_classes = (DjangoModelPermissions,)
pagination_class = ListViewPagination
queryset = Subnet.objects.all()


class SubnetView(RetrieveUpdateDestroyAPIView, SerializerContextMixin):
class SubnetView(RetrieveUpdateDestroyAPIView):
serializer_class = SubnetSerializer
authentication_classes = (BearerAuthentication, SessionAuthentication)
permission_classes = (
Expand All @@ -212,7 +194,7 @@ class SubnetView(RetrieveUpdateDestroyAPIView, SerializerContextMixin):
queryset = Subnet.objects.all()


class IpAddressView(RetrieveUpdateDestroyAPIView, SerializerContextMixin):
class IpAddressView(RetrieveUpdateDestroyAPIView):
serializer_class = IpAddressSerializer
authentication_classes = (BearerAuthentication, SessionAuthentication)
permission_classes = (
Expand Down Expand Up @@ -245,15 +227,22 @@ def post(self, request, *args, **kwargs):
return Response(None)


class ImportSubnetView(ImportSubnetCSVMixin, CreateAPIView):
class ImportSubnetView(CreateAPIView, AuthorizeCSVOrgManaged):
subnet_model = Subnet
queryset = Subnet.objects.none()
serializer_class = ImportSubnetSerializer
authentication_classes = (BearerAuthentication, SessionAuthentication)
permission_classes = (DjangoModelPermissions,)

def get_csv_organization(self):
data = self.subnet_model._get_csv_reader(
self, deepcopy(self.request.FILES['csvfile'])
)
org = Organization.objects.get(name=list(data)[2][0].strip())
return org

def post(self, request, *args, **kwargs):
super().post(request)
self.assert_organization_permissions(request)
file = request.FILES['csvfile']
if not file.name.endswith(('.csv', '.xls', '.xlsx')):
return Response({'error': _('File type not supported.')}, status=400)
Expand Down

0 comments on commit f88f335

Please sign in to comment.