1
# Copyright (C) 2009 Google Inc. All rights reserved.
3
# Redistribution and use in source and binary forms, with or without
4
# modification, are permitted provided that the following conditions are
7
# * Redistributions of source code must retain the above copyright
8
# notice, this list of conditions and the following disclaimer.
9
# * Redistributions in binary form must reproduce the above
10
# copyright notice, this list of conditions and the following disclaimer
11
# in the documentation and/or other materials provided with the
13
# * Neither the name of Google Inc. nor the names of its
14
# contributors may be used to endorse or promote products derived from
15
# this software without specific prior written permission.
17
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
from google.appengine.api import memcache
33
from model.queues import Queue
34
from model.queuestatus import QueueStatus
35
from model.workitems import WorkItems
38
class Attachment(object):
40
def dirty(cls, attachment_id):
41
memcache.delete(str(attachment_id), namespace="attachment-summary")
44
def recent(cls, limit=1):
45
statuses = QueueStatus.all().order("-date")
46
# Notice that we use both a set and a list here to keep the -date ordering.
49
for status in statuses:
50
attachment_id = status.active_patch_id
53
if attachment_id in visited_ids:
55
visited_ids.add(attachment_id)
56
ids.append(attachment_id)
57
if len(visited_ids) >= limit:
61
def __init__(self, attachment_id):
62
self.id = attachment_id
64
self._cached_queue_positions = None
69
self._summary = memcache.get(str(self.id), namespace="attachment-summary")
72
self._summary = self._fetch_summary()
73
memcache.set(str(self.id), self._summary, namespace="attachment-summary")
76
def state_from_queue_status(self, status):
81
state = table.get(status.message)
84
if status.message.startswith("Error:"):
90
def position_in_queue(self, queue):
91
return self._queue_positions().get(queue.name())
93
def status_for_queue(self, queue):
94
# summary() is a horrible API and should be killed.
95
queue_summary = self.summary().get(queue.name_with_underscores())
98
return queue_summary.get("status")
101
return self.summary().get("bug_id")
103
def _queue_positions(self):
104
if self._cached_queue_positions:
105
return self._cached_queue_positions
106
# FIXME: Should we be mem-caching this?
107
self._cached_queue_positions = self._calculate_queue_positions()
108
return self._cached_queue_positions
110
def _calculate_queue_positions(self):
111
all_work_items = WorkItems.all().fetch(limit=len(Queue.all()))
112
return dict([(items.queue.name(), items.display_position_for_attachment(self.id)) for items in all_work_items])
114
# FIXME: This is controller/view code and does not belong in a model.
115
def _fetch_summary(self):
116
summary = { "attachment_id" : self.id }
118
first_status = QueueStatus.all().filter('active_patch_id =', self.id).get()
120
# We don't have any record of this attachment.
122
summary["bug_id"] = first_status.active_bug_id
124
for queue in Queue.all():
125
summary[queue.name_with_underscores()] = None
126
status = QueueStatus.all().filter('queue_name =', queue.name()).filter('active_patch_id =', self.id).order('-date').get()
128
# summary() is a horrible API and should be killed.
129
summary[queue.name_with_underscores()] = {
130
"state": self.state_from_queue_status(status),