Skip to content

Commit

Permalink
ref: have models.User extend Model instead of BaseModel
Browse files Browse the repository at this point in the history
most of our db helpers expected .id to be a Field[int, int] and use Model as the type for that
  • Loading branch information
asottile-sentry committed Jun 18, 2024
1 parent 4cdd6f9 commit b3aa840
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/sentry/api/bases/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ class UserEndpoint(Endpoint):
permission_classes: tuple[type[BasePermission], ...] = (UserPermission,)

@override
def convert_args(self, request: Request, user_id: str | None = None, *args, **kwargs):
def convert_args(self, request: Request, user_id: int | str | None = None, *args, **kwargs):
if user_id == "me":
if not request.user.is_authenticated:
raise ResourceDoesNotExist
Expand Down
4 changes: 2 additions & 2 deletions src/sentry/api/endpoints/api_tokens.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class ApiTokensEndpoint(Endpoint):
permission_classes = (IsAuthenticated,)

@classmethod
def _get_appropriate_user_id(cls, request: Request) -> str:
def _get_appropriate_user_id(cls, request: Request) -> int:
"""
Gets the user id to use for the request, based on what the current state of the request is.
If the request is made by a superuser, then they are allowed to act on behalf of other user's data.
Expand All @@ -54,7 +54,7 @@ def _get_appropriate_user_id(cls, request: Request) -> str:
if has_elevated_mode(request):
datastore = request.GET if request.GET else request.data
# If a userId override is not found, use the id for the user who made the request
user_id = datastore.get("userId", user_id)
user_id = int(datastore.get("userId", user_id))

return user_id

Expand Down
15 changes: 4 additions & 11 deletions src/sentry/models/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,7 @@
from sentry.backup.helpers import ImportFlags
from sentry.backup.sanitize import SanitizableField, Sanitizer
from sentry.backup.scopes import ImportScope, RelocationScope
from sentry.db.models import (
BaseManager,
BaseModel,
BoundedBigAutoField,
control_silo_model,
sane_repr,
)
from sentry.db.models import BaseManager, Model, control_silo_model, sane_repr
from sentry.db.models.utils import unique_db_instance
from sentry.db.postgres.transactions import enforce_constraints
from sentry.integrations.types import EXTERNAL_PROVIDERS, ExternalProviders
Expand Down Expand Up @@ -94,13 +88,12 @@ def get_users_with_only_one_integration_for_provider(


@control_silo_model
class User(BaseModel, AbstractBaseUser):
class User(Model, AbstractBaseUser):
__relocation_scope__ = RelocationScope.User
__relocation_custom_ordinal__ = ["username"]

replication_version: int = 2

id = BoundedBigAutoField(primary_key=True)
username = models.CharField(_("username"), max_length=MAX_USERNAME_LENGTH, unique=True)
# this column is called first_name for legacy reasons, but it is the entire
# display name
Expand Down Expand Up @@ -368,7 +361,7 @@ def merge_to(from_user: User, to_user: User) -> None:
with enforce_constraints(
transaction.atomic(using=router.db_for_write(OrganizationMemberMapping))
):
control_side_org_models: tuple[type[BaseModel], ...] = (
control_side_org_models: tuple[type[Model], ...] = (
OrgAuthToken,
OrganizationMemberMapping,
)
Expand All @@ -383,7 +376,7 @@ def merge_to(from_user: User, to_user: User) -> None:
# While it would be nice to make the following changes in a transaction, there are too many
# unique constraints to make this feasible. Instead, we just do it sequentially and ignore
# the `IntegrityError`s.
user_related_models: tuple[type[BaseModel], ...] = (
user_related_models: tuple[type[Model], ...] = (
Authenticator,
Identity,
UserAvatar,
Expand Down
6 changes: 3 additions & 3 deletions src/sentry/tasks/summaries/weekly_reports.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import heapq
import logging
import uuid
from collections.abc import Mapping
from collections.abc import Mapping, Sequence
from dataclasses import dataclass
from datetime import timedelta
from functools import partial
Expand Down Expand Up @@ -452,7 +452,7 @@ def get_group_status_badge(group: Group) -> tuple[str, str, str]:
return ("Ongoing", "rgba(219, 214, 225, 1)", "rgba(219, 214, 225, 1)")


def render_template_context(ctx, user_id):
def render_template_context(ctx, user_id: int | None) -> dict[str, Any] | None:
# Serialize ctx for template, and calculate view parameters (like graph bar heights)
# Fetch the list of projects associated with the user.
# Projects owned by teams that the user has membership of.
Expand Down Expand Up @@ -748,7 +748,7 @@ def issue_summary():


def prepare_template_context(
ctx: OrganizationReportContext, user_ids: list[int]
ctx: OrganizationReportContext, user_ids: Sequence[int | None]
) -> list[Mapping[str, Any]] | list:
user_template_context_by_user_id_list = []
for user_id in user_ids:
Expand Down

0 comments on commit b3aa840

Please sign in to comment.