~homeworkprod/byceps

ref: 4237b3ec9496efe95dcce82bea3207ab9de4d520 byceps/byceps/services/user/models/user.py -rw-r--r-- 3.4 KiB
4237b3ec — Jochen Kupperschmidt Move ticketing blueprint into `site` subpackage 2 years 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
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
"""
byceps.services.user.models.user
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

:Copyright: 2006-2020 Jochen Kupperschmidt
:License: Modified BSD, see LICENSE for details.
"""

from datetime import datetime
from typing import Optional
from uuid import UUID

from flask import g
from sqlalchemy.ext.associationproxy import association_proxy
from werkzeug.utils import cached_property

from ....database import db, generate_uuid
from ....util.instances import ReprBuilder

from ...user_avatar.models import AvatarSelection

from ..transfer.models import User as UserDTO


GUEST_USER_ID = UUID('00000000-0000-0000-0000-000000000000')


class AnonymousUser:

    id = GUEST_USER_ID
    deleted = False

    @property
    def avatar(self) -> None:
        return None

    @property
    def avatar_url(self) -> None:
        return None

    @property
    def is_orga(self) -> bool:
        return False

    def __eq__(self, other) -> bool:
        return self.id == other.id

    def __repr__(self) -> str:
        return ReprBuilder(self) \
            .add_with_lookup('id') \
            .build()


class User(db.Model):
    """A user."""

    __tablename__ = 'users'

    id = db.Column(db.Uuid, default=generate_uuid, primary_key=True)
    created_at = db.Column(db.DateTime, nullable=False)
    screen_name = db.Column(db.UnicodeText, unique=True, nullable=True)
    email_address = db.Column(db.UnicodeText, unique=True, nullable=True)
    email_address_verified = db.Column(db.Boolean, default=False, nullable=False)
    initialized = db.Column(db.Boolean, default=False, nullable=False)
    suspended = db.Column(db.Boolean, default=False, nullable=False)
    deleted = db.Column(db.Boolean, default=False, nullable=False)
    legacy_id = db.Column(db.Integer)

    avatar = association_proxy('avatar_selection', 'avatar',
                               creator=lambda avatar:
                                    AvatarSelection(None, avatar.id))

    def __init__(
        self,
        created_at: datetime,
        screen_name: str,
        email_address: Optional[str],
    ) -> None:
        self.created_at = created_at
        self.screen_name = screen_name
        self.email_address = email_address

    @property
    def avatar_url(self) -> Optional[str]:
        avatar = self.avatar
        return avatar.url if (avatar is not None) else None

    @cached_property
    def is_orga(self) -> bool:
        party_id = getattr(g, 'party_id', None)

        if party_id is None:
            return False

        from ...orga_team import service as orga_team_service

        return orga_team_service.is_orga_for_party(self.id, party_id)

    def to_dto(self, *, include_avatar=False):
        avatar_url = self.avatar_url if include_avatar else None
        is_orga = False  # Information is deliberately not obtained here.

        return UserDTO(
            self.id,
            self.screen_name,
            self.suspended,
            self.deleted,
            avatar_url,
            is_orga,
        )

    def __eq__(self, other) -> bool:
        return (other is not None) and (self.id == other.id)

    def __hash__(self) -> int:
        if self.id is None:
            raise ValueError(
                'User instance is unhashable because its id is None.'
            )

        return hash(self.id)

    def __repr__(self) -> str:
        return ReprBuilder(self) \
            .add_with_lookup('id') \
            .add_with_lookup('screen_name') \
            .build()