Beispiel #1
0
    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)
Beispiel #2
0
def posts_are_created_on_first_newebe_with_tag_tag(step, nbposts, tag):
    for i in range(int(nbposts)):
        micropost = MicroPost(author=world.user.name,
                              authorKey=world.user.key,
                              content="content %s" % i,
                              tags=[tag])
        micropost.save()
        time.sleep(1)
Beispiel #3
0
def posts_are_created_on_first_newebe_with_tag_tag(step, nbposts, tag):
    for i in range(int(nbposts)):
        micropost = MicroPost(
            author = world.user.name,
            authorKey = world.user.key,
            content = "content %s" % i,
            tags = [tag]
        )
        micropost.save()
        time.sleep(1)
Beispiel #4
0
def createMicropost(content):
    micropost = MicroPost()
    micropost.author = UserManager.getUser().name
    micropost.authorKey = UserManager.getUser().key
    micropost.content = content
    micropost.isMine = True
    micropost.save()
    return micropost
Beispiel #5
0
    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)
Beispiel #6
0
def creates_x_microposts(step, nb_docs):
    world.documents = []
    for i in range(int(nb_docs)):
        post = MicroPost(author="me",
                         content="test_content_%d" % i,
                         date=datetime.datetime.utcnow())
        world.documents.append(post)
Beispiel #7
0
    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"],
                                  pictures=data.get("pictures", []),
                                  commons=data.get("commons", []))
            micropost.save()
            converter.add_files(micropost)

            self.create_owner_creation_activity(micropost, "writes",
                                                "micropost")
            self.send_creation_to_contacts(CONTACT_PATH, micropost)
            postIndexer = indexer.Indexer()
            postIndexer.index_micropost(micropost)

            logger.info("Micropost successfuly posted.")
            self.return_json(micropost.toJson())

        else:
            self.return_failure(
                "Sent data were incorrects. No post was created.", 400)
Beispiel #8
0
    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)
            postIndexer = indexer.Indexer()
            postIndexer.index_micropost(micropost)

            logger.info("Micropost successfuly posted.")
            self.return_json(micropost.toJson())

        else:
            self.return_failure(
                    "Sent data were incorrects. No post was created.", 400)
Beispiel #9
0
    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)
Beispiel #10
0
    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)
Beispiel #11
0
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()
Beispiel #12
0
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()