Esempio n. 1
0
def decide(order):
    decision_type = DecisionType.IGNORE
    o = order
    message = ""

    if order.order_type() == OrderType.CREATE_APPLICATION:
        kintone = kintoneInterface()
        infos = kintone.find_similar_applications(order.target)
        if len(infos) > 0:
            decision_type = DecisionType.HOLD
            Order.hold_order(o)
            names = [info.name for info in infos][:2]
            message = "{0}などの似ているアプリケーションがありますがよろしいですか?".format("、".join(names))
        else:
            decision_type = DecisionType.EXECUTE
    elif order.order_type() == OrderType.ALLOW:
        o = Order.pop_order(order.user_address)
        decision_type = DecisionType.EXECUTE
    else:
        if order.app_id:
            decision_type = DecisionType.EXECUTE
        else:
            message = "このアドレスで登録されているアプリケーションが見つかりません。"

    a = Action(decision_type, o, message)
    return a
Esempio n. 2
0
def decide(order):
    decision_type = DecisionType.IGNORE
    o = order
    message = ""

    if order.order_type() == OrderType.CREATE_APPLICATION:
        kintone = kintoneInterface()
        infos = kintone.find_similar_applications(order.target)
        if len(infos) > 0:
            decision_type = DecisionType.HOLD
            Order.hold_order(o)
            names = [info.name for info in infos][:2]
            message = "{0}などの似ているアプリケーションがありますがよろしいですか?".format(
                "、".join(names))
        else:
            decision_type = DecisionType.EXECUTE
    elif order.order_type() == OrderType.ALLOW:
        o = Order.pop_order(order.user_address)
        decision_type = DecisionType.EXECUTE
    else:
        if order.app_id:
            decision_type = DecisionType.EXECUTE
        else:
            message = "このアドレスで登録されているアプリケーションが見つかりません。"

    a = Action(decision_type, o, message)
    return a
Esempio n. 3
0
    def test_update_item(self):
        letter = Letter("報告日を追加してほしい", "")
        order = Order(OrderType.ADD_ITEM, "test_user", app_id=self.TEST_APP.app_id, letter=letter)
        order.target = "報告日"
        action = Action(DecisionType.EXECUTE, order)

        result = executor.update_application(action)
        self.assertTrue(result.ok)

        order.order_type_text = OrderType.DELETE_ITEM.value
        result = executor.update_application(action)
        self.assertTrue(result.ok)
Esempio n. 4
0
    def create_order(self, order_type, subject, body=""):
        from kanaria.core.model.letter import Letter
        from kanaria.core.model.order import Order

        letter = Letter(subject, body)
        order = Order(order_type, "test_service_brain_executor", subject, letter=letter)
        return order
Esempio n. 5
0
def interpret(letter):
    kintone = kintoneInterface()
    order_type = OrderType.NONE
    app_id = ""
    target = ""
    kanaria = kintone.get_kanaria()

    if letter.to_includes(Brain.MY_USER_NAME):
        # to kanaria administrator
        if kanaria:
            app_id = kanaria.app_id

        if letter.subject:
            order_type = interpret_allow(letter.subject)
            if order_type != OrderType.ALLOW:
                order_type = OrderType.CREATE_APPLICATION
                target = extract_application_name(letter.subject)
    else:
        # to application's address
        for a in letter.to_addresses:
            app = kintone.get_application_by_code(Letter.get_user(a))
            if app:
                app_id = app.app_id

        # get order type
        if app_id:
            if letter.subject:
                target, order_type = interpret_operation(letter.subject)
            if order_type == OrderType.NONE:
                order_type = OrderType.POST_LETTER

    o = Order(order_type,
              letter.from_address,
              app_id=app_id,
              target=target,
              letter=letter)
    # post letter to kanaria
    if kanaria:
        # todo have to support attached file
        if len(letter.attached_files) == 0:
            created = kanaria.create(letter)
            o.letter_id = created.record_id

    return o
