Commit e2637443102bd5929ab9cf2f502192d065c1f378

Authored by Antoine Goutenoir
1 parent 7662554f
Exists in master

Send emails to admins.

Showing 1 changed file with 64 additions and 38 deletions   Show diff stats
flaskr/controllers/main_controller.py
... ... @@ -4,21 +4,30 @@ from copy import deepcopy
4 4 import geopy
5 5 import sqlalchemy
6 6  
7   -from flask import Blueprint, render_template, flash, request, redirect, \
8   - url_for, abort, send_from_directory, Response
9 7 from os.path import join
10   -
11   -from os import unlink
12   -
13   -from flask_mail import Message
14   -
15   -from flaskr.extensions import cache, basic_auth, mail
  8 +from os import unlink, getenv
  9 +
  10 +from flask import (
  11 + Blueprint,
  12 + Response,
  13 + render_template,
  14 + flash,
  15 + request,
  16 + redirect,
  17 + url_for,
  18 + abort,
  19 + send_from_directory,
  20 +)
  21 +from flaskr.extensions import cache, basic_auth, mail, send_email
16 22 from flaskr.forms import LoginForm, EstimateForm
17 23 from flaskr.models import db, User, Estimation, StatusEnum, ScenarioEnum
18 24 from flaskr.geocoder import CachedGeocoder
19 25  
20   -from flaskr.core import generate_unique_id, \
21   - get_emission_models, increment_hit_counter
  26 +from flaskr.core import (
  27 + generate_unique_id,
  28 + get_emission_models,
  29 + increment_hit_counter,
  30 +)
22 31 from flaskr.content import content
23 32  
24 33 from wtforms import validators
... ... @@ -39,6 +48,9 @@ OUT_ENCODING = 'utf-8'
39 48  
40 49  
41 50 # -----------------------------------------------------------------------------
  51 +
  52 +pi_email = "didier.barret@irap.omp.eu" # todo: move to content YAML
  53 +
42 54 # -----------------------------------------------------------------------------
43 55  
44 56  
... ... @@ -157,7 +169,7 @@ def gather_addresses(from_list, from_file):
157 169  
158 170 @main.route("/estimate", methods=["GET", "POST"])
159 171 @main.route("/estimate.html", methods=["GET", "POST"])
160   -def estimate():
  172 +def estimate(): # register new estimation request, more accurately
161 173 models = get_emission_models()
162 174 form = EstimateForm()
163 175  
... ... @@ -203,6 +215,12 @@ def estimate():
203 215 db.session.add(estimation)
204 216 db.session.commit()
205 217  
  218 + send_email(
  219 + to_recipient=pi_email,
  220 + subject="[TCFM] New Estimation: %s" % estimation.public_id,
  221 + message="TODO"
  222 + )
  223 +
