Typehint object managers

This commit is contained in:
Ariel Rin 2025-04-29 02:47:11 +00:00
parent 9dad53f763
commit 5c6dda0eac
10 changed files with 28 additions and 19 deletions

View File

@ -1,4 +1,5 @@
import logging import logging
from typing import ClassVar
from django.contrib.auth.models import User, Permission from django.contrib.auth.models import User, Permission
from django.db import models, transaction from django.db import models, transaction
@ -27,7 +28,7 @@ class State(models.Model):
help_text="Factions to whose members this state is available.") help_text="Factions to whose members this state is available.")
public = models.BooleanField(default=False, help_text="Make this state available to any character.") public = models.BooleanField(default=False, help_text="Make this state available to any character.")
objects = StateManager() objects: ClassVar[StateManager] = StateManager()
class Meta: class Meta:
ordering = ['-priority'] ordering = ['-priority']
@ -137,8 +138,10 @@ class UserProfile(models.Model):
sender=self.__class__, user=self.user, state=self.state sender=self.__class__, user=self.user, state=self.state
) )
def __str__(self): def __str__(self) -> str:
return str(self.user) return str(self.user)
class CharacterOwnership(models.Model): class CharacterOwnership(models.Model):
class Meta: class Meta:
default_permissions = ('change', 'delete') default_permissions = ('change', 'delete')
@ -148,7 +151,7 @@ class CharacterOwnership(models.Model):
owner_hash = models.CharField(max_length=28, unique=True) owner_hash = models.CharField(max_length=28, unique=True)
user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='character_ownerships') user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='character_ownerships')
objects = CharacterOwnershipManager() objects: ClassVar[CharacterOwnershipManager] = CharacterOwnershipManager()
def __str__(self): def __str__(self):
return f"{self.user}: {self.character}" return f"{self.user}: {self.character}"

View File

@ -1,5 +1,6 @@
import logging import logging
import os import os
from typing import ClassVar
from allianceauth.authentication.models import CharacterOwnership, UserProfile from allianceauth.authentication.models import CharacterOwnership, UserProfile
from bravado.exception import HTTPForbidden from bravado.exception import HTTPForbidden
@ -40,9 +41,9 @@ class CorpStats(models.Model):
verbose_name = "corp stats" verbose_name = "corp stats"
verbose_name_plural = "corp stats" verbose_name_plural = "corp stats"
objects = CorpStatsManager() objects: ClassVar[CorpStatsManager] = CorpStatsManager()
def __str__(self): def __str__(self) -> str:
return f"{self.__class__.__name__} for {self.corp}" return f"{self.__class__.__name__} for {self.corp}"
def update(self): def update(self):

View File

@ -1,4 +1,5 @@
import logging import logging
from typing import ClassVar
from django.db import models, transaction from django.db import models, transaction
from django.contrib.auth.models import Group, User from django.contrib.auth.models import Group, User
from django.core.exceptions import ObjectDoesNotExist from django.core.exceptions import ObjectDoesNotExist
@ -78,7 +79,7 @@ class AutogroupsConfig(models.Model):
max_length=10, default='', blank=True, max_length=10, default='', blank=True,
help_text='Any spaces in the group name will be replaced with this.') help_text='Any spaces in the group name will be replaced with this.')
objects = AutogroupsConfigManager() objects: ClassVar[AutogroupsConfigManager] = AutogroupsConfigManager()
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)

View File

