From cc4f4b521ebd704195507defb221099b83f4b88e Mon Sep 17 00:00:00 2001 From: Sebastien Mirolo Date: Fri, 4 Oct 2024 09:51:25 -0700 Subject: [PATCH] adds button to test 500 error reporting --- Makefile | 40 +++++++++++++--------- djaoapp/api/todos.py | 10 ++++-- djaoapp/templates/rules/app_dashboard.html | 8 ++--- djaoapp/urls/api.py | 4 ++- 4 files changed, 39 insertions(+), 23 deletions(-) diff --git a/Makefile b/Makefile index 30d744a3..5a72f114 100644 --- a/Makefile +++ b/Makefile @@ -5,16 +5,17 @@ APP_NAME ?= $(notdir $(abspath $(srcDir))) APP_PORT ?= 8000 +LIVEDEMO_ENTRY_POINT ?= http://djaopsp-demo srcDir ?= . installTop ?= $(VIRTUAL_ENV) binDir ?= $(installTop)/bin libDir ?= $(installTop)/lib -SYSCONFDIR := $(installTop)/etc -LOCALSTATEDIR := $(installTop)/var -CONFIG_DIR := $(SYSCONFDIR)/$(APP_NAME) -ASSETS_DIR := $(srcDir)/htdocs/assets -LIVEDEMO_ENTRY_POINT ?= http://djaopsp-demo +SYSCONFDIR ?= $(installTop)/etc +LOCALSTATEDIR ?= $(installTop)/var +CONFIG_DIR ?= $(SYSCONFDIR)/$(APP_NAME) +ASSETS_DIR ?= $(srcDir)/htdocs/assets +RUN_DIR ?= $(abspath $(srcDir)) installDirs ?= /usr/bin/install -d installFiles ?= /usr/bin/install -p -m 644 @@ -23,7 +24,7 @@ ESCHECK ?= eslint NPM ?= npm PIP ?= pip PYTHON ?= python -SASSC ?= sassc --style=compressed +SASSC ?= sassc --style=compressed --source-map-urls absolute # As of sqlite3 version 3.42.0 (2023-05-16) we need to pass `-unsafe-testing` # to make adjustments in the demo database. SQLITE_NO_UNSAFE_TESTING := $(shell echo '.schema' | sqlite3 -unsafe-testing > /dev/null 2>&1; echo $$?) @@ -45,13 +46,14 @@ WEBPACK ?= NODE_PATH=$(libDir)/node_modules:$(NODE_PATH) webpack --stats-e MANAGE := DJAOAPP_SETTINGS_LOCATION=$(CONFIG_DIR) $(PYTHON) manage.py RUNSYNCDB = $(if $(findstring --run-syncdb,$(shell cd $(srcDir) && $(MANAGE) migrate --help 2>/dev/null)),--run-syncdb,) + ifneq ($(wildcard $(CONFIG_DIR)/site.conf),) # `make initdb` will install site.conf but only after `grep` is run -# and DB_FILNAME set to "". We use the default value in the template site.conf +# and DB_FILENAME set to "". We use the default value in the template site.conf # here to prevent that issue. -DB_FILENAME := $(shell grep ^DB_NAME $(CONFIG_DIR)/site.conf | cut -f 2 -d '"') +DB_FILENAME ?= $(shell grep ^DB_NAME $(CONFIG_DIR)/site.conf | cut -f 2 -d '"') else -DB_FILENAME ?= $(if $(wildcard $(LOCALSTATEDIR)/db/),$(LOCALSTATEDIR)/db/db.sqlite,$(srcDir)/db.sqlite) +DB_FILENAME ?= $(RUN_DIR)/db.sqlite endif LIVEDEMO_DB_FILENAME := $(srcDir)/db.sqlite MULTITIER_DB_FILENAME := $(dir $(DB_FILENAME))cowork.sqlite @@ -63,7 +65,7 @@ MY_EMAIL ?= $(shell cd $(srcDir) && git config user.email) EMAIL_FIXTURE_OPT := $(if $(MY_EMAIL),--email="$(MY_EMAIL)",) -.PHONY: build-assets doc generateschema initdb makemessages package-docker setup-livedemo vendor-assets-prerequisites +.PHONY: build-assets doc generateschema initdb makemessages setup-livedemo vendor-assets-prerequisites all: @echo "Nothing to be done for 'make'." @@ -120,13 +122,17 @@ makemessages: cd $(srcDir) && $(MANAGE) makemessages -l fr -l es -l pt --symlinks --no-wrap cd $(srcDir) && $(MANAGE) makemessages -d djangojs -l fr -l es -l pt --symlinks --no-wrap -# !!! Attention !!! -# You will need to run `make initdb` or `make setup-livedemo` before -# running `package-docker` in order to create the dummy db.sqilte file -# to package. -package-docker: build-assets +ifeq ($(MY_EMAIL),) + +.PHONY: package-docker + +# We build a local sqlite3 database to be packaged with the Docker image +# such that the container can be started without prior configuration. +package-docker: build-assets initdb + [[ -f $(srcDir)/db.sqlite ]] || cp $(DB_FILENAME) $(srcDir)/db.sqlite cd $(srcDir) && $(DOCKER) build $(DOCKER_OPTS) . +endif # download and install prerequisites then create the db. require: require-pip require-resources initdb @@ -364,7 +370,8 @@ $(DESTDIR)$(CONFIG_DIR)/site.conf: $(srcDir)/etc/site.conf -e 's,%(APP_NAME)s,$(APP_NAME),' \ -e "s,%(ADMIN_EMAIL)s,$(MY_EMAIL)," \ -e 's,%(installTop)s,$(installTop),' \ - -e "s,%(DB_FILENAME)s,$(abspath $(DB_FILENAME))," \ + -e "s,%(DB_NAME)s,$(APP_NAME)," \ + -e "s,%(DB_FILENAME)s,$(DB_FILENAME)," \ -e "s,%(binDir)s,$(binDir)," $< > $@ $(DESTDIR)$(CONFIG_DIR)/credentials: $(srcDir)/etc/credentials @@ -404,6 +411,7 @@ $(DESTDIR)$(SYSCONFDIR)/monit.d/%: $(srcDir)/etc/monit.conf $(installDirs) $(dir $@) [ -e $@ ] || sed \ -e 's,%(APP_NAME)s,$(APP_NAME),g' \ + -e 's,%(APP_PORT)s,$(APP_NAME),g' \ -e 's,%(LOCALSTATEDIR)s,$(LOCALSTATEDIR),' $< > $@ $(DESTDIR)$(SYSCONFDIR)/sysconfig/%: $(srcDir)/etc/sysconfig.conf diff --git a/djaoapp/api/todos.py b/djaoapp/api/todos.py index ade0ae04..68556744 100644 --- a/djaoapp/api/todos.py +++ b/djaoapp/api/todos.py @@ -1,4 +1,4 @@ -# Copyright (c) 2021, DjaoDjin inc. +# Copyright (c) 2024, DjaoDjin inc. # see LICENSE from __future__ import unicode_literals @@ -8,7 +8,7 @@ from django.utils.decorators import method_decorator from django.views.decorators.csrf import ensure_csrf_cookie from rest_framework import status -from rest_framework.generics import ListAPIView, RetrieveAPIView +from rest_framework.generics import CreateAPIView, ListAPIView, RetrieveAPIView from rest_framework.response import Response from saas.mixins import ProviderMixin from signup.serializers import ActivitySerializer @@ -54,6 +54,12 @@ def get(self, request, *args, **kwargs): return Response(serializer.data, status=status.HTTP_200_OK) +class GenerateErrorAPIView(ProviderMixin, CreateAPIView): + + def create(self, request, *args, **kwargs): + raise RuntimeError("Testing: Generated Error") + + def list_todos(request, provider=None): #pylint:disable=unused-argument return [] diff --git a/djaoapp/templates/rules/app_dashboard.html b/djaoapp/templates/rules/app_dashboard.html index 7eb5b344..cf0e7e2e 100644 --- a/djaoapp/templates/rules/app_dashboard.html +++ b/djaoapp/templates/rules/app_dashboard.html @@ -302,16 +302,14 @@

