Skip to content

Commit

Permalink
add admin and fixtures
Browse files Browse the repository at this point in the history
  • Loading branch information
danangmassandy committed Oct 30, 2024
1 parent 3c86a1e commit 2d33d09
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
34 changes: 33 additions & 1 deletion django_project/gap_api/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
12 changes: 12 additions & 0 deletions django_project/gap_api/fixtures/2.apiratelimiter.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[
{
"model": "gap_api.apiratelimiter",
"pk": 1,
"fields": {
"user": null,
"minute_limit": 100,
"hour_limit": 1000,
"day_limit": 10000
}
}
]

0 comments on commit 2d33d09

Please sign in to comment.