Пример #1
0
    def put(self):
        '''
        When a delete request from a contact is incoming, it executes the
        delete request locally then it creates a new activity corresponding
        to this deletion.
        '''

        data = self.get_body_as_dict(expectedFields=["date", "authorKey"])

        if data:
            authorKey = data["authorKey"]
            date = data["date"]

            micropost = MicroPostManager.get_contact_micropost(authorKey, date)
            contact = ContactManager.getTrustedContact(authorKey)

            if micropost and contact:
                self.create_deletion_activity(contact, micropost, "deletes",
                        "micropost")
                postIndexer = indexer.Indexer()
                postIndexer.remove_doc(micropost)
                micropost.delete()

                self._write_delete_log(micropost)
                self.return_success("Micropost deleted.")

            else:
                self.return_failure("Micropost not found", 404)

        else:
            self.return_failure("No data sent.", 400)
Пример #2
0
    def delete(self, id):
        '''
        Deletes the post of which id is equal to postId, add an activity and
        forwards the delete request to each trusted contact.

        put instead of delete because tornado does not support body in DELETE
        requests...
        '''

        micropost = MicroPostManager.get_micropost(id)

        if micropost:
            user = UserManager.getUser()

            if micropost.authorKey == user.key:
                self.create_owner_deletion_activity(micropost, "deletes",
                                                    "micropost")
                self.send_deletion_to_contacts(CONTACT_PATH, micropost)
            postIndexer = indexer.Indexer()
            postIndexer.remove_doc(micropost)
            micropost.delete()
            self.return_success("Micropost deletion succeeds.")

        else:
            self.return_failure("Micropost not found.", 404)
Пример #3
0
    def put(self):
        '''
        When a delete request from a contact is incoming, it executes the
        delete request locally then it creates a new activity corresponding
        to this deletion.
        '''

        data = self.get_body_as_dict(expectedFields=["date", "authorKey"])

        if data:
            authorKey = data["authorKey"]
            date = data["date"]

            micropost = MicroPostManager.get_contact_micropost(authorKey, date)
            contact = ContactManager.getTrustedContact(authorKey)

            if micropost and contact:
                self.create_deletion_activity(contact, micropost, "deletes",
                                              "micropost")
                postIndexer = indexer.Indexer()
                postIndexer.remove_doc(micropost)
                micropost.delete()

                self._write_delete_log(micropost)
                self.return_success("Micropost deleted.")

            else:
                self.return_failure("Micropost not found", 404)

        else:
            self.return_failure("No data sent.", 400)
Пример #4
0
    def on_download_finished(self, response):
        logger.info(self.picture)

        if response.code == 200:
            filename = '%s.jpg' % self.picture._id
            self.picture.put_attachment(response.body, filename)
            thumbnail = self.get_thumbnail(response.body, filename,
                                           (1000, 1000))
            thbuffer = thumbnail.read()
            self.picture.put_attachment(thbuffer, "prev_" + filename)
            thpath = os.path.join(CONFIG.main.path, "th_" + filename)
            os.remove(thpath)
            self.picture.isFile = True
            self.picture.save()

            micropost = MicroPostManager.get_picture_micropost(
                self.picture._id)
            if micropost is not None:
                micropost.pictures.append(self.picture._id)
                micropost.pictures_to_download.remove(self.picture._id)
                micropost.save()

            CURRENT_DOWNLOADS.remove(self.picture.toDict()["_id"])
            self.return_success("Picture successfuly downloaded.")

        else:
            CURRENT_DOWNLOADS.remove(self.picture.toDict()["_id"])
            self.return_failure("Picture cannot be retrieved.")
Пример #5
0
    def post(self, postId):
        """
        Grab from contact the file corresponding to given path and given post
        (post of which ID is equal to *postId*).
        """

        data = self.get_body_as_dict(expectedFields=["path"])

        micropost = MicroPostManager.get_micropost(postId)
        contact = ContactManager.getTrustedContact(micropost.authorKey)
        user = UserManager.getUser()
        if micropost and data and contact:
            path = data["path"]
            client = ContactClient()
            body = {"date": date_util.get_db_date_from_date(micropost.date), "contactKey": user.key, "path": path}

            client.post(
                contact, "microposts/contacts/attach/", json_encode(body), callback=(yield gen.Callback("getattach"))
            )
            response = yield gen.Wait("getattach")

            if response.error:
                self.return_failure("An error occured while retrieving picture.")
            else:
                micropost.put_attachment(response.body, data["path"])
                self.return_success("Download succeeds.")

        else:
            if not data:
                self.return_failure("Wrong data.", 400)
            elif not contact:
                self.return_failure("Contact no more available.", 400)
            else:
                self.return_failure("Micropost not found.", 404)
