def get_period_data(date_str: str) -> Dict[str, Any]:
    """
    Get information about the period

    :date_str: A string contaning date. Valid formats: "2019-01", "2019-01-01", "2019-01-02:2019-01-03" and ""
    """

    dates = parse_date(date_str, format_str="%Y-%m")
    if dates["from"] is None or dates["to"] is None:
        dates = parse_date(date_str, format_str="%Y-%m-%d")

    if dates["from"] is None or dates["to"] is None:
        current_month = date.today()
        dates = {"from": current_month, "to": current_month}

    total_workdays = 0
    holidays = []
    for dt in month_range(dates["from"], dates["to"]):
        month_str = f"{dt.year}-{dt.month:02}"
        api_url = f"https://api2.codelabs.se/{month_str}.json"

        response = requests.get(url=api_url, timeout=1)
        if response.status_code == 200:
            data = response.json()
            total_workdays += data["antal_arbetsdagar"]
            holidays += data["helgdagar"]
        else:
            log.debug(
                f"Got response code {response.status_code} for month {month_str}"
            )

    return dict(total_workdays=total_workdays, holidays=holidays)
Example #2
0
def test_parse_single_date():
    single_date = "2019-01-01"
    test_date = parse_date(date=single_date)

    assert isinstance(test_date, dict)
    assert test_date["from"] == datetime.strptime(single_date, "%Y-%m-%d")
    assert test_date["to"] == datetime.strptime(single_date, "%Y-%m-%d")
Example #3
0
def factory(json_order, format_str="%Y-%m-%d"):
    """
    Extract necessary values from the interactive message sent via slack

    :param json_order: A dict based on slack interactive message payload
    :param format_str: datetime format
    :return: list
    """

    log.debug(f"json_order is: {json_order}")

    fields = json_order["original_message"]["attachments"][0]["fields"]

    user_id = json_order["user"]["id"]
    user_name = fields[0]["value"]
    reason = fields[1]["value"]
    dates = parse_date(fields[2]["value"], format_str=format_str)
    hours = fields[3]["value"]

    events = []

    for date in date_range(start_date=dates["from"], stop_date=dates["to"]):
        log.info(f"date is {date}")
        document = {
            "user_name": user_name,
            "reason": reason,
            "event_date": date.strftime(format_str),
            "hours": hours,
            "user_id": user_id,
        }
        events.append(document)
    return events
Example #4
0
def test_parse_date_today():
    today = datetime.now().strftime(format_str)

    today_test_dict = parse_date(date="today", format_str=format_str)
    assert isinstance(today_test_dict, dict)
    assert isinstance(today_test_dict["from"], datetime)
    assert isinstance(today_test_dict["to"], datetime)
    assert today == today_test_dict["from"].strftime(format_str)
    assert today == today_test_dict["to"].strftime(format_str)
Example #5
0
def test_parse_multiple_dates():
    first_date = "2019-01-01"
    second_date = "2019-02-01"
    multiple_dates = f"{first_date}:{second_date}"

    test_dates = parse_date(date=multiple_dates)
    assert isinstance(test_dates, dict)
    assert test_dates["from"] == datetime.strptime(first_date, "%Y-%m-%d")
    assert test_dates["to"] == datetime.strptime(second_date, "%Y-%m-%d")
Example #6
0
def test_parse_invalid_multiple_dates():
    # First date is bigger than second date, which should result in an empty dict
    first_date = "2019-02-01"
    second_date = "2019-01-01"
    multiple_dates = f"{first_date}:{second_date}"

    test_dates = parse_date(date=multiple_dates)
    assert isinstance(test_dates, dict)
    assert test_dates["from"] is None
    assert test_dates["to"] is None
Example #7
0
def test_parse_today():
    test_date = parse_date(date="today")
    assert isinstance(test_date, dict)
    assert isinstance(test_date["from"], datetime)
Example #8
0
def test_invalid_date_format():
    invalid_format = "2019:01:02"
    test_date = parse_date(date=invalid_format)
    assert test_date["from"] is None
    assert test_date["to"] is None
Example #9
0
def test_parse_date_invalid_format():
    test_date = parse_date(date="not_supported_format")
    assert isinstance(test_date, dict)
    assert test_date == {"to": None, "from": None}