Files
Hassan Raza 033bcda99d Hraza/add embargo restricted country (#36398)
* feat: add country disabling feature in embargo app

* revert: disabled countries list in env

* fix: resolved linter issues

---------

Co-authored-by: Hassan Raza <h.raza@192.168.1.12>
2025-03-19 12:22:45 +05:00

61 lines
1.8 KiB
Python

"""
Django admin page for embargo models
"""
import textwrap
from config_models.admin import ConfigurationModelAdmin
from django.contrib import admin
from .forms import IPFilterForm, RestrictedCourseForm
from .models import CountryAccessRule, GlobalRestrictedCountry, IPFilter, RestrictedCourse
class IPFilterAdmin(ConfigurationModelAdmin):
"""Admin for blacklisting/whitelisting specific IP addresses"""
form = IPFilterForm
fieldsets = (
(None, {
'fields': ('enabled', 'whitelist', 'blacklist'),
'description': textwrap.dedent("""Enter specific IP addresses to explicitly
whitelist (not block) or blacklist (block) in the appropriate box below.
Separate IP addresses with a comma. Do not surround with quotes.
""")
}),
)
class CountryAccessRuleInline(admin.StackedInline):
"""Inline editor for country access rules. """
model = CountryAccessRule
extra = 1
def has_delete_permission(self, request, obj=None):
return True
class RestrictedCourseAdmin(admin.ModelAdmin):
"""Admin for configuring course restrictions. """
inlines = [CountryAccessRuleInline]
form = RestrictedCourseForm
search_fields = ('course_key',)
class GlobalRestrictedCountryAdmin(admin.ModelAdmin):
"""
Admin configuration for the Global Country Restriction model.
"""
list_display = ("country",)
def delete_queryset(self, request, queryset):
"""
Override the delete_queryset method to clear the cache when objects are deleted in bulk.
"""
super().delete_queryset(request, queryset)
GlobalRestrictedCountry.update_cache()
admin.site.register(IPFilter, IPFilterAdmin)
admin.site.register(RestrictedCourse, RestrictedCourseAdmin)
admin.site.register(GlobalRestrictedCountry, GlobalRestrictedCountryAdmin)