示例#1
0
文件: views.py 项目: s-tip/stip-rs
def upload_stix_file(request):
    ctirs_auth_user = api_root.authentication(request)
    if ctirs_auth_user is None:
        return api_root.error(Exception('You have no permission for this operation.'))
    try:
        via = Vias.get_via_rest_api_upload(uploader=ctirs_auth_user.id)
        upload_common(request, via)
        return api_root.get_put_normal_status()
    except Exception as e:
        import traceback
        traceback.print_exc()
        return api_root.error(e)
示例#2
0
def post_language_contents(request, object_ref, ctirs_auth_user):
    try:
        j = json.loads(request.body)
        # S-TIP Identity 作成する
        stip_identity = _get_stip_identname(request.user)
        # bundle 作成
        bundle = Bundle(stip_identity)
        # 参照元の obejct を取得
        object_ = get_object(object_ref)
        if object_ is None:
            return error(
                Exception('No document. (object_ref=%s)' % (object_ref)))

        for language_content in j['language_contents']:
            selector_str = language_content['selector']
            content_value = language_content['content']
            language = language_content['language']
            try:
                selector_elems = selector_str.split('.')
                last_elem = object_
                # selector の 要素をチェックする
                if len(selector_elems) == 1:
                    # selector が . でつながられていない場合
                    last_selector = selector_str
                    last_elem = is_exist_objects(selector_str, last_elem)
                else:
                    # selector が . でつながられている場合は最後までたどる
                    for selector in selector_elems[:-1]:
                        last_selector = selector
                        last_elem = is_exist_objects(selector, last_elem)
                        if last_elem is None:
                            raise Exception('selector is invalid: ' +
                                            str(selector_str))

                if isinstance(last_elem, list):
                    # 空要素で初期化し、該当 index の要素だけ上書きする
                    lc_lists = [''] * len(last_elem)
                    lc_lists[get_list_index_from_selector(
                        selector_elems[-1])] = content_value
                    content = lc_lists
                    selector = '.'.join(selector_elems[:-1])
                elif isinstance(last_elem, dict):
                    # 空辞書で初期化し、該当 index の要素だけ上書きする
                    content = {}
                    content[selector_elems[-1]] = content_value
                    selector = '.'.join(selector_elems[:-1])
                else:
                    # list ではない
                    content = content_value
                    selector = last_selector
            except Exception as e:
                traceback.print_exc()
                raise e

            contents = {}
            contents[language] = {selector: content}
            language_content = LanguageContent(
                created_by_ref=stip_identity,
                object_ref=object_ref,
                object_modified=object_['modified'],
                contents=contents)
            bundle.objects.append(language_content)

        # viaを取得
        via = Vias.get_via_rest_api_upload(uploader=ctirs_auth_user.id)
        community = Communities.get_default_community()
        # stixファイルを一時ファイルに出力
        stix_file_path = tempfile.mktemp(suffix='.json')
        with open(stix_file_path, 'wb+') as fp:
            fp.write(bundle.serialize(indent=4, ensure_ascii=False)).encode()
        # 登録処理
        regist(stix_file_path, community, via)
        resp = get_normal_response_json()
        bundle_json = json.loads(str(bundle))
        resp['data'] = {'bundle': bundle_json}
        return JsonResponse(resp, status=201, safe=False)
    except Exception as e:
        traceback.print_exc()
        return error(e)