forked from typeddjango/django-stubs
-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make managers in
django.contrib
generic (#190)
This is similar to the base model and managers and to the user models and manager. It also reflects the runtime behavior as a model's manager is always associated with the model class itself (represented by `Self`), not the parent class where it is defined. Also did some trivial fixes in the those files.
- Loading branch information
Showing
8 changed files
with
82 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,43 @@ | ||
from typing import Any, ClassVar | ||
import datetime as dt | ||
from typing import Any, ClassVar, TypeVar | ||
from typing_extensions import Self | ||
from uuid import UUID | ||
|
||
from django.contrib.contenttypes.models import ContentType | ||
from django.db import models | ||
from django.db.models.base import Model | ||
|
||
ADDITION: int | ||
CHANGE: int | ||
DELETION: int | ||
ACTION_FLAG_CHOICES: Any | ||
ACTION_FLAG_CHOICES: list[tuple[int, str]] | ||
|
||
class LogEntryManager(models.Manager[LogEntry]): | ||
_LogEntryT = TypeVar("_LogEntryT", bound=LogEntry) | ||
|
||
class LogEntryManager(models.Manager[_LogEntryT]): | ||
def log_action( | ||
self, | ||
user_id: int, | ||
content_type_id: int, | ||
object_id: int | str | UUID, | ||
object_repr: str, | ||
action_flag: int, | ||
change_message: Any = ..., | ||
) -> LogEntry: ... | ||
change_message: str | list[Any] = ..., | ||
) -> _LogEntryT: ... | ||
|
||
class LogEntry(models.Model): | ||
action_time: models.DateTimeField[Any] = ... | ||
user: models.ForeignKey[Any] = ... | ||
content_type: models.ForeignKey[Any] = ... | ||
object_id: models.TextField[Any] = ... | ||
object_repr: models.CharField[Any] = ... | ||
action_flag: models.PositiveSmallIntegerField[Any] = ... | ||
change_message: models.TextField[Any] = ... | ||
objects: ClassVar[LogEntryManager] = ... | ||
objects: ClassVar[LogEntryManager[Self]] # type: ignore[assignment] | ||
|
||
action_time: models.DateTimeField[dt.datetime] | ||
user: models.ForeignKey[Any] | ||
content_type: models.ForeignKey[ContentType | None] | ||
object_id: models.TextField[str | None] | ||
object_repr: models.CharField[str] | ||
action_flag: models.PositiveSmallIntegerField[int] | ||
change_message: models.TextField[str] | ||
|
||
def is_addition(self) -> bool: ... | ||
def is_change(self) -> bool: ... | ||
def is_deletion(self) -> bool: ... | ||
def get_change_message(self) -> str: ... | ||
def get_edited_object(self) -> Model: ... | ||
def get_edited_object(self) -> models.Model: ... | ||
def get_admin_url(self) -> str | None: ... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,25 @@ | ||
from datetime import datetime | ||
from typing import Any, ClassVar | ||
from typing import ClassVar, TypeVar | ||
from typing_extensions import Self | ||
|
||
from django.contrib.sessions.backends.base import SessionBase | ||
from django.db import models | ||
|
||
class BaseSessionManager(models.Manager[Any]): | ||
_SessionT = TypeVar("_SessionT", bound=AbstractBaseSession) | ||
|
||
class BaseSessionManager(models.Manager[_SessionT]): | ||
def encode(self, session_dict: dict[str, int]) -> str: ... | ||
def save( | ||
self, session_key: str, session_dict: dict[str, int], expire_date: datetime | ||
) -> AbstractBaseSession: ... | ||
) -> _SessionT: ... | ||
|
||
class AbstractBaseSession(models.Model): | ||
expire_date: datetime | ||
session_data: str | ||
session_key: str | ||
objects: ClassVar[BaseSessionManager] = ... | ||
objects: ClassVar[BaseSessionManager[Self]] # type: ignore[assignment] | ||
|
||
session_key: models.CharField[str] | ||
session_data: models.TextField[str] | ||
expire_date: models.DateTimeField[datetime] | ||
|
||
@classmethod | ||
def get_session_store_class(cls) -> type[SessionBase] | None: ... | ||
def get_decoded(self) -> dict[str, int]: ... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,15 @@ | ||
from typing import ClassVar, TypeVar | ||
from typing_extensions import Self | ||
|
||
from django.contrib.sessions.backends.db import SessionStore | ||
from django.contrib.sessions.base_session import AbstractBaseSession, BaseSessionManager | ||
|
||
class SessionManager(BaseSessionManager): ... | ||
class Session(AbstractBaseSession): ... | ||
_T = TypeVar("_T", bound=AbstractBaseSession) | ||
|
||
class SessionManager(BaseSessionManager[_T]): ... | ||
|
||
class Session(AbstractBaseSession): | ||
objects: ClassVar[SessionManager[Self]] # type: ignore[assignment] | ||
|
||
@classmethod | ||
def get_session_store_class(cls) -> type[SessionStore]: ... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
from typing import Any | ||
from typing import TypeVar | ||
|
||
from django.db import models | ||
|
||
class CurrentSiteManager(models.Manager[Any]): | ||
_T = TypeVar("_T", bound=models.Model) | ||
|
||
class CurrentSiteManager(models.Manager[_T]): | ||
def __init__(self, field_name: str | None = ...) -> None: ... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters