def edit(id): '''goal detail''' g = Goals() goal = g.find_one(id) if request.method == 'POST': error = [] # change name, description, due date and reward file if 'name' in request.form and request.form['name']: goal['name'] = request.form['name'] if 'description' in request.form and request.form['description']: goal['description'] = request.form['description'] if 'file' in request.files and request.files['file']: # save image c = CDN() image = c.replace(goal['reward'], { 'image': utils.file_to_mongo(request.files['file'])}) goal['date']['end'] = utils.timestamp_new( year = request.form['due-date[year]'], month = request.form['due-date[month]'], day = request.form['due-date[day]']) # tags if 'tags' in request.form and request.form['tags']: tags = [utils.slugify(tag) for tag in request.form['tags'].split(',')] goal['tags'] = tags # two different systems if goal['variant'] == 'value': # target value # validate if 'values[begin]' not in request.form or not request.form['values[begin]']: error.append('A starting value is missing') if 'values[end]' not in request.form or not request.form['values[end]']: error.append('A target value is missing') if 'target[points]' not in request.form or not request.form['target[points]']: error.append('Target amount of points is missing') # change the values if not error: goal['values']['begin'] = float(request.form['values[begin]']) goal['values']['end'] = float(request.form['values[end]']) goal['points']['target'] = int(request.form['target[points]']) goal['points']['conversion'] = (float(request.form['values[end]']) - float(request.form['values[begin]'])) / float(request.form['target[points]']) goal['points']['unit'] = request.form['unit'] else: # points # validate if 'points[target]' not in request.form or not request.form['points[target]']: error.append('Target amount of points is missing') if 'points[name-0]' not in request.form or not request.form['points[name-0]']: error.append('At least one currency activity is missing') # change the values if not error: goal['points']['target'] = int(request.form['points[target]']) currency = [] for n in range(10): if 'points[name-'+str(n)+']' in request.form and len(request.form['points[name-'+str(n)+']']) > 0: currency.append({ 'name': request.form['points[name-'+str(n)+']'], 'points': int(request.form['points[currency-'+str(n)+']']) }) goal['points']['currency'] = currency if not error: # update g.replace(goal['_id'], goal) return redirect(url_for('goals.goal', id=id)) goals = utils.sort_list_by_points(g.to_list(g.find_all())) fibonacci = [1,2,3,5,8,13,20,35,50,100,150,200,400] date = utils.date_list(goal['date']['end']) return render_template(goal['variant']+'-edit.html', **locals())
def edit(id): '''goal detail''' g = Goals() goal = g.find_one(id) if request.method == 'POST': error = [] # change name, description, due date and reward file if 'name' in request.form and request.form['name']: goal['name'] = request.form['name'] if 'description' in request.form and request.form['description']: goal['description'] = request.form['description'] if 'file' in request.files and request.files['file']: # save image c = CDN() image = c.replace( goal['reward'], {'image': utils.file_to_mongo(request.files['file'])}) goal['date']['end'] = utils.timestamp_new( year=request.form['due-date[year]'], month=request.form['due-date[month]'], day=request.form['due-date[day]']) # tags if 'tags' in request.form and request.form['tags']: tags = [ utils.slugify(tag) for tag in request.form['tags'].split(',') ] goal['tags'] = tags # two different systems if goal['variant'] == 'value': # target value # validate if 'values[begin]' not in request.form or not request.form[ 'values[begin]']: error.append('A starting value is missing') if 'values[end]' not in request.form or not request.form[ 'values[end]']: error.append('A target value is missing') if 'target[points]' not in request.form or not request.form[ 'target[points]']: error.append('Target amount of points is missing') # change the values if not error: goal['values']['begin'] = float(request.form['values[begin]']) goal['values']['end'] = float(request.form['values[end]']) goal['points']['target'] = int(request.form['target[points]']) goal['points']['conversion'] = ( float(request.form['values[end]']) - float(request.form['values[begin]'])) / float( request.form['target[points]']) goal['points']['unit'] = request.form['unit'] else: # points # validate if 'points[target]' not in request.form or not request.form[ 'points[target]']: error.append('Target amount of points is missing') if 'points[name-0]' not in request.form or not request.form[ 'points[name-0]']: error.append('At least one currency activity is missing') # change the values if not error: goal['points']['target'] = int(request.form['points[target]']) currency = [] for n in range(10): if 'points[name-' + str(n) + ']' in request.form and len( request.form['points[name-' + str(n) + ']']) > 0: currency.append({ 'name': request.form['points[name-' + str(n) + ']'], 'points': int(request.form['points[currency-' + str(n) + ']']) }) goal['points']['currency'] = currency if not error: # update g.replace(goal['_id'], goal) return redirect(url_for('goals.goal', id=id)) goals = utils.sort_list_by_points(g.to_list(g.find_all())) fibonacci = [1, 2, 3, 5, 8, 13, 20, 35, 50, 100, 150, 200, 400] date = utils.date_list(goal['date']['end']) return render_template(goal['variant'] + '-edit.html', **locals())