Commit 77e86148d96741a411ca6683686c1635285ec54b

Authored by Antoine Goutenoir
1 parent 466912a2
Exists in master

Fake support for model selection.

We still use all the models.
flaskr/controllers/main_controller.py
1   -import importlib
2 1 import geopy
3 2 import sqlalchemy
4 3  
... ... @@ -11,7 +10,7 @@ from flaskr.forms import LoginForm, EstimateForm
11 10 from flaskr.models import db, User, Estimation, StatusEnum
12 11 from flaskr.geocoder import CachedGeocoder
13 12  
14   -from flaskr.core import generate_unique_id
  13 +from flaskr.core import generate_unique_id, get_emission_models
15 14 from flaskr.content import content
16 15  
17 16 from yaml import safe_dump as yaml_dump
... ... @@ -29,20 +28,6 @@ OUT_ENCODING = 'utf-8'
29 28 # -----------------------------------------------------------------------------
30 29 # refactor this outta here, like in core?
31 30  
32   -def get_emission_models():
33   - emission_models_confs = content.models
34   - emission_models = []
35   -
36   - for model_conf in emission_models_confs:
37   - model_file = model_conf.file
38   - the_module = importlib.import_module("flaskr.laws.%s" % model_file)
39   -
40   - model = the_module.EmissionModel(model_conf)
41   - # model.configure(extra_model_conf)
42   -
43   - emission_models.append(model)
44   -
45   - return emission_models
46 31  
47 32 # -----------------------------------------------------------------------------
48 33  
... ... @@ -69,6 +54,7 @@ def home():
69 54  
70 55 @main.route("/estimate", methods=["GET", "POST"])
71 56 def estimate():
  57 + models = get_emission_models()
72 58 form = EstimateForm()
73 59  
74 60 if form.validate_on_submit():
... ... @@ -84,6 +70,11 @@ def estimate():
84 70 estimation.origin_addresses = form.origin_addresses.data
85 71 estimation.destination_addresses = form.destination_addresses.data
86 72 # estimation.compute_optimal_destination = form.compute_optimal_destination.data
  73 + models_slugs = []
  74 + for model in models:
  75 + if getattr(form, 'use_model_' % model.slug).data:
  76 + models_slugs.append(model.slug)
  77 + estimation.models_slugs = "\n".join(models_slugs)
87 78  
88 79 db.session.add(estimation)
89 80 db.session.commit()
... ... @@ -96,7 +87,7 @@ def estimate():
96 87 ))
97 88 # return render_template("estimate-debrief.html", form=form)
98 89  
99   - return render_template("estimate.html", form=form)
  90 + return render_template("estimate.html", form=form, models=models)
100 91  
101 92  
102 93 @main.route("/invalidate")
... ...
flaskr/forms.py
... ... @@ -8,6 +8,7 @@ from wtforms import validators
8 8  
9 9 from .models import User
10 10 from .content import content_dict as content
  11 +from .core import models
11 12  
12 13 form_content = content['estimate']['form']
13 14  
... ... @@ -15,6 +16,7 @@ form_content = content['estimate']['form']
15 16 # ESTIMATION FORM #############################################################
16 17  
17 18 class EstimateForm(FlaskForm):
  19 +
18 20 # email = StringField(
19 21 # label=form_content['email']['label'],
20 22 # description=form_content['email']['description'],
... ... @@ -23,6 +25,7 @@ class EstimateForm(FlaskForm):
23 25 # validators.Email(),
24 26 # ],
25 27 # )
  28 +
26 29 first_name = StringField(
27 30 label=form_content['first_name']['label'],
28 31 description=form_content['first_name']['description'],
... ... @@ -81,6 +84,7 @@ class EstimateForm(FlaskForm):
81 84 "placeholder": form_content['destination_addresses']['placeholder']
82 85 },
83 86 )
  87 +
