diff --git a/bin/packages/Item.py b/bin/packages/Item.py new file mode 100755 index 00000000..1bc77b79 --- /dev/null +++ b/bin/packages/Item.py @@ -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 diff --git a/bin/packages/Paste.py b/bin/packages/Paste.py index 1087880b..6c464610 100755 --- a/bin/packages/Paste.py +++ b/bin/packages/Paste.py @@ -115,6 +115,17 @@ class Paste(object): self.p_duplicate = 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): """ 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) def _get_p_tags(self): - self.p_tags = self.store_metadata.smembers('tag:'+path, tag) - if self.self.p_tags is not None: + self.p_tags = self.store_metadata.smembers('tag:'+self.p_rel_path) + if self.p_tags is not None: return list(self.p_tags) else: return '[]' diff --git a/bin/packages/Tags.py b/bin/packages/Tags.py index 88963732..d6636ca7 100755 --- a/bin/packages/Tags.py +++ b/bin/packages/Tags.py @@ -10,6 +10,7 @@ from pytaxonomies import Taxonomies from pymispgalaxies import Galaxies, Clusters r_serv_tags = Flask_config.r_serv_tags +r_serv_metadata = Flask_config.r_serv_metadata def get_taxonomie_from_tag(tag): 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): return False return True + +def get_item_tags(item_id): + tags = r_serv_metadata.smembers('tag:'+item_id) + if tags: + return list(tags) + else: + return '[]' diff --git a/var/www/modules/restApi/Flask_restApi.py b/var/www/modules/restApi/Flask_restApi.py index ae3f0375..2c10ad62 100644 --- a/var/www/modules/restApi/Flask_restApi.py +++ b/var/www/modules/restApi/Flask_restApi.py @@ -14,6 +14,8 @@ import redis import datetime import Import_helper +import Item +import Paste import Tags 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') +# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # +# GET +# +# { +# "id": item_id, mandatory +# } +# +# response: { +# "id": "item_id", +# "date": "date", +# "tags": [], +# "content": "item content" +# } +# +# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # +@restApi.route("api/get/item/info/", 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/", 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/", 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') # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #