def field(field_id): form_delete = forms.DeleteForm() form_field = forms.FieldForm() field = mongo.db.fields.find_one({'_id': ObjectId(field_id)}) crop_types = list(mongo.db.crop_types.find()) form_field.name.data = field["name"] # Delete field if request.method == 'POST' and 'delete' in request.form.keys(): if mongo.db.fields.remove({"_id": ObjectId(field_id)}): return redirect(url_for('fields')) print "root" # Save field if request.method == 'POST': print "post" print form_field.size.data if form_field.validate_on_submit(): field['name'] = form_field.name.data field['size'] = form_field.size.data field['geo'] = form_field.geo_data.data field['province'] = g.province field_id = mongo.db.fields.save(field) return redirect(url_for('field', field_id=field_id)) # Set field form_field.name.data = field['name'] form_field.size.data = field['size'] form_field.geo_data.data = field['geo'] return render_template('field.html', field = field, form_field=form_field, form_delete=form_delete, crop_types=crop_types)
def contract(contract_id): contract = mongo.db.contracts.find_one({"_id": ObjectId(contract_id)}) form = forms.DeleteForm() if request.method == 'POST': if mongo.db.fields.remove({'_id': ObjectId(contract_id)}): return redirect(url_for('contracts')) else: return render_template('contract.html', contract=contract, form=form)
def bin(bin_id): form = forms.DeleteForm() if request.method == 'POST' and 'delete' in request.form.keys(): if mongo.db.bins.remove({ "_id": ObjectId(bin_id) }): return redirect(url_for('fields')) bin = mongo.db.bins.find_one({"_id": ObjectId(bin_id) }) return render_template('bin.html', bin = bin, form = form)
def section(field_id, index): field = mongo.db.fields.find_one({'_id': ObjectId(field_id)}) form = forms.DeleteForm() section = field['section'][int(index)] if request.method == 'POST': form['section'].remove(section) mongo.db.fields.save(form) return redirect(url_for('field', field_id=field_id)) else: return render_template('section.html', field=field, section=section, form=form)
def section_edit(field_id, index): form = forms.SectionForm() delete_form = forms.DeleteForm() crop_types = list(mongo.db.crop_types.find()) choices = [(x['name'],x['label']) for x in crop_types] form.crop.choices = choices field = mongo.db.fields.find_one({'_id': ObjectId(field_id)}) section = field['section'][int(index)] if request.method == 'POST': if 'delete' in request.form.keys(): field['section'].remove(section) mongo.db.fields.save(field) return redirect(url_for('field', field_id=field_id)) if form.validate_on_submit(): section['name'] = form.name.data section['crop'] = form.crop.data section['acres'] = form.acres.data mongo.db.fields.save(field) return redirect(url_for('field', field_id=field_id)) else: form.name.data = section['name'] form.acres.data = section['acres'] form.crop.data = section['crop'] return render_template('section_edit.html', form=form, form_delete=delete_form, field_id=field_id, index=index)