From 845f8749226cb343109306535dc0b863fd75bf4a Mon Sep 17 00:00:00 2001
From: Tom Teichler <tom.teichler@teckids.org>
Date: Wed, 21 Aug 2019 00:20:03 +0200
Subject: [PATCH] Add custom error pages. Closes #17.

---
 biscuit/core/locale/de/LC_MESSAGES/django.po | 115 ++++++++++++-------
 biscuit/core/settings.py                     |   2 +
 biscuit/core/templates/error.html            |  11 ++
 biscuit/core/urls.py                         |   5 +
 biscuit/core/views.py                        |  30 +++++
 5 files changed, 124 insertions(+), 39 deletions(-)
 create mode 100644 biscuit/core/templates/error.html

diff --git a/biscuit/core/locale/de/LC_MESSAGES/django.po b/biscuit/core/locale/de/LC_MESSAGES/django.po
index 274f08161..be43c608e 100644
--- a/biscuit/core/locale/de/LC_MESSAGES/django.po
+++ b/biscuit/core/locale/de/LC_MESSAGES/django.po
@@ -7,8 +7,8 @@ msgid ""
 msgstr ""
 "Project-Id-Version: BiscuIT - School Information System (SIS)\n"
 "Report-Msgid-Bugs-To: \n"
-"POT-Creation-Date: 2019-08-19 00:10+0200\n"
-"PO-Revision-Date: 2019-08-19 00:11+0200\n"
+"POT-Creation-Date: 2019-08-21 01:06+0200\n"
+"PO-Revision-Date: 2019-08-21 01:06+0200\n"
 "Last-Translator: Tom Teichler <tom.teichler@teckids.org>\n"
 "Language-Team: \n"
 "Language: de\n"
@@ -18,116 +18,116 @@ msgstr ""
 "Plural-Forms: nplurals=2; plural=(n != 1);\n"
 "X-Generator: Poedit 2.2.3\n"
 
-#: biscuit/core/migrations/0007_school.py:11
+#: migrations/0007_school.py:11
 msgid "Default school"
 msgstr "Standardschule"
 
-#: biscuit/core/models.py:17
+#: models.py:17
 msgid "Name"
 msgstr "Name"
 
-#: biscuit/core/models.py:19
+#: models.py:19
 msgid "Official name of the school, e.g. as given by supervisory authority"
-msgstr "Offizieller Name der Schule, wie er z.B. von der Behörde vorgegeben ist"
+msgstr ""
+"Offizieller Name der Schule, wie er z.B. von der Behörde vorgegeben ist"
 
-#: biscuit/core/models.py:31
+#: models.py:31
 msgid "female"
 msgstr "weiblich"
 
-#: biscuit/core/models.py:32
+#: models.py:32
 msgid "male"
 msgstr "männlich"
 
-#: biscuit/core/models.py:39
+#: models.py:39
 msgid "Is person active?"
 msgstr "Ist die Person aktiv?"
 
-#: biscuit/core/models.py:41
+#: models.py:41
 msgid "First name"
 msgstr "Vorname"
 
-#: biscuit/core/models.py:42
+#: models.py:42
 msgid "Last name"
 msgstr "Nachname"
 
-#: biscuit/core/models.py:44
+#: models.py:44
 msgid "Additional name(s)"
 msgstr "Zusätzliche Namen"
 
-#: biscuit/core/models.py:47
+#: models.py:47
 msgid "Short name"
 msgstr "Kurzname"
 
-#: biscuit/core/models.py:50
+#: models.py:50
 msgid "Street"
 msgstr "Straße"
 
-#: biscuit/core/models.py:52
+#: models.py:52
 msgid "Street number"
 msgstr "Hausnummer"
 
-#: biscuit/core/models.py:54
+#: models.py:54
 msgid "Postal code"
 msgstr "Postleitzahl"
 
-#: biscuit/core/models.py:56
+#: models.py:56
 msgid "Place"
 msgstr "Ort"
 
-#: biscuit/core/models.py:58
+#: models.py:58
 msgid "Home phone"
 msgstr "Festnetz"
 
