get_old_messages: Refactor handling of UserMessage flags.

This is in preparation for having a case in which we query the
database directly to get the message flags, without going through a
UserMessage object.

(imported from commit d5218974680b0c4b028a84f3aae1c8242ceb08ce)
This commit is contained in:
Tim Abbott 2013-06-19 15:05:22 -04:00
parent e5e0544816
commit a8d86d5fb2
2 changed files with 13 additions and 12 deletions

View File

@ -406,9 +406,11 @@ class UserMessage(models.Model):
display_recipient = get_display_recipient(self.message.recipient)
return (u"<UserMessage: %s / %s (%s)>" % (display_recipient, self.user_profile.email, self.flags_dict())).encode("utf-8")
def flags_dict(self):
return dict(flags = [flag for flag in self.flags.keys() if getattr(self.flags, flag).is_set])
def flags_list(self):
return [flag for flag in self.flags.keys() if getattr(self.flags, flag).is_set]
def flags_dict(self):
return dict(flags = [self.flags_list()])
class Subscription(models.Model):
user_profile = models.ForeignKey(UserProfile)

View File

@ -855,18 +855,21 @@ def get_old_messages_backend(request, user_profile,
# 'messages' list.
search_fields = dict()
message_ids = []
user_message_flags = {}
if include_history:
user_messages = dict((user_message.message_id, user_message) for user_message in
UserMessage.objects.filter(user_profile=user_profile,
message__in=query_result))
user_message_flags = dict((user_message.message_id, user_message.flags_list()) for user_message in
UserMessage.objects.filter(user_profile=user_profile,
message__in=query_result))
for message in query_result:
message_ids.append(message.id)
if user_message_flags.get(message.id) is None:
user_message_flags[message.id] = ["read", "historical"]
if is_search:
search_fields[message.id] = dict([('match_subject', message.match_subject),
('match_content', message.match_content)])
else:
user_messages = dict((user_message.message_id, user_message)
for user_message in query_result)
user_message_flags = dict((user_message.message_id, user_message.flags_list())
for user_message in query_result)
for user_message in query_result:
message_ids.append(user_message.message_id)
if is_search:
@ -897,14 +900,10 @@ def get_old_messages_backend(request, user_profile,
message_list = []
for message_id in message_ids:
if include_history:
flags_dict = {'flags': ["read", "historical"]}
if message_id in user_messages:
flags_dict = user_messages[message_id].flags_dict()
key = to_dict_cache_key_id(message_id, apply_markdown)
elt = bulk_messages.get(key)
msg_dict = dict(elt)
msg_dict.update(flags_dict)
msg_dict.update({"flags": user_message_flags[message_id]})
msg_dict.update(search_fields.get(elt['id'], {}))
message_list.append(msg_dict)