Beispiel #1
0
    def get_file_ref(self, doc: dict, structure: str, query=None) -> list:
        """
        ファイルリファレンス情報を取得

        :param dict doc:
        :param str structure:
        :param query:
        :type: list or None
        :return: files_list
        :rtype: list
        """
        if structure == 'emb' and query is None:
            sys.exit('embにはクエリが必要です')
        if structure != 'emb' and structure != 'ref':
            sys.exit('構造の選択はembまたはrefが必要です')

        files_list = []
        if structure == 'ref':
            if self.file_ref in doc:
                files_list = doc[self.file_ref]
        else:
            if not Utils.query_check(query, doc):
                sys.exit('対象のドキュメントに対してクエリーが一致しません.')
            # docから対象クエリを利用してファイルのリストを取得
            # deepcopyを使用しないとなぜか子のスコープのqueryがクリヤーされる
            query_c = copy.deepcopy(query)
            try:
                files_list = self._get_emb_files_list(doc, query_c)
            except Exception as e:
                sys.exit(e)
        return files_list
Beispiel #2
0
    def test__query_check(self):

        # 正常系
        query = ['bbb', '2', 'eee', '0', 'fff']
        doc = {
            'aaa':
            '123',
            'bbb': [{
                'ccc': '456'
            }, {
                'ddd': '789'
            }, {
                'eee': [{
                    'fff': {
                        'ans': 'OK'
                    }
                }, {
                    'ggg': '1'
                }]
            }]
        }
        actual = Utils.query_check(query, doc)
        self.assertIsInstance(actual, bool)
        self.assertTrue(actual)

        # 異常系 間違ったクエリ
        query = ['bbb', '2', 'eee', '1', 'fff']  # インデックスの指定ミスを想定
        doc = {
            'aaa':
            '123',
            'bbb': [{
                'ccc': '456'
            }, {
                'ddd': '789'
            }, {
                'eee': [{
                    'fff': {
                        'ans': 'OK'
                    }
                }, {
                    'ggg': '1'
                }]
            }]
        }
        actual = Utils.query_check(query, doc)
        self.assertIsInstance(actual, bool)
        self.assertFalse(actual)
Beispiel #3
0
    def add_file_reference(self,
                           collection: str,
                           oid: Union[ObjectId, str],
                           file_path: Tuple[Path],
                           structure: str,
                           query=None,
                           compress=False) -> bool:
        """
        ドキュメントにファイルリファレンスを追加する
        ファイルのインサート処理、圧縮処理なども行う

        :param str collection:
        :param oid:
        :type oid: ObjectId or str
        :param tuple file_path:
        :param str structure:
        :param query:
        :type query: list or None
        :param bool compress: default False
        :return:
        :rtype: bool
        """

        oid = Utils.conv_objectid(oid)

        # ドキュメント存在確認&対象ドキュメント取得
        doc = self.db[collection].find_one({'_id': oid})
        if doc is None:
            sys.exit('対象のドキュメントが存在しません')

        if structure == 'emb':
            # クエリーがドキュメントのキーとして存在するかチェック
            if not Utils.query_check(query, doc):
                sys.exit('対象のドキュメントに対してクエリーが一致しません.')

        # ファイルのインサート
        inserted_file_oids = []
        for file in self.file_gen(file_path):
            file_obj = file[1]
            metadata = {'filename': file[0]}

            if compress:
                file_obj = gzip.compress(file_obj, compresslevel=6)
                metadata.update({'compress': 'gzip'})

            inserted_file_oids.append(self.fs.put(file_obj, **metadata))

        if structure == 'ref':
            new_doc = self._file_list_attachment(doc, inserted_file_oids)

        elif structure == 'emb':
            try:
                new_doc = Utils.doc_traverse(doc, inserted_file_oids, query,
                                             self._file_list_attachment)
            except Exception as e:
                sys.exit(e)
        else:
            sys.exit('構造はrefかembが必要です')

        # ドキュメント差し替え
        replace_result = self.db[collection].replace_one({'_id': oid}, new_doc)

        if replace_result.modified_count == 1:
            return True
        else:  # 差し替えができなかった時は添付ファイルは削除
            self.fs_delete(inserted_file_oids)
            return False