-#: biscuit/core/models.py:60
+#: models.py:60
 msgid "Mobile phone"
 msgstr "Handy"
 
-#: biscuit/core/models.py:62
+#: models.py:62
 msgid "E-mail address"
 msgstr "E-Mail-Adresse"
 
-#: biscuit/core/models.py:65
+#: models.py:65
 msgid "Date of birth"
 msgstr "Geburtsdatum"
 
-#: biscuit/core/models.py:67
+#: models.py:67
 msgid "Sex"
 msgstr "Geschlecht"
 
-#: biscuit/core/models.py:69
+#: models.py:69
 msgid "Photo"
 msgstr "Foto"
 
-#: biscuit/core/models.py:72
+#: models.py:72
 msgid "Reference ID of import source"
 msgstr "Referenz-ID der Import-Quelle"
 
-#: biscuit/core/models.py:74
+#: models.py:74
 msgid "Guardians / Parents"
 msgstr "Erziehungsberechtigte / Eltern"
 
-#: biscuit/core/models.py:90
+#: models.py:90
 msgid "Long name of group"
 msgstr "Langer Name der Gruppe"
 
-#: biscuit/core/models.py:92
+#: models.py:92
 msgid "Short name of group"
 msgstr "Kurzer Name der Gruppe"
 
-#: biscuit/core/templates/core/group_full.html:10
+#: templates/core/group_full.html:10
 msgid "Edit group"
 msgstr "Gruppe editiern"
 
-#: biscuit/core/templates/core/group_full.html:13
+#: templates/core/group_full.html:13
 msgid "Details"
 msgstr "Details"
 
-#: biscuit/core/templates/core/group_full.html:26
-#: biscuit/core/templates/core/person_full.html:64
+#: templates/core/group_full.html:26 templates/core/person_full.html:64
 msgid "Person not found"
 msgstr "Person nicht gefunden"
 
-#: biscuit/core/templates/core/group_full.html:29
+#: templates/core/group_full.html:29
 msgid ""
 "\n"
 "    There is no group with this id.\n"
@@ -137,23 +137,23 @@ msgstr ""
 "    Es existiert keine Gruppe mit dieser ID.\n"
 "   "
 
-#: biscuit/core/templates/core/groups.html:5
+#: templates/core/groups.html:5
 msgid "Groups"
 msgstr "Gruppen"
 
-#: biscuit/core/templates/core/groups.html:9
+#: templates/core/groups.html:9
 msgid "List of all groups"
 msgstr "Liste aller Gruppen"
 
-#: biscuit/core/templates/core/person_full.html:10
+#: templates/core/person_full.html:10
 msgid "Edit person"
 msgstr "Person editiern"
 
-#: biscuit/core/templates/core/person_full.html:13
+#: templates/core/person_full.html:13
 msgid "Contact details"
 msgstr "Kontaktdetails"
 
-#: biscuit/core/templates/core/person_full.html:67
+#: templates/core/person_full.html:67
 msgid ""
 "\n"
 "    There is no person with this id.\n"
@@ -163,13 +163,50 @@ msgstr ""
 "    Es existiert keine Person mit dieser ID.\n"
 "   "
 
-#: biscuit/core/templates/core/persons.html:5
+#: templates/core/persons.html:5
 msgid "Persons"
 msgstr "Personen"
 
-#: biscuit/core/templates/core/persons.html:9
+#: templates/core/persons.html:9
 msgid "List of all persons"
 msgstr "Liste aller Personen"
 