@ -14,10 +14,10 @@ class EveCharacterProviderManager:
class EveCharacterManager(models.Manager): class EveCharacterManager(models.Manager):
provider = EveCharacterProviderManager() provider = EveCharacterProviderManager()
def create_character(self, character_id): def create_character(self, character_id) -> models.Model:
return self.create_character_obj(self.provider.get_character(character_id)) return self.create_character_obj(self.provider.get_character(character_id))
def create_character_obj(self, character: providers.Character): def create_character_obj(self, character: providers.Character) -> models.Model:
return self.create( return self.create(
character_id=character.id, character_id=character.id,
character_name=character.name, character_name=character.name,

View File

@ -1,5 +1,5 @@
import logging import logging
from typing import Union from typing import ClassVar, Union
from django.core.exceptions import ObjectDoesNotExist from django.core.exceptions import ObjectDoesNotExist
from django.db import models from django.db import models
@ -75,8 +75,8 @@ class EveAllianceInfo(models.Model):
alliance_ticker = models.CharField(max_length=254) alliance_ticker = models.CharField(max_length=254)
executor_corp_id = models.PositiveIntegerField() executor_corp_id = models.PositiveIntegerField()
objects = EveAllianceManager() objects: ClassVar[EveAllianceManager] = EveAllianceManager()
provider = EveAllianceProviderManager() provider: ClassVar[EveAllianceProviderManager] = EveAllianceProviderManager()
class Meta: class Meta:
indexes = [models.Index(fields=['executor_corp_id',])] indexes = [models.Index(fields=['executor_corp_id',])]
@ -147,7 +147,7 @@ class EveCorporationInfo(models.Model):
EveAllianceInfo, blank=True, null=True, on_delete=models.SET_NULL EveAllianceInfo, blank=True, null=True, on_delete=models.SET_NULL
) )
objects = EveCorporationManager() objects: ClassVar[EveCorporationManager] = EveCorporationManager()
provider = EveCorporationProviderManager() provider = EveCorporationProviderManager()
class Meta: class Meta:
@ -214,7 +214,7 @@ class EveCharacter(models.Model):
faction_id = models.PositiveIntegerField(blank=True, null=True, default=None) faction_id = models.PositiveIntegerField(blank=True, null=True, default=None)
faction_name = models.CharField(max_length=254, blank=True, null=True, default='') faction_name = models.CharField(max_length=254, blank=True, null=True, default='')
objects = EveCharacterManager() objects: ClassVar[EveCharacterManager] = EveCharacterManager()
provider = EveCharacterProviderManager() provider = EveCharacterProviderManager()
class Meta: class Meta:

View File

@ -1,3 +1,4 @@
from typing import ClassVar
from django.contrib.auth.models import User from django.contrib.auth.models import User
from django.db import models from django.db import models
from sortedm2m.fields import SortedManyToManyField from sortedm2m.fields import SortedManyToManyField
@ -40,7 +41,7 @@ class Application(models.Model):
reviewer_character = models.ForeignKey(EveCharacter, on_delete=models.SET_NULL, blank=True, null=True) reviewer_character = models.ForeignKey(EveCharacter, on_delete=models.SET_NULL, blank=True, null=True)
created = models.DateTimeField(auto_now_add=True) created = models.DateTimeField(auto_now_add=True)
objects = ApplicationManager() objects: ClassVar[ApplicationManager] = ApplicationManager()
def __str__(self): def __str__(self):
return str(self.user) + " Application To " + str(self.form) return str(self.user) + " Application To " + str(self.form)

View File

@ -1,3 +1,4 @@
from typing import ClassVar
from django.db import models from django.db import models
from django.utils.translation import gettext_lazy as _ from django.utils.translation import gettext_lazy as _
@ -68,7 +69,7 @@ class MenuItem(models.Model):
help_text=_("External URL this menu items will link to"), help_text=_("External URL this menu items will link to"),
) )
objects = MenuItemManager() objects: ClassVar[MenuItemManager] = MenuItemManager()
def __str__(self) -> str: def __str__(self) -> str:
return self.text return self.text

View File

@ -1,4 +1,5 @@
import logging import logging
from typing import ClassVar
from django.db import models from django.db import models
from django.contrib.auth.models import User from django.contrib.auth.models import User
@ -56,7 +57,7 @@ class Notification(models.Model):
timestamp = models.DateTimeField(auto_now_add=True, db_index=True) timestamp = models.DateTimeField(auto_now_add=True, db_index=True)
viewed = models.BooleanField(default=False, db_index=True) viewed = models.BooleanField(default=False, db_index=True)
objects = NotificationManager() objects: ClassVar[NotificationManager] = NotificationManager()
def __str__(self) -> str: def __str__(self) -> str:
return f"{self.user}: {self.title}" return f"{self.user}: {self.title}"

View File

@ -1,5 +1,5 @@
import logging import logging
from typing import Optional from typing import ClassVar, Optional
from requests.exceptions import HTTPError from requests.exceptions import HTTPError
@ -59,7 +59,7 @@ class DiscordUser(models.Model):
help_text='Date & time this service account was activated' help_text='Date & time this service account was activated'
) )
objects = DiscordUserManager() objects: ClassVar[DiscordUserManager] = DiscordUserManager()
class Meta: class Meta:
permissions = ( permissions = (

View File

@ -1,5 +1,6 @@
import random import random
import string import string
from typing import ClassVar
from passlib.hash import bcrypt_sha256 from passlib.hash import bcrypt_sha256
from django.db import models from django.db import models
@ -116,7 +117,7 @@ class MumbleUser(AbstractServiceModel):
help_text="Timestamp of the users Last Disconnection to Mumble" help_text="Timestamp of the users Last Disconnection to Mumble"
) )
objects = MumbleManager() objects: ClassVar[MumbleManager] = MumbleManager()
def __str__(self): def __str__(self):
return self.username return self.username