Skip to content

Commit

Permalink
Fix lazy loading in Card
Browse files Browse the repository at this point in the history
Previous implementation was wrong as it forced you to call the property twice.
  • Loading branch information
cesarob committed Apr 15, 2018
1 parent 551e923 commit 47834a2
Showing 1 changed file with 17 additions and 24 deletions.
41 changes: 17 additions & 24 deletions trello/card.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class Card(TrelloBase):
"""
Class representing a Trello card. Card attributes are stored on
the object
https://developers.trello.com/advanced-reference/card
"""

Expand Down Expand Up @@ -73,47 +73,35 @@ def comments(self):
"""
Lazily loads and returns the comments
"""
try:
if self._comments is None:
self._comments = self.fetch_comments()
except AttributeError:
self._comments = None
if self._comments is None:
self._comments = self.fetch_comments()
return self._comments

@property
def checklists(self):
"""
Lazily loads and returns the checklists
"""
try:
if self._checklists is None:
self._checklists = self.fetch_checklists()
except AttributeError:
self._checklists = None
if self._checklists is None:
self._checklists = self.fetch_checklists()
return self._checklists

@property
def plugin_data(self):
"""
Lazily loads and returns the plugin data
"""
try:
if self._plugin_data is None:
self._plugin_data = self.fetch_plugin_data()
except AttributeError:
self._plugin_data = None
if self._plugin_data is None:
self._plugin_data = self.fetch_plugin_data()
return self._plugin_data

@property
def attachments(self):
"""
Lazily loads and returns the attachments
"""
try:
if self._attachments is None:
self._attachments = self.fetch_attachments()
except AttributeError:
self._attachments = None
if self._attachments is None:
self._attachments = self.fetch_attachments()
return self._attachments

def __init__(self, parent, card_id, name=''):
Expand All @@ -132,6 +120,11 @@ def __init__(self, parent, card_id, name=''):
self.id = card_id
self.name = name

self._checklists = None
self._comments = None
self._plugin_data = None
self._attachments = None

@classmethod
def from_json(cls, parent, json_obj):
"""
Expand Down Expand Up @@ -525,14 +518,14 @@ def set_due(self, due):

def set_due_complete(self):
"""Set due complete
:return: None
"""
self._set_due_complete(True)

def remove_due_complete(self):
"""Remove due complete
:return: None
"""
self._set_due_complete(False)
Expand Down Expand Up @@ -717,7 +710,7 @@ def add_checklist(self, title, items, itemstates=None):

def _set_due_complete(self, is_complete):
"""Set due is complete or not complete
https://developers.trello.com/advanced-reference/card#put-1-cards-card-id-or-shortlink-dueComplete
:param is_complete: boolean
:return: None
Expand Down

0 comments on commit 47834a2

Please sign in to comment.