API endpoint /api/nodes/tagged/<tag>
This endpoint returns nodes in public projects that have the given tag. The returned JSON is cached for 5 minutes.
This commit is contained in:
@@ -760,6 +760,8 @@ class PillarServer(BlinkerCompatibleEve):
|
||||
coll.create_index([('properties.status', pymongo.ASCENDING),
|
||||
('node_type', pymongo.ASCENDING),
|
||||
('_created', pymongo.DESCENDING)])
|
||||
# Used for asset tags
|
||||
coll.create_index([('properties.tags', pymongo.ASCENDING)])
|
||||
|
||||
coll = db['projects']
|
||||
# This index is used for statistics, and for fetching public projects.
|
||||
|
@@ -1,6 +1,7 @@
|
||||
import base64
|
||||
import functools
|
||||
import logging
|
||||
import typing
|
||||
import urllib.parse
|
||||
|
||||
import pymongo.errors
|
||||
@@ -89,6 +90,48 @@ def share_node(node_id):
|
||||
return jsonify(short_link_info(short_code), status=status)
|
||||
|
||||
|
||||
@blueprint.route('/tagged/')
|
||||
@blueprint.route('/tagged/<tag>')
|
||||
def tagged(tag=''):
|
||||
"""Return all tagged nodes of public projects as JSON."""
|
||||
|
||||
# We explicitly register the tagless endpoint to raise a 404, otherwise the PATCH
|
||||
# handler on /api/nodes/<node_id> will return a 405 Method Not Allowed.
|
||||
if not tag:
|
||||
raise wz_exceptions.NotFound()
|
||||
|
||||
return _tagged(tag)
|
||||
|
||||
|
||||
def _tagged(tag: str):
|
||||
"""Fetch all public nodes with the given tag.
|
||||
|
||||
This function is cached, see setup_app().
|
||||
"""
|
||||
nodes_coll = current_app.db('nodes')
|
||||
agg = nodes_coll.aggregate([
|
||||
{'$match': {'properties.tags': tag,
|
||||
'_deleted': {'$ne': True}}},
|
||||
|
||||
# Only get nodes from public projects. This is done after matching the
|
||||
# tagged nodes, because most likely nobody else will be able to tag
|
||||
# nodes anyway.
|
||||
{'$lookup': {
|
||||
'from': 'projects',
|
||||
'localField': 'project',
|
||||
'foreignField': '_id',
|
||||
'as': '_project',
|
||||
}},
|
||||
{'$match': {'_project.is_private': False}},
|
||||
|
||||
# Don't return the entire project for each node.
|
||||
{'$project': {'_project': False}},
|
||||
|
||||
{'$sort': {'_created': -1}}
|
||||
])
|
||||
return jsonify(list(agg))
|
||||
|
||||
|
||||
def generate_and_store_short_code(node):
|
||||
nodes_coll = current_app.data.driver.db['nodes']
|
||||
node_id = node['_id']
|
||||
@@ -442,6 +485,11 @@ def parse_markdowns(items):
|
||||
|
||||
|
||||
def setup_app(app, url_prefix):
|
||||
global _tagged
|
||||
|
||||
cached = app.cache.memoize(timeout=300)
|
||||
_tagged = cached(_tagged)
|
||||
|
||||
from . import patch
|
||||
patch.setup_app(app, url_prefix=url_prefix)
|
||||
|
||||
|
Reference in New Issue
Block a user