def add_three_pictures_to_the_database_with_different_dates(step): size = 200, 200 image = Image.open("test.jpg") image.thumbnail(size, Image.ANTIALIAS) image.save("th_test.jpg") file = open("th_test.jpg") for i in range(1, 4): picture = Picture( title = "Pic 0%d" % i, author = world.browser.user.name, authorKey = world.browser.user.key, date = datetime.datetime(2011, 11, i), path = "test.jpg", isMine = i != 3 ) picture.save() picture.put_attachment(file.read(), "th_test.jpg")
def post(self): ''' Extract picture and file linked to the picture from request, then creates a picture in database for the contact who sends it. An activity is created too. If author is not inside trusted contacts, the request is rejected. ''' file = self.request.files['picture'][0] data = json_decode(self.get_argument("json")) if file and data: contact = ContactManager.getTrustedContact( data.get("authorKey", "")) if contact: date = date_util.get_date_from_db_date(data.get("date", "")) picture = PictureManager.get_contact_picture( contact.key, data.get("date", "")) if not picture: picture = Picture( title = data.get("title", ""), path = data.get("path", ""), contentType = data.get("contentType", ""), authorKey = data.get("authorKey", ""), author = data.get("author", ""), tags = contact.tags, date = date, isMine = False, isFile = False ) picture.save() picture.put_attachment(content=file["body"], name="th_" + file['filename']) picture.save() self.create_creation_activity(contact, picture, "publishes", "picture") logger.info("New picture from %s" % contact.name) self.return_success("Creation succeeds", 201) else: self.return_failure("Author is not trusted.", 400) else: self.return_failure("No data sent.", 405)
def and_5_pictures_are_created_on_first_newebe(step): file = open("../../pictures/tests/test.jpg") for i in range(1, 6): picture = Picture( title = "Pic 0%d" % i, author = world.user.name, authorKey = world.user.key, date = datetime.datetime(2011, 11, i), path = "test.jpg", contentType = "image/jpeg", isMine = True ) picture.save() picture.put_attachment(file.read(), "th_test.jpg") picture.save()
def post(self): ''' Creates a picture and corresponding activity. Then picture is propagated to all trusted contacts. Errors are stored inside activity. ''' file = self.request.files['picture'][0] if file: filebody = file["body"] filename = file['filename'] picture = Picture( title = "New Picture", path = filename, contentType =file["content_type"], authorKey = UserManager.getUser().key, author = UserManager.getUser().name, isFile = True ) picture.save() picture.put_attachment(filebody, filename) thumbnail = self.get_thumbnail(filebody, filename, (200, 200)) thbuffer = thumbnail.read() picture.put_attachment(thbuffer, "th_" + filename) os.remove("th_" + filename) preview = self.get_thumbnail(filebody, filename, (1000, 1000)) picture.put_attachment(preview.read(), "prev_" + filename) os.remove("th_" + filename) picture.save() self.create_owner_creation_activity( picture, "publishes", "picture") self.send_files_to_contacts("pictures/contact/", fields = { "json": str(picture.toJson(localized=False)) }, files = [("picture", str(picture.path), thbuffer)]) logger.info("Picture %s successfuly posted." % filename) self.return_json(picture.toJson(), 201) else: self.return_failure("No picture posted.", 400)
def post(self): ''' Creates a picture and corresponding activity. Then picture is propagated to all trusted contacts. Errors are stored inside activity. ''' filebody = self.request.body filename = self.get_argument("qqfile") try: tag = self.get_argument("tag") except: tag = "all" filetype = mimetypes.guess_type(filename)[0] or \ 'application/octet-stream' if filebody: picture = Picture( title = "New Picture", path=filename, contentType=filetype, authorKey = UserManager.getUser().key, author = UserManager.getUser().name, isMine = True, isFile = True, tags = [tag] ) picture.save() picture.put_attachment(content=filebody, name=filename) thumbnail = self.get_thumbnail(filebody, filename, (200, 200)) thbuffer = thumbnail.read() picture.put_attachment(thbuffer, "th_" + filename) os.remove("th_" + filename) preview = self.get_thumbnail(filebody, filename, (1000, 1000)) picture.put_attachment(preview.read(), "prev_" + filename) os.remove("th_" + filename) picture.save() self.create_owner_creation_activity( picture, "publishes", "picture") self.send_files_to_contacts("pictures/contact/", fields = { "json": str(picture.toJson(localized=False)) }, files = [("picture", str(picture.path), thbuffer)], tag=tag) logger.info("Picture %s successfuly posted." % filename) self.return_json(picture.toJson(), 201) else: self.return_failure("No picture posted.", 400)