-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
models.py
175 lines (136 loc) · 6.18 KB
/
models.py
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
"""Shared models."""
from __future__ import annotations
from uuid import UUID # noqa: TCH003
from advanced_alchemy.base import UUIDAuditBase
from sqlalchemy import BigInteger, ForeignKey, String, UniqueConstraint
from sqlalchemy.ext.associationproxy import AssociationProxy, association_proxy
from sqlalchemy.orm import Mapped, mapped_column, relationship
__all__ = ("GitHubConfig", "Guild", "SOTagsConfig", "User", "AllowedUsersConfig")
class Guild(UUIDAuditBase):
"""Guild configuration.
A single guild will contain base defaults (e.g., ``prefix``, boolean flags for linking, etc.)
with configurable options that can be set by the guild owner or ``allowed_users``.
Part of the feature set of Byte is that you have interactivity with your git repositories,
StackOverflow, Discord forums, and other external services.
Here, a guild should be able to configure their own GitHub organization, StackOverflow tags, etc.
"""
__tablename__ = "guild" # type: ignore[reportAssignmentType]
__table_args__ = {"comment": "Configuration for a Discord guild."}
guild_id: Mapped[int] = mapped_column(BigInteger, unique=True, index=True)
guild_name: Mapped[str] = mapped_column(String(100))
prefix: Mapped[str] = mapped_column(String(5), server_default="!", default="!")
help_channel_id: Mapped[int | None] = mapped_column(BigInteger)
sync_label: Mapped[str | None]
issue_linking: Mapped[bool] = mapped_column(default=False)
comment_linking: Mapped[bool] = mapped_column(default=False)
pep_linking: Mapped[bool] = mapped_column(default=False)
# =================
# ORM Relationships
# =================
github_config: Mapped[GitHubConfig | None] = relationship(
lazy="noload",
back_populates="guild",
cascade="save-update, merge, delete",
)
sotags_configs: Mapped[list[SOTagsConfig]] = relationship(
lazy="noload",
back_populates="guild",
cascade="all, delete-orphan",
)
allowed_users: Mapped[list[AllowedUsersConfig]] = relationship(
lazy="noload",
back_populates="guild",
cascade="save-update, merge, delete",
)
class GitHubConfig(UUIDAuditBase):
"""GitHub configuration.
A guild will be able to configure which organization or user they want as a default
base, which repository they want as a default, and whether they would like to sync
discussions with forum posts.
"""
__tablename__ = "github_config" # type: ignore[reportAssignmentType]
__table_args__ = {"comment": "GitHub configuration for a guild."}
guild_id: Mapped[UUID] = mapped_column(ForeignKey("guild.id", ondelete="cascade"))
discussion_sync: Mapped[bool] = mapped_column(default=False)
github_organization: Mapped[str | None]
github_repository: Mapped[str | None]
# =================
# ORM Relationships
# =================
guild: Mapped[Guild] = relationship(
back_populates="github_config",
innerjoin=True,
lazy="noload",
cascade="save-update, merge, delete",
)
class SOTagsConfig(UUIDAuditBase):
"""SQLAlchemy association model for a guild's Stack Overflow tags config."""
__tablename__ = "so_tags" # type: ignore[reportAssignmentType]
__table_args__ = (
UniqueConstraint("guild_id", "tag_name"),
{"comment": "Configuration for a Discord guild's Stack Overflow tags."},
)
guild_id: Mapped[UUID] = mapped_column(ForeignKey("guild.id", ondelete="cascade"))
guild_name: AssociationProxy[str] = association_proxy("guild", "guild_name")
tag_name: Mapped[str] = mapped_column(String(50))
# =================
# ORM Relationships
# =================
guild: Mapped[Guild] = relationship(
back_populates="sotags_configs",
foreign_keys="SOTagsConfig.guild_id",
innerjoin=True,
lazy="noload",
)
class AllowedUsersConfig(UUIDAuditBase):
"""SQLAlchemy association model for a guild's allowed users' config.
A guild normally has a set of users to perform administrative actions, but sometimes
we don't want to give full administrative access to a user.
This model allows us to configure which users are allowed to perform administrative
actions on Byte specifically without giving them full administrative access to the Discord guild.
.. todo:: More preferably, this should be more generalized to a user OR role ID.
"""
__tablename__ = "allowed_users" # type: ignore[reportAssignmentType]
__table_args__ = (
UniqueConstraint("guild_id", "user_id"),
{"comment": "Configuration for allowed users in a Discord guild."},
)
guild_id: Mapped[int] = mapped_column(ForeignKey("guild.guild_id", ondelete="cascade"))
user_id: Mapped[UUID] = mapped_column(ForeignKey("user.id", ondelete="cascade"))
guild_name: AssociationProxy[str] = association_proxy("guild", "guild_name")
user_name: AssociationProxy[str] = association_proxy("user", "name")
# =================
# ORM Relationships
# =================
guild: Mapped[Guild] = relationship(
back_populates="allowed_users",
foreign_keys="AllowedUsersConfig.guild_id",
innerjoin=True,
lazy="noload",
)
user: Mapped[User] = relationship(
back_populates="guilds_allowed",
foreign_keys="AllowedUsersConfig.user_id",
innerjoin=True,
lazy="noload",
)
class User(UUIDAuditBase):
"""SQLAlchemy model representing a user.
.. todo:: This may not really be needed?
Currently, a user in the Byte context is an individual user assigned to the
guild's allowed users config.
In the future we may want to expand this to allow for more granular permissions.
"""
__tablename__ = "user" # type: ignore[reportAssignmentType]
__table_args__ = {"comment": "A user."}
name: Mapped[str] = mapped_column(String(100))
avatar_url: Mapped[str | None]
discriminator: Mapped[str] = mapped_column(String(4))
# =================
# ORM Relationships
# =================
guilds_allowed: Mapped[list[AllowedUsersConfig]] = relationship(
back_populates="user",
lazy="noload",
cascade="save-update, merge, delete",
)