From 8c97922bb9f03bf19b1ba4835713948dfe61d50d Mon Sep 17 00:00:00 2001 From: Eibriel Date: Mon, 20 Apr 2015 08:46:54 -0300 Subject: [PATCH] Added File collection --- attractsdk/__init__.py | 1 + attractsdk/files.py | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100755 attractsdk/files.py diff --git a/attractsdk/__init__.py b/attractsdk/__init__.py index 600bca2..2eeaccd 100644 --- a/attractsdk/__init__.py +++ b/attractsdk/__init__.py @@ -2,5 +2,6 @@ from .api import Api from .nodes import Node from .nodes import NodeType from .users import User +from .files import File from .exceptions import ResourceNotFound, UnauthorizedAccess, MissingConfig from .config import __version__, __pypi_packagename__ diff --git a/attractsdk/files.py b/attractsdk/files.py new file mode 100755 index 0000000..824923f --- /dev/null +++ b/attractsdk/files.py @@ -0,0 +1,41 @@ +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 . import utils + + +class File(List, Find, Create, Post, Update, Delete, Replace): + """Node class wrapping the REST nodes endpoint + """ + path = "files" + + def replace_picture(self, picture_file, api=None): + """Replaces the picture field in the node. + :param picture_file: A file object + """ + api = api or self.api + attributes = self.to_dict() + etag = attributes['_etag'] + attributes.pop('_id') + attributes.pop('_etag') + attributes.pop('_created') + attributes.pop('_updated') + attributes.pop('_links') + if 'parent' in attributes: + attributes.pop('parent') + if 'properties' not in attributes: + attributes['properties'] = {} + url = utils.join_url(self.path, str(self['_id'])) + headers = utils.merge_dict( + self.http_headers(), + {'If-Match': str(etag)}) + files = {'picture': picture_file} + new_attributes = api.patch(url, attributes, headers, files) + self.error = None + self.merge(new_attributes) + return self.success()