chg: [API] add GET: item metadata + item content + item tags

This commit is contained in:
Terrtia 2019-07-26 15:44:29 +02:00
parent 0a756294fe
commit 6af9514a48
No known key found for this signature in database
GPG key ID: 1E1B1F50D84613D0
4 changed files with 114 additions and 2 deletions

16
bin/packages/Item.py Executable file
View file

@ -0,0 +1,16 @@
#!/usr/bin/env python3
# -*-coding:UTF-8 -*
import os
import redis
import Flask_config
import Date
PASTES_FOLDER = Flask_config.PASTES_FOLDER
def exist_item(item_id):
if os.path.isfile(os.path.join(PASTES_FOLDER, item_id)):
return True
else:
return False

View file

@ -115,6 +115,17 @@ class Paste(object):
self.p_duplicate = None self.p_duplicate = None
self.p_tags = None self.p_tags = None
def get_item_dict(self):
dict_item = {}
dict_item['id'] = self.p_rel_path
dict_item['date'] = str(self.p_date)
dict_item['content'] = self.get_p_content()
tags = self._get_p_tags()
if tags:
dict_item['tags'] = tags
return dict_item
def get_p_content(self): def get_p_content(self):
""" """
Returning the content of the Paste Returning the content of the Paste
@ -321,8 +332,8 @@ class Paste(object):
return self.store_metadata.scard('dup:'+self.p_path) + self.store_metadata.scard('dup:'+self.p_rel_path) return self.store_metadata.scard('dup:'+self.p_path) + self.store_metadata.scard('dup:'+self.p_rel_path)
def _get_p_tags(self): def _get_p_tags(self):
self.p_tags = self.store_metadata.smembers('tag:'+path, tag) self.p_tags = self.store_metadata.smembers('tag:'+self.p_rel_path)
if self.self.p_tags is not None: if self.p_tags is not None:
return list(self.p_tags) return list(self.p_tags)
else: else:
return '[]' return '[]'

View file

@ -10,6 +10,7 @@ from pytaxonomies import Taxonomies
from pymispgalaxies import Galaxies, Clusters from pymispgalaxies import Galaxies, Clusters
r_serv_tags = Flask_config.r_serv_tags r_serv_tags = Flask_config.r_serv_tags
r_serv_metadata = Flask_config.r_serv_metadata
def get_taxonomie_from_tag(tag): def get_taxonomie_from_tag(tag):
return tag.split(':')[0] return tag.split(':')[0]
@ -61,3 +62,10 @@ def is_valid_tags_taxonomies_galaxy(list_tags, list_tags_galaxy):
if not is_galaxy_tag_enabled(galaxy, tag): if not is_galaxy_tag_enabled(galaxy, tag):
return False return False
return True return True
def get_item_tags(item_id):
tags = r_serv_metadata.smembers('tag:'+item_id)
if tags:
return list(tags)
else:
return '[]'

View file

@ -14,6 +14,8 @@ import redis
import datetime import datetime
import Import_helper import Import_helper
import Item
import Paste
import Tags import Tags
from flask import Flask, render_template, jsonify, request, Blueprint, redirect, url_for, Response from flask import Flask, render_template, jsonify, request, Blueprint, redirect, url_for, Response
@ -144,6 +146,81 @@ def items():
return Response(json.dumps({'test': 2}), mimetype='application/json') return Response(json.dumps({'test': 2}), mimetype='application/json')
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# GET
#
# {
# "id": item_id, mandatory
# }
#
# response: {
# "id": "item_id",
# "date": "date",
# "tags": [],
# "content": "item content"
# }
#
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
@restApi.route("api/get/item/info/<path:item_id>", methods=['GET'])
@token_required('admin')
def get_item_id(item_id):
try:
item_object = Paste.Paste(item_id)
except FileNotFoundError:
return Response(json.dumps({'status': 'error', 'reason': 'Item not found'}, indent=2, sort_keys=True), mimetype='application/json'), 400
data = item_object.get_item_dict()
return Response(json.dumps(data, indent=2, sort_keys=True), mimetype='application/json')
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# GET
#
# {
# "id": item_id, mandatory
# }
#
# response: {
# "id": "item_id",
# "tags": [],
# }
#
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
@restApi.route("api/get/item/tag/<path:item_id>", methods=['GET'])
@token_required('admin')
def get_item_tag(item_id):
if not Item.exist_item(item_id):
return Response(json.dumps({'status': 'error', 'reason': 'Item not found'}, indent=2, sort_keys=True), mimetype='application/json'), 400
tags = Tags.get_item_tags(item_id)
dict_tags = {}
dict_tags['id'] = item_id
dict_tags['tags'] = tags
return Response(json.dumps(dict_tags, indent=2, sort_keys=True), mimetype='application/json')
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# GET
#
# {
# "id": item_id, mandatory
# }
#
# response: {
# "id": "item_id",
# "content": "item content"
# }
#
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
@restApi.route("api/get/item/content/<path:item_id>", methods=['GET'])
@token_required('admin')
def get_item_content(item_id):
try:
item_object = Paste.Paste(item_id)
except FileNotFoundError:
return Response(json.dumps({'status': 'error', 'reason': 'Item not found'}, indent=2, sort_keys=True), mimetype='application/json'), 400
item_object = Paste.Paste(item_id)
dict_content = {}
dict_content['id'] = item_id
dict_content['content'] = item_object.get_p_content()
return Response(json.dumps(dict_content, indent=2, sort_keys=True), mimetype='application/json')
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# #