~sircmpwn/core.sr.ht

core.sr.ht/srht/oauth/user.py -rw-r--r-- 2.1 KiB
0e245224Ryan Gonzalez srht.Validation: Don't reject enums with 0 values 3 days 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
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
import sqlalchemy as sa
import sqlalchemy_utils as sau
from enum import Enum
from sqlalchemy.ext.declarative import declared_attr

class UserType(Enum):
    unconfirmed = "unconfirmed"
    active_non_paying = "active_non_paying"
    active_free = "active_free"
    active_paying = "active_paying"
    active_delinquent = "active_delinquent"
    admin = "admin"
    unknown = "unknown"
    suspended = "suspended"

class UserMixin:
    @declared_attr
    def __tablename__(cls):
        return "user"

    id = sa.Column(sa.Integer, primary_key=True)
    created = sa.Column(sa.DateTime, nullable=False)
    updated = sa.Column(sa.DateTime, nullable=False)
    username = sa.Column(sa.Unicode(256), index=True, unique=True)
    email = sa.Column(sa.String(256), nullable=False, unique=True)
    user_type = sa.Column(
            sau.ChoiceType(UserType, impl=sa.String()),
            nullable=False,
            default=UserType.unconfirmed)
    url = sa.Column(sa.String(256))
    location = sa.Column(sa.Unicode(256))
    bio = sa.Column(sa.Unicode(4096))
    suspension_notice = sa.Column(sa.Unicode(4096))

    @property
    def canonical_name(self):
        return "~" + self.username

    def is_authenticated(self):
        return True

    def is_active(self):
        return True

    def is_anonymous(self):
        return False

    def get_id(self):
        return self.username

    def __repr__(self):
        return '<User {} {}>'.format(self.id, self.username)

    def __str__(self):
        return self.canonical_name

    def to_dict(self, short=False):
        return {
            "canonical_name": self.canonical_name,
            "name": self.username,
            **({
                "email": self.email,
                "url": self.url,
                "location": self.location,
                "bio": self.bio,
            } if not short else {})
        }

class ExternalUserMixin(UserMixin):
    oauth_token = sa.Column(sa.String(256))
    oauth_token_expires = sa.Column(sa.DateTime)
    oauth_token_scopes = sa.Column(sa.String)
    oauth_revocation_token = sa.Column(sa.String(256))