def remove_collaborator(list_id, collaborator_id): if collaborator_id != str(session.get('userID')): if not db_is_list_owner(list_id, session.get('userID')): json_abort(404, 'List not found') if db_get_user_by_id(collaborator_id) == None: json_abort(400, 'Invalid request parameters') db_remove_collaborator(list_id, collaborator_id) return jsonify({'result': True})
def remove_collaborator(list_id, collaborator_id): # collaborators can only be removed by a list owner, unless a collaborator want to remove himself if collaborator_id != str(session.get('userID')): if not db_is_list_owner(list_id, session.get('userID')): json_abort(404, 'List not found') if db_get_user_by_id(collaborator_id) == None: json_abort(400, 'Invalid request parameters') # TODO: remove collaborator return jsonify({'result': True})
def update_task(list_id, task_id): data = request.get_json() task = db_get_task(list_id, task_id) if task == None: json_abort(404, 'Task not found') title = data.get('title', None) status = data.get('status', None) description = data.get('description', None) due = data.get('due', None) revision = data.get('revision', None) if title == None or status == None or description == None or \ revision == None: json_abort(400, 'Invalid request parameters') # Only update tasks with there is no newer version on the server if revision < task.revision: json_abort(409, 'Newer version of task available') task.title = title task.status = status task.description = description task.due = due task.revision = task.revision + 1 task = db_update_task(list_id, task) if task == None: json_abort(500, 'Could not update task') return jsonify(task.__dict__)
def create_task(list_id): ''' creates a new task for a list ''' data = request.get_json() title = data.get('title', None) if title == None: json_abort(400, 'Invalid request parameters') newTask = db_create_task(list_id, title) if newTask == None: json_abort(400, 'Could not create task') return jsonify(newTask.__dict__)
def remove_task(list_id, task_id): # 1. Check whether the specified list exists if (len([l for l in myLists if l.id == list_id]) < 1): json_abort(404, 'List not found') # 2. Check whether the specified task exists tasks = [t for t in myTasks if t.id == task_id and t.list == list_id] if (len(tasks) < 1): json_abort(404, 'Task not found') # 3. finally remove the task myTasks.remove(tasks[0]) return jsonify({'result': True})
def create_task(list_id): ''' creates a new task for a list ''' # 1. Check whether the specified list exists if (len([l for l in myLists if l.id == list_id]) < 1): json_abort(404, 'List not found') # 2. Check whether the required parameters have been sent try: data = request.get_json() except: json_abort(400, 'No JSON provided') if data == None: json_abort(400, 'Invalid Content-Type') title = data.get('title', None) if title == None: json_abort(400, 'Invalid request parameters') # 3. calculate the next id id = max([int(t.id) for t in myTasks] + [-1]) + 1 newTask = Task(title, list_id, id=str(id), status=Task.NORMAL) # 4. append task to array myTasks.append(newTask) # 5. return new task return jsonify(newTask.__dict__)
def add_collaborator(list_id, collaborator_email): # TODO: check whether user exists user = if user == None: json_abort(404, 'User not found') # TODO: Add collaborator to list db_add_collaborator(l.id, user.id) # TODO: Get list from database and return it l = if l == None: json_abort(404, "List not updated") return jsonify(l.__dict__)
def upload_file(list_id, task_id): # each file is save in a folder named after the corresponding tasks id and list_id directory = os.path.join(app.config['UPLOAD_FOLDER'], list_id, task_id) if not os.path.exists(directory): os.makedirs(directory) # Get the name of the uploaded files uploaded_files = request.files.getlist('files[]') for file in uploaded_files: if file and allowed_file(file.filename): # sanitize the filename filename = secure_filename(file.filename) # save uploaded file permanently file.save(os.path.join(directory, filename)) # save reference in database db_create_file(task_id, filename) # return the updated task task = db_get_task(list_id, task_id) if task == None: json_abort(500, 'Could not upload file') return jsonify(task.__dict__)
def upload_file(list_id, task_id): # each file is save in a folder named after the corresponding tasks id and list_id # TODO: compute right path directory = if not os.path.exists(directory): os.makedirs(directory) # Get the name of the uploaded files uploaded_files = request.files.getlist('files[]') for file in uploaded_files: if file and allowed_file(file.filename): # sanitize the filename filename = secure_filename(file.filename) # TODO: save uploaded file permanently # TODO: save reference in database # TODO: return the updated task task = if task == None: json_abort(500, 'Could not upload file') return jsonify(task.__dict__)
def add_collaborator(list_id, collaborator_email): user = db_get_user(collaborator_email) if user == None: json_abort(404, 'User not found') l = db_get_list(list_id) if l == None: json_abort(404, 'List not found') db_add_collaborator(l.id, user.id) l = db_get_list(l.id) if l == None: json_abort(404, "List not found") return jsonify(l.__dict__), 201
def update_task(list_id, task_id): # 1. Check whether the specified list exists if (len([l for l in myLists if l.id == list_id]) < 1): json_abort(404, 'List not found') # 2. Check whether the specified task exists tasks = [t for t in myTasks if t.id == task_id and t.list == list_id] if (len(tasks) < 1): json_abort(404, 'Task not found') # 3. Check whether the required parameters have been sent try: data = request.get_json() except: json_abort(400, 'No JSON provided') if data == None: json_abort(400, 'Invalid Content-Type') title = data.get('title', None) status = data.get('status', None) description = data.get('description', None) due = data.get('due', None) revision = data.get('revision', None) if title == None or status == None or description == None or \ due == None or revision == None: json_abort(400, 'Invalid request parameters') if revision < tasks[0].revision: json_abort(409, 'Newer version of task available') # TODO: ignoring 'list' for now. Implement moving tasks from one list to another tasks[0].title = title tasks[0].status = status tasks[0].description = description tasks[0].due = due tasks[0].revision = tasks[0].revision + 1 return jsonify(tasks[0].__dict__)