Skip to content
This repository has been archived by the owner on Apr 17, 2024. It is now read-only.

Commit

Permalink
Add list option to lock
Browse files Browse the repository at this point in the history
It's now possible to list locks:
`/timereport lock list`
  • Loading branch information
parberge authored Jun 30, 2020
1 parent 79b02d7 commit 9b354b9
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 24 deletions.
57 changes: 48 additions & 9 deletions chalicelib/action.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,15 +315,24 @@ def _lock_action(self):
"""
Lock the timereport for month
create lock:
/timereport lock 2019-08
list locks:
/timereport lock list 2020
"""
if not self.arguments:

if not self._valid_number_of_args(min_args=1, max_args=2):
return self.send_response(
f"Missing required argument. Here is an helpful message: {self._lock_action.__doc__}"
message=f"Got the wrong number of arguments for {self.action}. See these examples: {self._lock_action.__doc__}"
)

event = create_lock(user_id=self.user_id, event_date=self.params[1])
if self.arguments[0] == "list":
return self._lock_list_action()

event = create_lock(user_id=self.user_id, event_date=self.arguments[0])
log.debug(f"lock event: {event}")

response = lock_event(url=self.config["backend_url"], event=json.dumps(event))
log.debug(f"response was: {response.text}")
if response.status_code == 200:
Expand All @@ -333,26 +342,27 @@ def _lock_action(self):
self.send_response(message=f"Lock failed! :cry:")
return ""

def _check_locks(self, date: datetime, second_date: datetime) -> bool:
def _check_locks(self, date: datetime, second_date: datetime) -> list:
"""
Check dates for lock.
"""
is_locked = False
dates_to_check = list()
locked_dates = list()
for date in date_range(start_date=date, stop_date=second_date):
if not date.strftime("%Y-%m") in dates_to_check:
dates_to_check.append(date.strftime("%Y-%m"))

log.debug(f"Got {len(dates_to_check)} date(s) to check")
for date in dates_to_check:
respone = read_lock(
response = read_lock(
url=self.config["backend_url"], user_id=self.user_id, date=date
)
if respone.json():
if response.json():
log.info(f"Date {date} is locked")
is_locked = True
dates_to_check.append(response.json())
locked_dates.append(date)

return is_locked
return locked_dates

def _valid_number_of_args(self, min_args: int, max_args: int) -> bool:
"""
Expand Down Expand Up @@ -395,3 +405,32 @@ def _create_list_message(self, data) -> None:
text=f"Date: *{event_date}*\nReason: *{reason}*\nHours: *{hours}*"
)
self.slack.add_divider_block()

def _lock_list_action(self) -> None:
"""
Lists all locks for a given year
"""
year = None

try:
year = int(self.arguments[1])
except IndexError:
now = datetime.now()
year = now.year

response = read_lock(
url=self.config["backend_url"], user_id=self.user_id, date=year
)
locks = response.json()

if not locks:
return self.send_response(f"No locks found for year *{year}*")

self.slack.add_section_block(text=f"Locks found for months in *{year}*")
self.slack.add_divider_block()

for lock in locks:
self.slack.add_section_block(text=f"{lock.get('event_date')} :lock:")

self.slack.post_message(message="From timereport", channel=self.user_id)
return ""
23 changes: 12 additions & 11 deletions chalicelib/lib/slack.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import base64
import hashlib
import hmac
import json
import logging
import os
import requests
from urllib.parse import parse_qs
import logging
import json
import hmac
import hashlib
import base64

import requests

log = logging.getLogger(__name__)

Expand Down Expand Up @@ -57,10 +58,11 @@ def _handle_response(
f"Slack responded with not ok. Message was: {validated_response}"
)
except (AttributeError, ValueError) as error:
log.critical(
f"Unable get valid json from response. Error was: {error}",
exc_info=True,
)
if not response.text == "ok" and response.status_code == 200:
log.critical(
f"Unable get valid json from response. Error was: {error}",
exc_info=True,
)

return response

Expand Down Expand Up @@ -247,4 +249,3 @@ def delete_message_menu(user_name, date):
}
]
return attachment

4 changes: 0 additions & 4 deletions tests/test_slack.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,6 @@ def test_delete_menu():
assert test_result_dict.get("fields")


def test_slack_handle_response_wrong_data_type():
assert fake_slack._handle_response("wrong data type") is not None


def test_add_divider():
fake_slack = Slack(slack_token="fake")
fake_slack.add_divider_block()
Expand Down

0 comments on commit 9b354b9

Please sign in to comment.