{% trans %}Testing{% endtrans %}

- {% if FEATURES_DEBUG %}
-
+
- {% endif %} @@ -344,10 +342,12 @@

{% trans %}Testing{% endtrans %}

jQuery(document).ready(function($) { $("#error500").click(function (evt) { + var self = this; + //const url = self.parents('form').attr('action'); evt.preventDefault(); jQuery.ajax({ method: 'POST', - url: '/api', + url: "{{'/api/proxy/generate-error'|site_url}}", beforeSend: function(xhr, settings) { if( !(/^(GET|HEAD|OPTIONS|TRACE)$/.test(settings.type)) ) { var csrfToken = getCSRFToken(); diff --git a/djaoapp/urls/api.py b/djaoapp/urls/api.py index 45d516fc..6265594b 100644 --- a/djaoapp/urls/api.py +++ b/djaoapp/urls/api.py @@ -14,7 +14,7 @@ from ..api.organizations import (DjaoAppProfileDetailAPIView, DjaoAppProfileListAPIView, DjaoAppProfilePictureAPIView) from ..api.roles import DjaoAppRoleByDescrListAPIView -from ..api.todos import DjaoAppAPIVersion, TodosAPIView +from ..api.todos import DjaoAppAPIVersion, TodosAPIView, GenerateErrorAPIView from ..api.users import (RecentActivityAPIView, DjaoAppUserDetailAPIView, DjaoAppUserNotificationsAPIView, DjaoAppUserOTPAPIView) from ..urlbuilders import (url_authenticated, url_direct, @@ -35,6 +35,8 @@ NotificationAPIView.as_view(), name='api_notification_base'), url_direct(r'^api/proxy/recent', RecentActivityAPIView.as_view(), name='api_recent_activity'), + url_direct(r'^api/proxy/generate-error', + GenerateErrorAPIView.as_view(), name='api_generate_error'), url_direct(r'^api/', include('rules.urls.api.proxy')), url_direct(r'^api/themes$', DjaoAppThemePackageListAPIView.as_view(),