groups: Move constants inside NamedUserGroup.

This commit is contained in:
Sahil Batra
2024-04-18 14:20:51 +05:30
committed by Tim Abbott
parent 85b7dbddbc
commit 8e9c22afdc
7 changed files with 57 additions and 58 deletions

View File

@@ -18,11 +18,8 @@ class SystemGroups:
class UserGroup(models.Model): # type: ignore[django-manager-missing] # django-stubs cannot resolve the custom CTEManager yet https://github.com/typeddjango/django-stubs/issues/1023
MAX_NAME_LENGTH = 100
INVALID_NAME_PREFIXES = ["@", "role:", "user:", "stream:", "channel:"]
objects: CTEManager = CTEManager()
name = models.CharField(max_length=MAX_NAME_LENGTH)
name = models.CharField(max_length=100)
direct_members = models.ManyToManyField(
UserProfile, through="zerver.UserGroupMembership", related_name="direct_groups"
)
@@ -39,6 +36,38 @@ class UserGroup(models.Model): # type: ignore[django-manager-missing] # django-
can_mention_group = models.ForeignKey("self", on_delete=models.RESTRICT)
class Meta:
unique_together = (("realm", "name"),)
class NamedUserGroup(UserGroup): # type: ignore[django-manager-missing] # django-stubs cannot resolve the custom CTEManager yet https://github.com/typeddjango/django-stubs/issues/1023
MAX_NAME_LENGTH = 100
INVALID_NAME_PREFIXES = ["@", "role:", "user:", "stream:", "channel:"]
# This field is automatically created by django, but we still need
# to add this here to keep mypy happy when accessing usergroup_ptr.
usergroup_ptr = models.OneToOneField(
auto_created=True,
on_delete=CASCADE,
parent_link=True,
primary_key=True,
serialize=False,
to=UserGroup,
# We are not using the auto-generated name here to avoid
# duplicate backward relation name because "can_mention_group"
# setting also points to a UserGroup object.
related_name="named_user_group",
)
named_group_name = models.CharField(max_length=MAX_NAME_LENGTH, db_column="name")
named_group_description = models.TextField(default="", db_column="description")
named_group_is_system_group = models.BooleanField(default=False, db_column="is_system_group")
named_group_can_mention_group = models.ForeignKey(
UserGroup, on_delete=models.RESTRICT, db_column="can_mention_group_id"
)
realm_for_sharding = models.ForeignKey("zerver.Realm", on_delete=CASCADE, db_column="realm_id")
# We do not have "Full members" and "Everyone on the internet"
# group here since there isn't a separate role value for full
# members and spectators.
@@ -78,37 +107,6 @@ class UserGroup(models.Model): # type: ignore[django-manager-missing] # django-
),
}
class Meta:
unique_together = (("realm", "name"),)
class NamedUserGroup(UserGroup): # type: ignore[django-manager-missing] # django-stubs cannot resolve the custom CTEManager yet https://github.com/typeddjango/django-stubs/issues/1023
MAX_NAME_LENGTH = 100
# This field is automatically created by django, but we still need
# to add this here to keep mypy happy when accessing usergroup_ptr.
usergroup_ptr = models.OneToOneField(
auto_created=True,
on_delete=CASCADE,
parent_link=True,
primary_key=True,
serialize=False,
to=UserGroup,
# We are not using the auto-generated name here to avoid
# duplicate backward relation name because "can_mention_group"
# setting also points to a UserGroup object.
related_name="named_user_group",
)
named_group_name = models.CharField(max_length=MAX_NAME_LENGTH, db_column="name")
named_group_description = models.TextField(default="", db_column="description")
named_group_is_system_group = models.BooleanField(default=False, db_column="is_system_group")
named_group_can_mention_group = models.ForeignKey(
UserGroup, on_delete=models.RESTRICT, db_column="can_mention_group_id"
)
realm_for_sharding = models.ForeignKey("zerver.Realm", on_delete=CASCADE, db_column="realm_id")
class Meta:
unique_together = (("realm_for_sharding", "named_group_name"),)