def addAlbum(self, parent, albumName, title, description=''): """ Adds an album to the given parent album parent(Album) : The parent Album object albumName(str) : The name of the album title(str) : The album title description(str) : The album description returns(Album) : The Album object that was created """ if not parent.can_edit: raise G3AuthError('You do not have permission to edit: %s' % parent.title) data = { 'type': 'album', 'name': albumName, 'title': title, 'description': description, } req = PostRequest(parent.url, self.apiKey, data) resp = self._openReq(req) newObjUrl = self._getUrlFromResp(resp) item = getItemFromResp(self.getRespFromUrl(newObjUrl), self, parent) parent._members.append(newObjUrl) parent.members.append(item) return item
def getRandomImage(self , album , direct=True): """ Returns a random RemoteImage object for the album. If "direct" is False, a random image can be pulled from nested albums. album(Album) : The album object to pull the random image from direct(bool) : If set to False, the image may be pulled from a sub-album returns(RemoteImage) : Returns a RemoteImage instance """ scope = ('all' , 'direct')[direct] data = { 'type': 'photo' , 'random': 'true' , 'scope': scope , } url = '%s?%s' % (album.url , urlencode(data)) resp = self.getRespFromUrl(url) # We get an album item back with a single member image images = getItemFromResp(resp , self).Images if not images: return None return images[0]
def addAlbum(self , parent , albumName , title , description=''): """ Adds an album to the given parent album parent(Album) : The parent Album object albumName(str) : The name of the album title(str) : The album title description(str) : The album description returns(Album) : The Album object that was created """ if not parent.can_edit: raise G3AuthError('You do not have permission to edit: %s' % parent.title) data = { 'type': 'album' , 'name': albumName , 'title': title , 'description': description , } req = PostRequest(parent.url , self.apiKey , data) resp = self._openReq(req) newObjUrl = self._getUrlFromResp(resp) item = getItemFromResp(self.getRespFromUrl(newObjUrl) , self , parent) parent._members.append(newObjUrl) # This appears to cause a race #parent.members.append(item) return item
def getRoot(self): """ Returns the root item (album) """ if self.root is None: resp = self.getRespFromUri(self._rootUri) self.root = getItemFromResp(resp, self) return self.root
def getRoot(self): """ Returns the root item (album) """ if self.root is None: resp = self.getRespFromUri(self._rootUri) self.root = getItemFromResp(resp , self) return self.root
def addImage(self, parent, image, title='', description='', name=''): """ Add a LocalImage to the parent album. parent(Album) : The parent album to add the image to image(LocalImage) : The local image to upload and add to the parent title(str) : The image title description(str) : The image description name(str) : The image file name returns(RemoteImage) : The RemoteImage instance for the item uploaded """ if not parent.can_edit: raise G3AuthError('You do not have permission to edit: %s' % parent.title) if name: image.Filename = name entity = { 'name': image.filename, 'type': image.type, 'title': title, 'description': description, } boundary = str(uuid4()) headers = { 'Content-Type': 'multipart/form-data; boundary=%s' % boundary } # this is more complicated than adding an album. We have to # construct the upload MIME headers, including build the string # data section data = '--%s\r\n' % boundary data += 'Content-Disposition: form-data; name="entity"\r\n' data += 'Content-Type: text/plain; ' \ 'charset=UTF-8\r\n' data += 'Content-Transfer-Encoding: 8bit\r\n' data += '\r\n' data += '%s\r\n' % json.dumps(entity, separators=(',', ':')) data += '--%s\r\n' % boundary data += image.getUploadContent() data += '--%s--\r\n' % boundary req = PostRequest(parent.url, self.apiKey, data, headers) resp = self._openReq(req) newObjUrl = self._getUrlFromResp(resp) item = getItemFromResp(self.getRespFromUrl(newObjUrl), self, parent) parent._members.append(newObjUrl) parent.members.append(item) return item
def addImage(self , parent , image , title='' , description='' , name=''): """ Add a LocalImage to the parent album. parent(Album) : The parent album to add the image to image(LocalImage) : The local image to upload and add to the parent title(str) : The image title description(str) : The image description name(str) : The image file name returns(RemoteImage) : The RemoteImage instance for the item uploaded """ if not parent.can_edit: raise G3AuthError('You do not have permission to edit: %s' % parent.title) if name: image.Filename = name entity = { 'name': image.filename , 'type': image.type , 'title': title , 'description': description , } boundary = str(uuid4()) headers = {'Content-Type': 'multipart/form-data; boundary=%s' % boundary} # this is more complicated than adding an album. We have to # construct the upload MIME headers, including build the string # data section data = '--%s\r\n' % boundary data += 'Content-Disposition: form-data; name="entity"\r\n' data += 'Content-Type: text/plain; ' \ 'charset=UTF-8\r\n' data += 'Content-Transfer-Encoding: 8bit\r\n' data += '\r\n' data += '%s\r\n' % json.dumps(entity , separators=(',' , ':')) data += '--%s\r\n' % boundary data += image.getUploadContent() data += '--%s--\r\n' % boundary req = PostRequest(parent.url , self.apiKey , data , headers) resp = self._openReq(req) newObjUrl = self._getUrlFromResp(resp) item = getItemFromResp(self.getRespFromUrl(newObjUrl) , self , parent) parent._members.append(newObjUrl) #parent.members.append(item) return item
def getRandomImage(self, album, direct=True): """ Returns a random RemoteImage object for the album. If "direct" is False, a random image can be pulled from nested albums. album(Album) : The album object to pull the random image from direct(bool) : If set to False, the image may be pulled from a sub-album returns(RemoteImage) : Returns a RemoteImage instance """ scope = ('all', 'direct')[direct] data = { 'type': 'photo', 'random': 'true', 'scope': scope, } url = '%s?%s' % (album.url, urlencode(data)) resp = self.getRespFromUrl(url) return getItemFromResp(resp, self)
def addComment(self, image, comment): """ Comment on this item with the string "comment" comment(str) : The comment returns(Comment) : The comment that was created """ data = { 'item': image.url, 'text': comment, } url = self._buildUrl('index.php/rest/comments') req = PostRequest(url, self.apiKey, data) resp = self._openReq(req) commUrl = json.loads(resp.read())['url'] resp = self.getRespFromUrl(commUrl) comm = getItemFromResp(resp, self, image) if hasattr(image, 'comments'): image.comments.append(comm) return comm
def addComment(self , image , comment): """ Comment on this item with the string "comment" comment(str) : The comment returns(Comment) : The comment that was created """ data = { 'item': image.url , 'text': comment , } url = self._buildUrl('index.php/rest/comments') req = PostRequest(url , self.apiKey , data) resp = self._openReq(req) commUrl = json.loads(resp.read())['url'] resp = self.getRespFromUrl(commUrl) comm = getItemFromResp(resp , self , image) if hasattr(image , 'comments'): image.comments.append(comm) return comm