From 0e14adf8918a4ac0c79a0d3bd9356366e6ab652b Mon Sep 17 00:00:00 2001 From: David <9059044+Tansito@users.noreply.github.com> Date: Fri, 23 Aug 2024 11:59:41 -0400 Subject: [PATCH] Admin panel (#1464) * Initial configuration for admin-panel * Remove duplicated configuration * Allow empty values in dependencies M2M field * Refactor urls * Fix tests for permissions * Added new migration * Changed url name * Original migration file recovered * Create a new migration file * Changed url name --- gateway/api/admin.py | 38 +++++++++++++++++-- .../0030_alter_program_instances.py | 19 ++++++++++ gateway/api/models.py | 2 +- gateway/main/settings.py | 5 +-- gateway/main/urls.py | 27 ++++++------- gateway/tests/fixtures/catalog_fixtures.json | 4 +- gateway/tests/fixtures/fixtures.json | 4 +- gateway/tests/fixtures/tasks_fixtures.json | 4 +- 8 files changed, 74 insertions(+), 29 deletions(-) create mode 100644 gateway/api/migrations/0030_alter_program_instances.py diff --git a/gateway/api/admin.py b/gateway/api/admin.py index 8ef79e386..f52962e93 100644 --- a/gateway/api/admin.py +++ b/gateway/api/admin.py @@ -1,19 +1,49 @@ """Admin module.""" from django.contrib import admin -from .models import Job, Program, ComputeResource +from api.models import JobConfig, Provider, Program, ComputeResource, Job, RuntimeJob + + +@admin.register(JobConfig) +class JobConfigAdmin(admin.ModelAdmin): + """JobConfigAdmin.""" + + +@admin.register(Provider) +class ProviderAdmin(admin.ModelAdmin): + """ProviderAdmin.""" + + search_fields = ["name"] @admin.register(Program) class ProgramAdmin(admin.ModelAdmin): """ProgramAdmin.""" + search_fields = ["title", "author__username"] + list_filter = ["provider", "type"] + exclude = ["env_vars"] + + +@admin.register(ComputeResource) +class ComputeResourceAdmin(admin.ModelAdmin): + """ComputeResourceAdmin.""" + + search_fields = ["title", "owner__username"] + list_filter = ["active"] + @admin.register(Job) class JobAdmin(admin.ModelAdmin): """JobAdmin.""" + search_fields = ["author__username", "program__title"] + list_filter = ["status"] + exclude = ["arguments", "env_vars", "logs"] -@admin.register(ComputeResource) -class ComputeResourceAdmin(admin.ModelAdmin): - """ComputeResourceAdmin.""" + +@admin.register(RuntimeJob) +class RuntimeJobAdmin(admin.ModelAdmin): + """RuntimeJobAdmin.""" + + search_fields = ["job__id"] diff --git a/gateway/api/migrations/0030_alter_program_instances.py b/gateway/api/migrations/0030_alter_program_instances.py new file mode 100644 index 000000000..7331fdf15 --- /dev/null +++ b/gateway/api/migrations/0030_alter_program_instances.py @@ -0,0 +1,19 @@ +# Generated by Django 5.1 on 2024-08-21 18:24 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ("api", "0029_program_additional_info_program_documentation_url_and_more"), + ("auth", "0012_alter_user_first_name_max_length"), + ] + + operations = [ + migrations.AlterField( + model_name="program", + name="instances", + field=models.ManyToManyField(blank=True, to="auth.group"), + ), + ] diff --git a/gateway/api/models.py b/gateway/api/models.py index 8110d7a91..e04fae682 100644 --- a/gateway/api/models.py +++ b/gateway/api/models.py @@ -96,7 +96,7 @@ class Program(ExportModelOperationsMixin("program"), models.Model): env_vars = models.TextField(null=False, blank=True, default="{}") dependencies = models.TextField(null=False, blank=True, default="[]") - instances = models.ManyToManyField(Group) + instances = models.ManyToManyField(Group, blank=True) author = models.ForeignKey( settings.AUTH_USER_MODEL, on_delete=models.CASCADE, diff --git a/gateway/main/settings.py b/gateway/main/settings.py index 36da4ee17..2121b2fc8 100644 --- a/gateway/main/settings.py +++ b/gateway/main/settings.py @@ -48,6 +48,7 @@ # Application definition INSTALLED_APPS = [ + "django.contrib.admin", "django.contrib.auth", "django.contrib.contenttypes", "django.contrib.sessions", @@ -71,6 +72,7 @@ "django_prometheus.middleware.PrometheusBeforeMiddleware", "django.middleware.security.SecurityMiddleware", "django.contrib.sessions.middleware.SessionMiddleware", + "django.contrib.auth.middleware.AuthenticationMiddleware", "django.middleware.common.CommonMiddleware", "django.middleware.csrf.CsrfViewMiddleware", "django.contrib.messages.middleware.MessageMiddleware", @@ -273,9 +275,6 @@ "REFRESH_TOKEN_LIFETIME": timedelta(days=20), } -MEDIA_ROOT = os.path.join(sanitize_file_path(str(BASE_DIR)), "media") -MEDIA_URL = "/media/" - # custom token auth SETTINGS_TOKEN_AUTH_URL = os.environ.get("SETTINGS_TOKEN_AUTH_URL", None) SETTINGS_TOKEN_AUTH_USER_FIELD = os.environ.get( diff --git a/gateway/main/urls.py b/gateway/main/urls.py index c4c644596..bfe6b7555 100644 --- a/gateway/main/urls.py +++ b/gateway/main/urls.py @@ -16,6 +16,7 @@ from django.conf import settings from django.conf.urls.static import static +from django.contrib import admin from django.urls import path, include, re_path from rest_framework import routers, permissions from drf_yasg.views import get_schema_view @@ -40,13 +41,18 @@ router = routers.DefaultRouter() +urlpatterns = [ + path("readiness/", probes.views.readiness, name="readiness"), + path("liveness/", probes.views.liveness, name="liveness"), + path("version/", version.views.version, name="version"), + path("", include("django_prometheus.urls")), + path("backoffice/", admin.site.urls), + re_path(r"^api/v1/", include(("api.v1.urls", "api"), namespace="v1")), +] +urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) + if settings.DEBUG: - urlpatterns = [ - path("readiness/", probes.views.readiness, name="readiness"), - path("liveness/", probes.views.liveness, name="liveness"), - path("version/", version.views.version, name="version"), - path("", include("django_prometheus.urls")), - re_path(r"^api/v1/", include(("api.v1.urls", "api"), namespace="v1")), + urlpatterns += [ re_path( r"^swagger(?P\.json|\.yaml)$", schema.without_ui(cache_timeout=0), @@ -62,12 +68,3 @@ ), ] urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) - urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) -else: - urlpatterns = [ - path("readiness/", probes.views.readiness, name="readiness"), - path("liveness/", probes.views.liveness, name="liveness"), - path("version/", version.views.version, name="version"), - path("", include("django_prometheus.urls")), - re_path(r"^api/v1/", include(("api.v1.urls", "api"), namespace="v1")), - ] diff --git a/gateway/tests/fixtures/catalog_fixtures.json b/gateway/tests/fixtures/catalog_fixtures.json index 20a27aeed..43c8a3eca 100644 --- a/gateway/tests/fixtures/catalog_fixtures.json +++ b/gateway/tests/fixtures/catalog_fixtures.json @@ -61,7 +61,7 @@ "pk": 100, "fields": { "name": "ibm-q/open/main", - "permissions": [60] + "permissions": [64] } }, { @@ -69,7 +69,7 @@ "pk": 105, "fields": { "name": "admin-group", - "permissions": [60, 61] + "permissions": [64, 65] } }, { diff --git a/gateway/tests/fixtures/fixtures.json b/gateway/tests/fixtures/fixtures.json index 7eeda1a34..5071fee74 100644 --- a/gateway/tests/fixtures/fixtures.json +++ b/gateway/tests/fixtures/fixtures.json @@ -161,7 +161,7 @@ "pk": 100, "fields": { "name": "runner", - "permissions": [61] + "permissions": [65] } }, { @@ -169,7 +169,7 @@ "pk": 101, "fields": { "name": "viewer", - "permissions": [60] + "permissions": [64] } }, { diff --git a/gateway/tests/fixtures/tasks_fixtures.json b/gateway/tests/fixtures/tasks_fixtures.json index 1a3191285..735a1ec85 100644 --- a/gateway/tests/fixtures/tasks_fixtures.json +++ b/gateway/tests/fixtures/tasks_fixtures.json @@ -17,7 +17,7 @@ "pk": 100, "fields": { "name": "runner", - "permissions": [60] + "permissions": [64] } }, { @@ -25,7 +25,7 @@ "pk": 101, "fields": { "name": "manager", - "permissions": [60] + "permissions": [64] } } ]