Blame view

flaskr/models.py 3.4 KB
16f69d07   Antoine Goutenoir   Add an (unsecured...
1
2
from flask_admin.contrib.sqla import ModelView

fa3d2b43   Antoine Goutenoir   Conflate the data...
3
from flaskr.core import generate_unique_id
1b39d5ec   Goutte   Add a skeleton fo...
4
5
6
from flask_sqlalchemy import SQLAlchemy
from flask_login import UserMixin, AnonymousUserMixin
from werkzeug.security import generate_password_hash, check_password_hash
6a9e49da   Antoine Goutenoir   Load the output a...
7
from yaml import safe_load as yaml_load
97fe903e   Goutte   Add the base data...
8
import enum
1b39d5ec   Goutte   Add a skeleton fo...
9

c10e71f4   Antoine Goutenoir   Document the models.
10
11
12
13
14
15
16
# These are not the emission "models" in the scientific meaning of the word.
# They are the SQL Database Models.
# These are also named Entities, in other conventions (we're following flasks")
# If you're looking for the Emission Models (aka scaling laws),
# look in `flaskr/laws/`.


1b39d5ec   Goutte   Add a skeleton fo...
17
18
19
db = SQLAlchemy()


c10e71f4   Antoine Goutenoir   Document the models.
20
21
class StatusEnum(enum.Enum):
    pending = 'pending'
03c194bf   Antoine Goutenoir   Actually implemen...
22
    working = 'working'
c10e71f4   Antoine Goutenoir   Document the models.
23
    success = 'success'
5b6d9d3d   Antoine Goutenoir   Add a method to t...
24
    failure = 'failure'
c10e71f4   Antoine Goutenoir   Document the models.
25
26
27
28


class Estimation(db.Model):
    id = db.Column(db.Integer(), primary_key=True)
fa3d2b43   Antoine Goutenoir   Conflate the data...
29
30
31
32
33
    public_id = db.Column(
        db.Unicode(),
        default=lambda: generate_unique_id(),
        unique=True
    )
c10e71f4   Antoine Goutenoir   Document the models.
34
35
36
    email = db.Column(db.Unicode(1024))
    first_name = db.Column(db.Unicode(1024))  # Antoine
    last_name = db.Column(db.Unicode(1024))   # Goutenoir
16f69d07   Antoine Goutenoir   Add an (unsecured...
37
    institution = db.Column(db.Unicode(1024))   # IRAP
fa3d2b43   Antoine Goutenoir   Conflate the data...
38
    status = db.Column(db.Enum(StatusEnum), default=StatusEnum.pending)
c10e71f4   Antoine Goutenoir   Document the models.
39
40
41

    # City, Country
    # One address per line
16f69d07   Antoine Goutenoir   Add an (unsecured...
42
43
    origin_addresses = db.Column(db.UnicodeText())
    destination_addresses = db.Column(db.UnicodeText())
c10e71f4   Antoine Goutenoir   Document the models.
44

466912a2   Antoine Goutenoir   Prepare the estim...
45
46
47
48
    # One slug per line (or blankchar?)
    models_slugs = db.Column(db.UnicodeText())

    # Deprecated, we detect this scenario from the amount of locations.
c10e71f4   Antoine Goutenoir   Document the models.
49
50
    compute_optimal_destination = db.Column(db.Boolean())

77a9e740   Antoine Goutenoir   Pickling is overk...
51
    output_yaml = db.Column(db.UnicodeText())
fa3d2b43   Antoine Goutenoir   Conflate the data...
52
53
54
    warnings = db.Column(db.UnicodeText())
    errors = db.Column(db.UnicodeText())

5b6d9d3d   Antoine Goutenoir   Add a method to t...
55
56
    def has_failed(self):
        return self.status == StatusEnum.failure
c10e71f4   Antoine Goutenoir   Document the models.
57

6a9e49da   Antoine Goutenoir   Load the output a...
58
59
60
    def get_output_dict(self):
        return yaml_load(self.output_yaml)

f3694728   Antoine Goutenoir   Display a summary.
61
62
63
    def has_many_to_many(self):
        return 'cities' in self.get_output_dict()

6a9e49da   Antoine Goutenoir   Load the output a...
64

16f69d07   Antoine Goutenoir   Add an (unsecured...
65
66
67
68
69
70
71
class EstimationView(ModelView):
    # Show only name and email columns in list view
    column_list = (
        'public_id',
        'status',
        'first_name',
        'last_name',
466912a2   Antoine Goutenoir   Prepare the estim...
72
        'models_slugs',
16f69d07   Antoine Goutenoir   Add an (unsecured...
73
74
75
76
77
78
79
80
81
82
        'origin_addresses',
        'destination_addresses',
        'warnings',
        'errors',
    )

    # Enable search functionality - it will search for terms in
    # name and email fields
    # column_searchable_list = ('name', 'email')

16f69d07   Antoine Goutenoir   Add an (unsecured...
83
84
85
    column_filters = ('first_name', 'last_name')


c10e71f4   Antoine Goutenoir   Document the models.
86
87
# USERS #######################################################################

1b39d5ec   Goutte   Add a skeleton fo...
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
class User(db.Model, UserMixin):
    id = db.Column(db.Integer(), primary_key=True)
    username = db.Column(db.String())
    password = db.Column(db.String())

    def __init__(self, username, password):
        self.username = username
        self.set_password(password)

    def set_password(self, password):
        self.password = generate_password_hash(password)

    def check_password(self, value):
        return check_password_hash(self.password, value)

    @property
    def is_authenticated(self):
        if isinstance(self, AnonymousUserMixin):
            return False
        else:
            return True

    def is_active(self):
        return True

    def is_anonymous(self):
        if isinstance(self, AnonymousUserMixin):
            return True
        else:
            return False

    def get_id(self):
        return self.id

    def __repr__(self):
fa3d2b43   Antoine Goutenoir   Conflate the data...
123
        return '<User %r>' % self.username