diff --git a/django_project/gap_api/admin.py b/django_project/gap_api/admin.py index eac3535..ff752f2 100644 --- a/django_project/gap_api/admin.py +++ b/django_project/gap_api/admin.py @@ -6,15 +6,23 @@ """ import random +import json from django.contrib import admin from django.db.models import Count, TextField from django.db.models.fields.json import KeyTextTransform from django.db.models.functions import TruncDay, Cast +from django.http import HttpResponse +from django.core.serializers.json import DjangoJSONEncoder from rest_framework_tracking.admin import APIRequestLogAdmin from rest_framework_tracking.models import APIRequestLog as BaseAPIRequestLog from gap.models import DatasetType -from gap_api.models import APIRequestLog, DatasetTypeAPIConfig, Location +from gap_api.models import ( + APIRequestLog, + DatasetTypeAPIConfig, + Location, + APIRateLimiter +) admin.site.unregister(BaseAPIRequestLog) @@ -182,6 +190,30 @@ class LocationAdmin(admin.ModelAdmin): list_filter = ('user',) +@admin.action(description='Export rate limiter as json') +def export_rate_limiter_as_json(modeladmin, request, queryset): + """Download rate limiter.""" + fields_to_include = [ + 'pk', 'user_id', 'minute_limit', 'hour_limit', 'day_limit'] + data = list(queryset.all().values(*fields_to_include)) + + # Convert the data to JSON + response_data = json.dumps(data, cls=DjangoJSONEncoder) + + # Create the HttpResponse with the correct content_type for JSON + response = HttpResponse(response_data, content_type='application/json') + response['Content-Disposition'] = 'attachment; filename=rate_limiter.json' + return response + + +class APIRateLimiterAdmin(admin.ModelAdmin): + """Admin class for APIRateLimiter.""" + + list_display = ('config_name', 'minute_limit', 'hour_limit', 'day_limit',) + actions = (export_rate_limiter_as_json,) + + admin.site.register(APIRequestLog, GapAPIRequestLogAdmin) admin.site.register(DatasetTypeAPIConfig, GapAPIDatasetTypeConfigAdmin) admin.site.register(Location, LocationAdmin) +admin.site.register(APIRateLimiter, APIRateLimiterAdmin) diff --git a/django_project/gap_api/fixtures/2.apiratelimiter.json b/django_project/gap_api/fixtures/2.apiratelimiter.json new file mode 100644 index 0000000..e737c1e --- /dev/null +++ b/django_project/gap_api/fixtures/2.apiratelimiter.json @@ -0,0 +1,12 @@ +[ +{ + "model": "gap_api.apiratelimiter", + "pk": 1, + "fields": { + "user": null, + "minute_limit": 100, + "hour_limit": 1000, + "day_limit": 10000 + } +} +]