def delete_resources(notes): """ Deletes resources that have been removed from a note :param notes: list of existing NoteMetadata objects :return: """ for note in notes: # Get the note instance note_instance = get_or_create(Note, guid=note.guid) # Check if the note has been updated before looking at its resources. if (note_instance.updated is None) or \ (datetime.fromtimestamp(note.updated/1000) > note_instance.updated): # Get the full Note object note = ehelper.get_note(note.guid) if note.resources is None: deleted_resources = note_instance.resources.all() else: resource_guids = [res.guid for res in note.resources] if resource_guids: deleted_resources = Resource.query.join(Note)\ .filter_by(guid=note.guid)\ .filter(~Resource.guid.in_(resource_guids))\ .all() for d in deleted_resources: db.session.delete(d) db.session.commit()
def load_notes(notes, notebook): """ Load each note into the database :param notes: list of NoteMetadata objects :return: """ for note in notes: # Get the database object if it exists instance = get_or_create(Note, guid=note.guid) if (instance.updated is None) or \ (datetime.fromtimestamp(note.updated/1000) > instance.updated) or \ (instance.tags.count() != len(note.tagGuids)): # Get the full Note object note = ehelper.get_note(note.guid) instance.title = note.title.decode('utf-8') instance.content = note.content.decode('utf-8') instance.created = datetime.fromtimestamp(note.created/1000) instance.updated = datetime.fromtimestamp(note.updated/1000) # update tags tags = db.session.query(Tag)\ .filter(Tag.guid.in_(note.tagGuids))\ .all() instance.tags = tags # update resources if note.resources: resources = load_resources(note) instance.resources = resources notebook.notes.append(instance) db.session.commit()