def __update_to_do_item(user, id): form = ToDoForm(request.form) if form.validate(): to_do = ToDo.objects.get(id=id) to_do.title = form.title.data to_do.description = form.description.data to_do.pomodoro_length = form.pomodoro_length.data to_do.break_length = form.break_length.data to_do.long_break_length = form.long_break_length.data prev_num_pomodoros = to_do.pomodoros.count() # adding pomodoros if form.num_pomodoros.data > prev_num_pomodoros: # update remaining length of already existing pomodoros, # skipping ones that are complete for i in range(prev_num_pomodoros): pomodoro = to_do.pomodoros[i] if not pomodoro.complete: pomodoro.remaining_length = form.pomodoro_length.data # append additional pomodoros diff = form.num_pomodoros.data - prev_num_pomodoros for i in range(diff): new_pomodoro = Pomodoro(remaining_length=form.pomodoro_length.data) to_do.pomodoros.append(new_pomodoro) # removing pomodoros elif form.num_pomodoros.data < prev_num_pomodoros: # remove difference in prev vs updated number of pomodoros for i in range(prev_num_pomodoros - form.num_pomodoros.data): to_do.pomodoros.pop() # update remaining length of already existing pomodoros, # skipping ones that are complete or in progress for i in range(form.num_pomodoros.data): pomodoro = to_do.pomodoros[i] if not pomodoro.complete: pomodoro.remaining_length = form.pomodoro_length.data if to_do.save(): return jsonify(to_do = to_do, message = "To do updated successfully!") else: return jsonify(error="Could not update to do item."), 401 else: return jsonify(errors=form.errors.items()), 400
def __update_to_do_item(user, id): form = ToDoForm(request.form) if form.validate(): to_do = ToDo.objects.get(id=id) to_do.title = form.title.data to_do.description = form.description.data to_do.pomodoro_length = form.pomodoro_length.data to_do.break_length = form.break_length.data to_do.long_break_length = form.long_break_length.data prev_num_pomodoros = to_do.pomodoros.count() # adding pomodoros if form.num_pomodoros.data > prev_num_pomodoros: # update remaining length of already existing pomodoros, # skipping ones that are complete for i in range(prev_num_pomodoros): pomodoro = to_do.pomodoros[i] if not pomodoro.complete: pomodoro.remaining_length = form.pomodoro_length.data # append additional pomodoros diff = form.num_pomodoros.data - prev_num_pomodoros for i in range(diff): new_pomodoro = Pomodoro( remaining_length=form.pomodoro_length.data) to_do.pomodoros.append(new_pomodoro) # removing pomodoros elif form.num_pomodoros.data < prev_num_pomodoros: # remove difference in prev vs updated number of pomodoros for i in range(prev_num_pomodoros - form.num_pomodoros.data): to_do.pomodoros.pop() # update remaining length of already existing pomodoros, # skipping ones that are complete or in progress for i in range(form.num_pomodoros.data): pomodoro = to_do.pomodoros[i] if not pomodoro.complete: pomodoro.remaining_length = form.pomodoro_length.data if to_do.save(): return jsonify(to_do=to_do, message="To do updated successfully!") else: return jsonify(error="Could not update to do item."), 401 else: return jsonify(errors=form.errors.items()), 400
def __create_to_do_item(user): form = ToDoForm(request.form) if form.validate(): new_to_do = ToDo(title=form.title.data, description=form.description.data, pomodoro_length=form.pomodoro_length.data, break_length=form.break_length.data, long_break_length=form.long_break_length.data) new_to_do.author = user for i in range(form.num_pomodoros.data): new_pomodoro = Pomodoro(remaining_length=form.pomodoro_length.data) new_to_do.pomodoros.append(new_pomodoro) if new_to_do.save(): return jsonify(to_do = new_to_do, message = "To do created successfully!") else: return jsonify(error="Could not create to do item."), 401 else: return jsonify(errors=form.errors.items()), 400
def __create_to_do_item(user): form = ToDoForm(request.form) if form.validate(): new_to_do = ToDo(title=form.title.data, description=form.description.data, pomodoro_length=form.pomodoro_length.data, break_length=form.break_length.data, long_break_length=form.long_break_length.data) new_to_do.author = user for i in range(form.num_pomodoros.data): new_pomodoro = Pomodoro(remaining_length=form.pomodoro_length.data) new_to_do.pomodoros.append(new_pomodoro) if new_to_do.save(): return jsonify(to_do=new_to_do, message="To do created successfully!") else: return jsonify(error="Could not create to do item."), 401 else: return jsonify(errors=form.errors.items()), 400