]> git.ipfire.org Git - thirdparty/patchwork.git/commitdiff
REST: Add 'has_version' helper
authorStephen Finucane <stephen@that.guru>
Sun, 8 Sep 2019 22:31:46 +0000 (23:31 +0100)
committerStephen Finucane <stephen@that.guru>
Thu, 17 Oct 2019 14:54:14 +0000 (15:54 +0100)
We're going to use this functionality elsewhere shortly.

Signed-off-by: Stephen Finucane <stephen@that.guru>
patchwork/api/base.py
patchwork/api/utils.py [new file with mode: 0644]

index 943afa21998fe24bb904c0de965074437d77382c..89a43114f9e721e6b4b223433bc3e3f622c1d74f 100644 (file)
@@ -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 (file)
index 0000000..7d46e84
--- /dev/null
@@ -0,0 +1,14 @@
+# Patchwork - automated patch tracking system
+# Copyright (C) 2018 Stephen Finucane <stephen@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)