Пример #6
0
    def delete(self, id):
        '''
        Deletes the post of which id is equal to postId, add an activity and
        forwards the delete request to each trusted contact.

        put instead of delete because tornado does not support body in DELETE
        requests...
        '''

        micropost = MicroPostManager.get_micropost(id)

        if micropost:
            user = UserManager.getUser()

            if micropost.authorKey == user.key:
                self.create_owner_deletion_activity(
                    micropost, "deletes", "micropost")
                self.send_deletion_to_contacts(CONTACT_PATH, micropost)
            postIndexer = indexer.Indexer()
            postIndexer.remove_doc(micropost)
            micropost.delete()
            self.return_success("Micropost deletion succeeds.")

        else:
            self.return_failure("Micropost not found.", 404)
Пример #7
0
    def post(self):
        '''
        Returns file which is attached to post corresponding to a given
        date (we assumed a user can't post two posts at the same time).
        Expected data :
        * path : file name
        * date : date on which post was posted
        * contactKey : the key of the contact which claims the file.
        '''

        data = self.get_body_as_dict(expectedFields=["path", "date",
            "contactKey"])

        if data:
            contact = ContactManager.getTrustedContact(data["contactKey"])
            micropost = MicroPostManager.get_first(data["date"])

            if micropost and contact:
                try:
                    fileContent = micropost.fetch_attachment(data["path"])
                    self.return_file(data["path"], fileContent)
                except ResourceNotFound:
                    self.return_failure("File not found", 404)
            else:
                self.return_failure("Micropost not found.", 404)
        else:
            self.return_failure("Wrong data.", 400)
Пример #8
0
    def post(self):
        '''
        Returns file which is attached to post corresponding to a given
        date (we assumed a user can't post two posts at the same time).
        Expected data :
        * path : file name
        * date : date on which post was posted
        * contactKey : the key of the contact which claims the file.
        '''

        data = self.get_body_as_dict(
            expectedFields=["path", "date", "contactKey"])

        if data:
            contact = ContactManager.getTrustedContact(data["contactKey"])
            micropost = MicroPostManager.get_first(data["date"])

            if micropost and contact:
                try:
                    fileContent = micropost.fetch_attachment(data["path"])
                    self.return_file(data["path"], fileContent)
                except ResourceNotFound:
                    self.return_failure("File not found", 404)
            else:
                self.return_failure("Micropost not found.", 404)
        else:
            self.return_failure("Wrong data.", 400)
Пример #9
0
    def post(self, key):
        '''
        Resend post 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"}
        '''

        micropost = MicroPostManager.get_micropost(key)

        data = self.get_body_as_dict(
            expectedFields=["contactId", "activityId"])

        if micropost and data:

            contactId = data["contactId"]
            activityId = data["activityId"]

            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:
                if contact.name:
                    info_str = "Attempt to resend a post to contact: %s."
                    logger.info(info_str % contact.name)
                self.forward_to_contact(micropost, contact, activity)
        else:
            self.return_failure("Micropost not found", 404)
Пример #10
0
    def post(self, key):
        '''
        Resend post 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"}
        '''

        micropost = MicroPostManager.get_micropost(key)

        data = self.get_body_as_dict(expectedFields=["contactId",
                                                     "activityId"])

        if micropost and data:

            contactId = data["contactId"]
            activityId = data["activityId"]

            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:
                if contact.name:
                    info_str = "Attempt to resend a post to contact: %s."
                    logger.info(info_str % contact.name)
                self.forward_to_contact(micropost, contact, activity)
        else:
            self.return_failure("Micropost not found", 404)
Пример #11
0
    def on_download_finished(self, response):
        logger.info(self.picture)

        if response.code == 200:
            filename = "%s.jpg" % self.picture._id
            self.picture.put_attachment(response.body, filename)
            thumbnail = self.get_thumbnail(response.body, filename, (1000, 1000))
            thbuffer = thumbnail.read()
            self.picture.put_attachment(thbuffer, "prev_" + filename)
            thpath = os.path.join(CONFIG.main.path, "th_" + filename)
            os.remove(thpath)
            self.picture.isFile = True
            self.picture.save()

            micropost = MicroPostManager.get_picture_micropost(self.picture._id)
            if micropost is not None:
                micropost.pictures.append(self.picture._id)
                micropost.pictures_to_download.remove(self.picture._id)
                micropost.save()

            CURRENT_DOWNLOADS.remove(self.picture._id)
            self.return_success("Picture successfuly downloaded.")

        else:
            CURRENT_DOWNLOADS.remove(self.picture._id)
            self.return_failure("Picture cannot be retrieved.")
Пример #12
0
def and_this_micropost_has_timezone_date(step):
    world.date_micropost = world.microposts[-1]
    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)
