-
-
Notifications
You must be signed in to change notification settings - Fork 225
[monitoring/js] Consistent datetime format in RADIUS sessions tab #665
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| from django.utils import formats, timezone | ||
| from rest_framework import serializers | ||
| from swapper import load_model | ||
|
|
||
| RadiusAccounting = load_model("openwisp_radius", "RadiusAccounting") | ||
|
|
||
|
|
||
| class MonitoringRadiusAccountingSerializer(serializers.ModelSerializer): | ||
| """ | ||
| Read-only serializer for RADIUS accounting in monitoring integration | ||
| that formats datetime fields server-side using Django's localization | ||
| for consistency with Django admin datetime formatting. | ||
| """ | ||
|
|
||
| start_time = serializers.SerializerMethodField() | ||
| stop_time = serializers.SerializerMethodField() | ||
|
|
||
| def _format_datetime(self, dt): | ||
| """ | ||
| Format a datetime using Django's localization settings. | ||
| Handles both naive and timezone-aware datetimes. | ||
| """ | ||
| if dt is None: | ||
| return None | ||
| if timezone.is_aware(dt): | ||
| dt = timezone.localtime(dt) | ||
| return formats.date_format(dt, "DATETIME_FORMAT") | ||
|
|
||
| def get_start_time(self, obj): | ||
| """Format start_time using Django's localization settings""" | ||
| return self._format_datetime(obj.start_time) | ||
|
|
||
| def get_stop_time(self, obj): | ||
| """Format stop_time using Django's localization settings""" | ||
| return self._format_datetime(obj.stop_time) | ||
|
|
||
| class Meta: | ||
| model = RadiusAccounting | ||
| fields = [ | ||
| "session_id", | ||
| "unique_id", | ||
| "username", | ||
| "input_octets", | ||
| "output_octets", | ||
| "calling_station_id", | ||
| "called_station_id", | ||
| "start_time", | ||
| "stop_time", | ||
| ] | ||
| read_only_fields = fields | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| from django_filters.rest_framework import DjangoFilterBackend | ||
| from rest_framework.generics import ListAPIView | ||
| from swapper import load_model | ||
|
|
||
| from openwisp_radius.api.freeradius_views import ( | ||
| AccountingFilter, | ||
| AccountingViewPagination, | ||
| ) | ||
| from openwisp_users.api.mixins import FilterByOrganizationManaged, ProtectedAPIMixin | ||
|
|
||
| from .serializers import MonitoringRadiusAccountingSerializer | ||
|
|
||
| RadiusAccounting = load_model("openwisp_radius", "RadiusAccounting") | ||
|
|
||
|
|
||
| class MonitoringAccountingView( | ||
| ProtectedAPIMixin, FilterByOrganizationManaged, ListAPIView | ||
| ): | ||
| """ | ||
| API view for RADIUS accounting in monitoring integration. | ||
| Uses server-side datetime formatting for consistency with Django admin. | ||
| """ | ||
|
|
||
| throttle_scope = "radius_accounting_list" | ||
| serializer_class = MonitoringRadiusAccountingSerializer | ||
| pagination_class = AccountingViewPagination | ||
| filter_backends = (DjangoFilterBackend,) | ||
| filterset_class = AccountingFilter | ||
| queryset = RadiusAccounting.objects.all().order_by("-start_time") | ||
|
Comment on lines
+1
to
+29
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new MonitoringRadiusAccountingSerializer lacks test coverage. The repository has comprehensive API test coverage, and serializers are typically tested alongside their views. Consider adding tests that verify: