Refactor to avoid direct string comparison

Direct string comparison in multiple places is prone to typos. It
also means that a consumer of the class needs to know that whether
a user is pending or active is held in the `state` property, which
is an implementation detail.
This commit is contained in:
Chris Hill-Scott
2022-05-04 16:44:59 +01:00
parent 5ac6efc580
commit c5d4bfd8ef

View File

@@ -151,7 +151,7 @@ class User(JSONModel, UserMixin):
return session.get('current_session_id') != self.current_session_id
def activate(self):
if self.state == 'pending':
if self.is_pending:
user_data = user_api_client.activate_user(self.id)
return self.__class__(user_data['data'])
else:
@@ -192,6 +192,10 @@ class User(JSONModel, UserMixin):
def is_active(self):
return self.state == 'active'
@property
def is_pending(self):
return self.state == 'pending'
@property
def is_gov_user(self):
return is_gov_user(self.email_address)
@@ -443,7 +447,7 @@ class User(JSONModel, UserMixin):
def is_editable_by(self, other_user):
if other_user == self:
return False
if self.state == 'active':
if self.is_active:
return True
return False