Skip to content

Commit

Permalink
fix defect: views's filter doesn't work in django 2.0+
Browse files Browse the repository at this point in the history
  • Loading branch information
alexzhangs committed May 12, 2024
1 parent 4b403a8 commit 9d88fb8
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 15 deletions.
8 changes: 4 additions & 4 deletions shadowsocks_manager/domain/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
from __future__ import unicode_literals
from __future__ import absolute_import

from rest_framework import viewsets
from shadowsocks_manager.utils.viewsets import CompatModelViewSet

from . import models, serializers


# Create your views here.

class NameServerViewSet(viewsets.ModelViewSet):
class NameServerViewSet(CompatModelViewSet):
"""
This viewset automatically provides `list` and `detail` actions.
"""
Expand All @@ -20,7 +20,7 @@ class NameServerViewSet(viewsets.ModelViewSet):
filter_fields = ['name', 'api_cls_name', 'user']


class DomainViewSet(viewsets.ModelViewSet):
class DomainViewSet(CompatModelViewSet):
"""
This viewset automatically provides `list` and `detail` actions.
"""
Expand All @@ -29,7 +29,7 @@ class DomainViewSet(viewsets.ModelViewSet):
filter_fields = ['name', 'nameserver', 'nameserver__name']


class RecordViewSet(viewsets.ModelViewSet):
class RecordViewSet(CompatModelViewSet):
"""
This viewset automatically provides `list` and `detail` actions.
"""
Expand Down
4 changes: 2 additions & 2 deletions shadowsocks_manager/notification/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
from __future__ import unicode_literals
from __future__ import absolute_import

from rest_framework import viewsets
from shadowsocks_manager.utils.viewsets import CompatModelViewSet

from . import models, serializers


# Create your views here.

class TemplateViewSet(viewsets.ModelViewSet):
class TemplateViewSet(CompatModelViewSet):
"""
This viewset automatically provides `list` and `detail` actions.
"""
Expand Down
12 changes: 6 additions & 6 deletions shadowsocks_manager/shadowsocks/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,30 @@
from __future__ import unicode_literals
from __future__ import absolute_import

from rest_framework import viewsets
from shadowsocks_manager.utils.viewsets import CompatModelViewSet

from . import models, serializers


# Create your views here.

class ConfigViewSet(viewsets.ModelViewSet):
class ConfigViewSet(CompatModelViewSet):
"""
This viewset automatically provides `list` and `detail` actions.
"""
queryset = models.Config.objects.all()
serializer_class = serializers.ConfigSerializer


class AccountViewSet(viewsets.ModelViewSet):
class AccountViewSet(CompatModelViewSet):
"""
This viewset automatically provides `list` and `detail` actions.
"""
queryset = models.Account.objects.all()
serializer_class = serializers.AccountSerializer


class NodeViewSet(viewsets.ModelViewSet):
class NodeViewSet(CompatModelViewSet):
"""
This viewset automatically provides `list` and `detail` actions.
"""
Expand All @@ -36,15 +36,15 @@ class NodeViewSet(viewsets.ModelViewSet):
filter_fields = ['name']


class NodeAccountViewSet(viewsets.ModelViewSet):
class NodeAccountViewSet(CompatModelViewSet):
"""
This viewset automatically provides `list` and `detail` actions.
"""
queryset = models.NodeAccount.objects.all()
serializer_class = serializers.NodeAccountSerializer


class SSManagerViewSet(viewsets.ModelViewSet):
class SSManagerViewSet(CompatModelViewSet):
"""
This viewset automatically provides `list` and `detail` actions.
"""
Expand Down
6 changes: 3 additions & 3 deletions shadowsocks_manager/statistic/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,22 @@
# py2.7 and py3 compatibility imports
from __future__ import unicode_literals

from rest_framework import viewsets
from shadowsocks_manager.utils.viewsets import CompatModelViewSet

from . import models, serializers


# Create your views here.

class PeriodViewSet(viewsets.ModelViewSet):
class PeriodViewSet(CompatModelViewSet):
"""
This viewset automatically provides `list` and `detail` actions.
"""
queryset = models.Period.objects.all()
serializer_class = serializers.PeriodSerializer


class StatisticViewSet(viewsets.ModelViewSet):
class StatisticViewSet(CompatModelViewSet):
"""
This viewset automatically provides `list` and `detail` actions.
"""
Expand Down
30 changes: 30 additions & 0 deletions shadowsocks_manager/utils/viewsets.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from django import VERSION as DJANGO_VERSION
from rest_framework.viewsets import ModelViewSet


class CompatModelViewSet(ModelViewSet):
"""
A compatibility class that extends the ModelViewSet class from Django REST Framework.
This class provides compatibility for different versions of Django.
Attributes:
filter_fields (list):
A list of fields to be used for filtering the queryset.
Used in Django 1.x.
filterset_fields (list):
Used since Django 2.x.
A list of fields to be used for filtering the queryset.
Methods:
__init__(self, *args, **kwargs): Initializes the CompatModelViewSet instance.
"""

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
if DJANGO_VERSION < (2, 0):
if not hasattr(self, 'filter_fields') and hasattr(self, 'filterset_fields'):
self.filter_fields = getattr(self, 'filterset_fields', [])

Check warning on line 27 in shadowsocks_manager/utils/viewsets.py

View check run for this annotation

Codecov / codecov/patch

shadowsocks_manager/utils/viewsets.py#L24-L27

Added lines #L24 - L27 were not covered by tests
else:
if not hasattr(self, 'filterset_fields') and hasattr(self, 'filter_fields'):
self.filterset_fields = getattr(self, 'filter_fields', [])

Check warning on line 30 in shadowsocks_manager/utils/viewsets.py

View check run for this annotation

Codecov / codecov/patch

shadowsocks_manager/utils/viewsets.py#L29-L30

Added lines #L29 - L30 were not covered by tests

0 comments on commit 9d88fb8

Please sign in to comment.