def patch(self, task_id): """ Endpoint to update task""" request_data = request.get_json() user = get_jwt_identity() task = Task.get_or_404(task_id) schema = TaskSchema(context={'id': task_id}) if 'task_assignees' in request_data: project = Project.get_or_404(task.project_id) assignee_list = check_assignee(request_data.get('task_assignees'), project.assignees) if assignee_list is not None: user_list = assign_user(assignee_list) assignees = user_list if user_list is not None else [] del request_data['task_assignees'] data = schema.load_object_into_schema(request_data, partial=True) data['task_assignees'] = assignees task.update_(**data) else: data = schema.load_object_into_schema(request_data, partial=True) task.update_(**data) return response('success', message=success_messages['updated'].format('Task'), data=schema.dump(task).data, status_code=200)
def post(self): """ Endpoint to create new project""" data = request.get_json() title = data.get('title') user = get_jwt_identity() project_exist = Project.find_by_title_and_user(title=title, user_id=user.get('id')) if project_exist: raise ValidationError( { "message": serialization_messages['exists'].format( 'Project with title') }, 409) data['createdBy'] = user.get("id") project = Project() user_list = assign_user(data.get('assignees')) convert_date_to_date_time(data['due_date']) data['assignees'] = user_list if user_list is not None else [] assignee_ids = data['assignees'] del data['assignees'] schema = ProjectSchema() project_data = schema.load_object_into_schema(data) project.created_by = data['createdBy'] project.title = data['title'] project.description = data['description'] project.due_date = data['due_date'] project.assignees = assignee_ids project.save() return response('success', message=success_messages['created'].format('Project'), data=schema.dump(project).data, status_code=201)
def get(self): """ Get all tasks :param None: :return: Tasks List """ # user = get_jwt_identity() schema = TaskSchema(many=True) tasks = Task.get_all() if tasks is None: raise ValidationError({'message': 'No Task Found'}) return response('success', success_messages['retrieved'].format('Tasks'), schema.dump(tasks).data)
def post(self): """ Endpoint to create new Task""" data = request.get_json() title = data.get('title') formatted_date = convert_date_to_date_time(data['due_date']) schema = TaskSchema() project_id = data['projectId'] project = Project.get_or_404(project_id) task_exist = Task.find_by_title_and_project_id(title=title, project_id=project.id) if task_exist: raise ValidationError( { "message": serialization_messages['exists'].format('Task with title') }, 409) if not check_date_difference(formatted_date, project.due_date): raise ValidationError( { "message": "Tasks cannot be created under this project because of dat difference" }, 401) assignee_list = check_assignee(data.get('task_assignees'), project.assignees) if assignee_list is not None: user_list = assign_user(assignee_list) data['task_assignees'] = user_list else: data['task_assignees'] = [] assignee_ids = data['task_assignees'] if data[ 'task_assignees'] is not None else [] del data['task_assignees'] task_data = schema.load_object_into_schema(data) task = Task() task.title = data['title'] task.description = data['description'] task.due_date = data['due_date'] task.task_assignees = assignee_ids task.project_id = project.id task.save() task.save() return response('success', message=success_messages['created'].format('Task'), data=schema.dump(task).data, status_code=201)
def get(self, task_id): """ Get a single task :param task_id: :return: Task object """ user = get_jwt_identity() schema = TaskSchema() task = Task.get(task_id) if task is None: raise ValidationError({'message': 'Task not found'}) return response('success', success_messages['retrieved'].format('Task'), schema.dump(task).data)
def get(self): """ Get all Project :param None: :return: Project object """ # user = get_jwt_identity() schema = ProjectSchema(many=True) projects = Project.get_all() if projects is None: raise ValidationError({'message': 'No Project Found'}) projects_list = schema.dump(projects).data return response('success', success_messages['retrieved'].format('Projects'), projects_list)
def get(self, project_id): """ Get a single project :param project_id: :return: Project object """ user = get_jwt_identity() schema = ProjectSchema() project = Project.get(project_id) if project is None: raise ValidationError({'message': 'Project not found'}) project = schema.dump(project).data return response('success', success_messages['retrieved'].format('Project'), project)
def post(self): """ An endpoint to register a user """ request_data = request.get_json() user_data = schema.load_object_into_schema(request_data) email = request_data.get('email') user_exist = User.find_by_email(email) if user_exist: raise ValidationError({ "message": serialization_messages['exists'].format('User') }, 409) user = User(**user_data) user.save() token = generate_token(user) data = { 'token': token, 'user': schema.dump(user).data } return response('success', message=success_messages['created'].format('User'), data=data, status_code=201)
def patch(self, project_id): """ Endpoint to update project""" request_data = request.get_json() user = get_jwt_identity() project = Project.get(project_id) if user.get('id') != project.created_by: raise ValidationError({ 'message': 'Unauthorized user, you cannot perform this operation' }) schema = ProjectSchema(context={'id': project_id}) if 'assignees' in request_data: user_list = assign_user(request_data.get('assignees')) assignees = user_list if user_list is not None else [] del request_data['assignees'] data = schema.load_object_into_schema(request_data, partial=True) data['assignees'] = assignees else: data = schema.load_object_into_schema(request_data, partial=True) project.update_(**data) return response('success', message=success_messages['updated'].format('Project'), data=schema.dump(project).data, status_code=200)