[PATCH 1/2] REST: Add 'has_version' helper
Stephen Finucane
stephen at that.guru
Mon Sep 9 08:31:46 AEST 2019
We're going to use this functionality elsewhere shortly.
Signed-off-by: Stephen Finucane <stephen at that.guru>
---
patchwork/api/base.py | 12 +++---------
patchwork/api/utils.py | 14 ++++++++++++++
2 files changed, 17 insertions(+), 9 deletions(-)
create mode 100644 patchwork/api/utils.py
diff --git a/patchwork/api/base.py b/patchwork/api/base.py
index 943afa21..89a43114 100644
--- a/patchwork/api/base.py
+++ b/patchwork/api/base.py
@@ -3,7 +3,6 @@
#
# SPDX-License-Identifier: GPL-2.0-or-later
-from distutils.version import StrictVersion
from django.conf import settings
from django.shortcuts import get_object_or_404
@@ -13,6 +12,8 @@ from rest_framework.response import Response
from rest_framework.serializers import HyperlinkedIdentityField
from rest_framework.serializers import HyperlinkedModelSerializer
+from patchwork.api import utils
+
class LinkHeaderPagination(PageNumberPagination):
"""Provide pagination based on rfc5988.
@@ -90,17 +91,10 @@ class BaseHyperlinkedModelSerializer(HyperlinkedModelSerializer):
instance)
request = self.context.get('request')
- if not request or not request.version:
- # without version information, we have to assume the latest
- return data
-
- requested_version = StrictVersion(request.version)
-
for version in getattr(self.Meta, 'versioned_fields', {}):
# if the user has requested a version lower that than in which the
# field was added, we drop it
- required_version = StrictVersion(version)
- if required_version > requested_version:
+ if not utils.has_version(request, version):
for field in self.Meta.versioned_fields[version]:
data.pop(field)
diff --git a/patchwork/api/utils.py b/patchwork/api/utils.py
new file mode 100644
index 00000000..7d46e846
--- /dev/null
+++ b/patchwork/api/utils.py
@@ -0,0 +1,14 @@
+# Patchwork - automated patch tracking system
+# Copyright (C) 2018 Stephen Finucane <stephen at that.guru>
+#
+# SPDX-License-Identifier: GPL-2.0-or-later
+
+from distutils.version import StrictVersion
+
+
+def has_version(request, version):
+ if not request.version:
+ # without version information, we have to assume the latest
+ return True
+
+ return StrictVersion(request.version) >= StrictVersion(version)
--
2.21.0
More information about the Patchwork
mailing list