+#: templates/error.html:5
+msgid "Oops!"
+msgstr "Ups!"
+
+#: views.py:23
+msgid ""
+"This page does not exist. If you were redirected by a link on an external "
+"page, it would be possible that the link was wrong. But it you were "
+"redirected to this page by a link on this site, please contact"
+msgstr ""
+"Diese Seite existiert leider nicht. Sollten Sie durch einen Link von einer "
+"externen Seite hierher gelangt sein, kann es sein, dass dieser falsch ist. "
+"Sollten Sie jedoch durch einen Link auf dieser Seite hierher gelangt sein, "
+"kontaktieren Sie bitte umgehend"
+
+#: views.py:38
+msgid ""
+"An unexpected error has occurred. This is probably due a bug in BiscuIT. "
+"Please contact"
+msgstr ""
+"Ein unerwarteter Fehler ist aufgetreten. Das liegt vermutlich an einem Bug "
+"in BiscuIT. Bitte kontaktieren Sie"
+
+#~ msgid ""
+#~ "\n"
+#~ "   Something went wrong. Please contact <a href=\"mailto:"
+#~ "schulsupport@teckids.org\">schulsupport@teckids.org</a>.\n"
+#~ "   "
+#~ msgstr ""
+#~ "\n"
+#~ "   Etwas ist schiefgelaufen. Bitte kontaktieren Sie <a href=\"mailto:"
+#~ "schulsupport@teckids.org\">schulsupport@teckids.org</a>.\n"
+#~ "   "
+
+#~ msgid "Oops! [%(error)s]"
+#~ msgstr "Ups! [%(error)s]"
+
 #~ msgid "Interfaces"
 #~ msgstr "Schnittstellen"
diff --git a/biscuit/core/settings.py b/biscuit/core/settings.py
index 63ae76df4..6ffb3b74e 100644
--- a/biscuit/core/settings.py
+++ b/biscuit/core/settings.py
@@ -189,4 +189,6 @@ COLOUR_DANGER = _settings.get('theme.colours.danger', '#dc3545')
 COLOUR_LIGHT = _settings.get('theme.colours.light', '#f8f9fa')
 COLOUR_DARK = _settings.get('theme.colours.dark', '#343a40')
 
+ADMINS = _settings.get('admins', [('John Doe', 'mail@example.com')])
+
 _settings.populate_obj(sys.modules[__name__])
diff --git a/biscuit/core/templates/error.html b/biscuit/core/templates/error.html
new file mode 100644
index 000000000..0f1047efe
--- /dev/null
+++ b/biscuit/core/templates/error.html
@@ -0,0 +1,11 @@
+{% extends "core/base.html" %}
+{% load bootstrap4 i18n staticfiles %}
+
+{% block content %}
+ <h1>{% blocktrans %}Oops!{% endblocktrans %} [{{ error }}]</h1>
+ <div class="jumbotron">
+  <p>
+   {{ message }} <a href="mailto:{{ admins[1] }}">{{ admins[0] }}</a>.
+  </p>
+ </div>
+{% endblock %}
diff --git a/biscuit/core/urls.py b/biscuit/core/urls.py
index 47e3dcba8..64f939e45 100644
--- a/biscuit/core/urls.py
+++ b/biscuit/core/urls.py
@@ -21,6 +21,11 @@ urlpatterns = [
     path('', views.index, name='index'),
 ]
 
+# Custom error pages
+
+handler404 = views.handler404
+handler500 = views.handler500
+
 # Serve javascript-common if in development
 if settings.DEBUG:
     urlpatterns += static('/javascript/',
diff --git a/biscuit/core/views.py b/biscuit/core/views.py
index 9eea88d56..d63cde23b 100644
--- a/biscuit/core/views.py
+++ b/biscuit/core/views.py
@@ -11,6 +11,36 @@ def index(request):
     return render(request, 'core/index.html', context)
 
 
+def handler404(request, exception):
+    context = {}
+
+    error = 404
+    context['error'] = error
+
+    admins = settings.ADMINS
+    context['admins'] = admins
+
+    message = _('This page does not exist. If you were redirected by a link on an external page, it would be possible that the link was wrong. But it you were redirected to this page by a link on this site, please contact')
+    context['message'] = message
+
+    return render(request, 'error.html', context, status=404)
+
+
+def handler500(request):
+    context = {}
+
+    error = 500
+    context['error'] = error
+
+    admins = settings.ADMINS
+    context['admins'] = admins
+
+    message = _('An unexpected error has occurred. This is probably due a bug in BiscuIT. Please contact')
+    context['message'] = message
+
+    return render(request, 'error.html', context, status=500)
+
+
 @login_required
 def persons(request):
     context = {}
-- 
GitLab