Пример #13
0
def and_this_micropost_has_timezone_date(step):
    world.date_micropost = world.microposts[-1]
    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)
Пример #14
0
    def get(self, postId):
        '''
        GET request returns post corresponding to the id given in the request
        URL.
        '''

        micropost = MicroPostManager.get_micropost(postId)
        self.return_one_document_or_404(micropost, "No post found")
Пример #15
0
    def get(self, postId):
        '''
        GET request returns post corresponding to the id given in the request
        URL.
        '''

        micropost = MicroPostManager.get_micropost(postId)
        self.return_one_document_or_404(micropost, "No post found")
Пример #16
0
def and_one_activity_for_first_micropost_with_one_error_for_my_contact(step):
    author = world.browser.user
    world.contact = world.browser2.user.asContact()
    world.micropost = MicroPostManager.get_list().first()

    world.activity = Activity(
        author=author.name,
        verb="posts",
        docType="micropost",
        docId=world.micropost._id,
    )
    world.activity.add_error(world.contact)
    world.activity.save()
Пример #17
0
def and_one_activity_for_first_micropost_with_one_error_for_my_contact(step):
    author = world.browser.user
    world.contact = world.browser2.user.asContact()
    world.micropost = MicroPostManager.get_list().first()

    world.activity = Activity(
        author=author.name,
        verb="posts",
        docType="micropost",
        docId=world.micropost._id,
    )
    world.activity.add_error(world.contact)
    world.activity.save()
Пример #18
0
def and_add_one_deletion_activity_for_first_micropost_with_one_error(step):
    author = world.browser.user
    world.contact = world.browser2.user.asContact()
    world.micropost = MicroPostManager.get_list().first()

    world.activity = Activity(author=author.name,
                              verb="deletes",
                              docType="micropost",
                              docId=world.micropost._id,
                              method="PUT")
    date = date_util.get_db_date_from_date(world.micropost.date)
    world.activity.add_error(world.contact, extra=date)
    world.activity.save()
Пример #19
0
    def post(self):
        '''
        Expect query in sent JSON. Process search for this query then
        send results as response.
        '''

        data = self.get_body_as_dict(expectedFields=["query"])
        if data:
            postIndexer = indexer.Indexer()
            ids = postIndexer.search_microposts(data["query"])
            posts = MicroPostManager.get_microposts(ids)
            self.return_documents(posts)
        else:
            self.return_failure("No query given", 400)
Пример #20
0
    def get(self, postId):
        '''
        Returns for given id the HTML representation of corresponding
        micropost.
        '''

        micropost = MicroPostManager.get_micropost(postId)
        if micropost:
            if micropost.content:
                micropost.content = markdown.markdown(micropost.content)

            self.render("templates/micropost.html", micropost=micropost)
        else:
            self.return_failure("Micropost not found.", 404)
Пример #21
0
    def post(self):
        '''
        Expect query in sent JSON. Process search for this query then
        send results as response.
        '''

        data = self.get_body_as_dict(expectedFields=["query"])
        if data:
            postIndexer = indexer.Indexer()
            ids = postIndexer.search_microposts(data["query"])
            posts = MicroPostManager.get_microposts(ids)
            self.return_documents(posts)
        else:
            self.return_failure("No query given", 400)
Пример #22
0
    def get(self, postId):
        '''
        Returns for given id the HTML representation of corresponding
        micropost.
        '''

        micropost = MicroPostManager.get_micropost(postId)
        if micropost:
            if micropost.content:
                micropost.content = markdown.markdown(micropost.content)

            self.render("templates/micropost.html", micropost=micropost)
        else:
            self.return_failure("Micropost not found.", 404)
Пример #23
0
    def send_posts_to_contact(self, client, contact, now, date):
        '''
        Send microposts from last month to given contact.
        '''
        microposts = MicroPostManager.get_mine(
                startKey=date_util.get_db_date_from_date(now),
                endKey=date_util.get_db_date_from_date(date))

        for micropost in microposts:
            if tags_match(micropost, contact):
                body = micropost.toJson(localized=False)

                client.post(contact, MICROPOST_PATH, body,
                            self.onContactResponse)
Пример #24
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)
Пример #25
0
    def get(self, postId, fileName):
        '''
        Return file which corresponds to *filename* and which is attached to
        micropost of which ID is equal to postId.
        '''

        micropost = MicroPostManager.get_micropost(postId)
        if micropost:
            try:
                fileContent = micropost.fetch_attachment(fileName)
                self.return_file(fileName, fileContent)
            except ResourceNotFound:
                self.return_failure("File not found", 404)
        else:
            self.return_failure("Micropost not found.", 404)
