From 0a3dc8a94472294918b074c8ec91bf889276732c Mon Sep 17 00:00:00 2001 From: Anders Kaseorg Date: Wed, 12 Apr 2023 13:40:35 -0700 Subject: [PATCH] ruff: Fix DJ012 Order of model's inner classes, methods, and fields. Signed-off-by: Anders Kaseorg --- confirmation/models.py | 6 ++-- corporate/models.py | 6 ++-- zerver/models.py | 78 +++++++++++++++++++++--------------------- 3 files changed, 45 insertions(+), 45 deletions(-) diff --git a/confirmation/models.py b/confirmation/models.py index be907bd153..46d4332b1e 100644 --- a/confirmation/models.py +++ b/confirmation/models.py @@ -186,12 +186,12 @@ class Confirmation(models.Model): REALM_REACTIVATION = 8 type = models.PositiveSmallIntegerField() - def __str__(self) -> str: - return f"{self.content_object!r}" - class Meta: unique_together = ("type", "confirmation_key") + def __str__(self) -> str: + return f"{self.content_object!r}" + class ConfirmationType: def __init__( diff --git a/corporate/models.py b/corporate/models.py index 5bab42b7eb..99330f71f7 100644 --- a/corporate/models.py +++ b/corporate/models.py @@ -28,6 +28,9 @@ class Customer(models.Model): # they purchased. exempt_from_from_license_number_check = models.BooleanField(default=False) + def __str__(self) -> str: + return f"{self.realm!r} {self.stripe_customer_id}" + @property def is_self_hosted(self) -> bool: is_self_hosted = self.remote_server is not None @@ -42,9 +45,6 @@ class Customer(models.Model): assert self.remote_server is None return is_cloud - def __str__(self) -> str: - return f"{self.realm!r} {self.stripe_customer_id}" - def get_customer_by_realm(realm: Realm) -> Optional[Customer]: return Customer.objects.filter(realm=realm).first() diff --git a/zerver/models.py b/zerver/models.py index 7041b66130..67774d6f47 100644 --- a/zerver/models.py +++ b/zerver/models.py @@ -692,14 +692,6 @@ class Realm(models.Model): # type: ignore[django-manager-missing] # django-stub }, } - def get_giphy_rating_options(self) -> Dict[str, Dict[str, object]]: - """Wrapper function for GIPHY_RATING_OPTIONS that ensures evaluation - of the lazily evaluated `name` field without modifying the original.""" - return { - rating_type: {"name": str(rating["name"]), "id": rating["id"]} - for rating_type, rating in self.GIPHY_RATING_OPTIONS.items() - } - # maximum rating of the GIFs that will be retrieved from GIPHY giphy_rating = models.PositiveSmallIntegerField(default=GIPHY_RATING_OPTIONS["g"]["id"]) @@ -793,6 +785,17 @@ class Realm(models.Model): # type: ignore[django-manager-missing] # django-stub ) night_logo_version = models.PositiveSmallIntegerField(default=1) + def __str__(self) -> str: + return f"{self.string_id} {self.id}" + + def get_giphy_rating_options(self) -> Dict[str, Dict[str, object]]: + """Wrapper function for GIPHY_RATING_OPTIONS that ensures evaluation + of the lazily evaluated `name` field without modifying the original.""" + return { + rating_type: {"name": str(rating["name"]), "id": rating["id"]} + for rating_type, rating in self.GIPHY_RATING_OPTIONS.items() + } + def authentication_methods_dict(self) -> Dict[str, bool]: """Returns the mapping from authentication flags to their status, showing only those authentication flags that are supported on @@ -813,9 +816,6 @@ class Realm(models.Model): # type: ignore[django-manager-missing] # django-stub ret[k] = v return ret - def __str__(self) -> str: - return f"{self.string_id} {self.id}" - # `realm` instead of `self` here to make sure the parameters of the cache key # function matches the original method. @cache_with_key(get_realm_emoji_cache_key, timeout=3600 * 24 * 7) @@ -1125,9 +1125,6 @@ class RealmEmoji(models.Model): PATH_ID_TEMPLATE = "{realm_id}/emoji/images/{emoji_file_name}" STILL_PATH_ID_TEMPLATE = "{realm_id}/emoji/images/still/{emoji_filename_without_extension}.png" - def __str__(self) -> str: - return f"{self.realm.string_id}: {self.id} {self.name} {self.deactivated} {self.file_name}" - class Meta: constraints = [ models.UniqueConstraint( @@ -1137,6 +1134,9 @@ class RealmEmoji(models.Model): ), ] + def __str__(self) -> str: + return f"{self.realm.string_id}: {self.id} {self.name} {self.deactivated} {self.file_name}" + def get_realm_emoji_dicts(realm: Realm, only_active_emojis: bool = False) -> Dict[str, EmojiInfo]: # RealmEmoji objects with file_name=None are still in the process @@ -1286,6 +1286,9 @@ class RealmFilter(models.Model): class Meta: unique_together = ("realm", "pattern") + def __str__(self) -> str: + return f"{self.realm.string_id}: {self.pattern} {self.url_format_string}" + def clean(self) -> None: """Validate whether the set of parameters in the URL Format string match the set of parameters in the regular expression. @@ -1328,9 +1331,6 @@ class RealmFilter(models.Model): params={"name": name}, ) - def __str__(self) -> str: - return f"{self.realm.string_id}: {self.pattern} {self.url_format_string}" - def get_linkifiers_cache_key(realm_id: int) -> str: return f"{cache.KEY_PREFIX}:all_linkifiers_for_realm:{realm_id}" @@ -1486,14 +1486,14 @@ class Recipient(models.Model): # N.B. If we used Django's choice=... we would get this for free (kinda) _type_names = {PERSONAL: "personal", STREAM: "stream", HUDDLE: "huddle"} - def type_name(self) -> str: - # Raises KeyError if invalid - return self._type_names[self.type] - def __str__(self) -> str: display_recipient = get_display_recipient(self) return f"{display_recipient} ({self.type_id}, {self.type})" + def type_name(self) -> str: + # Raises KeyError if invalid + return self._type_names[self.type] + class UserBaseSettings(models.Model): """This abstract class is the container for all preferences/personal @@ -2623,6 +2623,11 @@ class Stream(models.Model): ), } + class Meta: + indexes = [ + models.Index(Upper("name"), name="upper_stream_name_idx"), + ] + def __str__(self) -> str: return self.name @@ -2680,11 +2685,6 @@ class Stream(models.Model): is_announcement_only=self.stream_post_policy == Stream.STREAM_POST_POLICY_ADMINS, ) - class Meta: - indexes = [ - models.Index(Upper("name"), name="upper_stream_name_idx"), - ] - post_save.connect(flush_stream, sender=Stream) post_delete.connect(flush_stream, sender=Stream) @@ -4115,12 +4115,6 @@ class UserPresence(models.Model): https://zulip.readthedocs.io/en/latest/subsystems/presence.html """ - class Meta: - unique_together = ("user_profile", "client") - index_together = [ - ("realm", "timestamp"), - ] - user_profile = models.ForeignKey(UserProfile, on_delete=CASCADE) realm = models.ForeignKey(Realm, on_delete=CASCADE) client = models.ForeignKey(Client, on_delete=CASCADE) @@ -4146,6 +4140,12 @@ class UserPresence(models.Model): # timestamp being old. status = models.PositiveSmallIntegerField(default=ACTIVE) + class Meta: + unique_together = ("user_profile", "client") + index_together = [ + ("realm", "timestamp"), + ] + @staticmethod def status_to_string(status: int) -> str: if status == UserPresence.ACTIVE: @@ -4345,16 +4345,16 @@ class ScheduledMessage(models.Model): default=SEND_LATER, ) + def __str__(self) -> str: + display_recipient = get_display_recipient(self.recipient) + return f"{display_recipient} {self.subject} {self.sender!r} {self.scheduled_timestamp}" + def topic_name(self) -> str: return self.subject def set_topic_name(self, topic_name: str) -> None: self.subject = topic_name - def __str__(self) -> str: - display_recipient = get_display_recipient(self.recipient) - return f"{display_recipient} {self.subject} {self.sender!r} {self.scheduled_timestamp}" - EMAIL_TYPES = { "followup_day1": ScheduledEmail.WELCOME, @@ -4662,6 +4662,9 @@ class CustomProfileField(models.Model): class Meta: unique_together = ("realm", "name") + def __str__(self) -> str: + return f"{self.realm!r} {self.name} {self.field_type} {self.order}" + def as_dict(self) -> ProfileDataElementBase: data_as_dict: ProfileDataElementBase = { "id": self.id, @@ -4681,9 +4684,6 @@ class CustomProfileField(models.Model): return True return False - def __str__(self) -> str: - return f"{self.realm!r} {self.name} {self.field_type} {self.order}" - def custom_profile_fields_for_realm(realm_id: int) -> QuerySet[CustomProfileField]: return CustomProfileField.objects.filter(realm=realm_id).order_by("order")