Example #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")
                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)
Example #2
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)
Example #3
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:
                    logger.info("Attempt to resend a post to contact: %s." % contact.name)
                self.forward_to_contact(micropost, contact, activity)
        else:
            self.return_failure("Micropost not found", 404)
Example #4
0
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)
Example #5
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")
Example #6
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()
Example #7
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:
            body = micropost.toJson(localized=False)

            client.post(contact, MICROPOST_PATH, body, self.onContactResponse)
Example #8
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)
Example #9
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)
Example #10
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()
Example #11
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", []),
                        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)
Example #12
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)
Example #13
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)
            micropost.delete()
            self.return_success("Micropost deletion succeeds.")
            
        else:
            self.return_failure("Micropost not found.", 404)
Example #14
0
def when_i_retrieve_a_post_for_a_given_date(step):
    world.micropost = MicroPostManager.get_first("2011-01-01T11:05:12Z")
Example #15
0
def when_i_retrieve_a_post_for_a_given_id(step):
    world.micropost_id = MicroPostManager.get_micropost(world.micropost._id)
Example #16
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")
Example #17
0
def when_i_retrieve_my_last_posts(step):
    world.microposts = MicroPostManager.get_mine().all()
Example #18
0
def when_i_retrieve_the_last_posts(step):
    world.microposts = MicroPostManager.get_list().all()
Example #19
0
import sys

sys.path.append("../")
from newebe.news.models import MicroPostManager
from newebe.lib.date_util import get_db_date_from_date
from newebe.lib.indexer import Indexer

indexer = Indexer()

print "indexation starts...."

# Fill index with posts from DB
posts = MicroPostManager.get_list()
while len(posts) > 9:

    print "indexing %d" % len(posts)
    if len(posts) > 0:
        lastPost = list(posts)[-1]
    indexer.index_microposts(posts)

    if lastPost:
        date = get_db_date_from_date(lastPost.date)
        print date
        posts = MicroPostManager.get_list(startKey=date)
    else:
        posts = []

print "indexation finished"
Example #20
0
import sys

sys.path.append("../")
from newebe.news.models import MicroPostManager
from newebe.lib.date_util import get_db_date_from_date
from newebe.lib.indexer import Indexer

indexer = Indexer()

print "indexation starts...."

# Fill index with posts from DB
posts = MicroPostManager.get_list()
while len(posts) > 9:

    print "indexing %d" % len(posts)
    if len(posts) > 0:
        lastPost = list(posts)[-1]
    indexer.index_microposts(posts)

    if lastPost:
        date = get_db_date_from_date(lastPost.date)
        print date
        posts = MicroPostManager.get_list(startKey=date)
    else:
        posts = []


print "indexation finished"