models.py
5.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import enum
import shelve
from os.path import join, isfile
from flask_admin.contrib.sqla import ModelView
from flaskr.core import generate_unique_id, models
from flask_sqlalchemy import SQLAlchemy
from flask_login import UserMixin, AnonymousUserMixin
from werkzeug.security import generate_password_hash, check_password_hash
from yaml import safe_load as yaml_load
from content import get_path
# 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/`.
db = SQLAlchemy()
class StatusEnum(enum.Enum):
pending = 'pending'
working = 'working'
success = 'success'
failure = 'failure'
class ScenarioEnum(enum.Enum):
one_to_one = 'one_to_one'
many_to_one = 'many_to_one'
one_to_many = 'one_to_many'
many_to_many = 'many_to_many'
class Estimation(db.Model):
id = db.Column(db.Integer(), primary_key=True)
public_id = db.Column(
db.Unicode(),
default=lambda: generate_unique_id(),
unique=True
)
email = db.Column(db.Unicode(1024))
first_name = db.Column(db.Unicode(1024)) # Antoine
last_name = db.Column(db.Unicode(1024)) # Goutenoir
institution = db.Column(db.Unicode(1024)) # IRAP
status = db.Column(db.Enum(StatusEnum), default=StatusEnum.pending)
run_name = db.Column(db.Unicode(1024)) # JPGU 2020
# City, Country
# One address per line
origin_addresses = db.Column(db.UnicodeText())
destination_addresses = db.Column(db.UnicodeText())
# For (single, not round) trips below this distance, use the train
use_train_below_km = db.Column(db.Integer())
# One slug per line (or blank char?)
models_slugs = db.Column(db.UnicodeText())
# Deprecated, we detect this scenario from the amount of locations.
compute_optimal_destination = db.Column(db.Boolean())
# Outputs
scenario = db.Column(db.Enum(ScenarioEnum), default=ScenarioEnum.many_to_many)
output_yaml = db.Column(db.UnicodeText())
warnings = db.Column(db.UnicodeText())
errors = db.Column(db.UnicodeText())
def has_failed(self):
return self.status == StatusEnum.failure
def get_display_name(self):
if self.run_name:
return self.run_name
return self.public_id
def get_output_filename(self):
runs_dir = get_path("var/runs")
return join(runs_dir, self.public_id)
def set_output_dict(self, output):
# with shelve.open(filename=self.get_output_filename(), protocol=2) as shelf:
# shelf['output'] = output
shelf = shelve.open(filename=self.get_output_filename(), protocol=2)
shelf['output'] = output
shelf.close()
_output_dict = None
def get_output_dict(self):
if self._output_dict is None:
if self.output_yaml is None:
output_filename = self.get_output_filename()
if isfile(output_filename):
# with shelve.open(filename=output_filename,
# protocol=2) as shelf:
# self._output_dict = shelf['output']
shelf = shelve.open(filename=output_filename, protocol=2)
self._output_dict = shelf['output']
# self._output_dict = copy(shelf['output'])
shelf.close()
else:
self._output_dict = None
else:
self._output_dict = yaml_load(self.output_yaml)
return self._output_dict
def is_one_to_one(self):
return self.scenario == ScenarioEnum.one_to_one
def is_one_to_many(self):
return self.scenario == ScenarioEnum.one_to_many
def is_many_to_one(self):
return self.scenario == ScenarioEnum.many_to_one
def is_many_to_many(self):
return self.scenario == ScenarioEnum.many_to_many
_models = None
def get_models(self):
if self._models is None:
mdl_slugs = self.models_slugs.split("\n")
self._models = [m for m in models if m.slug in mdl_slugs]
return self._models
class EstimationView(ModelView):
# Show only name and email columns in list view
column_list = (
'public_id',
'run_name',
'status',
'first_name',
'last_name',
'models_slugs',
'scenario',
'origin_addresses',
'destination_addresses',
'warnings',
'errors',
)
# Enable search functionality - it will search for terms in
# name and email fields
# column_searchable_list = ('name', 'email')
column_filters = ('first_name', 'last_name')
# USERS #######################################################################
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):
return '<User %r>' % self.username