206 224 flash("Estimation request submitted successfully.", "success")
207 225 return redirect(url_for(
208 226 endpoint=".consult_estimation",
... ... @@ -297,7 +315,7 @@ def compute(): # process the queue of estimation requests
297 315 failed_addresses = []
298 316 geocoder = CachedGeocoder()
299 317  
300   - # GEOCODE ORIGINS #########################################################
  318 + # GEOCODE ORIGINS #####################################################
301 319  
302 320 origins_addresses = estimation.origin_addresses.strip().split("\n")
303 321 origins_addresses_count = len(origins_addresses)
... ... @@ -350,7 +368,7 @@ def compute(): # process the queue of estimation requests
350 368 origin.latitude, origin.longitude,
351 369 )
352 370  
353   - # GEOCODE DESTINATIONS ####################################################
  371 + # GEOCODE DESTINATIONS ################################################
354 372  
355 373 destinations_addresses = estimation.destination_addresses.strip().split("\n")
356 374 destinations_addresses_count = len(destinations_addresses)
... ... @@ -378,7 +396,9 @@ def compute(): # process the queue of estimation requests
378 396 continue
379 397  
380 398 try:
381   - destination = geocoder.geocode(destination_address.encode('utf-8'))
  399 + destination = geocoder.geocode(
  400 + destination_address.encode('utf-8')
  401 + )
382 402 except geopy.exc.GeopyError as e:
383 403 warning = u"Ignoring destination `%s` " \
384 404 u"since we failed to geocode it.\n%s\n" % (
... ... @@ -410,7 +430,7 @@ def compute(): # process the queue of estimation requests
410 430  
411 431 geocoder.close()
412 432  
413   - # GTFO IF NO ORIGINS OR NO DESTINATIONS ###################################
  433 + # GTFO IF NO ORIGINS OR NO DESTINATIONS ###############################
414 434  
415 435 if 0 == len(origins):
416 436 response += u"Failed to geocode ALL the origin(s).\n"
... ... @@ -421,7 +441,7 @@ def compute(): # process the queue of estimation requests
421 441 _handle_failure(estimation, response)
422 442 return _respond(response)
423 443  
424   - # GRAB AND CONFIGURE THE EMISSION MODELS ##################################
  444 + # GRAB AND CONFIGURE THE EMISSION MODELS ##############################
425 445  
426 446 emission_models = estimation.get_models()
427 447 # print(emission_models)
... ... @@ -431,11 +451,11 @@ def compute(): # process the queue of estimation requests
431 451 # 'use_train_below_distance': 300,
432 452 }
433 453  
434   - # PREPARE RESULT DICTIONARY THAT WILL BE STORED ###########################
  454 + # PREPARE RESULT DICTIONARY THAT WILL BE STORED #######################
435 455  
436 456 results = {}
437 457  
438   - # UTILITY PRIVATE FUNCTION(S) #############################################
  458 + # UTILITY PRIVATE FUNCTION(S) #########################################
439 459  
440 460 def get_city_key(_location):
441 461 # Will this hack hold? Suspense...
... ... @@ -550,9 +570,9 @@ def compute(): # process the queue of estimation requests
550 570  
551 571 return _results
552 572  
553   - # SCENARIO A : One Origin, At Least One Destination #######################
  573 + # SCENARIO A : One Origin, At Least One Destination ###################
554 574 #
555   - # In this scenario, we compute the sum of each of the travels' footprint,
  575 + # We compute the sum of each of the travels' footprint,
556 576 # for each of the Emission Models, and present a mean of all Models.
557 577 #
558 578 if 1 == len(origins):
... ... @@ -563,7 +583,7 @@ def compute(): # process the queue of estimation requests
563 583 _extra_config=extra_config,
564 584 )
565 585  
566   - # SCENARIO B : At Least One Origin, One Destination #######################
  586 + # SCENARIO B : At Least One Origin, One Destination ###################
567 587 #
568 588 # Same as A for now.
569 589 #
... ... @@ -575,7 +595,7 @@ def compute(): # process the queue of estimation requests
575 595 _extra_config=extra_config,
576 596 )
577 597  
578   - # SCENARIO C : At Least One Origin, At Least One Destination ##############
  598 + # SCENARIO C : At Least One Origin, At Least One Destination ##########
579 599 #
580 600 # Run Scenario A for each Destination, and expose optimum Destination.
581 601 #
... ... @@ -605,15 +625,24 @@ def compute(): # process the queue of estimation requests
605 625 'cities': result_cities,
606 626 }
607 627  
608   - # WRITE RESULTS INTO THE DATABASE #########################################
  628 + # WRITE RESULTS INTO THE DATABASE #####################################
609 629  
610 630 estimation.status = StatusEnum.success
  631 + # Don't use YAML, it is too slow for big data.
611 632 # estimation.output_yaml = u"%s" % yaml_dump(results)
612 633 estimation.informations = response
613 634 estimation.set_output_dict(results)
614 635 db.session.commit()
615 636  
616   - # FINALLY, RESPOND ########################################################
  637 + # SEND AN EMAIL #######################################################
  638 +
  639 + send_email(
  640 + to_recipient=pi_email,
  641 + subject="[TCFM] Run completed: %s" % estimation.public_id,
  642 + message="TODO"
  643 + )
  644 +
  645 + # FINALLY, RESPOND ####################################################
617 646  
618 647 # response += yaml_dump(results) + "\n"
619 648  
... ... @@ -742,17 +771,14 @@ def get_scaling_laws_csv():
742 771 def dev_test():
743 772 import os
744 773  
745   - # return os.getenv('ADMIN_USERNAME')
746   -
747   -
748   -def send_email(to_recipient, subject, message):
749   - try:
750   - msg = Message(
751   - subject=subject,
752   - html=message,
753   - sender="bot-noreply@travel-carbon-footprint.irap.omp.eu",
754   - recipients=[to_recipient],
755   - )
756   - mail.send(msg)
757   - except:
758   - pass
  774 + # email_content = render_template(
  775 + # 'new_run.email.html',
  776 + # # run=run,
  777 + # )
  778 + # send_email(
  779 + # 'goutte@protonmail.com',
  780 + # subject=u"[TCFC] New run request",
  781 + # message=email_content
  782 + # )
  783 +
  784 + return "ok"
... ...