Esempio n. 6
0
def interpret(letter):
    kintone = kintoneInterface()
    order_type = OrderType.NONE
    app_id = ""
    target = ""
    kanaria = kintone.get_kanaria()

    if letter.to_includes(Brain.MY_USER_NAME):
        # to kanaria administrator
        if kanaria:
            app_id =kanaria.app_id

        if letter.subject:
            order_type = interpret_allow(letter.subject)
            if order_type != OrderType.ALLOW:
                order_type = OrderType.CREATE_APPLICATION
                target = extract_application_name(letter.subject)
    else:
        # to application's address
        for a in letter.to_addresses:
            app = kintone.get_application_by_code(Letter.get_user(a))
            if app:
                app_id = app.app_id

        # get order type
        if app_id:
            if letter.subject:
                target, order_type = interpret_operation(letter.subject)
            if order_type == OrderType.NONE:
                order_type = OrderType.POST_LETTER

    o = Order(order_type, letter.from_address, app_id=app_id, target=target, letter=letter)
    # post letter to kanaria
    if kanaria:
        # todo have to support attached file
        if len(letter.attached_files) == 0:
            created = kanaria.create(letter)
            o.letter_id = created.record_id

    return o
Esempio n. 7
0
    def test_post_letter(self):
        body = """
        2015/1/1

        本日お客様から、掃除したはずなのにサッシに埃がついているじゃないとクレームがありました。
        現場に行ってみてみると、確かにまだ汚れがありました。

        お客様連絡先
        0322221111
        """

        letter = Letter("", body)
        order = Order(OrderType.POST_LETTER, "test_user", app_id=self.TEST_APP.app_id, letter=letter)
        action = Action(DecisionType.EXECUTE, order)

        result = executor.post(action)
        self.assertTrue(result.ok)
Esempio n. 8
0
def execute(action):
    reply = None
    order_type = action.order.order_type()

    if action.decision_type() == DecisionType.EXECUTE:
        if order_type == OrderType.CREATE_APPLICATION:
            app, app_index = create_application(action)
            if app:
                # first record for new application
                post_order = Order(OrderType.POST_LETTER,
                                   action.order.user_address,
                                   app.app_id,
                                   letter=action.order.letter())
                post(Action(DecisionType.EXECUTE, post_order))
                message = "{0}アプリケーションを作成しました。今後こちらにメールを送っていただければ、アプリケーションにデータが登録されます。".format(
                    app_index.name)
                reply = action.make_reply(message=message,
                                          from_user=app_index.code)
            else:
                message = "ごめんなさい、アプリケーションの作成に失敗しちゃいました。。。"
                reply = action.make_reply(message=message)
        elif order_type == OrderType.POST_LETTER:
            created, app_index = post(action)
            if not created.ok:
                message = "ごめんなさい、データの登録に失敗しちゃいました。。。このメッセージを担当の人に伝えてください。\n{0}".format(
                    created.error.message)
                reply = action.make_reply(message=message,
                                          from_user=app_index.code)
        else:
            updated, app_index = update_application(action)
            op_txt = "追加" if order_type == OrderType.ADD_ITEM else "削除"
            if updated.ok:
                message = "{0}を{1}しました".format(action.order.target, op_txt)
                reply = action.make_reply(message=message,
                                          from_user=app_index.code)
            else:
                message = "ごめんなさい、{0}の{1}に失敗しました。。。このメッセージを担当の人に伝えてください。\n{2}".format(
                    action.order.target, op_txt, updated.error.message)
                reply = action.make_reply(message=message,
                                          from_user=app_index.code)

    else:
        reply = action.make_reply()

    return reply
Esempio n. 9
0
    def test_make_reply(self):
        from kanaria.core.environment import Environment
        from kanaria.core.service.brain import Brain

        env = Environment()
        domain = env.mail_domain
        kanaria_admin = Brain.MY_USER_NAME + "@" + domain
        from_address = "test_user@" + domain
        letter = Letter("報告日を追加してほしい", "", from_address=from_address, to_addresses=kanaria_admin)
        order = Order(OrderType.ADD_ITEM, "test_user", app_id=self.TEST_APP.app_id, letter=letter)
        action = Action(DecisionType.EXECUTE, order)

        reply = action.make_reply(message="返信を書きました")
        self.assertTrue(reply.subject)
        self.assertTrue(from_address in reply.to_addresses)

        # system mail
        letter.from_address = kanaria_admin
        letter.to_addresses = [kanaria_admin, from_address]
        reply = action.make_reply(message="管理者から返信を書きました")
        self.assertTrue(kanaria_admin not in reply.to_addresses)