|
19 | 19 | from django.utils.translation import gettext_lazy as _ |
20 | 20 | from django.utils.translation.trans_real import get_language_from_request |
21 | 21 | from django.views.decorators.csrf import csrf_exempt |
| 22 | +from django_filters import rest_framework as filters |
22 | 23 | from django_filters.rest_framework import CharFilter, DjangoFilterBackend |
23 | 24 | from drf_yasg.utils import no_body, swagger_auto_schema |
24 | 25 | from rest_framework import serializers, status |
25 | 26 | from rest_framework.authentication import SessionAuthentication |
26 | 27 | from rest_framework.authtoken.models import Token as UserToken |
27 | 28 | from rest_framework.authtoken.views import ObtainAuthToken as BaseObtainAuthToken |
28 | 29 | from rest_framework.exceptions import NotFound, ParseError, PermissionDenied |
| 30 | +from rest_framework.filters import SearchFilter |
29 | 31 | from rest_framework.generics import ( |
30 | 32 | CreateAPIView, |
31 | 33 | GenericAPIView, |
32 | 34 | ListAPIView, |
| 35 | + ListCreateAPIView, |
33 | 36 | RetrieveAPIView, |
| 37 | + RetrieveUpdateDestroyAPIView, |
34 | 38 | ) |
| 39 | +from rest_framework.pagination import PageNumberPagination |
35 | 40 | from rest_framework.permissions import ( |
36 | 41 | DjangoModelPermissions, |
37 | 42 | IsAdminUser, |
|
42 | 47 |
|
43 | 48 | from openwisp_radius.api.serializers import RadiusUserSerializer |
44 | 49 | from openwisp_users.api.authentication import BearerAuthentication, SesameAuthentication |
| 50 | +from openwisp_users.api.filters import OrganizationManagedFilter |
45 | 51 | from openwisp_users.api.mixins import FilterByOrganizationManaged, ProtectedAPIMixin |
46 | 52 | from openwisp_users.api.permissions import IsOrganizationManager |
47 | 53 | from openwisp_users.api.views import ChangePasswordView as BasePasswordChangeView |
|
62 | 68 | ChangePhoneNumberSerializer, |
63 | 69 | RadiusAccountingSerializer, |
64 | 70 | RadiusBatchSerializer, |
| 71 | + RadiusGroupSerializer, |
65 | 72 | UserRadiusUsageSerializer, |
66 | 73 | ValidatePhoneTokenSerializer, |
67 | 74 | ) |
|
84 | 91 | RadiusToken = load_model("RadiusToken") |
85 | 92 | RadiusBatch = load_model("RadiusBatch") |
86 | 93 | RadiusUserGroup = load_model("RadiusUserGroup") |
| 94 | +RadiusGroup = load_model("RadiusGroup") |
87 | 95 | RadiusGroupCheck = load_model("RadiusGroupCheck") |
88 | 96 | auth_backend = UsersAuthenticationBackend() |
89 | 97 |
|
@@ -847,3 +855,90 @@ class RadiusAccountingView(ProtectedAPIMixin, FilterByOrganizationManaged, ListA |
847 | 855 |
|
848 | 856 |
|
849 | 857 | radius_accounting = RadiusAccountingView.as_view() |
| 858 | + |
| 859 | + |
| 860 | +class RadiusGroupFilter(OrganizationManagedFilter, filters.FilterSet): |
| 861 | + """ |
| 862 | + Filter RADIUS groups by organizations managed by the user. |
| 863 | + """ |
| 864 | + |
| 865 | + class Meta(OrganizationManagedFilter.Meta): |
| 866 | + model = RadiusGroup |
| 867 | + |
| 868 | + |
| 869 | +class RadiusGroupPaginator(PageNumberPagination): |
| 870 | + page_size = 20 |
| 871 | + page_size_query_param = "page_size" |
| 872 | + max_page_size = 100 |
| 873 | + |
| 874 | + |
| 875 | +@method_decorator( |
| 876 | + name="get", |
| 877 | + decorator=swagger_auto_schema( |
| 878 | + operation_description=""" |
| 879 | + Returns a list of RADIUS groups for the organizations managed by the user. |
| 880 | + """, |
| 881 | + ), |
| 882 | +) |
| 883 | +@method_decorator( |
| 884 | + name="post", |
| 885 | + decorator=swagger_auto_schema( |
| 886 | + operation_description=""" |
| 887 | + Creates a new RADIUS group for an organization managed by the user. |
| 888 | + """, |
| 889 | + ), |
| 890 | +) |
| 891 | +class RadiusGroupListView( |
| 892 | + ProtectedAPIMixin, FilterByOrganizationManaged, ListCreateAPIView |
| 893 | +): |
| 894 | + serializer_class = RadiusGroupSerializer |
| 895 | + queryset = RadiusGroup.objects.order_by("name") |
| 896 | + filterset_class = RadiusGroupFilter |
| 897 | + filter_backends = [DjangoFilterBackend, SearchFilter] |
| 898 | + search_fields = ["name"] |
| 899 | + pagination_class = RadiusGroupPaginator |
| 900 | + |
| 901 | + |
| 902 | +radius_group_list = RadiusGroupListView.as_view() |
| 903 | + |
| 904 | + |
| 905 | +@method_decorator( |
| 906 | + name="get", |
| 907 | + decorator=swagger_auto_schema( |
| 908 | + operation_description=""" |
| 909 | + Returns a single RADIUS group by its UUID. |
| 910 | + """, |
| 911 | + ), |
| 912 | +) |
| 913 | +@method_decorator( |
| 914 | + name="put", |
| 915 | + decorator=swagger_auto_schema( |
| 916 | + operation_description=""" |
| 917 | + Updates a RADIUS group identified by its UUID. |
| 918 | + """, |
| 919 | + ), |
| 920 | +) |
| 921 | +@method_decorator( |
| 922 | + name="patch", |
| 923 | + decorator=swagger_auto_schema( |
| 924 | + operation_description=""" |
| 925 | + Partially updates a RADIUS group identified by its UUID. |
| 926 | + """, |
| 927 | + ), |
| 928 | +) |
| 929 | +@method_decorator( |
| 930 | + name="delete", |
| 931 | + decorator=swagger_auto_schema( |
| 932 | + operation_description=""" |
| 933 | + Deletes a RADIUS group identified by its UUID. |
| 934 | + """, |
| 935 | + ), |
| 936 | +) |
| 937 | +class RadiusGroupDetailView( |
| 938 | + ProtectedAPIMixin, FilterByOrganizationManaged, RetrieveUpdateDestroyAPIView |
| 939 | +): |
| 940 | + serializer_class = RadiusGroupSerializer |
| 941 | + queryset = RadiusGroup.objects.order_by("name") |
| 942 | + |
| 943 | + |
| 944 | +radius_group_detail = RadiusGroupDetailView.as_view() |
0 commit comments