147 lines
5.0 KiB
Python
Executable File
147 lines
5.0 KiB
Python
Executable File
import json
|
|
import copy
|
|
|
|
import os.path
|
|
|
|
from .resource import List
|
|
from .resource import Find
|
|
from .resource import Create
|
|
from .resource import Post
|
|
from .resource import Update
|
|
from .resource import Delete
|
|
from .resource import Replace
|
|
from .exceptions import ResourceNotFound
|
|
|
|
from . import utils
|
|
from .api import Api
|
|
|
|
|
|
class Node(List, Find, Create, Post, Update, Delete, Replace):
|
|
"""Node class wrapping the REST nodes endpoint
|
|
"""
|
|
path = "nodes"
|
|
ensure_query_projections = {'project': 1, 'node_type': 1}
|
|
|
|
@classmethod
|
|
def find_one(cls, params, api=None):
|
|
"""Get one resource starting from parameters different than the resource
|
|
id. TODO if more than one match for the query is found, raise exception.
|
|
"""
|
|
api = api or Api.Default()
|
|
|
|
# Force delivery of only 1 result
|
|
params['max_results'] = 1
|
|
cls._ensure_projections(params, cls.ensure_query_projections)
|
|
url = utils.join_url_params(cls.path, params)
|
|
|
|
response = api.get(url)
|
|
# Keep the response a dictionary, and cast it later into an object.
|
|
if response['_items']:
|
|
item = utils.convert_datetime(response['_items'][0])
|
|
return cls(item)
|
|
else:
|
|
raise ResourceNotFound(response)
|
|
|
|
def update(self, attributes=None, api=None):
|
|
api = api or self.api
|
|
attributes = attributes or self.to_dict()
|
|
etag = attributes['_etag']
|
|
attributes = utils.remove_private_keys(attributes)
|
|
attributes = utils.remove_none_attributes(attributes)
|
|
|
|
url = utils.join_url(self.path, str(self['_id']))
|
|
headers = utils.merge_dict(
|
|
self.http_headers(),
|
|
{'If-Match': str(etag)})
|
|
new_attributes = api.put(url, attributes, headers)
|
|
self.error = None
|
|
self.merge(new_attributes)
|
|
return self.success()
|
|
|
|
def has_method(self, method):
|
|
if method in self.allowed_methods:
|
|
return True
|
|
return False
|
|
|
|
@classmethod
|
|
def latest(cls, node_type, api=None):
|
|
"""Get list of latestnodes."""
|
|
|
|
api = api or Api.Default()
|
|
url = 'latest/%s' % node_type
|
|
|
|
response = api.get(url)
|
|
for item in response['_items']:
|
|
utils.convert_datetime(item)
|
|
return cls.list_class(response)
|
|
|
|
@classmethod
|
|
def create_asset_from_file(cls, project_id, parent_node_id, asset_type, filename,
|
|
always_create_new_node=False,
|
|
api=None):
|
|
"""Uploads the file to the Cloud and creates an asset node.
|
|
|
|
If a node with the project, node_type (always 'asset') and name (always
|
|
basename of the given filename) exists, it will be updated (unless
|
|
always_create_new_node is True).
|
|
|
|
:param project_id: the project ID
|
|
:param parent_node_id: node ID to attach this asset node to. Can be None.
|
|
:param asset_type: 'image', 'file', 'video', etc.
|
|
:param filename: path of the file to upload. Must be readable.
|
|
:param always_create_new_node: when True, a new node is always created,
|
|
possibly with the same name & parent as an existing one.
|
|
:returns: the updated/created node
|
|
:rtype: Node
|
|
"""
|
|
|
|
api = api or Api.Default()
|
|
|
|
# Upload the file.
|
|
with open(filename, mode='rb') as infile:
|
|
file_upload_resp = api.post('storage/stream/%s' % project_id,
|
|
files={'file': infile})
|
|
if file_upload_resp['status'] != 'ok':
|
|
raise ValueError('Received bad status %s from Pillar: %s' %
|
|
(file_upload_resp['status'], json.dumps(file_upload_resp)))
|
|
file_id = file_upload_resp['file_id']
|
|
|
|
# Create or update the node.
|
|
basic_properties = {
|
|
'project': project_id,
|
|
'node_type': 'asset',
|
|
'name': os.path.basename(filename)
|
|
}
|
|
if parent_node_id:
|
|
basic_properties['parent'] = parent_node_id
|
|
|
|
if not always_create_new_node:
|
|
# Try to find an existing one to see if there is anything to update.
|
|
existing_node = cls.find_first({'where': basic_properties}, api=api)
|
|
if existing_node:
|
|
# Just update the file ID and we're done.
|
|
existing_node.properties.content_type = asset_type
|
|
existing_node.properties.file = file_id
|
|
existing_node.update(api=api)
|
|
return existing_node
|
|
|
|
basic_properties.update({
|
|
'properties': {'content_type': asset_type,
|
|
'file': file_id},
|
|
})
|
|
node = cls(basic_properties)
|
|
node.create(api=api)
|
|
|
|
return node
|
|
|
|
|
|
class NodeType(List, Find, Create, Post, Delete):
|
|
"""NodeType class wrapping the REST node_types endpoint
|
|
"""
|
|
path = "node_types"
|
|
|
|
def has_method(self, method):
|
|
if method in self.allowed_methods:
|
|
return True
|
|
return False
|