Error handling

This commit is contained in:
Kumi 2019-04-18 13:59:06 +00:00
parent 344d48a6f8
commit df681072e2
5 changed files with 43 additions and 6 deletions

View file

@ -7,5 +7,12 @@ register = template.Library()
@register.inclusion_tag('manager/notifications.html', takes_context=True) @register.inclusion_tag('manager/notifications.html', takes_context=True)
def getNotifications(context, login=False): def getNotifications(context, login=False):
return { 'notifications': [notification for notification in Notification.objects.filter(Q(expiry__gte = timezone.now()) | Q(expiry = None)) if (notification.on_login or (not login)) ] } notifications = [notification for notification in Notification.objects.filter(Q(expiry__gte = timezone.now()) | Q(expiry = None)) if (notification.on_login or (not login)) ]
if context.dicts[3]["exception"]:
exception = Notification()
exception.status = 2
exception.text = context.dicts[3]["exception"].human
if context.dicts[3]["user"].is_superuser:
exception.text += "<br><br><code>" + str(context.dicts[3]["exception"]) + "</code>"
notifications = [ exception ] + notifications
return { 'notifications': notifications }

View file

@ -135,8 +135,8 @@ def ping(request, device_id):
return HttpResponse(ajax, content_type="application/json") return HttpResponse(ajax, content_type="application/json")
@login_required @login_required
def devices(request): def devices(request, code=200, exception=None):
return render(request, "manager/index.html", {"title": "Device Administration"}) return render(request, "manager/index.html", {"title": "Device Administration", "exception": exception}, status=code)
@login_required @login_required
def editdevice(request, device_id): def editdevice(request, device_id):
@ -479,3 +479,26 @@ def saveUserStatus(sender, instance, **kwargs):
except: except:
UserStatus.objects.create(user=instance) UserStatus.objects.create(user=instance)
def errorhandler(request, code, exception):
if code == 403:
exception.human = "You have no permission to access the requested page."
elif code == 400:
exception.human = "Something seems to be wrong with the request you sent. Please try again."
elif code == 404:
exception.human = "Sorry, we were unable to find the page you requested. Please check the address and try again."
elif code == 500:
exception.human = "Sorry, something seems to be wrong on our end. Please try again later or contact support if the problem persists."
return devices(request, code=code, exception=exception)
def handler400(request, exception):
return errorhandler(request, 400, exception)
def handler403(request, exception):
return errorhandler(request, 403, exception)
def handler404(request, exception):
return errorhandler(request, 404, exception)
def handler500(request):
return errorhandler(request, 500, Exception("Please check the server logs for additional information."))

View file

@ -15,8 +15,10 @@
&dash; <a id="linkorgas" href="#" onclick="showorgas();">Organizations</a> &dash; <a id="linkorgas" href="#" onclick="showorgas();">Organizations</a>
{% endif %} {% endif %}
</div> </div>
<p></p>
{% autoescape off %}
{% getNotifications %} {% getNotifications %}
{% endautoescape %}
<div name="devicespart" id="devicespart"> <div name="devicespart" id="devicespart">
<h2>Devices</h2> <h2>Devices</h2>

View file

@ -7,7 +7,7 @@ BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
SECRET_KEY = '=go8&h#^kh6ksr11e2z-@4qqd6t%63$x#-!s#l_yhw@oyanrys' SECRET_KEY = '=go8&h#^kh6ksr11e2z-@4qqd6t%63$x#-!s#l_yhw@oyanrys'
# SECURITY WARNING: don't run with debug turned on in production! # SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True DEBUG = False
ALLOWED_HOSTS = ["admin360.kumi.host", "test360.kumi.host"] ALLOWED_HOSTS = ["admin360.kumi.host", "test360.kumi.host"]

View file

@ -19,3 +19,8 @@ urlpatterns = [
path('admin/', admin.site.urls), path('admin/', admin.site.urls),
path('accounts/', include('django.contrib.auth.urls')) path('accounts/', include('django.contrib.auth.urls'))
] ]
handler400 = "manager.views.handler400"
handler403 = "manager.views.handler403"
handler404 = "manager.views.handler404"
handler500 = "manager.views.handler500"