Don’t have service model inherit from dict

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`.
This commit is contained in:
Chris Hill-Scott
2018-10-30 14:55:00 +00:00
parent 1e6b79a546
commit b48305c50d
3 changed files with 21 additions and 7 deletions
+18 -4
View File
@@ -268,7 +268,7 @@ class AnonymousUser(AnonymousUserMixin):
return False return False
class Service(dict): class Service():
ALLOWED_PROPERTIES = { ALLOWED_PROPERTIES = {
'active', 'active',
@@ -291,16 +291,30 @@ class Service(dict):
def __init__(self, _dict): def __init__(self, _dict):
# in the case of a bad request current service may be `None` # 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): def __getattr__(self, attr):
if attr in self.ALLOWED_PROPERTIES: if attr in self.ALLOWED_PROPERTIES:
return self[attr] return self._dict[attr]
raise AttributeError('`{}` is not a service attribute'.format(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 @property
def trial_mode(self): def trial_mode(self):
return self['restricted'] return self._dict['restricted']
def has_permission(self, permission): def has_permission(self, permission):
return permission in self.permissions return permission in self.permissions
@@ -7,7 +7,7 @@
</h1> </h1>
<p> <p>
You can only send {{ current_service.message_limit }} messages per day You can only send {{ current_service.message_limit }} messages per day
{%- if current_service.restricted %} {%- if current_service.trial_mode %}
in <a href="{{ url_for('.using_notify')}}#trial-mode">trial mode</a> in <a href="{{ url_for('.using_notify')}}#trial-mode">trial mode</a>
{%- endif -%} {%- endif -%}
. .
+2 -2
View File
@@ -253,7 +253,7 @@
{% endcall %} {% endcall %}
</div> </div>
{% if current_service.restricted %} {% if current_service.trial_mode %}
<h2 class="heading-medium">Your service is in trial mode</h2> <h2 class="heading-medium">Your service is in trial mode</h2>
<ul class='list list-bullet'> <ul class='list list-bullet'>
@@ -334,7 +334,7 @@
<ul> <ul>
<li class="bottom-gutter"> <li class="bottom-gutter">
<a href="{{ url_for('.service_switch_live', service_id=current_service.id) }}" class="button"> <a href="{{ url_for('.service_switch_live', service_id=current_service.id) }}" class="button">
{{ 'Make service live' if current_service.restricted else 'Revert service to trial mode' }} {{ 'Make service live' if current_service.trial_mode else 'Revert service to trial mode' }}
</a> </a>
</li> </li>
<li class="bottom-gutter"> <li class="bottom-gutter">