Exemplo n.º 1
0
def get_time_zone():
    """
    Get time zone according to administrator account.
    reference: https://developers.worksmobile.com/kr/document/100300528?lang=en
    """
    external_key = load_external_key()
    time_zone_url = API_BO["TZone"]["time_zone_url"]
    time_zone_url = time_zone_url.replace("DOMAIN_ID", str(DOMAIN_ID))
    time_zone_url = time_zone_url.replace("EXTERNAL_KEY", external_key)

    headers = {
        "content-type": "application/json",
        "charset": "UTF-8",
        "consumerKey": OPEN_API["consumerKey"]
    }

    response = auth_get(time_zone_url, headers=headers)
    if response.status_code != 200 or response.content is None:
        LOGGER.info("get external key failed. url:%s text:%s body:%s",
                    time_zone_url, response.text, response.content)
        raise Exception("get timezone. http code error.")

    tmp_req = json.loads(response.content)
    time_zone = tmp_req.get("timeZone", None)
    if time_zone is None:
        raise Exception("get timezone. no timeZone filed.")
    return time_zone
Exemplo n.º 2
0
def modify_schedule(calendar_uid, current, end, begin, account_id):
    summarys = {"kr": "근무시간", "jp": "勤務時間", "en": "Working hours"}

    calendar_data = make_icalendar_data(calendar_uid, summarys[LOCAL], current,
                                        end, begin, account_id)
    body = {"ical": calendar_data}

    calendar_id = get_value(API_BO["calendar"]["name"], None)
    if calendar_id is None:
        LOGGER.error("get calendar from cached failed.")
        raise HTTPError(500, "internal error. get calendar is failed.")

    url = API_BO["calendar"]["modify_schedule_url"]
    url = url.replace("_EXTERNAL_KEY_", load_external_key())
    url = url.replace("_CALENDAR_ID_", calendar_id)
    url = url.replace("_CALENDAR_UUID_", calendar_uid)

    headers = create_headers()
    response = auth_put(url, data=json.dumps(body), headers=headers)
    if response.status_code != 200:
        LOGGER.error("modify schedules failed. url:%s text:%s body:%s", url,
                     response.text, response.content)
        raise HTTPError(500,
                        "internal error. create schedule http code error.")

    LOGGER.info("modify schedules. url:%s text:%s body:%s", url, response.text,
                response.content)

    tmp_req = json.loads(response.content)
    if tmp_req["result"] != "success":
        LOGGER.error("modify schedule failed. url:%s text:%s body:%s", url,
                     response.text, response.content)
        raise HTTPError(500, "internal error. http response error.")
Exemplo n.º 3
0
def create_calendar():

    body = {
        "name": "Attendance management bot",
        "description": "Attendance management bot",
        "invitationUserList": [{
            "email": ADMIN_ACCOUNT,
            "actionType": "insert",
            "roleId": 2
        }]
    }

    headers = create_headers()
    url = API_BO["calendar"]["create_calendar_url"]
    url = url.replace("_EXTERNAL_KEY_", load_external_key())
    LOGGER.info("create calendar. url:%s body:%s", url, str(body))

    response = auth_post(url, data=json.dumps(body), headers=headers)
    if response.status_code != 200:
        LOGGER.error("create calendar failed. url:%s text:%s body:%s",
                    url, response.text, response.content)
        raise Exception("create calendar id. http response code error.")

    LOGGER.info("create calendar id. url:%s txt:%s body:%s",
                url, response.text, response.content)
    tmp_req = json.loads(response.content)
    if tmp_req["result"] != "success":
        LOGGER.error("create calendar failed. url:%s text:%s body:%s",
                     url, response.text, response.content)
        raise Exception("create calendar id. response no success.")
    return tmp_req["returnValue"]
