Blame view

flaskr/models.py 2.56 KB
fa3d2b43   Antoine Goutenoir   Conflate the data...
1
from flaskr.core import generate_unique_id
1b39d5ec   Goutte   Add a skeleton fo...
2
3
4
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...
5
from yaml import safe_load as yaml_load
97fe903e   Goutte   Add the base data...
6
import enum
1b39d5ec   Goutte   Add a skeleton fo...
7

c10e71f4   Antoine Goutenoir   Document the models.
8
9
10
11
12
13
14
# 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...
15
16
17
db = SQLAlchemy()


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


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

    # City, Country
    # One address per line
    origin_addresses = db.Column(db.Unicode())
    destination_addresses = db.Column(db.Unicode())

    compute_optimal_destination = db.Column(db.Boolean())

77a9e740   Antoine Goutenoir   Pickling is overk...
44
    output_yaml = db.Column(db.UnicodeText())
fa3d2b43   Antoine Goutenoir   Conflate the data...
45
46
47
    warnings = db.Column(db.UnicodeText())
    errors = db.Column(db.UnicodeText())

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

6a9e49da   Antoine Goutenoir   Load the output a...
51
52
53
54
    def get_output_dict(self):
        return yaml_load(self.output_yaml)


c10e71f4   Antoine Goutenoir   Document the models.
55
56
# USERS #######################################################################

1b39d5ec   Goutte   Add a skeleton fo...
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
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...
92
        return '<User %r>' % self.username