84 88 # compute_optimal_destination = BooleanField(
85 89 # label=form_content['compute_optimal_destination']['label'],
86 90 # description=form_content['compute_optimal_destination']['description'],
... ... @@ -105,9 +109,40 @@ class EstimateForm(FlaskForm):
105 109 if not check_validate:
106 110 return False
107 111  
  112 + uses_at_least_one_model = False
  113 + for model in models:
  114 + use_model = getattr(self, 'use_model_%s' % model.slug)
  115 + #print("Model data", model.slug, use_model.data)
  116 + if use_model.data:
  117 + uses_at_least_one_model = True
  118 +
  119 + if not uses_at_least_one_model:
  120 + last_model = getattr(self, 'use_model_%s' % models[-1].slug)
  121 + last_model.errors.append("Please select at least one model."
  122 + "  " # It's been a while
  123 + "<em>What are you doing?</em>")
  124 + return False
  125 +
108 126 return True
109 127  
110 128  
  129 +# Add the models' checkboxes to the above Form
  130 +for model in models:
  131 + setattr( # setattr() takes no keyword arguments -.-
  132 + EstimateForm,
  133 + 'use_model_%s' % model.slug,
  134 + BooleanField(
  135 + label=model.name,
  136 + # description=model.short_description,
  137 + default=True,
  138 + # default=model.default, # todo: from config
  139 + validators=[
  140 + validators.Optional(),
  141 + ],
  142 + )
  143 + )
  144 +
  145 +
111 146 # LOGIN FORM ##################################################################
112 147  
113 148 class LoginForm(FlaskForm):
... ...
flaskr/templates/estimate.html
... ... @@ -12,6 +12,11 @@
12 12 {% endblock %}
13 13  
14 14  
  15 +
  16 +{#############################################################################}
  17 +{# MACROS ######################################################################}
  18 +
  19 +
15 20 {% macro render_field(field) %}
16 21 <dt>
17 22 {{ field.label }}
... ... @@ -21,8 +26,9 @@
21 26 </dt>
22 27 <dd>
23 28 {{ field(title=field.description, class_="form-control", **kwargs) | safe }}
  29 +
24 30 {% if field.errors -%}
25   - <ul class=errors>
  31 + <ul class="errors text-danger has-error">
26 32 {% for error in field.errors %}
27 33 <li>{{ error }}</li>
28 34 {% endfor %}
... ... @@ -38,9 +44,18 @@
38 44 class="form-check-label",
39 45 title=field.description
40 46 ) }}
  47 +
  48 +{% if field.errors -%}
  49 +<p class="text-danger help-block">{{ field.errors[0] }}</p>
  50 +{%- endif %}
41 51 {% endmacro %}
42 52  
43 53  
  54 +
  55 +{#############################################################################}
  56 +{# BODY ######################################################################}
  57 +
  58 +
44 59 {% block body %}
45 60 <div class="row">
46 61 <div class="col-md-2"></div>
... ... @@ -68,7 +83,9 @@
68 83 <div class="form-group">
69 84 {{ render_field(form.origin_addresses) }}
70 85 <small class="form-text text-muted">
71   - Use <code>en_US</code> city and country names, without diacritics. The comma matters.
  86 + Use <code>en_US</code> city and country names, without diacritics.
  87 + &nbsp;
  88 + The comma matters.
72 89 <br>
73 90 These usually are the addresses of your participants.
74 91 </small>
... ... @@ -88,6 +105,24 @@
88 105 {# {{ render_checkbox(form.use_atmosfair_rfi) }}#}
89 106 {# <small class="form-text text-muted">Disabled. Work in Progress. RFI=1.9</small>#}
90 107 {# </div>#}
  108 +
  109 + <div class="form-group">
  110 +
  111 + <h6>Emission Models to consider</h6>
  112 + <div class="form-check">
  113 + {% for model in models %}
  114 + <div>
  115 + {{ render_checkbox(form['use_model_'~model.slug]) }}
  116 + </div>
  117 + {% endfor %}
  118 + </div>
  119 + <small class="form-text text-muted">
  120 + We will use a mean of the selected models.
  121 + &nbsp;
  122 + Please select at least one.
  123 + </small>
  124 + </div>
  125 +
91 126 <div class="form-group">
92 127 {{ render_field(form.comment) }}
93 128 </div>
... ...