Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion openwisp_radius/api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,10 +297,11 @@ class Meta:
class UserGroupCheckSerializer(serializers.ModelSerializer):
result = serializers.SerializerMethodField()
type = serializers.SerializerMethodField()
reset = serializers.SerializerMethodField()

class Meta:
model = RadiusGroupCheck
fields = ("attribute", "op", "value", "result", "type")
fields = ("attribute", "op", "value", "result", "type", "reset")

def get_result(self, obj):
try:
Expand All @@ -326,6 +327,19 @@ def get_type(self, obj):
else:
return counter.get_attribute_type()

def get_reset(self, obj):
try:
Counter = app_settings.CHECK_ATTRIBUTE_COUNTERS_MAP[obj.attribute]
counter = Counter(
user=self.context["user"],
group=self.context["group"],
group_check=obj,
)
_, end_time = counter.get_reset_timestamps()
return end_time
except (SkipCheck, ValueError, KeyError):
return None
Comment on lines +333 to +344
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick | 🔵 Trivial

Consider converting Unix timestamp to ISO datetime string for API consistency.

The get_reset method returns a raw integer Unix timestamp from get_reset_timestamps(), while typical REST API datetime fields return ISO 8601 formatted strings. API consumers might expect a human-readable datetime format rather than a Unix timestamp.

If this is intentional for performance or client-side handling reasons, consider documenting the format in the API specification.

♻️ Optional: Convert to ISO datetime string
+from datetime import datetime
+
 def get_reset(self, obj):
     try:
         Counter = app_settings.CHECK_ATTRIBUTE_COUNTERS_MAP[obj.attribute]
         counter = Counter(
             user=self.context["user"],
             group=self.context["group"],
             group_check=obj,
         )
         _, end_time = counter.get_reset_timestamps()
+        if end_time is not None:
+            return datetime.fromtimestamp(end_time, tz=timezone.utc).isoformat()
         return end_time
     except (SkipCheck, ValueError, KeyError):
         return None
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@openwisp_radius/api/serializers.py` around lines 333 - 344, The get_reset
method currently returns a raw Unix timestamp from
counter.get_reset_timestamps(); update get_reset (in the serializer method
get_reset) to convert the integer end_time into an ISO 8601 datetime string
before returning (e.g., use datetime.fromtimestamp/end_time with UTC and
.isoformat() or append 'Z' for UTC) so API consumers receive a human-readable
ISO datetime; preserve the existing exception handling (SkipCheck, ValueError,
KeyError) and still return None on failure.



class UserRadiusUsageSerializer(serializers.Serializer):
def to_representation(self, obj):
Expand Down
10 changes: 10 additions & 0 deletions openwisp_radius/tests/test_api/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -1070,6 +1070,7 @@ def test_user_group_check_serializer_counter_does_not_exist(self):
"result": None,
"type": None,
"value": "2000000000",
"reset": None,
},
)

Expand Down Expand Up @@ -1098,6 +1099,7 @@ def test_user_radius_usage_view(self):
"value": "10800",
"result": 0,
"type": "seconds",
"reset": checks[0]["reset"],
},
)
self.assertDictEqual(
Expand All @@ -1108,6 +1110,7 @@ def test_user_radius_usage_view(self):
"value": "3000000000",
"result": 0,
"type": "bytes",
"reset": checks[1]["reset"],
},
)

Expand Down Expand Up @@ -1139,6 +1142,7 @@ def test_user_radius_usage_view(self):
"value": "10800",
"result": 261,
"type": "seconds",
"reset": checks[0]["reset"],
},
)
self.assertDictEqual(
Expand All @@ -1149,6 +1153,7 @@ def test_user_radius_usage_view(self):
"value": "3000000000",
"result": 2000000000,
"type": "bytes",
"reset": checks[1]["reset"],
},
)

Expand Down Expand Up @@ -1177,6 +1182,7 @@ def test_user_radius_usage_view(self):
"value": "10800",
"result": 522,
"type": "seconds",
"reset": checks[0]["reset"],
},
)
self.assertDictEqual(
Expand All @@ -1187,6 +1193,7 @@ def test_user_radius_usage_view(self):
"value": "3000000000",
"result": 3000000000,
"type": "bytes",
"reset": checks[1]["reset"],
},
)

Expand Down Expand Up @@ -1215,6 +1222,7 @@ def test_user_radius_usage_view(self):
"value": "10800",
"result": 783,
"type": "seconds",
"reset": checks[0]["reset"],
},
)
self.assertDictEqual(
Expand All @@ -1225,6 +1233,7 @@ def test_user_radius_usage_view(self):
"value": "3000000000",
"result": 3000000000,
"type": "bytes",
"reset": checks[1]["reset"],
},
)

Expand All @@ -1251,6 +1260,7 @@ def test_user_group_check_serializer_counter_does_not_exist(self):
"result": None,
"type": None,
"value": "2000000000",
"reset": None,
},
)

Expand Down
Loading