Skip to content

Commit

Permalink
Merge pull request #21 from Ctri-The-Third/cache_and_date_updating
Browse files Browse the repository at this point in the history
Cache and date updating
  • Loading branch information
Ctri-The-Third authored Dec 26, 2022
2 parents d1eed7d + 1467646 commit 45a9059
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 14 deletions.
65 changes: 53 additions & 12 deletions serviceHelpers/trello.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def __init__(self, board_id, key, token) -> None:
pass

def find_trello_card(self, regex) -> dict:
"uses regexes to search name and description of cached / fetched cards. Returns the first it finds."
"uses regexes to search name and description of cached / fetched cards, or exactly matching IDs. Returns the first it finds. "
cards = (
self.fetch_trello_cards()
if self.dirty_cache
Expand All @@ -32,7 +32,8 @@ def find_trello_card(self, regex) -> dict:
for card in cards:
card:dict
try:
if re.search(regex, card.get("name","")) or re.search(regex, card.get("desc","")):

if card.get("id","") == regex or re.search(regex, card.get("name","")) or re.search(regex, card.get("desc","")):
return card

except (Exception,) as err:
Expand All @@ -48,7 +49,7 @@ def find_trello_cards(self,regex):
foundCards = []
for card in cards:
card:dict
if re.search(regex,card.get("name","")) or re.search(regex,card.get("desc","")):
if card.get("id","") == regex or re.search(regex, card.get("name","")) or re.search(regex, card.get("desc","")):
foundCards.append(card)
return foundCards

Expand Down Expand Up @@ -196,26 +197,42 @@ def create_card(self,title, list_id, description = None, labelID = None, dueTim
print(r.content)
return
card = json.loads(r.content)
self.dirty_cache = True

if not self._try_update_cache(r.content):
self.dirty_cache = True
return card


def update_card( self, card_id:str, title:str, description:str = None, pos:float = None, new_list_id:str = None):
"Update the card with a new title, description, position. use list_id to move to another list. "
def update_card(
self,
card_id: str,
title: str,
description: str = None,
pos: float = None,
new_list_id: str = None,
new_due_timestamp: str = None,
):
"Update the card with a new title, description, position. use list_id to move to another list."
params = self._get_trello_params()
params["name"] = title

if description is not None:
params["desc"] =description
params["desc"] = description
if pos is not None:
params["pos"] = pos
if new_list_id is not None:
params["idList"] = new_list_id
if new_due_timestamp is not None:
params["due"] = new_due_timestamp
url = "https://api.trello.com/1/cards/%s" % (card_id)
r = requests.put(url,params=params)
r = requests.put(url, params=params)
if r.status_code != 200:
print("ERROR: %s couldn't update the Gmail Trello card's name" % (r.status_code))
self.dirty_cache = True
print(
"ERROR: %s couldn't update the Gmail Trello card's name"
% (r.status_code)
)
if not self._try_update_cache(r.content):
self.dirty_cache = True
return


Expand Down Expand Up @@ -357,4 +374,28 @@ def _populate_cache(self,array_of_cards:list) -> None:
lo.warn("skipped adding a card to the cache, no id present")
continue

self._cached_cards[card.get("id")] = card
self._cached_cards[card.get("id")] = card

def _try_update_cache(self,response_content) -> bool:
"attempts to parse the raw response from update/create and turn it into a cached card. Returns True on success"

try:
response_content = response_content.decode()
except (UnicodeDecodeError, AttributeError):
pass

if not isinstance(response_content,str):
logger.error("error in _try_update_cache, supplied object not a string.")
return False
try:
card = json.loads(response_content)
card:dict
except Exception as err:
logger.error("error in _try_update_cache, couldn't parse supplied object becase '%s'",err)
logger.debug("supplied object %s", response_content)
return False

self._cached_cards[card.get("id")] = card
return True


2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

setup(
name="hex-helpers",
version="2.8.0",
version="2.9.0",
description="A series of light helpers for `freshdesk`,`gmail`,`habitica`,`hue lights`,`jira`,`slack`,`trello`",
long_description=README,
long_description_content_type="text/markdown",
Expand Down
13 changes: 13 additions & 0 deletions tests/test_trello_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,16 @@ def test_move_card_from_list_to_list():
assert card["id"] == new_card["id"]
assert new_card.get("idList","") == TEST_LIST_ID_TWO



def test_update_card():
old_ts = "2022-10-01 23:59:00"
new_ts = "2011-11-11 11:11:00"
helper = trello(TEST_BOARD_ID,os.environ["TRELLO_KEY"],os.environ["TRELLO_TOKEN"])
card = helper.create_card("TEST CARD, PLEASE IGNORE",TEST_LIST_ID,dueTimestamp=old_ts)
helper.update_card(card["id"], card["name"], new_due_timestamp=new_ts)
new_card = helper.find_trello_card(card["id"])

helper.deleteTrelloCard(new_card["id"])
assert new_card["due"] == "2011-11-11T11:11:00.000Z"

2 changes: 1 addition & 1 deletion tests/test_zendesk.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def test_get_user(caplog):
assert entry.levelno < logging.ERROR
assert isinstance(user, ZendeskUser)

assert user.name == "test test"
assert user.name == "test fg"
assert user.email == "[email protected]"
assert user.user_id == 417316391
assert user.organisationID is None
Expand Down

0 comments on commit 45a9059

Please sign in to comment.