def _merge_notes(user_key, notes_from_client, old_last_synchronized, new_last_synchronized): """ Merge notes from client with notes from the server. :param user_key: Key of the :class:`google.appengine.api.users.User` with which the notes are associated. :param notes_from_client: Iterable of Note-like objects. :param datetime.datetime old_last_synchronized: Datetime after which to fetch server notes. :param datetime.datetime new_last_synchronized: Datetime of the current merge operation. :type user_key: :class:`google.appengine.ext.db.Key` :return: Notes to be merged back into the client. :rtype: list """ from_server_map = {o.key: o for o in Note.get_synchronized_after( user_key, old_last_synchronized)} to_persist = [] for client_note in notes_from_client: server_note, is_created = Note.get_or_create(user_key, client_note.id) if is_created or client_note.modified >= server_note.modified: server_note.update_from_note(client_note, new_last_synchronized) to_persist.append(server_note) # The ``client_note`` supersedes the ``server_note``, so don't # return the ``server_note`` to the client. from_server_map.pop(server_note.key, None) if to_persist: ndb.put_multi_async(to_persist) return from_server_map.values()
def delete(self): """ Delete the current :class:`google.appengine.api.users.User`. """ user = self.get_user() if user: if is_connected(user): disconnect_user(user) user_key = user.key Note.delete_all(user_key) user_key.delete() return self.render_json('')
def _copy_notes_between_users(from_user_key, to_user_key): """ Copy notes from one :class:`google.appengine.api.users.User` to another. :param from_user_key: Key of the :class:`google.appengine.api.users.User` from which to copy the notes. :param to_user_key: Key of the :class:`google.appengine.api.users.User` to which to copy the notes. :type from_user_key: :class:`google.appengine.ext.db.Key` :type to_user_key: :class:`google.appengine.ext.db.Key` """ notes = [src.copy_to_user(to_user_key) for src in Note.get_active(from_user_key)] if notes: ndb.put_multi_async(notes)