def _to_tuple(note_dict): """ Return a ``namedtuple`` representation of the supplied ``note_dict``. :param dict note_dict: ``dict`` representation of a :class:`spidernotes.models.Note` """ is_deleted = bool(note_dict.get('isDeleted')) return _NoteTuple( id=get_param(note_dict, 'id'), body=get_param(note_dict, 'body') if not is_deleted else '', url=get_param(note_dict, 'url') if not is_deleted else '', is_deleted=is_deleted, created=from_timestamp(get_param(note_dict, 'created')), modified=from_timestamp(get_param(note_dict, '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)})