示例#1
0
文件: resources.py 项目: cfierro/todo
    def get(self):
        """Method gets a list of all the todos and returns them in an OK
        response. The API can be filtered by todoListId to return a list of
        todos in the specified todoList.
        """
        userId = session.get('userId')
        todoListId = request.args.get('todoListId')

        if todoListId is None:
            permissions = TodoListPermission.query.filter_by(userId=userId)
            todoListIds = [permission.todoListId for permission in permissions]
            if not todoListIds:
                return response_util.buildOkResponse([])

            myTodos = Todo.query.filter(Todo.todoListId.in_(todoListIds))

            return response_util.buildOkResponse([todo.toDict() for todo in myTodos])
        else:
            todoListId = int(todoListId)  # bad request if not int
            permissions = TodoListPermission.query.filter(
                            and_(TodoListPermission.todoListId == todoListId,
                                TodoListPermission.userId == userId)).all()

            if not permissions:
                raise status.Forbidden()

            todos = Todo.query.filter_by(todoListId=todoListId)

            return response_util.buildOkResponse([todo.toDict() for todo in todos])
示例#2
0
文件: resources.py 项目: cfierro/todo
    def get(self):
        """Method gets a list of logged in user's todoLists and returns them in
        an Ok response.
        """
        userId = session.get('userId')
        permissions = TodoListPermission.query.filter_by(userId=userId)
        todoListIds = [permission.todoListId for permission in permissions]

        if not todoListIds:
            return response_util.buildOkResponse([])

        todoLists = TodoList.query.filter(TodoList.id.in_(todoListIds))

        return response_util.buildOkResponse([todoList.toDict()
                                              for todoList in todoLists])
示例#3
0
文件: resources.py 项目: cfierro/todo
    def get(self, userId):
        """Method to get a user id and create a new session.
        """
        user = User.query.get(userId)
        session['userId'] = user.id

        return response_util.buildOkResponse(user.toDict())
示例#4
0
文件: resources.py 项目: cfierro/todo
    def put(self, todoId):
        """Method updates a todo instance and returns it todo in an OK response.

        Args:
            todoId - Interger, primary key identifying the todo.
        """
        userId = session.get('userId')
        todo = Todo.query.get(todoId)

        if todo is None:
            raise status.NotFound()

        permission = TodoListPermission.query.filter(
                        and_(TodoListPermission.todoListId == todo.todoListId,
                             TodoListPermission.userId == userId)).first()

        if permission is None:
            raise status.Forbidden()

        todo.subject = request.form.get('subject') or todo.subject
        todo.dueDate = request.form.get('dueDate') or todo.dueDate
        todo.description = request.form.get('description') or todo.description
        todo.priority = request.form.get('priority') or todo.priority
        todo.completed = request.form.get('completed') or todo.completed
        todo.assigneeId = request.form.get('assigneeId') or todo.assigneeId

        db.session.commit()

        return response_util.buildOkResponse(todo.toDict())
示例#5
0
文件: resources.py 项目: cfierro/todo
    def post(self):
        """Method adds a new todo and returns the todo in an OK response..
        """
        userId = session.get('userId')
        todoListId = request.form.get('todoListId')

        if not(request.form.get('subject') and todoListId):
            raise status.BadRequest()

        permission = TodoListPermission.query.filter(
                        and_(TodoListPermission.todoListId == todoListId,
                             TodoListPermission.userId == userId)).first()

        if permission is None:
            raise status.Forbidden()

        todo = Todo(request.form.get('subject'),
                    todoListId,
                    userId,
                    request.form.get('dueDate'),
                    request.form.get('description'),
                    request.form.get('priority'),
                    request.form.get('completed'),
                    request.form.get('assigneeId'))
        db.session.add(todo)
        db.session.commit()

        return response_util.buildOkResponse(todo.toDict())
示例#6
0
    def get(self, userId):
        """This method gets a single user and returns the user in an OK response.

        Args:
            userId - An integer, primary key that identifies the user.
        """
        user = User.query.get(userId)
        return buildOkResponse(_returnUser(user))
示例#7
0
    def post(self):
        """This method adds a new user and returns the user in an OK response.
        """
        newUser = User(request.form.get('name'),
                       request.form.get('email'),
                       request.form.get('password'))
        db.session.add(newUser)
        db.session.commit()

        return buildOkResponse(_returnUser(newUser))
