Exemplo n.º 1
0
def like(request):
    # Like元のパッケージID
    package_id = request.POST['package_id']
    feed = Feed.get_feeds_from_package_id(request.user, package_id)

    # user は STIPUser
    stip_user = request.user

    # liker情報取得
    likers = rs.get_likers_from_rs(stip_user, package_id)
    # すでにLikeされているか判定
    # 自分自身の liker 文字列は instance_name + space + user_name
    myliker = '%s %s' % (SNSConfig.get_sns_identity_name(), stip_user.username)
    like = myliker in likers
    # Like/Unlike 用の STIX イメージ作成
    feed_stix_like = FeedStixLike(feed, like, creator=stip_user)

    if like:
        # notify の unlike処理
        stip_user.unotify_liked(package_id, feed.user)
    else:
        # notify の like処理
        stip_user.notify_liked(package_id, feed.user)

    # 一時ファイルにstixの中身を書き出す
    tmp_file_path = write_like_comment_attach_stix(
        feed_stix_like.get_xml_content())
    # RS に登録する
    rs.regist_ctim_rs(stip_user, feed_stix_like.file_name, tmp_file_path)
    os.remove(tmp_file_path)

    # 現在の Like 情報を取得する
    likers = rs.get_likers_from_rs(stip_user, package_id)
    return HttpResponse(len(likers))
Exemplo n.º 2
0
def download_pdf(request):
    # 引数取得
    feed_file_name_id = request.POST['feed_id']
    package_id = request.POST['package_id']

    # STIX 情報作成
    feed_stix = get_feed_stix(feed_file_name_id)
    feed = Feed.get_feeds_from_package_id(request.user, package_id)
    # PDF 情報作成
    feed_pdf = FeedPDF(feed, feed_stix.stix_package)

    # HttpResponse作成
    file_name = '%s.pdf' % (feed_file_name_id)
    response = HttpResponse(content='application/pdf')
    # responseにPDF格納
    feed_pdf.make_pdf_content(response, feed)
    response['Content-Disposition'] = 'attachment; filename=%s' % (file_name)
    response['Content-Type'] = 'application/pdf'
    return response
Exemplo n.º 3
0
def post_comment(api_user, original_package_id, post, comment_user):
    # Feed 情報作成
    origin_feed = Feed.get_feeds_from_package_id(api_user, original_package_id)
    # comment 作成
    post = post.strip()
    if len(post) > 0:
        post = post[:10240]
    # Comment 用の STIX イメージ作成
    feed_stix_comment = FeedStixComment(origin_feed, post, api_user)
    # 一時ファイルにstixの中身を書き出す
    tmp_file_path = write_like_comment_attach_stix(
        feed_stix_comment.get_xml_content())
    # RS に登録する
    rs.regist_ctim_rs(api_user, feed_stix_comment.file_name, tmp_file_path)
    # 一時ファイルは削除
    os.remove(tmp_file_path)
    # 通知
    comment_user.notify_commented(original_package_id, origin_feed.user)
    notify_also_commented(original_package_id, origin_feed.user, comment_user)
Exemplo n.º 4
0
def create_sighting_object(request):
    try:
        package_id = request.GET['package_id']
        feed_id = request.GET['feed_id']
        value_ = request.GET['value']
        type_ = request.GET['type']
        count = int(request.GET['count'])
        first_seen = request.GET['first_seen']
        last_seen = request.GET['last_seen']
        observable_id = request.GET['observable_id']

        stip_user = request.user
        feed = Feed.get_feeds_from_package_id(stip_user, package_id)
        stix_file_path = Feed.get_cached_file_path(feed_id)
        stix2 = stip_sighting.convert_to_stix2_from_stix_file_path(
            stix_file_path)

        stix2 = stip_sighting.insert_sighting_object(stix2, type_, value_,
                                                     observable_id, count,
                                                     first_seen, last_seen,
                                                     stip_user)

        stix2_str = stix2.serialize(True, ensure_ascii=False)

        _, stix2_file_path = tempfile.mkstemp()
        with open(stix2_file_path, 'w', encoding='utf-8') as fp:
            fp.write(stix2_str)
        # RS に登録する
        rs.regist_ctim_rs(feed.user, stix2.id, stix2_file_path)
        os.remove(stix2_file_path)

        file_name = '%s.json' % (stix2.id)
        output = io.StringIO()
        output.write(str(stix2_str))
        response = HttpResponse(output.getvalue(),
                                content_type='application/json')
        response['Content-Disposition'] = 'attachment; filename=%s' % (
            file_name)
        return response
    except Exception as e:
        traceback.print_exc()
        return HttpResponseServerError(str(e))
Exemplo n.º 5
0
def call_jira(request):
    try:
        # JIRA が import されていない場合は何もしない
        if imported_jira is None:
            rsp = {}
            return JsonResponse(rsp)
        # feed情報取得
        feed_file_name_id = request.GET['feed_id']
        package_id_arg = request.GET['package_id']
        feed = Feed.get_feeds_from_package_id(request.user, package_id_arg)

        # JIRA instance
        proxies = System.get_request_proxies()
        j = JIRA(server=SNSConfig.get_jira_host(),
                 proxies=proxies,
                 basic_auth=(SNSConfig.get_jira_username(),
                             SNSConfig.get_jira_password()))
        # issues作成
        issue = j.create_issue(project=SNSConfig.get_jira_project(),
                               summary=feed.title,
                               description=feed.post,
                               issuetype={'name': SNSConfig.get_jira_type()})
        # 添付があればそれもつける
        for attach_file in feed.files.all():
            file_path = Feed.get_attach_file_path(attach_file.package_id)
            j.add_attachment(issue=issue,
                             attachment=file_path,
                             filename=str(attach_file.file_name))

        # STIX添付
        stix_package = STIXPackage.from_xml(feed.stix_file_path)
        package_id = stix_package.id_
        stix_file_name = '%s.xml' % (package_id)
        j.add_attachment(issue=issue,
                         attachment=feed.stix_file_path,
                         filename=stix_file_name)

        # CSV添付
        # CSVの中身を取得する
        content = get_csv_content(feed_file_name_id)
        csv_attachment = io.StringIO()
        csv_attachment.write(content)
        csv_file_name = '%s.csv' % (package_id)
        j.add_attachment(issue=issue,
                         attachment=csv_attachment,
                         filename=csv_file_name)

        # PDF添付
        feed_pdf = FeedPDF(feed, stix_package)
        pdf_attachment = io.BytesIO()
        feed_pdf.make_pdf_content(pdf_attachment, feed)
        pdf_file_name = '%s.pdf' % (package_id)
        j.add_attachment(issue=issue,
                         attachment=pdf_attachment,
                         filename=pdf_file_name)

        # isssue番号返却
        url = SNSConfig.get_jira_host(
        ) + '/projects/' + SNSConfig.get_jira_project() + '/issues/' + str(
            issue)
        rsp = {
            'issues': str(issue),
            'url': url,
        }
        return JsonResponse(rsp)
    except Exception as e:
        traceback.print_exc()
        return HttpResponseServerError(str(e))