diff --git a/flaskr/controllers/main_controller.py b/flaskr/controllers/main_controller.py index 4ff3c13..e1c527e 100644 --- a/flaskr/controllers/main_controller.py +++ b/flaskr/controllers/main_controller.py @@ -1,4 +1,3 @@ -import importlib import geopy import sqlalchemy @@ -11,7 +10,7 @@ from flaskr.forms import LoginForm, EstimateForm from flaskr.models import db, User, Estimation, StatusEnum from flaskr.geocoder import CachedGeocoder -from flaskr.core import generate_unique_id +from flaskr.core import generate_unique_id, get_emission_models from flaskr.content import content from yaml import safe_dump as yaml_dump @@ -29,20 +28,6 @@ OUT_ENCODING = 'utf-8' # ----------------------------------------------------------------------------- # refactor this outta here, like in core? -def get_emission_models(): - emission_models_confs = content.models - emission_models = [] - - for model_conf in emission_models_confs: - model_file = model_conf.file - the_module = importlib.import_module("flaskr.laws.%s" % model_file) - - model = the_module.EmissionModel(model_conf) - # model.configure(extra_model_conf) - - emission_models.append(model) - - return emission_models # ----------------------------------------------------------------------------- @@ -69,6 +54,7 @@ def home(): @main.route("/estimate", methods=["GET", "POST"]) def estimate(): + models = get_emission_models() form = EstimateForm() if form.validate_on_submit(): @@ -84,6 +70,11 @@ def estimate(): estimation.origin_addresses = form.origin_addresses.data estimation.destination_addresses = form.destination_addresses.data # estimation.compute_optimal_destination = form.compute_optimal_destination.data + models_slugs = [] + for model in models: + if getattr(form, 'use_model_' % model.slug).data: + models_slugs.append(model.slug) + estimation.models_slugs = "\n".join(models_slugs) db.session.add(estimation) db.session.commit() @@ -96,7 +87,7 @@ def estimate(): )) # return render_template("estimate-debrief.html", form=form) - return render_template("estimate.html", form=form) + return render_template("estimate.html", form=form, models=models) @main.route("/invalidate") diff --git a/flaskr/forms.py b/flaskr/forms.py index 7469ec1..5a217ee 100644 --- a/flaskr/forms.py +++ b/flaskr/forms.py @@ -8,6 +8,7 @@ from wtforms import validators from .models import User from .content import content_dict as content +from .core import models form_content = content['estimate']['form'] @@ -15,6 +16,7 @@ form_content = content['estimate']['form'] # ESTIMATION FORM ############################################################# class EstimateForm(FlaskForm): + # email = StringField( # label=form_content['email']['label'], # description=form_content['email']['description'], @@ -23,6 +25,7 @@ class EstimateForm(FlaskForm): # validators.Email(), # ], # ) + first_name = StringField( label=form_content['first_name']['label'], description=form_content['first_name']['description'], @@ -81,6 +84,7 @@ class EstimateForm(FlaskForm): "placeholder": form_content['destination_addresses']['placeholder'] }, ) + # compute_optimal_destination = BooleanField( # label=form_content['compute_optimal_destination']['label'], # description=form_content['compute_optimal_destination']['description'], @@ -105,9 +109,40 @@ class EstimateForm(FlaskForm): if not check_validate: return False + uses_at_least_one_model = False + for model in models: + use_model = getattr(self, 'use_model_%s' % model.slug) + #print("Model data", model.slug, use_model.data) + if use_model.data: + uses_at_least_one_model = True + + if not uses_at_least_one_model: + last_model = getattr(self, 'use_model_%s' % models[-1].slug) + last_model.errors.append("Please select at least one model." + " " # It's been a while + "What are you doing?") + return False + return True +# Add the models' checkboxes to the above Form +for model in models: + setattr( # setattr() takes no keyword arguments -.- + EstimateForm, + 'use_model_%s' % model.slug, + BooleanField( + label=model.name, + # description=model.short_description, + default=True, + # default=model.default, # todo: from config + validators=[ + validators.Optional(), + ], + ) + ) + + # LOGIN FORM ################################################################## class LoginForm(FlaskForm): diff --git a/flaskr/templates/estimate.html b/flaskr/templates/estimate.html index 86adeda..f5346cd 100644 --- a/flaskr/templates/estimate.html +++ b/flaskr/templates/estimate.html @@ -12,6 +12,11 @@ {% endblock %} + +{#############################################################################} +{# MACROS ######################################################################} + + {% macro render_field(field) %}
{{ field.errors[0] }}
+{%- endif %} {% endmacro %} + +{#############################################################################} +{# BODY ######################################################################} + + {% block body %}en_US
city and country names, without diacritics. The comma matters.
+ Use en_US
city and country names, without diacritics.
+
+ The comma matters.