Пример #26
0
def and_add_one_deletion_activity_for_first_micropost_with_one_error(step):
    author = world.browser.user
    world.contact = world.browser2.user.asContact()
    world.micropost = MicroPostManager.get_list().first()

    world.activity = Activity(
        author=author.name,
        verb="deletes",
        docType="micropost",
        docId=world.micropost._id,
        method="PUT"
    )
    date = date_util.get_db_date_from_date(world.micropost.date)
    world.activity.add_error(world.contact, extra=date)
    world.activity.save()
Пример #27
0
    def get(self, postId, fileName):
        '''
        Return file which corresponds to *filename* and which is attached to
        micropost of which ID is equal to postId.
        '''

        micropost = MicroPostManager.get_micropost(postId)
        if micropost:
            try:
                fileContent = micropost.fetch_attachment(fileName)
                self.return_file(fileName, fileContent)
            except ResourceNotFound:
                self.return_failure("File not found", 404)
        else:
            self.return_failure("Micropost not found.", 404)
Пример #28
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)
Пример #29
0
    def on_download_finished(self, response):
        logger.info(self.common)

        if response.code == 200:
            self.common.put_attachment(response.body, self.common.path)
            self.common.isFile = True
            self.common.save()

            micropost = MicroPostManager.get_common_micropost(self.common._id)
            if micropost is not None:
                micropost.commons.append(self.common._id)
                micropost.commons_to_download.remove(self.common._id)
                micropost.save()

            self.return_success("Common successfuly downloaded.")
        else:
            self.return_failure("Common cannot be retrieved.")
Пример #30
0
    def on_download_finished(self, response):
        logger.info(self.common)

        if response.code == 200:
            self.common.put_attachment(response.body, self.common.path)
            self.common.isFile = True
            self.common.save()

            micropost = MicroPostManager.get_common_micropost(self.common._id)
            if micropost is not None:
                micropost.commons.append(self.common._id)
                micropost.commons_to_download.remove(self.common._id)
                micropost.save()

            self.return_success("Common successfuly downloaded.")
        else:
            self.return_failure("Common cannot be retrieved.")
Пример #31
0
    def post(self, postId):
        '''
        Grab from contact the file corresponding to given path and given post
        (post of which ID is equal to *postId*).
        '''

        data = self.get_body_as_dict(expectedFields=["path"])

        micropost = MicroPostManager.get_micropost(postId)
        contact = ContactManager.getTrustedContact(micropost.authorKey)
        user = UserManager.getUser()
        if micropost and data and contact:
            path = data["path"]
            client = ContactClient()
            body = {
                "date": date_util.get_db_date_from_date(micropost.date),
                "contactKey": user.key,
                "path": path
            }

            client.post(contact,
                        "microposts/contacts/attach/",
                        json_encode(body),
                        callback=(yield gen.Callback("getattach")))
            response = yield gen.Wait("getattach")

            if response.error:
                self.return_failure(
                    "An error occured while retrieving picture.")
            else:
                micropost.put_attachment(response.body, data["path"])
                self.return_success("Download succeeds.")

        else:
            if not data:
                self.return_failure("Wrong data.", 400)
            elif not contact:
                self.return_failure("Contact no more available.", 400)
            else:
                self.return_failure("Micropost not found.", 404)
Пример #32
0
def when_i_retrieve_a_post_for_a_given_contact_and_a_given_date(step):
    world.micropost_contact = MicroPostManager.get_contact_micropost(
            world.user2.key, "2011-01-10T11:05:12Z")
Пример #33
0
def when_i_retrieve_the_last_posts(step):
    world.microposts = MicroPostManager.get_list().all()
Пример #34
0
def when_i_retrieve_my_last_posts(step):
    world.microposts = MicroPostManager.get_mine().all()
Пример #35
0
def when_i_retrieve_a_post_for_a_given_date(step):
    world.micropost = MicroPostManager.get_first("2011-01-01T11:05:12Z")
Пример #36
0
def when_i_retrieve_a_post_for_a_given_id(step):
    world.micropost_id = MicroPostManager.get_micropost(world.micropost._id)
Пример #37
0
def when_i_retrieve_a_post_for_a_given_contact_and_a_given_date(step):
    world.micropost_contact = MicroPostManager.get_contact_micropost(
        world.user2.key, "2011-01-10T11:05:12Z")
Пример #38
0
def when_i_retrieve_the_last_posts(step):
    world.microposts = MicroPostManager.get_list().all()
Пример #39
0
def when_i_retrieve_my_last_posts(step):
    world.microposts = MicroPostManager.get_mine().all()
Пример #40
0
def when_i_retrieve_a_post_for_a_given_date(step):
    world.micropost = MicroPostManager.get_first("2011-01-01T11:05:12Z")
Пример #41
0
def when_i_retrieve_a_post_for_a_given_id(step):
    world.micropost_id = MicroPostManager.get_micropost(world.micropost._id)