Example #1
0
def edit_task(task_id):
    error = None
    theTask = Task.query.get_or_404(task_id)
    form = EditTaskForm(obj=theTask)
    form.project.choices = [(x.id, x.title) for x in Project.query.order_by('title')]
    form.owner.choices = [(x.id, x.fullname) for x in User.query.order_by('lname')]
    if request.method == 'GET':
        form.project.data = theTask.project_id
        form.owner.data = theTask.owner_id
    if form.validate_on_submit():
        theTask.project_id = form.project.data
        theTask.title = form.title.data
        theTask.description = form.description.data
        theTask.owner = User.query.get_or_404(form.owner.data)
        theTask.start_date = form.start_date.data
        theTask.percent_complete = form.percent_complete.data
        if theTask.percent_complete == 100:
            theTask.complete_date = datetime.date.today()
        else:
            theTask.complete_date = None
        theTask.due_date = form.due_date.data
        if form.task_note.data:
            tn = TaskNote()
            tn.description = form.task_note.data
            theTask.notes.append(tn)
        if form.attachment.data:
            filename = form.attachment.data.filename
            safeFN = secure_filename(form.attachment.data.filename)
            form.attachment.data.save(os.path.join(app.config['UPLOAD_FOLDER'], safeFN))
            ta = TaskAttachment()
            ta.filename = safeFN
            theTask.attachments.append(ta)
        db.session.commit()
        return redirect(url_for('task_view', task_id=theTask.id))
    return render_template("project/edittask.html", error=error, theTask=theTask, form=form)
Example #2
0
def add_task():
    error = None
    form = AddTaskForm()
    form.project.choices = [(x.id, x.title) for x in Project.query.order_by('title')]
    form.owner.choices = [(x.id, x.fullname) for x in User.query.order_by('lname')]
    if request.method == 'GET' and request.args.get('project_id'):
        Project.query.get_or_404(request.args.get('project_id'))
        form.project.data = int(request.args.get('project_id'))
    if form.validate_on_submit():
        theTask = Task()
        theTask.project_id = form.project.data
        theTask.title = form.title.data
        theTask.description = form.description.data
        theTask.owner = User.query.get_or_404(form.owner.data)
        theTask.start_date = form.start_date.data
        theTask.percent_complete = form.percent_complete.data
        theTask.due_date = form.due_date.data
        if form.attachment.data:
            filename = form.attachment.data.filename
            safeFN = secure_filename(form.attachment.data.filename)
            form.attachment.data.save(os.path.join(app.config['UPLOAD_FOLDER'], safeFN))
            ta = TaskAttachment()
            ta.filename = safeFN
            theTask.attachments.append(ta)
        if not error:
            db.session.add(theTask)
            db.session.commit()
            return redirect(url_for('project_view', project_id=theTask.project_id))
    return render_template("project/addtask.html", error=error, form=form)