Esempio n. 1
0
    def post(self, username):
        user = UserModel.find_by_username(username)
        my_dict = user.item_json()
        data = sync_parser.parse_args()

        client_note_list = data["notes"]
        server_note_list = my_dict["notes"]

        client_note_id_list = [x["note_id"] for x in client_note_list]
        server_note_id_list = [x["note_id"] for x in server_note_list]

        a = set(client_note_id_list)
        b = set(server_note_id_list)

        add_list = [x for x in client_note_list if x["note_id"] not in b]
        delete_list = [x for x in server_note_list if x["note_id"] not in a]
        intersection_list = [x for x in client_note_list if x["note_id"] in b]

        for add_note in add_list:
            note = NoteModel(**add_note)
            note.save_to_db()

        for delete_note in delete_list:
            note = NoteModel.find_by_note_id(delete_note["note_id"])
            note.delete_from_db()

        for intersection_note in intersection_list:
            note = NoteModel.find_by_note_id(intersection_note["note_id"])
            note.title = intersection_note["title"]
            note.note = intersection_note["note"]
            note.priority = intersection_note["priority"]
            note.updated_at = intersection_note["updated_at"]


        return {"notes": add_list}
Esempio n. 2
0
    def put(self, title):

        note = NoteModel.filter_by_title(title)
        data = Note.parser.parse_args()
        author = AuthorModel.filter_by_name(data['author'])

        if author is None:
            return {'message': 'This author is not registered yet'}, 404

        if note is None:
            note = NoteModel(title, **data)
        else:
            Note.parser.add_argument('new_title', dest='title')
            data = Note.parser.parse_args()
            note.title = data['title']
            note.author_id = author.id
            note.note = data['note']
            note.updated_date = datetime.datetime.now()

        try:
            note.save_to_db()
        except Exception:
            return {'message': 'can not save the note'}, 500

        return note.json(), 200
Esempio n. 3
0
    def post(self):
        data = parser.parse_args()
        note = NoteModel(**data)

        try:
            note.save_to_db()
        except:
            return {"message": "An error occurred saving the item."}, 500

        return note.json(), 201
Esempio n. 4
0
    def post(self):
        data = NoteList.parser.parse_args()
        y, m, d = data['date_created'].split('-')
        note = NoteModel(data['title'], data['article_text'],
                         datetime.datetime(int(y), int(m), int(d)),
                         data['project_id'])

        try:
            note.save_to_db()
        except:
            return {'message': 'An error occured inserting the note'}, 500

        return note.json(), 201
Esempio n. 5
0
    def put(self, note_id):
        data = parser.parse_args()
        note = NoteModel.find_by_note_id(note_id)

        if note is None:
            note = NoteModel(**data)
        else:
            note.title = data["title"]
            note.note = data["note"]
            note.priority = data["priority"]
            note.updated_at = data["updated_at"]

        try:
            note.save_to_db()
        except:
            return {"message": "An error occurred saving the item."}, 500

        return note.json()
Esempio n. 6
0
    def post(self, title):
        if NoteModel.filter_by_title(title):
            return {"message": "note with that title already exists"}, 409

        data = Note.parser.parse_args()
        author = AuthorModel.filter_by_name(data['author'])

        if author.id is None:
            return {'message': 'This author is not registered yet'}, 404

        note = NoteModel(title, author.id, data['note'])

        try:
            note.save_to_db()
        except Exception:
            return {'message': 'can not save the note'}, 500

        return note.json(), 201