~unity-2d-team/unity-2d/Shell-MultiMonitor

« back to all changes in this revision

Viewing changes to grackle/model.py

  • Committer: William Grant
  • Date: 2012-01-22 12:35:09 UTC
  • Revision ID: william.grant@canonical.com-20120122123509-hh1m5nc5sv718t2o
Fix batching to almost work backwards too.

Show diffs side-by-side

added added

removed removed

Lines of Context:
116
116
            'message-id': message.get('message-id'),
117
117
            }
118
118
 
119
 
    def get_messages(self, archive_uuid, order, count, start):
 
119
    def get_messages(self, archive_uuid, order, count, memo):
120
120
        if order in ("date", "-date"):
121
121
            reversed = order[0] == '-'
122
122
        else:
123
123
            raise AssertionError("Unsupported order.")
124
 
        if start:
125
 
            start = uuid.UUID(start)
 
124
        if memo != '':
 
125
            memo = uuid.UUID(memo)
 
126
        # Get up to n+1 messages from the memo: the last item of the
 
127
        # previous batch (because that's where the memo starts) + this
 
128
        # batch.
126
129
        pairs = self.archive_messages.get(
127
130
            archive_uuid, column_count=count + 1,
128
 
            column_start=start, column_reversed=reversed).items()
 
131
            column_start=memo, column_reversed=reversed).items()
 
132
 
 
133
        if memo and len(pairs) and pairs[0][0] <= memo:
 
134
            pairs = pairs[1:]
 
135
        elif len(pairs) > count:
 
136
            pairs = pairs[:-1]
 
137
 
 
138
        if len(pairs) == 0:
 
139
            return (None, [], None)
 
140
 
 
141
        assert 0 < len(pairs) <= count
 
142
 
129
143
        ids = [v for k, v in pairs]
130
144
        messages = self.messages.multiget(
131
145
            ids, columns=['date', 'from', 'subject', 'message-id'])
132
 
        actual_count = len(pairs)
133
 
        if len(pairs) > count:
134
 
            assert len(pairs) == count + 1
135
 
            actual_count -= 1
136
 
            next_memo = str(pairs[count][0])
137
 
        else:
138
 
            next_memo = None
 
146
 
139
147
        return (
140
 
            None,
141
 
            [self._format_message(messages[id]) for id in ids[:actual_count]],
142
 
            next_memo,
 
148
            str(pairs[0][0]),
 
149
            [self._format_message(messages[id]) for id in ids],
 
150
            str(pairs[-1][0]),
143
151
            )