diff --git a/circle/common/models.py b/circle/common/models.py index 2bda712..74179bf 100644 --- a/circle/common/models.py +++ b/circle/common/models.py @@ -23,6 +23,7 @@ from logging import getLogger from time import time from warnings import warn +from django.contrib import messages from django.contrib.auth.models import User from django.core.cache import cache from django.core.serializers.json import DjangoJSONEncoder @@ -407,10 +408,17 @@ create_readable = HumanReadableObject.create class HumanReadableException(HumanReadableObject, Exception): """HumanReadableObject that is an Exception so can used in except clause. """ - pass + level = "error" + + def send_message(self, request): + if request.user and request.user.is_superuser: + msg = self.get_admin_text() + else: + msg = self.get_user_text() + getattr(messages, self.level)(request, msg) -def humanize_exception(message, exception=None, **params): +def humanize_exception(message, exception=None, level=None, **params): """Return new dynamic-class exception which is based on HumanReadableException and the original class with the dict of exception. @@ -420,6 +428,9 @@ def humanize_exception(message, exception=None, **params): Welcome! """ + if level is not None: + exception.level = level + Ex = type("HumanReadable" + type(exception).__name__, (HumanReadableException, type(exception)), exception.__dict__)