Skip to content

[change:api] Include reset period in UserRadiusUsageView response #669

@nemesifier

Description

@nemesifier

Related to openwisp/openwisp-wifi-login-pages#863.

The goal of this ticket is to modify OpenWISP RADIUS to return the counter reset period in the API.

Relevant parts of the code:

@method_decorator(
name="get",
decorator=swagger_auto_schema(
operation_description="""
**Requires the user auth token (Bearer Token).**
Returns the user's RADIUS usage and limit for the organization.
It executes the relevant counters and returns returns information that
shows how much time and/or traffic the user has consumed.
""",
),
)
class UserRadiusUsageView(ThrottledAPIMixin, DispatchOrgMixin, RetrieveAPIView):
authentication_classes = (BearerAuthentication, SessionAuthentication)
permission_classes = (IsAuthenticated,)
queryset = User.objects.none()
serializer_class = UserRadiusUsageSerializer
def get_object(self):
return self.request.user

class UserGroupCheckSerializer(serializers.ModelSerializer):
result = serializers.SerializerMethodField()
type = serializers.SerializerMethodField()
class Meta:
model = RadiusGroupCheck
fields = ("attribute", "op", "value", "result", "type")
def get_result(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,
)
consumed = counter.consumed()
value = int(obj.value)
if consumed > value:
consumed = value
return consumed
except (SkipCheck, ValueError, KeyError):
return None

resets = {
"daily": _daily,
"weekly": _weekly,
"monthly": _monthly,
"monthly_subscription": _monthly_subscription,
"never": _never,
}

I would add a reset property in the API response which returns the date time of the next reset, this means we need to use the "reset" attribute to calculate it.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions