def edit(id): errors = [] form = EditTaskForm(request.form) task = None possible_assigned = [elem.username for elem in list(User.view('users/by_username'))] possible_project = [elem.title for elem in list (Project.view('projects/by_title'))] if id == NEW_TASK_ID: task = Task() else: if not g.db.doc_exist(id): abort(404) task = Task.get(id) if request.method == 'GET': form = EditTaskForm(obj=task) form.assigned.choices = zip(possible_assigned, possible_assigned) form.project.choices = zip(possible_project, possible_project) # dirty hack here: we use referrer to determine from which # project we came from and set correct value to select field if PROJECT_ROUTE in request.referrer: project = request.referrer.split('/')[-1] project = Project.get(project_id) form.project.default = project.title form.process() if request.method == 'POST' and form.validate(): form.populate_obj(task) task.author = session['username'] task.update_date = datetime.datetime.utcnow() task.tags = ' '.join(set(task.tags.split())) if id == NEW_TASK_ID: task.create_date = task.update_date task.save() for ff in request.files.keys(): f = request.files[ff] if f: fname = secure_filename(f.filename) fld = os.path.join(UPLOADED_FILES, task._id) if not os.path.exists(fld): os.mkdir(fld) target_path = os.path.join(fld, fname) while os.path.exists(target_path): filename, ext = os.path.splitext(target_path) r = ''.join(random.choice('0123456789abcdef') for i in range(8)) target_path = os.path.join(fld, filename + '-' + r + ext) f.save(target_path) flash('Successfully uploaded %s' % fname) flash('Task was successfully %s' % ('created' if id == NEW_TASK_ID else 'updated')) return redirect(url_for('tasks.show', id=task._id)) errors.extend(format_form_errors(form.errors.items())) return render_template('task_edit.html', id = id, form = form, errors = errors)
def add_task(task): db = connect_db(app.config.get("DB")) Task.set_db(db) Project.set_db(db) # db_task = Task() # db_project = list(Project.view("projects/by_title", key=task["project"]))[0] db_task.author = task["author"] db_task.assigned = task["assigned"] db_task.priority = task["priority"] db_task.title = task["title"] db_task.text = task["text"] db_task.status = task["status"] db_task.project = db_project.title db_task.create_date = datetime.datetime.utcnow() db_task.update_date = datetime.datetime.utcnow() db_task.due_date = datetime.date.today() # db.save_doc(db_task)
def add_task(task): db = connect_db(app.config.get('DB')) Task.set_db(db) Project.set_db(db) # db_task = Task() # db_project = list(Project.view('projects/by_title', key=task['project']))[0] db_task.author = task['author'] db_task.assigned = task['assigned'] db_task.priority = task['priority'] db_task.title = task['title'] db_task.text = task['text'] db_task.status = task['status'] db_task.project = db_project.title db_task.create_date = datetime.datetime.utcnow() db_task.update_date = datetime.datetime.utcnow() db_task.due_date = datetime.date.today() # db.save_doc(db_task)
def edit(id): errors = [] form = EditTaskForm(request.form) task = None possible_assigned = [ elem.username for elem in list(User.view('users/by_username')) ] possible_project = [ elem.title for elem in list(Project.view('projects/by_title')) ] if id == NEW_TASK_ID: task = Task() else: if not g.db.doc_exist(id): abort(404) task = Task.get(id) if request.method == 'GET': form = EditTaskForm(obj=task) form.assigned.choices = zip(possible_assigned, possible_assigned) form.project.choices = zip(possible_project, possible_project) # dirty hack here: we use referrer to determine from which # project we came from and set correct value to select field if PROJECT_ROUTE in request.referrer: project = request.referrer.split('/')[-1] project = Project.get(project_id) form.project.default = project.title form.process() if request.method == 'POST' and form.validate(): form.populate_obj(task) task.author = session['username'] task.update_date = datetime.datetime.utcnow() task.tags = ' '.join(set(task.tags.split())) if id == NEW_TASK_ID: task.create_date = task.update_date task.save() for ff in request.files.keys(): f = request.files[ff] if f: fname = secure_filename(f.filename) fld = os.path.join(UPLOADED_FILES, task._id) if not os.path.exists(fld): os.mkdir(fld) target_path = os.path.join(fld, fname) while os.path.exists(target_path): filename, ext = os.path.splitext(target_path) r = ''.join( random.choice('0123456789abcdef') for i in range(8)) target_path = os.path.join(fld, filename + '-' + r + ext) f.save(target_path) flash('Successfully uploaded %s' % fname) flash('Task was successfully %s' % ('created' if id == NEW_TASK_ID else 'updated')) return redirect(url_for('tasks.show', id=task._id)) errors.extend(format_form_errors(form.errors.items())) return render_template('task_edit.html', id=id, form=form, errors=errors)