Make property methods into proper properties

Since these methods only take `self` as an argument they can be
properties. And this means we don’t need to follow the semi-convention
we have of indicating an attribute is a method by starting its name with
`get_`.

The other advantage of using `@property` to indicate a getter is that it
will raise an exception if someone tries to set the attribute, eg by
doing `contact_list.has_jobs = False`. This is because we (rightly)
haven’t defined a setter.
This commit is contained in:
Chris Hill-Scott
2020-07-21 15:12:44 +01:00
parent dbbff3ba64
commit 313b69f95c

View File

@@ -2122,7 +2122,8 @@ class ServiceContactList(db.Model):
updated_at = db.Column(db.DateTime, nullable=True, onupdate=datetime.datetime.utcnow)
archived = db.Column(db.Boolean, nullable=False, default=False)
def get_job_count(self):
@property
def job_count(self):
today = datetime.datetime.utcnow().date()
return Job.query.filter(
Job.contact_list_id == self.id,
@@ -2136,7 +2137,8 @@ class ServiceContactList(db.Model):
)
).count()
def get_has_jobs(self):
@property
def has_jobs(self):
return bool(Job.query.filter(
Job.contact_list_id == self.id,
).first())
@@ -2147,8 +2149,8 @@ class ServiceContactList(db.Model):
"id": str(self.id),
"original_file_name": self.original_file_name,
"row_count": self.row_count,
"recent_job_count": self.get_job_count(),
"has_jobs": self.get_has_jobs(),
"recent_job_count": self.job_count,
"has_jobs": self.has_jobs,
"template_type": self.template_type,
"service_id": str(self.service_id),
"created_by": self.created_by.name,