def task_add_edit(id=None): form = AddOrEditTask() form.category.choices = get_unique_categories("task") if id: task = Task.query.filter_by(id=int(id)).first() title = "Edit task" edit = True else: task = Task() title = "Add task" edit = False if request.method == "POST" and form.validate_on_submit(): task.status = form.status.data task.category = form.category.data if task.category and form.add_category.data: task.category = form.add_category.data task.title = form.title.data task.description = form.description.data if not task.description: task.description = "_[ No description ]_" if id: task.modified = datetime.utcnow() + timedelta(hours=1) db.session.commit() flash(f"The changes on '{task.title}' have been saved.") else: task.created = datetime.utcnow() + timedelta(hours=1) db.session.add(task) db.session.commit() flash(f"The task '{form.title.data}' was successfully added.") return redirect(url_for("tasks")) elif id and request.method == "GET": form.status.data = task.status form.category.data = task.category form.title.data = task.title if task.description == "_[ No description ]_": form.description.data = "" else: form.description.data = task.description return render_template("task.html", title=title, form=form, edit=edit)
def edit_task(task_id): task = Task(ObjectId(task_id)) task.flask_validate(edit=True) form = TaskForm(obj=task.to_struct()) form.time.data = task.date.time() if form.validate_on_submit(): date = form.date.data time = form.time.data dt = datetime.combine(date, time or datetime.min.time()) if date else None task.name = form.name.data task.description = form.description.data task.category = form.category.data task.date = dt return redirect( url_for('pages.view_class', class_id=str(task.class_.get_id()))) return render_template('edit_task.html', form=form, task=task)
def _tasks_from_json(app, content=''): if 'tasks' in content: for obj in content['tasks']: user = User.query.filter_by(email=obj['user']).first() if user is None: app.logger.error('Task: %s, Unknown user: %s' % (obj['name'], obj['user'])) break task = Task.query.filter_by(user=user, name=obj['name']).first() if task is None: task = Task() task.user = user task.name = obj['name'] task.description = obj['description'] app.logger.debug('adding task: %s' % task) db.session.add(task) db.session.commit()
def create_task(): """ This method creates a new task. Validates that the title is not empty. :return: The list with all the previous tasks plus the new task. """ # Retrieving the current values in the database to show all the tasks tasks_db = Task.query.all() tasks = [] for task in tasks_db: task_data = { 'id': task.id, 'title': task.title, 'description': task.description, 'done': task.done } tasks.append(task_data) data_received = request.json if not data_received or not 'title' in data_received: abort(400) # Created the object task task_db = Task() task_db.title = data_received['title'] task_db.description = data_received['description'] task_db.done = False db.session.add(task_db) db.session.commit() # Converting the object task to a dictionary task_data = { 'id': task_db.id, 'title': task_db.title, 'description': task_db.description, 'done': task_db.done } tasks.append(task_data) return jsonify({'tasks': tasks}), 201