def posts_on_first_newebe(step): for i in range(5): micropost = MicroPost( author = world.user.name, authorKey = world.user.key, content = "content %s" % i, ) time.sleep(1) micropost.save()
def post(self): ''' When post request is received, micropost content is expected inside a string under *content* of JSON object. It is extracted from it then stored inside a new Microposts object. Micropost author and date are set from incoming data. ''' data = self.get_body_as_dict(expectedFields=["date", "authorKey"]) if data: db_date = data.get("date") date = date_util.get_date_from_db_date(db_date) authorKey = data.get("authorKey") contact = ContactManager.getTrustedContact(authorKey) micropost = MicroPostManager.get_contact_micropost( authorKey, db_date) if contact: if not micropost: micropost = MicroPost( authorKey = authorKey, author = data["author"], content = data['content'], date = date, attachments = data.get("attachments", []), isMine = False, tags = contact.tags ) micropost.save() self._notify_suscribers(micropost) self.create_creation_activity(contact, micropost, "writes", "micropost") self._write_create_log(micropost) self.return_json(micropost.toJson(), 201) else: self.return_failure("Contact is not registered.", 405) else: self.return_failure("No data sent.", 405)
def post(self): ''' When post request is received, micropost data are expected as a JSON object. It is extracted from it then stored inside a new Microposts object. Micropost author is automatically set with current user and current date is set as date. Created microposts are forwarded to contacts. ''' logger.info("Micropost post received.") data = self.get_body_as_dict(expectedFields=["content", "tags"]) if data and data["content"]: user = UserManager.getUser() converter = Converter() micropost = MicroPost( authorKey = user.key, author = user.name, content = data['content'], attachments = converter.convert(data), tags = data["tags"] ) micropost.save() converter.add_files(micropost) self.create_owner_creation_activity(micropost, "writes", "micropost") self.send_creation_to_contacts(CONTACT_PATH, micropost) logger.info("Micropost successfuly posted.") self.return_json(micropost.toJson()) else: self.return_failure( "Sent data were incorrects. No post was created.", 405)
def put(self, key): ''' Resend deletion of micropost with *key* as key to the contact given in the posted JSON. Corresponding activity ID is given inside the posted json. Here is the format : {"contactId":"data","activityId":"data"} ''' data = self.get_body_as_dict( expectedFields=["contactId", "activityId", "extra"]) if data: contactId = data["contactId"] activityId = data["activityId"] date = data["extra"] contact = ContactManager.getTrustedContact(contactId) activity = ActivityManager.get_activity(activityId) if not contact: self.return_failure("Contact not found", 404) elif not activity: self.return_failure("Activity not found", 404) else: user = UserManager.getUser() micropost = MicroPost( authorKey = user.key, date = date_util.get_date_from_db_date(date) ) logger.info( "Attempt to resend a post deletion to contact: {}.".format( contact.name)) httpClient = ContactClient() body = micropost.toJson(localized=False) try: httpClient.put(contact, CONTACT_PATH, body, callback=(yield gen.Callback("retry"))) response = yield gen.Wait("retry") if response.error: self.return_failure( "Deleting micropost to contact failed.") else: for error in activity.errors: if error["contactKey"] == contact.key: activity.errors.remove(error) activity.save() self.return_success( "Micropost correctly redeleted.") except: self.return_failure("Deleting micropost to contact failed.") else: self.return_failure("Micropost not found", 404)
def given_there_are_3_posts_of_and_3_posts_of_my_contacts(step, nbposts, nbcontactposts): nbposts = int(nbposts) nbcontactposts = int(nbcontactposts) for i in range(1, nbposts + 1): micropost = MicroPost() micropost.author = UserManager.getUser().name micropost.authorKey = UserManager.getUser().key micropost.content = "my content {}".format(i) micropost.date = datetime.datetime(2011, i, 01, 11, 05, 12) micropost.isMine = True micropost.save() for i in range(1, nbcontactposts + 1): micropost = MicroPost() micropost.author = world.user2.name micropost.authorKey = world.user2.key micropost.content = "contact content {}".format(i) micropost.date = datetime.datetime(2011, i, 10, 11, 05, 12) micropost.isMine = False micropost.save()