From b48305c50d0d6b1674a39e0fd5a3c4768d281a66 Mon Sep 17 00:00:00 2001 From: Chris Hill-Scott Date: Fri, 26 Oct 2018 11:19:50 +0100 Subject: [PATCH] =?UTF-8?q?Don=E2=80=99t=20have=20service=20model=20inheri?= =?UTF-8?q?t=20from=20`dict`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Inheriting from `dict` has some unexpected side effects that don’t happen with plain object. The one we want to avoid right now is that a dict doesn’t seem to implement `__dict__` in a normal way, which is required by `werkzeug.utils.cached_property`. --- app/notify_client/models.py | 22 +++++++++++++++---- .../partials/check/too-many-messages.html | 2 +- app/templates/views/service-settings.html | 4 ++-- 3 files changed, 21 insertions(+), 7 deletions(-) diff --git a/app/notify_client/models.py b/app/notify_client/models.py index 137c21b20..c3bf3749c 100644 --- a/app/notify_client/models.py +++ b/app/notify_client/models.py @@ -268,7 +268,7 @@ class AnonymousUser(AnonymousUserMixin): return False -class Service(dict): +class Service(): ALLOWED_PROPERTIES = { 'active', @@ -291,16 +291,30 @@ class Service(dict): def __init__(self, _dict): # in the case of a bad request current service may be `None` - super().__init__(_dict or {}) + self._dict = _dict or {} + if 'permissions' not in self._dict: + self.permissions = {'email', 'sms', 'letter'} + + def __bool__(self): + return self._dict != {} def __getattr__(self, attr): if attr in self.ALLOWED_PROPERTIES: - return self[attr] + return self._dict[attr] raise AttributeError('`{}` is not a service attribute'.format(attr)) + def __getitem__(self, attr): + return self.__getattr__(attr) + + def get(self, attr, default=None): + try: + return self._dict[attr] + except KeyError: + return default + @property def trial_mode(self): - return self['restricted'] + return self._dict['restricted'] def has_permission(self, permission): return permission in self.permissions diff --git a/app/templates/partials/check/too-many-messages.html b/app/templates/partials/check/too-many-messages.html index b75dbb65f..073f906d7 100644 --- a/app/templates/partials/check/too-many-messages.html +++ b/app/templates/partials/check/too-many-messages.html @@ -7,7 +7,7 @@

You can only send {{ current_service.message_limit }} messages per day - {%- if current_service.restricted %} + {%- if current_service.trial_mode %} in trial mode {%- endif -%} . diff --git a/app/templates/views/service-settings.html b/app/templates/views/service-settings.html index e4b8e16cd..b6aa23ad3 100644 --- a/app/templates/views/service-settings.html +++ b/app/templates/views/service-settings.html @@ -253,7 +253,7 @@ {% endcall %} - {% if current_service.restricted %} + {% if current_service.trial_mode %}

Your service is in trial mode