Blame view

flaskr/controllers/main_controller.py 3.03 KB
7b4d2926   Goutte   Rework the contro...
1
2
3
4
from flask import Blueprint, render_template, flash, request, redirect, url_for

from flaskr.extensions import cache
from flaskr.forms import LoginForm, EstimateForm
24f55cde   Antoine Goutenoir   Geocode destinati...
5
6
from flaskr.models import db, User, Estimation, StatusEnum
from flaskr.geocoder import CachedGeocoder
7b4d2926   Goutte   Rework the contro...
7
8
9

from flaskr.core import generate_unique_id

24f55cde   Antoine Goutenoir   Geocode destinati...
10
11
12
import sqlalchemy
import geopy

7b4d2926   Goutte   Rework the contro...
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
main = Blueprint('main', __name__)


@main.route('/')
@cache.cached(timeout=1000)
def home():
    return render_template('index.html')


@main.route("/estimate", methods=["GET", "POST"])
def estimate():
    form = EstimateForm()

    if form.validate_on_submit():

7b4d2926   Goutte   Rework the contro...
28
29
        id = generate_unique_id()

7b4d2926   Goutte   Rework the contro...
30
31
32
33
        estimation = Estimation()
        estimation.email = form.email.data
        estimation.first_name = form.first_name.data
        estimation.last_name = form.last_name.data
24f55cde   Antoine Goutenoir   Geocode destinati...
34
35
36
37
        estimation.status = StatusEnum.pending
        estimation.origin_addresses = form.origin_addresses.data
        estimation.destination_addresses = form.destination_addresses.data
        estimation.compute_optimal_destination = form.compute_optimal_destination.data
7b4d2926   Goutte   Rework the contro...
38
39
40
41
42
43
44
45
46
47

        db.session.add(estimation)
        db.session.commit()

        flash("Estimation request submitted successfully.", "success")
        return redirect(url_for(".home"))
        # return render_template("estimate-debrief.html", form=form)

    return render_template("estimate.html", form=form)

4392f295   Goutte   Update the Estima...
48
49
50

@main.route("/compute")
def compute():
24f55cde   Antoine Goutenoir   Geocode destinati...
51
52
53
54
55
56
57
58

    def _respond(_msg):
        return "<pre>%s</pre>" % _msg

    def _handle_failure(_estimation, _failure_message):
        _estimation.status = StatusEnum.failed
        db.session.commit()

4392f295   Goutte   Update the Estima...
59
60
    response = ""

24f55cde   Antoine Goutenoir   Geocode destinati...
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
    try:
        estimation = Estimation.query \
            .filter_by(status=StatusEnum.pending) \
            .order_by(Estimation.id.desc()) \
            .one()
    except sqlalchemy.orm.exc.NoResultFound:
        return _respond("No estimation in the queue.")

    response += u"Processing estimation `%s` of `%s`...\n" % (estimation.id, estimation.email)

    geocoder = CachedGeocoder()

    destinations_addresses = estimation.destination_addresses.split("\n")
    destinations = []

    for i in range(len(destinations_addresses)):

        destination_address = str(destinations_addresses[i])

        try:
            destination = geocoder.geocode(destination_address)
        except geopy.exc.GeopyError as e:
            response += u"Failed to geolocalize destination `%s`.\n%s" % (
                destination_address, e,
            )
            _handle_failure(estimation, response)
            return _respond(response)

        if destination is None:
            response += u"Failed to geolocalize destination `%s`." % (
                destination_address,
            )
            _handle_failure(estimation, response)
            return _respond(response)

        destinations.append(destination)

        response += u"Destination: %s == %s (%f, %f)\n" % (
            destination_address, destination.address,
            destination.latitude, destination.longitude,
        )


4392f295   Goutte   Update the Estima...
104

24f55cde   Antoine Goutenoir   Geocode destinati...
105
    return _respond(response)