Esempio n. 1
0
def list_time_periods(params):
    """Show all time periods"""
    time_periods = []
    for time_period_id, time_period_details in load_timeperiods().items():
        alias = time_period_details['alias']
        if not isinstance(alias, str):  # check for mypy
            continue
        time_periods.append(
            constructors.collection_item(
                domain_type='time_period',
                obj={
                    'title': alias,
                    'id': time_period_id
                },
            ))
    time_period_collection = {
        'id':
        "timeperiod",
        'domainType':
        'time_period',
        'value':
        time_periods,
        'links': [
            constructors.link_rel('self',
                                  constructors.collection_href('time_period'))
        ],
    }
    return constructors.serve_json(time_period_collection)
Esempio n. 2
0
def list_time_periods(params):
    """Show all time periods"""
    time_periods = []
    for time_period_id, time_period_details in load_timeperiods().items():
        alias = time_period_details["alias"]
        if not isinstance(alias, str):  # check for mypy
            continue
        time_periods.append(
            constructors.collection_item(
                domain_type="time_period",
                title=alias,
                identifier=time_period_id,
            ))
    time_period_collection = {
        "id":
        "timeperiod",
        "domainType":
        "time_period",
        "value":
        time_periods,
        "links": [
            constructors.link_rel("self",
                                  constructors.collection_href("time_period"))
        ],
    }
    return constructors.serve_json(time_period_collection)
Esempio n. 3
0
def show_time_period(params):
    """Show a time period"""
    name = params['name']
    time_periods = load_timeperiods()
    if name not in time_periods:
        raise ProblemException(404, http.client.responses[404], f"Time period {name} not found")
    time_period = time_periods[name]
    return _serve_time_period(_to_api_format(time_period, name == "24X7"))
Esempio n. 4
0
def delete(params):
    """Delete a time period"""
    name = params['name']
    time_periods = load_timeperiods()
    if name not in time_periods:
        raise ProblemException(404, http.client.responses[404], f"Time period {name} not found")
    del time_periods[name]
    save_timeperiods(time_periods)
    return Response(status=204)
Esempio n. 5
0
def delete(params):
    """Delete a time period"""
    user.need_permission("wato.edit")
    user.need_permission("wato.timeperiods")
    name = params["name"]
    time_periods = load_timeperiods()
    if name not in time_periods:
        raise ProblemException(404, http.client.responses[404],
                               f"Time period {name} not found")
    del time_periods[name]
    save_timeperiods(time_periods)
    return Response(status=204)
Esempio n. 6
0
def show_time_period(params):
    """Show a time period"""
    name = params['name']
    time_periods = load_timeperiods()
    if name not in time_periods:
        raise ProblemException(404, http.client.responses[404], f"Time period {name} not found")
    time_period = time_periods[name]

    time_period_readable: Dict[str, Any] = {key: time_period[key] for key in ("alias", "exclude")}
    active_time_ranges = _active_time_ranges_readable(
        {key: time_period[key] for key in defines.weekday_ids()})
    time_period_readable["active_time_ranges"] = active_time_ranges
    time_period_readable["exceptions"] = _exceptions_readable({
        key: time_period[key]
        for key in time_period
        if key not in ['alias', 'exclude', *defines.weekday_ids()]
    })
    return _serve_time_period(time_period_readable)