Skip to content

Commit

Permalink
models: Move several __unicode__ methods to abstract classes.
Browse files Browse the repository at this point in the history
This makes it possible to print ArchivedMessage, ArchivedUserMessage,
and ArchivedAttachment objects.
  • Loading branch information
timabbott committed May 17, 2017
1 parent 34a8e6c commit be3dc12
Showing 1 changed file with 16 additions and 14 deletions.
30 changes: 16 additions & 14 deletions zerver/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1027,6 +1027,12 @@ class AbstractMessage(ModelReprMixin, models.Model):
class Meta(object):
abstract = True

def __unicode__(self):
# type: () -> Text
display_recipient = get_display_recipient(self.recipient)
return u"<%s: %s / %s / %r>" % (self.__class__.__name__, display_recipient,
self.subject, self.sender)


class ArchivedMessage(AbstractMessage):
archive_timestamp = models.DateTimeField(default=timezone_now, db_index=True) # type: datetime.datetime
Expand All @@ -1042,11 +1048,6 @@ def topic_name(self):
"""
return self.subject

def __unicode__(self):
# type: () -> Text
display_recipient = get_display_recipient(self.recipient)
return u"<Message: %s / %s / %r>" % (display_recipient, self.subject, self.sender)

def get_realm(self):
# type: () -> Realm
return self.sender.realm
Expand Down Expand Up @@ -1226,6 +1227,12 @@ def flags_list(self):
# type: () -> List[str]
return [flag for flag in self.flags.keys() if getattr(self.flags, flag).is_set]

def __unicode__(self):
# type: () -> Text
display_recipient = get_display_recipient(self.message.recipient)
return u"<%s: %s / %s (%s)>" % (self.__class__.__name__, display_recipient,
self.user_profile.email, self.flags_list())


class ArchivedUserMessage(AbstractUserMessage):
message = models.ForeignKey(ArchivedMessage) # type: Message
Expand All @@ -1235,11 +1242,6 @@ class ArchivedUserMessage(AbstractUserMessage):
class UserMessage(AbstractUserMessage):
message = models.ForeignKey(Message) # type: Message

def __unicode__(self):
# type: () -> Text
display_recipient = get_display_recipient(self.message.recipient)
return u"<UserMessage: %s / %s (%s)>" % (display_recipient, self.user_profile.email, self.flags_list())


def parse_usermessage_flags(val):
# type: (int) -> List[str]
Expand Down Expand Up @@ -1268,6 +1270,10 @@ class AbstractAttachment(ModelReprMixin, models.Model):
class Meta(object):
abstract = True

def __unicode__(self):
# type: () -> Text
return u"<%s: %s>" % (self.__class__.__name__, self.file_name,)


class ArchivedAttachment(AbstractAttachment):
archive_timestamp = models.DateTimeField(default=timezone_now, db_index=True) # type: datetime.datetime
Expand All @@ -1277,10 +1283,6 @@ class ArchivedAttachment(AbstractAttachment):
class Attachment(AbstractAttachment):
messages = models.ManyToManyField(Message) # type: Manager

def __unicode__(self):
# type: () -> Text
return u"<Attachment: %s>" % (self.file_name,)

def is_claimed(self):
# type: () -> bool
return self.messages.count() > 0
Expand Down

0 comments on commit be3dc12

Please sign in to comment.