From 8f8bb19fff3a6974bdbc86060fe3f5e4539d02b8 Mon Sep 17 00:00:00 2001 From: "Hassan D. M. Sambo" Date: Mon, 15 Jul 2024 15:01:37 -0400 Subject: [PATCH 1/2] Bug fix to allow filtering by waiver types from django admin console (#4088) --- backend/audit/admin.py | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/backend/audit/admin.py b/backend/audit/admin.py index 5073cc9601..3b03609331 100644 --- a/backend/audit/admin.py +++ b/backend/audit/admin.py @@ -14,6 +14,8 @@ validate_auditee_certification_json, validate_auditor_certification_json, ) +from django.contrib.admin import SimpleListFilter +from django.utils.translation import gettext_lazy as _ class SACAdmin(admin.ModelAdmin): @@ -93,6 +95,26 @@ class SubmissionEventAdmin(admin.ModelAdmin): search_fields = ("sac__report_id", "user__username") +class WaiverTypesFilter(SimpleListFilter): + title = _("Waiver Types") + parameter_name = "waiver_types" + + def lookups(self, request, model_admin): + waiver_types = set( + [ + waiver_type + for waiver in SacValidationWaiver.objects.all() + for waiver_type in waiver.waiver_types + ] + ) + return [(waiver_type, waiver_type) for waiver_type in waiver_types] + + def queryset(self, request, queryset): + if self.value(): + return queryset.filter(waiver_types__contains=[self.value()]) + return queryset + + class SacValidationWaiverAdmin(admin.ModelAdmin): form = SacValidationWaiverForm list_display = ( @@ -101,7 +123,7 @@ class SacValidationWaiverAdmin(admin.ModelAdmin): "approver_email", "requester_email", ) - list_filter = ("timestamp", "waiver_types") + list_filter = ("timestamp", WaiverTypesFilter) search_fields = ( "report_id__report_id", "approver_email", From 29eb254217b70b8a6fe5b9eb080ee6ac1a7571e9 Mon Sep 17 00:00:00 2001 From: "Hassan D. M. Sambo" Date: Mon, 15 Jul 2024 16:30:56 -0400 Subject: [PATCH 2/2] Added logic to set db url from vcap services (#4082) --- backend/config/db_url.py | 17 +++++++++++++++++ backend/config/settings.py | 7 ++++++- 2 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 backend/config/db_url.py diff --git a/backend/config/db_url.py b/backend/config/db_url.py new file mode 100644 index 0000000000..ce57d7bed7 --- /dev/null +++ b/backend/config/db_url.py @@ -0,0 +1,17 @@ +from django.core.exceptions import ImproperlyConfigured + + +def get_db_url_from_vcap_services( + vcap, +): + database_url = None + for db_service in vcap.get("aws-rds", []): + if db_service.get("instance_name") == "fac-db": + database_url = db_service["credentials"]["uri"] + break + + if not database_url: + raise ImproperlyConfigured( + "Database URL is not properly configured. Expected 'fac-db' URL." + ) + return database_url diff --git a/backend/config/settings.py b/backend/config/settings.py index 5775d4042a..94158fcadb 100644 --- a/backend/config/settings.py +++ b/backend/config/settings.py @@ -15,10 +15,11 @@ import sys import logging import json +from .db_url import get_db_url_from_vcap_services import environs from cfenv import AppEnv from audit.get_agency_names import get_agency_names, get_audit_info_lists - +import dj_database_url import newrelic.agent newrelic.agent.initialize() @@ -284,6 +285,10 @@ STATICFILES_STORAGE = "storages.backends.s3boto3.S3ManifestStaticStorage" DEFAULT_FILE_STORAGE = "report_submission.storages.S3PrivateStorage" vcap = json.loads(env.str("VCAP_SERVICES")) + + DB_URL = get_db_url_from_vcap_services(vcap) + DATABASES = {"default": dj_database_url.parse(DB_URL)} + for service in vcap["s3"]: if service["instance_name"] == "fac-public-s3": # Public AWS S3 bucket for the app