From 9c0a79d088ff857d4862e7845d86daf6bf670904 Mon Sep 17 00:00:00 2001 From: Sivert Kronen Hatteberg <skh@uio.no> Date: Tue, 7 Dec 2021 20:43:54 +0100 Subject: [PATCH] Add missing __str__ methods to the model --- greg/admin.py | 10 ++++++- greg/models.py | 77 +++++++++++++++++++++++++++++++++++-------------- gregui/admin.py | 2 +- 3 files changed, 66 insertions(+), 23 deletions(-) diff --git a/greg/admin.py b/greg/admin.py index c850afc5..2d9252c4 100644 --- a/greg/admin.py +++ b/greg/admin.py @@ -135,12 +135,20 @@ class OrganizationalUnitInline(admin.TabularInline): class SponsorAdmin(VersionAdmin): - list_display = ("id", "feide_id") + list_display = ("id", "feide_id", "first_name", "last_name") inlines = (OrganizationalUnitInline,) readonly_fields = ("id", "created", "updated") class SponsorOrganizationalUnitAdmin(VersionAdmin): + list_display = ( + "id", + "sponsor", + "organizational_unit", + "hierarchical_access", + "source", + "automatic", + ) readonly_fields = ("id", "created", "updated") diff --git a/greg/models.py b/greg/models.py index 4b0dd92d..4f805cd9 100644 --- a/greg/models.py +++ b/greg/models.py @@ -54,7 +54,7 @@ class Person(BaseModel): objects = PersonManager() def __str__(self): - return "{} {} ({})".format(self.first_name, self.last_name, self.pk) + return f"{self.first_name} {self.last_name} ({self.pk})" def __repr__(self): return "{}(id={!r}, first_name={!r}, last_name={!r})".format( @@ -197,7 +197,7 @@ class RoleType(BaseModel): max_days = models.IntegerField(default=365) def __str__(self): - return "{} ({})".format(str(self.name_en or self.name_nb), self.identifier) + return f"{str(self.name_en or self.name_nb)} ({self.identifier})" def __repr__(self): return "{}(pk={!r}, identifier={!r}, name_nb={!r}, name_en={!r})".format( @@ -243,6 +243,12 @@ class Role(BaseModel): self.__class__.__name__, self.pk, self.person, self.type ) + def __str__(self) -> str: + return ( + f"{self.__class__.__name__}(id={self.pk}, " + f"role={self.person}/{self.type}@{self.orgunit.name_nb})" + ) + class Notification(BaseModel): """A change notification that should be delivered to a message queue.""" @@ -253,7 +259,7 @@ class Notification(BaseModel): issued_at = models.IntegerField() meta = models.JSONField(null=True, blank=True) - def __repr__(self): + def __repr__(self) -> str: return "{}(id={!r}, identifier={!r}, object_type={!r}, operation={!r}, issued_at={!r}, meta={!r})".format( self.__class__.__name__, self.pk, @@ -295,7 +301,7 @@ class Identity(BaseModel): ) verified_at = models.DateTimeField(null=True, blank=True) - def __str__(self): + def __str__(self) -> str: return "{}(id={!r}, type={!r}, value={!r})".format( self.__class__.__name__, self.pk, @@ -303,7 +309,7 @@ class Identity(BaseModel): self.value, ) - def __repr__(self): + def __repr__(self) -> str: return "{}(id={!r}, person_id={!r}, type={!r}, source={!r}, value={!r}, verified_by={!r}, verified_at={!r})".format( self.__class__.__name__, self.pk, @@ -335,10 +341,10 @@ class ConsentType(BaseModel): user_allowed_to_change = models.BooleanField() mandatory = models.BooleanField(default=False) - def __str__(self): - return "{} ({})".format(str(self.name_en or self.name_nb), self.identifier) + def __str__(self) -> str: + return f"{str(self.name_en or self.name_nb)} ({self.identifier})" - def __repr__(self): + def __repr__(self) -> str: return "{}(id={!r}, identifier={!r}, name_en={!r}, valid_from={!r}, user_allowed_to_change={!r})".format( self.__class__.__name__, self.pk, @@ -370,12 +376,13 @@ class ConsentChoice(BaseModel): ), ) - def __str__(self): - return "{}: {} ({})".format( - str(self.consent_type), str(self.text_en or self.text_nb), self.value + def __str__(self) -> str: + return ( + f"{self.__class__.__name__}(id={self.pk}, value={self.value}" + f"type={self.consent_type}))" ) - def __repr__(self): + def __repr__(self) -> str: return "{}(id={!r}, consent_type={!r} value={!r}, text_en={!r}, text_nb={!r}, text_nn={!r})".format( self.__class__.__name__, self.pk, @@ -413,7 +420,14 @@ class Consent(BaseModel): ) ] - def __repr__(self): + def __str__(self) -> str: + return ( + f"{self.__class__.__name__}(id={self.pk}, " + f"person={self.person}, type={self.type}, " + f"consent_given_at={self.consent_given_at})" + ) + + def __repr__(self) -> str: return "{}(id={!r}, person={!r}, type={!r}, consent_given_at={!r})".format( self.__class__.__name__, self.pk, @@ -438,7 +452,13 @@ class OuIdentifier(BaseModel): models.UniqueConstraint(name="unique_identifier", fields=["name", "value"]) ] - def __repr__(self): + def __str__(self) -> str: + return ( + f"{self.__class__.__name__}(id={self.pk}, " + f"name={self.name}, value={self.value})" + ) + + def __repr__(self) -> str: return "{}(id={!r}, name={!r}, value={!r})".format( self.__class__.__name__, self.pk, self.name, self.value ) @@ -461,13 +481,13 @@ class OrganizationalUnit(BaseModel): active = models.BooleanField(default=True) deleted = models.BooleanField(default=False) - def __repr__(self): + def __repr__(self) -> str: return "{}(id={!r}, name_en={!r}, parent={!r})".format( self.__class__.__name__, self.pk, self.name_en, self.parent ) - def __str__(self): - return "{}".format(str(self.name_en or self.name_nb)) + def __str__(self) -> str: + return f"{self.name_en or self.name_nb}" def fetch_tree(self): """ @@ -509,10 +529,10 @@ class Sponsor(BaseModel): related_name="sponsor_unit", ) - def __str__(self): - return "{} ({} {})".format(self.feide_id, self.first_name, self.last_name) + def __str__(self) -> str: + return f"{self.feide_id} ({self.first_name} {self.last_name})" - def __repr__(self): + def __repr__(self) -> str: return "{}(id={!r}, feide_id={!r}, first_name={!r}, last_name={!r})".format( self.__class__.__name__, self.pk, @@ -568,7 +588,13 @@ class SponsorOrganizationalUnit(BaseModel): ) ] - def __repr__(self): + def __str__(self) -> str: + return ( + f"{self.__class__.__name__}(id={self.pk}, sponsor={self.sponsor}, " + f"org_unit={self.organizational_unit})" + ) + + def __repr__(self) -> str: return "{}(id={!r}, sponsor={!r}, organizational_unit={!r}, hierarchical_access={!r})".format( self.__class__.__name__, self.pk, @@ -592,6 +618,12 @@ class InvitationLink(BaseModel): ) expire = models.DateTimeField(blank=False, null=False) + def __str__(self) -> str: + return ( + f"{self.__class__.__name__}(id={self.pk}, invitation={self.invitation}, " + f"uuid={self.uuid}, expore={self.expire})" + ) + class Invitation(BaseModel): """ @@ -601,3 +633,6 @@ class Invitation(BaseModel): """ role = models.ForeignKey("Role", null=False, blank=False, on_delete=models.CASCADE) + + def __str__(self) -> str: + return f"{self.__class__.__name__}(id={self.pk}, role={self.role})" diff --git a/gregui/admin.py b/gregui/admin.py index 7c3f7717..7b80a29b 100644 --- a/gregui/admin.py +++ b/gregui/admin.py @@ -5,7 +5,7 @@ from gregui.models import EmailTemplate, GregUserProfile class GregUserProfileAdmin(VersionAdmin): - pass + list_display = ["id", "userid_feide", "person", "sponsor"] class EmailTemplateAdmin(VersionAdmin): -- GitLab