Skip to content

Commit

Permalink
DBC22-1906: added site settings to diable api endpints
Browse files Browse the repository at this point in the history
  • Loading branch information
ray-oxd authored and bcgov-brwang committed Apr 22, 2024
1 parent 8de79d5 commit 51ba308
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/backend/apps/shared/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from apps.shared.models import SiteSettings
from django.contrib import admin
from django.contrib.admin import ModelAdmin


class SiteSettingsAdmin(ModelAdmin):
readonly_fields = ('id', )


admin.site.register(SiteSettings, SiteSettingsAdmin)
21 changes: 21 additions & 0 deletions src/backend/apps/shared/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Generated by Django 4.2.3 on 2024-04-19 23:14

from django.db import migrations, models


class Migration(migrations.Migration):

initial = True

dependencies = [
]

operations = [
migrations.CreateModel(
name='SiteSettings',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('disable_apis', models.BooleanField(default=False)),
],
),
]
Empty file.
11 changes: 11 additions & 0 deletions src/backend/apps/shared/models.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from django.contrib.gis.db import models
from django.core.exceptions import ValidationError


class BaseModel(models.Model):
Expand All @@ -7,3 +8,13 @@ class BaseModel(models.Model):

class Meta:
abstract = True


class SiteSettings(models.Model):
disable_apis = models.BooleanField(default=False)

def save(self, *args, **kwargs):
if self.__class__.objects.exists() and not self.pk:
raise ValidationError(f"Only one instance of {self.__class__.__name__} allowed")

super().save(*args, **kwargs)
7 changes: 7 additions & 0 deletions src/backend/apps/shared/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@

import environ
from apps.shared.enums import SUBJECT_CHOICES, SUBJECT_TITLE, CacheKey, CacheTimeout
from apps.shared.models import SiteSettings
from django.core.cache import cache
from django.core.exceptions import ImproperlyConfigured
from django.core.mail import send_mail
from django.db import connection
from django.urls import re_path
Expand Down Expand Up @@ -81,6 +83,11 @@ def get_or_set_list_data(self):
)

def list(self, request, *args, **kwargs):
site_settings = SiteSettings.objects.first()
if site_settings:
if site_settings.disable_apis:
raise ImproperlyConfigured("API endpoints disabled for testing")

return Response(self.get_or_set_list_data())


Expand Down

0 comments on commit 51ba308

Please sign in to comment.