Exemplo n.º 4
0
def create_schedule(current, end, begin, account_id):
    """
    create schedule.
    reference: https://developers.worksmobile.com/kr/document/100702703?lang=ko

    :return: schedule id.
    """

    uid = str(uuid.uuid4()) + account_id
    schedule_data = make_icalendar_data(uid, "Clock-in time", current, end,
                                        begin, account_id, True)
    body = {"ical": schedule_data}

    calendar_id = get_value(API_BO["calendar"]["name"], None)
    if calendar_id is None:
        LOGGER.error("get calendar from cached failed.")
        raise HTTPError(500, "internal error. get calendar is failed.")

    headers = create_headers()
    url = API_BO["calendar"]["create_schedule_url"]
    url = url.replace("_EXTERNAL_KEY_", load_external_key())
    url = url.replace("_CALENDAR_ID_", calendar_id)

    response = auth_post(url, data=json.dumps(body), headers=headers)
    if response.status_code != 200:
        LOGGER.error("create schedules failed. url:%s text:%s body:%s", url,
                     response.text, response.content)
        raise HTTPError(500,
                        "internal error. create schedule http code error.")

    tmp_req = json.loads(response.content)
    if tmp_req["result"] != "success":
        LOGGER.error("create schedule failed. url:%s text:%s body:%s", url,
                     response.text, response.content)
        raise HTTPError(500, "internal error. http response error.")

    LOGGER.info("create schedule. url:%s text:%s body:%s", url, response.text,
                response.content)

    return_value = tmp_req.get("returnValue", None)
    if return_value is None:
        LOGGER.error("create schedule failed. url:%s text:%s body:%s", url,
                     response.text, response.content)
        raise HTTPError(500, "internal error. create schedule content error.")

    schedule_uid = return_value.get("icalUid", None)
    if schedule_uid is None:
        LOGGER.error("create schedule failed. url:%s text:%s body:%s", url,
                     response.text, response.content)
        raise HTTPError(500, "internal error. create schedule content error.")
    return schedule_uid
Exemplo n.º 5
0
def modify_schedule(calendar_uid, current, end, begin, account_id):
    """
    modify schedule.
    reference: https://developers.worksmobile.com/kr/document/100702704?lang=ko

    :return: schedule id.
    """

    calendar_data = make_icalendar_data(calendar_uid, "Working hours", current,
                                        end, begin, account_id)
    body = {"ical": calendar_data}

    calendar_id = get_value(API_BO["calendar"]["name"], None)
    if calendar_id is None:
        LOGGER.error("get calendar from cached failed.")
        raise HTTPError(500, "internal error. get calendar is failed.")

    url = API_BO["calendar"]["modify_schedule_url"]
    url = url.replace("_EXTERNAL_KEY_", load_external_key())
    url = url.replace("_CALENDAR_ID_", calendar_id)
    url = url.replace("_CALENDAR_UUID_", calendar_uid)

    headers = create_headers()
    response = auth_put(url, data=json.dumps(body), headers=headers)
    if response.status_code != 200:
        LOGGER.error("modify schedules failed. url:%s text:%s body:%s", url,
                     response.text, response.content)
        raise HTTPError(500,
                        "internal error. create schedule http code error.")

    LOGGER.info("modify schedules. url:%s text:%s body:%s", url, response.text,
                response.content)

    tmp_req = json.loads(response.content)
    if tmp_req["result"] != "success":
        LOGGER.error("modify schedule failed. url:%s text:%s body:%s", url,
                     response.text, response.content)
        raise HTTPError(500, "internal error. http response error.")
Exemplo n.º 6
0
def get_time_zone():
    external_key = load_external_key()
    time_zone_url = API_BO["TZone"]["time_zone_url"]
    time_zone_url = time_zone_url.replace("DOMAIN_ID", str(DOMAIN_ID))
    time_zone_url = time_zone_url.replace("EXTERNAL_KEY", external_key)

    headers = {
        "content-type": "application/json",
        "charset": "UTF-8",
        "consumerKey": OPEN_API["consumerKey"]
    }

    response = auth_get(time_zone_url, headers=headers)
    if response.status_code != 200 or response.content is None:
        LOGGER.info("get external key failed. url:%s text:%s body:%s",
                    time_zone_url, response.text, response.content)
        raise Exception("get timezone. http code error.")

    tmp_req = json.loads(response.content)
    time_zone = tmp_req.get("timeZone", None)
    if time_zone is None:
        raise Exception("get timezone. no timeZone filed.")
    return time_zone