~fluix/tilde

ref: 8385d93a45d225db407548099e08a11cb983d995 tilde/auth/models.py -rw-r--r-- 1.0 KiB
8385d93aSteven Guikal Update privacy policy and require acceptance of it for registration 9 months ago
                                                                                
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
# SPDX-FileCopyrightText: 2021 Steven Guikal <void@fluix.one>
#
# SPDX-License-Identifier: AGPL-3.0-only

from enum import Enum, auto

from flask import current_app
from flask_login.mixins import UserMixin
from werkzeug.security import check_password_hash, generate_password_hash

db = current_app.config["db"]


class Status(Enum):
    PENDING = auto()
    ACTIVE = auto()
    BANNED = auto()
    DELETED = auto()


class User(UserMixin, db.Model):
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(20, collation="NOCASE"), unique=True)
    email = db.Column(db.String())
    password_hash = db.Column(db.String())
    status = db.Column(db.Enum(Status), default=Status.PENDING)
    bio = db.Column(db.Text())

    @property
    def is_active(self):
        return self.status == Status.ACTIVE

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

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