Exemplo n.º 1
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
    def test_map_letter_to_field(self):
        from kanaria.core.model.letter import Letter
        import pykintone.application_settings.form_field as ff
        body = """
        本日のお仕事

        11/21
        今日もひたすらPythonのコードを書いた。やれやれだぜ。
        もし障害が起きたら以下に連絡をください。
        [email protected]

        09012221312
        """

        letter = Letter("", body)
        fields = [
            ff.BaseFormField.create("SINGLE_LINE_TEXT", "title", "タイトル"),
            ff.BaseFormField.create("MULTI_LINE_TEXT", "description", "報告内容"),
            ff.BaseFormField.create("DATE", "reported_date", "報告日時"),
            ff.BaseFormField.create("SINGLE_LINE_TEXT", "mail_address",
                                    "メールアドレス"),
            ff.BaseFormField.create("SINGLE_LINE_TEXT", "telephone", "電話番号"),
            ff.BaseFormField.create("MULTI_LINE_TEXT", "comment", "コメント")
        ]

        separator = TextAnalyzer()
        data = separator.map_letter_to_field(letter, fields)
        print(data)
        for n in [f.code for f in fields if f.code != "comment"]:
            self.assertTrue(n in data)
            self.assertTrue(data[n])
Exemplo n.º 3
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)
Exemplo n.º 4
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)
Exemplo n.º 5
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)
Exemplo 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
Exemplo n.º 7
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
Exemplo n.º 8
0
    def make_reply(self, subject="", message="", from_user=""):
        msg = message if message else self.message
        if not msg:
            return None

        sub = subject if subject else "Re: " + self.order.letter().subject
        env = Environment()
        letter = self.order.letter()
        admin_address = Brain.MY_USER_NAME + "@" + env.mail_domain
        from_address = from_user + "@" + env.mail_domain if from_user else admin_address
        to_addresses = [
            a for a in letter.to_addresses + [letter.from_address]
            if a != from_address
        ]
        to_addresses = [
            a for a in to_addresses if not a.startswith(Brain.MY_USER_NAME)
            and not a.endswith(env.mail_domain)
        ]
        letter = Letter(subject=sub,
                        body=msg,
                        from_address=from_address,
                        to_addresses=to_addresses)
        return letter
Exemplo n.º 9
0
    def get_letter(self, req):
        query = self.__parse_multipart(req)
        email_info = self.__extract_email_info(query)
        letter = Letter(**email_info)

        return letter