示例#8
0
    def delete(self, userId):
        """This method deletes a user and returns result none.

        Args:
            userId - An integer, primary key that identifies the user.
        """
        user = User.query.get(userId)
        db.session.delete(user)
        db.commit()

        return buildOkResponse(None)
示例#9
0
文件: resources.py 项目: cfierro/todo
    def post(self):
        """Method creates a todoList and returns it in an Ok response.
        """
        if not(request.form.get('name')):
            raise status.BadRequest()

        todoList = TodoList(request.form.get('name'), session.get('userId'))
        db.session.add(todoList)
        db.session.commit()

        permission = TodoListPermission(session.get('userId'), todoList.id)
        db.session.add(permission)
        db.session.commit()

        return response_util.buildOkResponse(todoList.toDict())
示例#10
0
    def put(self, userId):
        """This method updates user information and returns the user in an OK
        response.

        Args:
            userId - An integer, primary key that identifies the user.
        """
        user = User.query.get(userId)

        user.name = request.form.get('name') or user.name
        user.password = request.form.get('password') or user.password

        db.session.commit()

        return buildOkResponse(_returnUser(user))
示例#11
0
文件: resources.py 项目: cfierro/todo
    def get(self, todoId):
        """Method gets and returns a single todo in an OK response.

        Args:
            todoId - Interger, primary key identifying the todo.
        """
        userId = session.get('userId')
        todo = Todo.query.get(todoId)

        if todo is None:
            raise status.NotFound()

        permission = TodoListPermission.query.filter(
                        and_(TodoListPermission.todoListId == todo.todoListId,
                             TodoListPermission.userId == userId)).first()

        if permission is None:
            raise status.Forbidden()

        return response_util.buildOkResponse(todo.toDict())
示例#12
0
文件: resources.py 项目: cfierro/todo
    def get(self, todoListId):
        """Method that gets and returns a todoList in an Ok response.

        Args:
            todoListId - An integer, primary key that identifies the todo list.
        """
        userId = session.get('userId')
        todoList = TodoList.query.get(todoListId)

        if todoList is None:
            raise status.NotFound()

        permission = TodoListPermission.query.filter(
                        and_(TodoListPermission.todoListId == todoList.id,
                             TodoListPermission.userId == userId)).first()

        if permission is None:
            raise status.Unauthorized()

        return response_util.buildOkResponse(todoList.toDict())
示例#13
0
文件: resources.py 项目: cfierro/todo
    def delete(self, todoId):
        """Method deletes a todo and returns result none.

        Args:
            todoId - Interger, primary key identifying the todo.
        """
        userId = session.get('userId')
        todo = Todo.query.get(todoId)

        if todo is None:
            raise status.NotFound()

        permission = TodoListPermission.query.filter(
                        and_(TodoListPermission.todoListId == todo.todoListId,
                             TodoListPermission.userId == userId)).first()

        if permission is None:
            raise status.Forbidden()

        db.session.delete(todo)
        db.session.commit()

        return response_util.buildOkResponse(None)
示例#14
0
文件: resources.py 项目: cfierro/todo
    def delete(self, todoListId):
        """Method that deletes a todoList and returns result None.

        Args:
            todoListId - An integer, primary key that identifies the todo list.
        """
        userId = session.get('userId')
        todoList = TodoList.query.get(todoListId)

        if todoList is None:
            raise status.NotFound()

        permission = TodoListPermission.query.filter(
                        and_(TodoListPermission.todoListId == todoList.id,
                             TodoListPermission.userId == userId)).first()

        if permission is None:
            raise status.Unauthorized()

        db.session.delete(todoList)
        db.session.commit()

        return response_util.buildOkResponse(None)
示例#15
0
文件: resources.py 项目: cfierro/todo
    def get(self):
        """Method to get a user id and end the user's session.
        """
        session.pop('userId', None)

        return response_util.buildOkResponse(None)
示例#16
0
 def get(self):
     """This method gets a list of all the users and returns them in
     an OK response.
     """
     users = User.query.all()
     return buildOkResponse([_returnUser(user) for user in users])
示例#17
0
文件: resources.py 项目: cfierro/todo
 def get(self):
     """Method to get the user information of the user logged in.
     """
     userId = session.get('userId')
     user = User.query.get(userId)
     return response_util.buildOkResponse(user.toDict())