def list_notes(self, parent=None, list_deleted=False): """ Returns all notes (with sub-notes) as a JSON string. If parent is given returns all notes starting from the parent. If `list_deleted` is True returns only deleted notes. """ if parent: return json.dumps(parent.to_dict()) else: notes = Note.all().filter('parentNote ==', None).filter('deleted ==', list_deleted).order('position').fetch(None) res = [] for n in notes: res.append(n.to_dict()) return json.dumps(res)
def move_note(self, note, parent, after=None): """ Moves a note to a new parent. """ if after: position = after.position + 1 else: position = 0 notes = Note.all().filter("position >=", position).filter("parentNote ==", parent).order("position").fetch(None) cnt = 1 for n in notes: n.position = position + cnt n.put() cnt = cnt + 1 note.parentNote = parent note.position = position note.put() return json.dumps(note.to_dict())
def create_note(self, content, parent=None, after=None): """ Creates a new note, saves it to datastore and returns newly created note as JSON string. If `after` parameter is given, new note will have position property set to be greater than the one specified. """ if after: position = after.position + 1 else: position = 0 notes = Note.all().filter("position >=", position).filter("parentNote ==", parent).order("position").fetch(None) cnt = 1 for n in notes: n.position = position + cnt n.put() cnt = cnt + 1 note = Note(content=content, parentNote=parent, position=position) note.put() return json.dumps(note.to_dict())