Esempio n. 1
0
 def to_dict(self):
     """Return a ``dict`` representation of this instance."""
     is_deleted = self.is_deleted
     return {'id': self.key.id(),
             'body': self.body if not is_deleted else '',
             'url': self.url if not is_deleted else '',
             'isDeleted': is_deleted,
             'created': to_timestamp(self.created),
             'modified': to_timestamp(self.modified)}
    def post(self):
        """
        Merge notes from the client and return notes to the client for it to
        merge.

        :return: json string that contains a ``list`` of
            :class:`spidernotes.models.Note` instances that have changed
            since the last time the notes were synchronized, as well as a
            timestamp of when this request was processed.
        """
        user_key = self.get_valid_user().key
        try:
            ctx = json.loads(self.request.body)
        except ValueError:
            self.raise_error()

        old_last_synchronized = from_timestamp(ctx.get('lastSynchronized'))
        new_last_synchronized = datetime.utcnow()

        notes_from_client = imap(_to_tuple, ctx.get('notes'))
        notes_from_server = _merge_notes(user_key,
                                         notes_from_client,
                                         old_last_synchronized,
                                         new_last_synchronized)

        return self.render_json(
            {'notes': [o.to_dict() for o in notes_from_server],
             'lastSynchronized': to_timestamp(new_last_synchronized)})