def replace(self, params): new_username = params['username'] new_email = params['email'] user = User.replace(self.root.db, self.name, new_username=new_username, new_email=new_email) Photo.replace_author(self.root.db, self.name, new_username) new_item = Item(new_username, self.__parent__, self.request, user) user['href'] = new_item.url() user['photos'] = {'href': new_item['photos'].url()} return user
def replace(self, params): new_username = params['username'] new_email = params['email'] user = User.replace(self.root.db, self.name, new_username=new_username, new_email=new_email) Photo.replace_author(self.root.db, self.name, new_username) new_item = Item(new_username, self.parent, user) user['href'] = new_item.url() user['photos'] = { 'href': new_item['photos'].url() } return user
def test_post_photo(self): from example.model import Photo image_gif = pkg_resources.resource_stream('royal.tests.functional', 'image.gif') result = self.app.post( '/users/hadrien/photos', upload_files=[(u'image', u'image.gif', image_gif.read())] ) photo = Photo.get_one(self.db, author='hadrien') location = 'http://localhost/photos/%s/' % str(photo._id) headers = result.headers self.assertIn('Content-Type', headers) self.assertIn('Location', headers) self.assertEqual('application/json; charset=UTF-8', headers['Content-Type']) self.assertEqual(location, headers['Location']) expected = { 'href': location, '_id': str(photo._id), 'author': { 'href': 'http://localhost/users/hadrien/' } } self.assertEqual(expected, result.json)
def test_post_photo(self): from example.model import Photo image_gif = pkg_resources.resource_stream('royal.tests.functional', 'image.gif') result = self.app.post('/users/hadrien/photos', upload_files=[(u'image', u'image.gif', image_gif.read())]) photo = Photo.get_one(self.db, author='hadrien') location = 'http://localhost/photos/%s/' % str(photo._id) headers = result.headers self.assertIn('Content-Type', headers) self.assertIn('Location', headers) self.assertEqual('application/json; charset=UTF-8', headers['Content-Type']) self.assertEqual(location, headers['Location']) expected = { 'href': location, '_id': str(photo._id), 'author': { 'href': 'http://localhost/users/hadrien/' } } self.assertEqual(expected, result.json)
def index(self, params): offset = params['offset'] limit = params['limit'] cursor = Photo.get_newests(self.root.db, offset, limit) query = dict(offset=offset, limit=limit) return royal.PaginatedResult(self, cursor, Item, query, cursor.count())
def create(self, params): fs = params['image'] author = self.user.name mime_type = mimetypes.guess_extension(fs.filename) doc = Photo.create(self.root.db, author, fs.file, mime_type) return photos.Item(str(doc._id), self.root['photos'], self.request, doc)
def load_document(self): if self.document is None: self.document = Photo.get_by_id(self.root.db, self.__name__) if self.document is None: raise NotFound(self) return self.document
def index(self, query_params): offset = query_params["offset"] limit = query_params["limit"] cursor = Photo.get_newests(self.root.db, offset, limit, author=self.user.name) documents = [photos.Item(str(doc._id), self.root, self.request, doc).show() for doc in cursor] result = { "photos": documents, "href": self.url(offset=offset, limit=limit), "first": self.url(offset=0, limit=limit), } has_previous = offset > 0 if has_previous: result["previous"] = self.url(offset=max(offset - limit, 0), limit=limit) has_next = len(documents) == limit if has_next: result["next"] = self.url(offset=offset + limit, limit=limit) return result
def index(self, query_params): offset = query_params['offset'] limit = query_params['limit'] cursor = Photo.get_newests(self.root.db, offset, limit, author=self.parent.name) documents = [photos.Item(str(doc._id), self.root, doc).show() for doc in cursor] result = { 'photos': documents, 'href': self.url(offset=offset, limit=limit), 'first': self.url(offset=0, limit=limit), } has_previous = offset > 0 if has_previous: result['previous'] = self.url(offset=max(offset - limit, 0), limit=limit) has_next = len(documents) == limit if has_next: result['next'] = self.url(offset=offset + limit, limit=limit) return result
def index(self, query_params): offset = query_params['offset'] limit = query_params['limit'] cursor = Photo.get_newests(self.root.db, offset, limit, author=self.user.name) documents = [ photos.Item(str(doc._id), self.root, self.request, doc).show() for doc in cursor ] result = { 'photos': documents, 'href': self.url(offset=offset, limit=limit), 'first': self.url(offset=0, limit=limit), } has_previous = offset > 0 if has_previous: result['previous'] = self.url(offset=max(offset - limit, 0), limit=limit) has_next = len(documents) == limit if has_next: result['next'] = self.url(offset=offset + limit, limit=limit) return result
def create(self, params): fs = params['image'] author = unicode(self.parent.name) mime_type = mimetypes.guess_extension(fs.filename) doc = Photo.create(self.root.db, author, fs.file, mime_type) return photos.Item(str(doc._id), self.root['photos'], doc)
def create(self, **kwargs): fs = kwargs['image'] author = self.__parent__.__name__ mime_type = mimetypes.guess_extension(fs.filename) photo = Photo.create(self.root.db, author, fs.file, mime_type) return Resource(str(photo._id), self, photo)
def index(self, offset, limit): cursor = Photo.get_newests(self.root.db, offset, limit, author=self.__parent__.__name__) query = dict(offset=offset, limit=limit) return royal.PaginatedResult(self, cursor, Resource, query, cursor.count())
def create(self, params): fs = params["image"] author = self.user.name mime_type = mimetypes.guess_extension(fs.filename) doc = Photo.create(self.root.db, author, fs.file, mime_type) return photos.Item(str(doc._id), self.root["photos"], self.request, doc)