def patch(self, username, list_id: str): body_parser = reqparse.RequestParser(bundle_errors=True) body_parser.add_argument('name', type=str, required=True, help="Missing the name of the list") args = body_parser.parse_args( strict=False ) # Accepted only if these two parameters are strictly declared in body else raise exception if len(list_id) != 24: return sendErrorNotFound({"message": "todo_list id not found"}) try: todo_list = TodoList.objects(id=list_id, username=username).first() if todo_list is None: return sendErrorNotFound({"message": "todo_list id not found"}) if 'todo_list' in args: todo_list.update( name=args['name'], todo_list=args['todo_list'], ) else: todo_list.update(name=args['name'], ) todo_list = TodoList.objects(id=list_id).first() return sendSuccess({'todo_list': todo_list.asJson()}) except Exception as err: return sendJson(400, str(err), args)
def insert(self, content): """ Inserts data into the table. :params content: (str) description of the todo task :returns: (boolean) True/False depending on the success of insertion """ success = True data_rec = TodoList() data_rec.todo_content = content.todo_content self.__db_request_session.add(data_rec) try: self.__db_request_session.commit() self.__db_request_session.refresh(data_rec) except Exception as e: self.__db_request_session.rollback() self.__db_request_session.flush() # app.logger.debug("Exception: {}".format(e)) return not success, None return success, data_rec.list_item_id
def add_user_todolist(username): user = User.query.filter_by(username=username).first_or_404() try: todolist = TodoList(title=request.json.get('title'), creator=user.username).save() except: abort(400) return jsonify(todolist.to_dict()), 201
def test_delete_todo(self): todolist = TodoList(self.shopping_list_title).save() todo = Todo('A book about TDD', todolist.id).save() self.assertEqual(todolist.count_todos(), 1) todo_id = todo.id todo.delete() self.assertIsNone(Todo.query.get(todo_id)) self.assertEqual(todolist.count_todos(), 0)
def test_adding_two_todolists_with_the_same_title(self): user = self.add_user(self.username_adam) ftodolist = TodoList(title=self.shopping_list_title, creator=user.username).save() first_todolist = TodoList.query.filter_by(id=ftodolist.id).first() stodolist = TodoList(title=self.shopping_list_title, creator=user.username).save() second_todolist = TodoList.query.filter_by(id=stodolist.id).first() self.assertEqual(first_todolist.title, second_todolist.title) self.assertEqual(first_todolist.creator, second_todolist.creator) self.assertNotEqual(first_todolist.id, second_todolist.id)
def test_adding_new_todo_without_user(self): todo = Todo(description=self.read_todo_description, todolist_id=TodoList().save().id).save() todo_from_db = Todo.query.filter_by(id=todo.id).first() self.assertEqual(todo_from_db.description, self.read_todo_description) self.assertIsNone(todo_from_db.creator)
def test_add_todolist_todo(self): new_todolist = TodoList().save() # todolist with default title post_response = self.client.post(url_for('api.add_todolist_todo', todolist_id=new_todolist.id), headers=self.get_headers(), data=json.dumps({ 'description': 'new todo', 'creator': 'null', 'todolist_id': new_todolist.id })) self.assert_status(post_response, 201) response = self.client.get( url_for('api.get_todolist_todos', todolist_id=new_todolist.id)) self.assert_200(response) json_response = json.loads(response.data.decode('utf-8')) # check title, creator are set correctly and a total of one todo self.assertEqual(json_response['todos'][0]['description'], 'new todo') self.assertEqual(json_response['todos'][0]['creator'], None) self.assertEqual(len(json_response['todos']), 1)
def new_todolist(): form = TodoForm(todo=request.form.get("todo")) if form.validate(): todolist = TodoList(creator=_get_user()).save() Todo(form.todo.data, todolist.id).save() return redirect(url_for("main.todolist", id=todolist.id)) return redirect(url_for("main.index"))
def test_add_todolist_todo_without_todo_data(self): new_todolist = TodoList().save() post_response = self.client.post( url_for("api.add_todolist_todo", todolist_id=new_todolist.id), headers=self.get_headers(), ) self.assert400Response(post_response)
def add_todo(description, user, todolist_id=None): todo_data = { 'description': description, 'todolist_id': todolist_id or TodoList().save().id, 'creator': user.username } read_todo = Todo.from_dict(todo_data) return Todo.query.filter_by(id=read_todo.id).first()
def get(self, username): try: liste = list( map(lambda todoList: todoList.asJson(), TodoList.objects(username=username))) return sendSuccess(liste) except Exception as e: raise (e)
def test_adding_new_todolist_with_user(self): user = self.add_user(self.username_adam) todolist = TodoList(title=self.shopping_list_title, creator=user.username).save() todolist_from_db = TodoList.query.filter_by(id=todolist.id).first() self.assertEqual(todolist_from_db.title, self.shopping_list_title) self.assertEqual(todolist_from_db.creator, user.username)
def test_delete_todo(self): todolist = TodoList(self.shopping_list_title).save() todo = Todo("A book about TDD", todolist.id).save() self.assertEqual(todolist.todo_count, 1) todo_id = todo.id todo.delete() self.assertIsNone(Todo.query.get(todo_id)) self.assertEqual(todolist.todo_count, 0)
def add_todo(description, user, todolist_id=None): todo_data = { "description": description, "todolist_id": todolist_id or TodoList().save().id, "creator": user.username, } read_todo = Todo.from_dict(todo_data) return Todo.query.filter_by(id=read_todo.id).first()
def generate_fake_todolists(self, count): # for the creator relation we need users users = User.query.all() assert users != [] for _ in range(count): TodoList(title=forgery_py.forgery.lorem_ipsum.title(), creator=random.choice(users).username, created_at=self.generate_fake_date()).save()
def get_user_todo_list_by_id(todo_list_id): """ Create an user in the database :param todo_list_id: String, To do list id :return: List, user to do list. """ try: return TodoList.objects(id=todo_list_id).first() except Exception: return None
def index(): todos = get_all(TodoList) form = TodoListForm(request.form) if request.method == 'POST': todolist = TodoList(name=form.name.data) db.session.add(todolist) db.session.commit() return redirect(url_for('main.index')) return render_template('index.html', todos=todos, form=form)
def todo_list_or_create(): todo_form = TodoForm() if todo_form.validate_on_submit(): to_do_item = TodoList(title=todo_form.title.data) db.session.add(to_do_item) db.session.commit() return redirect(url_for('todo.todo_list_or_create')) to_do_list = TodoList.query.all() return render_template('to_do_list.html', form=todo_form, todo_list=to_do_list)
def get(self, username, list_id: str): if len(list_id) != 24: return sendErrorNotFound({"message": "todo_list id not found"}) try: todo_list = TodoList.objects(id=list_id, username=username).first() if todo_list is None: return sendErrorNotFound({"message": "todo_list id not found"}) return sendSuccess({'todo_list': todo_list.asJson()}) except Exception as err: return sendJson(400, str(err), {"list_id": list_id})
def put(self, username, list_id: str): body_parser = reqparse.RequestParser(bundle_errors=True) body_parser.add_argument('name', type=str, required=True, help="Missing the name of the list") body_parser.add_argument('description', type=str, required=True, help="Missing the description of the list") args = body_parser.parse_args( strict=False ) # Accepted only if these two parameters are strictly declared in body else raise exception if len(list_id) != 24: return sendErrorNotFound( {"message": "todo_list id not found (can't be alive)"}) try: todo_list = TodoList.objects(id=list_id, username=username).first() if todo_list is None: return sendErrorNotFound({"message": "todo_list id not found"}) todo = Todo(name=args['name'], description=args['description'], username=username, created_on=datetime.today()).save() todo_list.update(push__todo_list=todo, username=username) updated_todo_list = TodoList.objects(id=list_id, username=username).first() return sendSuccess({ "todo": todo.asJson(), "updated_todo_list": updated_todo_list.asJson() }) except Exception as err: return sendJson(400, str(err), {"list_id": list_id})
def test_adding_todo_to_todolist(self): user = self.add_user(self.username_adam) todolist = TodoList(title=self.shopping_list_title, creator=user.username).save() todolist_from_db = TodoList.query.filter_by(id=todolist.id).first() todo_description = "A book about TDD" todo = self.add_todo(todo_description, user, todolist_from_db.id) self.assertEqual(todolist_from_db.todo_count, 1) self.assertEqual(todolist.title, self.shopping_list_title) self.assertEqual(todolist.creator, user.username) self.assertEqual(todo.todolist_id, todolist_from_db.id) self.assertEqual(todolist.todos.first(), todo)
def show_todo_list(): form = TodoListForm() if request.method == 'POST': if form.validate_on_submit(): todolist = TodoList(current_user.id, form.title.data, form.status.data) db.session.add(todolist) db.session.commit() flash('You have add a new todo list', 'success') return redirect(url_for('web.show_todo_list')) else: flash(form.errors) todolists = TodoList.query.filter_by(user_id=current_user.id).all() return render_template('index.html', todolists=todolists, form=form)
def put(self, username): body_parser = reqparse.RequestParser(bundle_errors=True) body_parser.add_argument('name', type=str, required=True, help="Missing the name of the list") args = body_parser.parse_args( strict=False ) # Accepted only if these two parameters are strictly declared in body else raise exception try: if 'todo_list' in args: todo_list = TodoList(name=args['name'], todo_list=args['todo_list'], username=username, created_on=datetime.today()).save() else: todo_list = TodoList(name=args['name'], todo_list=[], username=username, created_on=datetime.today()).save() return sendSuccess({'todo_list': todo_list.asJson()}) except Exception as err: return sendJson(400, str(err), args)
def updateExistingToDo(): item = TodoList(**request.get_json()) isExist = False for i in toDoList: if i["id"] == item.id: i["title"] = item.title isExist = True break if isExist: response = jsonify({"message": "todo list updated successfull"}) response.status_code = 201 return response else: response = jsonify({"message": "todo list not exist"}) response.status_code = 404 return response
def post(self): try: new_list_request = request.get_json() validate(schema=new_list_schema, instance=new_list_request) username = get_jwt_identity() new_todo_list = TodoList(name=new_list_request['listname']) existing_user = User.query.filter_by(username=username).first() existing_user.todo_lists.append(new_todo_list) db.session.add(new_todo_list) db.session.commit() return {'message': 'Added new todo list', 'code': 200} except Exception as err: print(err) return {'message': 'Internal server error', 'code': 500}
def get_user_todo_list(user_id): """ Create an user in the database :param user_id: String, user id :return: List, user to do list. """ try: list = [] todo_list = TodoList.objects(user_id=user_id) for i in todo_list: list.append({ 'id': str(i.id), 'description': i.description, 'status': i.status, }) return list except Exception: return []
def delete(self, username, list_id: str, todo_id: str): if len(list_id) != 24: return sendErrorNotFound({"message": "todo_list id not found"}) if len(todo_id) != 24: return sendErrorNotFound({"message": "todo_id id not found"}) try: todo = Todo.objects(id=todo_id, username=username).first() if todo is None: return sendErrorNotFound({"message": "todo id not found"}) todo_list = TodoList.objects(id=list_id, username=username).first() if todo_list is None: return sendErrorNotFound({"message": "todo_list id not found"}) todo.delete() todo_list.update(pull__todo_list=todo.id, username=username) return sendSuccess({'todo_id': todo_id, "list_id": list_id}) except Exception as err: return sendJson(400, str(err), {"list_id": list_id})
def test_add_todolist_todo(self): new_todolist = TodoList().save() # todolist with default title post_response = self.client.post( url_for("api.add_todolist_todo", todolist_id=new_todolist.id), headers=self.get_headers(), data=json.dumps({ "description": "new todo", "creator": "null", "todolist_id": new_todolist.id, }), ) self.assert_status(post_response, 201) response = self.client.get( url_for("api.get_todolist_todos", todolist_id=new_todolist.id)) self.assert_200(response) json_response = json.loads(response.data.decode("utf-8")) # check title, creator are set correctly and a total of one todo todos = json_response["todos"] self.assertEqual(todos[0]["description"], "new todo") self.assertEqual(todos[0]["creator"], None) self.assertEqual(len(todos), 1)
def test_delete_todolist(self): todolist = TodoList(self.shopping_list_title).save() todolist_id = todolist.id todolist.delete() self.assertIsNone(TodoList.query.get(todolist_id))
def add_todolist(title, username=None): todolist = TodoList(title=title, creator=username).save() return TodoList.query.filter_by(id=todolist.id).first()
def add_todolist(): try: todolist = TodoList(title=request.json.get('title')).save() except: abort(400) return jsonify(todolist.to_dict()), 201
def add_todolist(): form = TodoListForm(todo=request.form.get("title")) if form.validate(): todolist = TodoList(form.title.data, _get_user()).save() return redirect(url_for("main.todolist", id=todolist.id)) return redirect(url_for("main.index"))