def read_one(task_id): """ This function responds to a request for /api/tasks/{task_id} with one matching task from tasks :param task_id: Id of task to find :return: task matching id """ # Get the task requested task = Task.query.filter(Task.task_id == task_id).one_or_none() # Did we find a task? if task is not None: # Serialize the data for the response task_schema = TaskSchema() data = task_schema.dump(task) return data # Otherwise, nope, didn't find that task else: abort( 404, "Task not found for Id: {task_id}".format(task_id=task_id), )
def create(user_id, task): """ This function creates a new note related to the passed in person id. :param person_id: Id of the person the note is related to :param note: The JSON containing the note data :return: 201 on success """ # get the parent person user = User.query.filter(User.user_id == user_id).one_or_none() # Was a person found? if user is None: abort(404, f"User not found for Id: {user_id}") # Create a note schema instance schema = TaskSchema() new_task = schema.load(task, session=db.session).data # Add the note to the person and database user.tasks.append(new_task) db.session.commit() # Serialize and return the newly created note in the response data = schema.dump(new_task).data return data, 201
def read_one(taskid): task = Task.query.filter(Task.taskid == taskid).one_or_none() if task is not None: schema = TaskSchema() data = schema.dump(task) return data, 200 else: abort(404, "Task Record not found for {}".format(taskid))
def create(name, game_id): try: task = Task(name, Status.active, game_id) schema = TaskSchema() db.session.add(task) db.session.commit() return schema.dump(task) except SQLAlchemyError as e: db.session.rollback() error = str(e.__dict__) return error
def read_all(): """ This function responds to a request for /api/tasks with the complete lists of tasks :return: json string of list of tasks """ tasks = Task.query.order_by(Task.title).all() # Serialize the data for the response task_schema = TaskSchema(many=True) data = task_schema.dump(tasks) return data
def update(task_id, task): """ This function updates an existing task in the tasks structure Throws an error if a task with the name we want to update to already exists in the database. :param task_id: Id of the task to update in the tasks structure :param task: task to update :return: updated task structure """ # Get the task requested from the db into session update_task = Task.query.filter(Task.task_id == task_id).one_or_none() # Try to find an existing task with the same name as the update title = task.get("title") existing_task = (Task.query.filter(Task.title == title).one_or_none()) # Are we trying to find a task that does not exist? if update_task is None: abort( 404, "Task not found for Id: {task_id}".format(task_id=task_id), ) # Would our update create a duplicate of another task already existing? elif (existing_task is not None and existing_task.task_id != task_id): abort( 409, "Task {title} exists already".format(title=title), ) # Otherwise go ahead and update! else: # turn the passed in task into a db object schema = TaskSchema() update = schema.load(task, session=db.session) # Set the id to the task we want to update update.task_id = update_task.task_id # merge the new object into the old and commit it to the db db.session.merge(update) db.session.commit() # return updated task in the response data = schema.dump(update_task) return data, 200
def read_all(): """ This function responds to a request for /api/people/notes with the complete list of notes, sorted by note timestamp :return: json list of all notes for all people """ # Query the database for all the notes tasks = Task.query.order_by(db.desc(Task.timestamp)).all() # Serialize the list of notes from our data task_schema = TaskSchema(many=True, exclude=["user.notes"]) data = task_schema.dump(tasks).data return data
def get_all_tasks(): try: task = Tasks.query.order_by(desc(Tasks.date)).all() if not task: message = "There is no task" return send_success_response(message) task_schema = TaskSchema(many=True) data = task_schema.dump(task).data message = "Successfully retrieved tasks" return send_success_response(message, data) except Exception as e: print(e) message = "Error in retrieving tasks" return send_error_response(message)
def get_task_info(task_id): try: task = Tasks.query.get(task_id) if not task: message = "There is no task with that id" return send_success_response(message) task_schema = TaskSchema() message = "Successfully retrieved task of " + task.task_title data = task_schema.dump(task).data return send_success_response(message, data) except Exception as e: print(e) message = "Error in retrieving task of " + str(task_id) return send_error_response(message)
def read_expiring(): """ This function responds to a request for /api/tasks-expiring with the complete lists of tasks that will expire today :return: json string of list of tasks """ today = datetime.today() print(today) tasks = Task.query.filter(Task.duedate <= today).order_by(Task.title).all() # Serialize the data for the response task_schema = TaskSchema(many=True) data = task_schema.dump(tasks) return data
def get_task(user_id): try: user = User.query.get(user_id) task = Tasks.query.filter_by(user=user).order_by(desc( Tasks.date)).all() if not task: message = "There is no task of user" + user.user_name return send_success_response(message) task_schema = TaskSchema(many=True) message = "Successfully retrieved task of " + user.user_name data = task_schema.dump(task).data return send_success_response(message, data) except Exception as e: print(e) message = "Error in retrieving task of " + str(user_id) return send_error_response(message)
def create(task_name): existing_task = Task.query.filter( Task.task_name == task_name).one_or_none() if existing_task is None: new_task = Task(task_name) db.session.add(new_task) db.session.commit() schema = TaskSchema() data = schema.dump(new_task) return data, 201 else: abort(409, "Task {} already exists.".format(task_name))
def update_task(task_id): try: task = Tasks.query.get(task_id) project_name = request.json.get('project_name') task.task_title = request.json.get('task_title', task.task_title) task.status = request.json.get('status', task.status) task.reason = request.json.get('reason', task.reason) task.project = Project.query.filter_by( project_name=project_name).first() db.session.commit() task_schema = TaskSchema() data = task_schema.dump(task).data message = "Successfully updated tasks" return send_success_response(message, data) except Exception as e: print(e) message = "Error in updating task" return send_error_response(message)
def update(taskid, task_name): update_task = Task.query.filter(Task.taskid == taskid).one_or_none() existing_task = Task.query.filter( Task.task_name == task_name).one_or_none() if update_task is None: abort(404, "Task not found for ID {}.".format(taskid)) elif existing_task is not None: abort(409, "Task {} already exists.".format(task_name)) else: update = Task(task_name) update.taskid = taskid db.session.merge(update) db.session.commit() schema = TaskSchema() data = schema.dump(update) return data, 200
def read_one(user_id, task_id): """ This function responds to a request for /api/people/{person_id}/notes/{note_id} with one matching note for the associated person :param person_id: Id of person the note is related to :param note_id: Id of the note :return: json string of note contents """ # Query the database for the note task = (Task.query.join(User, User.user_id == Task.user_id).filter( User.user_id == user_id).filter(Task.task_id == task_id).one_or_none()) # Was a note found? if task is not None: task_schema = TaskSchema() data = task_schema.dump(task).data return data # Otherwise, nope, didn't find that note else: abort(404, f"Note not found for Id: {task_id}")
def create(task): """ This function creates a new task in the tasks structure based on the passed in task data :param task: task to create in tasks structure :return: 201 on success, 406 on task exists """ title = task.get("title") duedate = task.get("duedate") existing_task = (Task.query.filter(Task.title == title).one_or_none()) # Can we insert this task? if existing_task is None: # Create a task instance using the schema and the passed in task schema = TaskSchema() print(duedate) datetime_object = datetime.strptime(duedate, '%d/%m/%Y') task["duedate"] = datetime_object.strftime('%Y-%m-%d') new_task = schema.load(task, session=db.session) # Add the task to the database db.session.add(new_task) db.session.commit() # Serialize and return the newly created task in the response data = schema.dump(new_task) return data, 201 # Otherwise, nope, task exists already else: abort( 409, "Task {title} exists already".format(title=title), )
def update(user_id, task_id, task): """ This function updates an existing note related to the passed in person id. :param person_id: Id of the person the note is related to :param note_id: Id of the note to update :param content: The JSON containing the note data :return: 200 on success """ update_task = (Task.query.filter(User.user_id == user_id).filter( Task.task_id == task_id).one_or_none()) # Did we find an existing note? if update_task is not None: # turn the passed in note into a db object schema = TaskSchema() update = schema.load(task, session=db.session).data # Set the id's to the note we want to update update.user_id = update_task.person_id update.task_id = update_task.task_id # merge the new object into the old and commit it to the db db.session.merge(update) db.session.commit() # return updated note in the response data = schema.dump(update_task).data return data, 200 # Otherwise, nope, didn't find that note else: abort(404, f"Task not found for Id: {task_id}")
def read_all(): tasks = Task.query.order_by(Task.task_name).all() task_schema = TaskSchema(many=True) data = task_schema.dump(tasks) return data
from flask_restful import Resource from flask import request from models import db, User, Task, TaskSchema import random import string tasks_schema = TaskSchema(many=True) task_schema = TaskSchema() class Tasks(Resource): def post(self): header = request.headers["Authorization"] json_data = request.get_json(force=True) if not header: return {'message': 'No Api Key'}, 400 else: user = User.query.filter_by(api_key=header).first() if user: tasks = Task(user_id=user.id, done=json_data['done'], repeats=json_data['repeats'], deadline=json_data['deadline'], reminder=json_data['reminder'], note=json_data['note'], title=json_data['title']) db.session.add(tasks) db.session.commit() result = task_schema.dump(tasks).data return {'status': 'success', 'data': result}, 201