def then_i_have_6_microposts_ordered_by_date(step, nbposts): nbposts = int(nbposts) assert nbposts == len(world.microposts) for i in range(0, nbposts -1): assert date_util.get_date_from_db_date(world.microposts[i].get("date")) > \ date_util.get_date_from_db_date(world.microposts[i+1].get("date"))
def checks_that_notes_are_sorted_by_date(step): for i in range(len(world.notes)): if i > 0: if isinstance(world.test_notes[i], dict): assert date_util.get_date_from_db_date(world.test_notes[i - 1]["lastModified"]).time() > \ date_util.get_date_from_db_date(world.test_notes[i]["lastModified"]).time(), (world.test_notes[i - 1]["lastModified"]) + u" " + (world.test_notes[i]["lastModified"]) else: assert world.test_notes[i - 1].lastModified.time() > \ world.test_notes[i].lastModified.time()
def and_this_micropost_has_same_date_as_the_posted_one(step, timezone): micropost = world.microposts[0] posted_date = date_util.get_date_from_db_date(world.date_micropost["date"]) posted_date = date_util.convert_timezone_date_to_utc(posted_date) tz = pytz.timezone(timezone) contact_date = date_util.get_date_from_db_date(micropost["date"]) contact_date = date_util.convert_timezone_date_to_utc(contact_date, tz) assert contact_date == posted_date
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 checks_that_dict_convert_date_to_the_current_time_zone(step): lastModifiedDate = world.note.lastModified.replace(tzinfo=pytz.utc) lastModifiedDictDate = world.note.toDict()["lastModified"] lastModifiedDictDate = date_util.get_date_from_db_date(lastModifiedDictDate) lastModifiedDictDate = date_util.convert_timezone_date_to_utc(lastModifiedDictDate) assert lastModifiedDate == lastModifiedDictDate
def then_my_activity_date_is_converted_to_my_timezone(step): assert 0 < len(world.data) date = world.data[0].get("date") date = date_util.get_date_from_db_date(date) date = date_util.convert_timezone_date_to_utc(date) assert world.activity.date.replace(tzinfo=pytz.utc) == date, date
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() picture = Picture(authorKey=user.key, date=date_util.get_date_from_db_date(date)) info = "Attempt to resend a picture deletion to contact: {}." logger.info(info.format(contact.name)) self.forward_to_contact(picture, contact, activity, method="PUT") else: self.return_failure("Micropost not found", 404)
def and_this_micropost_has_timezone_date(step): world.date_micropost = world.microposts[0] db_micropost = MicroPostManager.get_micropost(world.date_micropost["_id"]) date = date_util.get_date_from_db_date(world.date_micropost["date"]) assert db_micropost.date.replace(tzinfo=pytz.utc) == \ date_util.convert_timezone_date_to_utc(date)
def retrieve_through_handler_the_note_with_note_id(step): note = client.fetch_document("notes/" + world.note._id + "/") world.test_note = Note( author=note["author"], title=note["title"], content=note["content"], lastModified=date_util.convert_timezone_date_to_utc(date_util.get_date_from_db_date(note["lastModified"])), isMine=note["isMine"], )
def check_that_request_date_is_set_to_europe_paris_timezone(step, timezone): Contact._db = db2 contact = ContactManager.getRequestedContacts().first() Contact._db = db date = date_util.get_date_from_db_date(world.contacts[0]["requestDate"]) tz = pytz.timezone(timezone) date = date.replace(tzinfo=tz) assert_equals( date_util.convert_utc_date_to_timezone(contact.requestDate, tz), date)
def create_through_handler_a_note(step): world.note = Note(title="test note creation", content="test content creation", date=datetime.datetime.utcnow()) response = client.post("notes/all/", body=world.note.toJson()) noteDict = json_decode(response.body) world.note = Note( author=noteDict["author"], title=noteDict["title"], content=noteDict["content"], lastModified=date_util.convert_timezone_date_to_utc(date_util.get_date_from_db_date(noteDict["lastModified"])), isMine=noteDict["isMine"], ) world.note._id = noteDict["_id"]
def creates_x_activities(step, nb_activities, nb_owner_activities, date): for i in range(int(nb_activities)): activity = Activity( author="me", docId="aaavvvbbbb%d" % i, verb="write", method="POST", isMine=i < int(nb_owner_activities), errors=[], docType="micropost", date=date_util.get_date_from_db_date(date), ) activity.save()
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( _id=data.get("_id", ""), 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_" + picture._id) 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 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", []), pictures_to_download=data.get("pictures", []), commons_to_download=data.get("commons", []), isMine=False, tags=contact.tags, ) micropost.save() self.create_creation_activity(contact, micropost, "writes", "micropost") self._write_create_log(micropost) postIndexer = indexer.Indexer() postIndexer.index_micropost(micropost) for websocket_client in websocket_clients: websocket_client.write_message(micropost.toJson()) 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): ''' Extract common and file linked to the common from request, then creates a common in database for the contact who sends it. An activity is created too. If author is not inside trusted contacts, the request is rejected. ''' data = self.get_body_as_dict( expectedFields=["title", "path", "contentType", "authorKey", "author", "date"]) if file and data: contact = ContactManager.getTrustedContact( data.get("authorKey", "")) if contact: date = date_util.get_date_from_db_date(data.get("date", "")) common = CommonManager.get_contact_common( contact.key, data.get("date", "")) if not common: common = Common( _id=data.get("_id", ""), 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 ) common.save() self.create_creation_activity(contact, common, "publishes", "common") logger.info("New common 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 toDict(self, localized=True): """ Return a dict representation of the document (copy). Removes _rev key and convert date field and request date field to local timezone if *localized* is set to True. """ docDict = NewebeDocument.toDict(self, localized) if localized and docDict.get("requestDate", ""): utc_date = date_util.get_date_from_db_date(docDict.get("requestDate")) date = date_util.convert_utc_date_to_timezone(utc_date) docDict["requestDate"] = date_util.get_db_date_from_date(date) return docDict
def toDict(self, localized=True): ''' Return a dict representation of the document (copy). Removes _rev key and convert date field and last modified field to local timezone if *localized* is set to True. ''' docDict = NewebeDocument.toDict(self, localized) if localized and "lastModified" in docDict: utc_date = get_date_from_db_date(docDict.get("lastModified")) date = convert_utc_date_to_timezone(utc_date) docDict["lastModified"] = get_db_date_from_date(date) return docDict
def toDict(self, localized=True): ''' Return a dict representation of the document (copy). Removes _rev key. ''' docDict = self.__dict__["_doc"].copy() if "_rev" in docDict: del docDict["_rev"] if localized and docDict.get("date", None): utc_date = get_date_from_db_date(docDict.get("date")) docDict["date"] = get_db_date_from_date(utc_date) return docDict
def toDict(self, localized=True): ''' Return a dict representation of the document (copy). Removes _rev key and convert date field and request date field to local timezone if *localized* is set to True. ''' docDict = NewebeDocument.toDict(self, localized) if localized and docDict.get("requestDate", ""): utc_date = date_util.get_date_from_db_date( docDict.get("requestDate")) date = date_util.convert_utc_date_to_timezone(utc_date) docDict["requestDate"] = date_util.get_db_date_from_date(date) return docDict
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 toDict(self, localized=True): ''' Return a dict representation of the document (copy). Removes _rev key and convert date field to local timezone if *localized* is set to True. ''' docDict = self.__dict__["_doc"].copy() if "_rev" in docDict: del docDict["_rev"] if localized and docDict.get("date", None): utc_date = get_date_from_db_date(docDict.get("date")) date = convert_utc_date_to_timezone(utc_date) docDict["date"] = get_db_date_from_date(date) return docDict
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() common = Common(authorKey=user.key, date=date_util.get_date_from_db_date(date)) info = "Attemp to resend a common deletion to contact: {}." logger.info(info.format(contact.name)) self.forward_to_contact(common, contact, activity, method="PUT") else: self.return_failure("Micropost not found", 404)
def when_i_convert_2011_02_01_13_45_32_to_utc_date(step, db_date): date = date_util.get_date_from_db_date(db_date) world.date = date_util.convert_timezone_date_to_utc(date)
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 then_dict_date_field_is_the_timezone_date(step): date = world.user.toDict().get("date") date = date_util.get_date_from_db_date(date) date = date_util.convert_timezone_date_to_utc(date) assert world.user.date.replace(tzinfo=pytz.utc) == date
def assert_that_date_is_well_converted_to_2011_02_01t12_45_32z(step, db_date): date = date_util.get_date_from_db_date(db_date) assert db_date == date.strftime(date_util.DB_DATETIME_FORMAT)
def when_i_convert_url_date_2011_02_01_13_45_32_to_utc_date(step, urlDate): world.date = date_util.get_db_utc_date_from_url_date(urlDate) world.date = date_util.get_date_from_db_date(world.date)