Home | History | Annotate | Download | only in model
      1 # Copyright (C) 2010 Google Inc. All rights reserved.
      2 #
      3 # Redistribution and use in source and binary forms, with or without
      4 # modification, are permitted provided that the following conditions are
      5 # met:
      6 #
      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
     12 # distribution.
     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.
     16 #
     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.
     28 
     29 from google.appengine.ext import db
     30 
     31 from model.queuepropertymixin import QueuePropertyMixin
     32 
     33 
     34 class WorkItems(db.Model, QueuePropertyMixin):
     35     queue_name = db.StringProperty()
     36     item_ids = db.ListProperty(int)
     37     date = db.DateTimeProperty(auto_now_add=True)
     38 
     39     @classmethod
     40     def key_for_queue(cls, queue_name):
     41         return "work-items-%s" % (queue_name)
     42 
     43     @classmethod
     44     def lookup_by_queue(cls, queue_name):
     45         return cls.get_or_insert(key_name=cls.key_for_queue(queue_name), queue_name=queue_name)
     46 
     47     def display_position_for_attachment(self, attachment_id):
     48         """Returns a 1-based index corresponding to the position
     49         of the attachment_id in the queue.  If the attachment is
     50         not in this queue, this returns None"""
     51         if attachment_id in self.item_ids:
     52             return self.item_ids.index(attachment_id) + 1
     53         return None
     54 
     55     @staticmethod
     56     def _unguarded_add(key, attachment_id):
     57         work_items = db.get(key)
     58         if attachment_id in work_items.item_ids:
     59             return
     60         work_items.item_ids.append(attachment_id)
     61         work_items.put()
     62 
     63     # Because this uses .key() self.is_saved() must be True or this will throw NotSavedError.
     64     def add_work_item(self, attachment_id):
     65         db.run_in_transaction(self._unguarded_add, self.key(), attachment_id)
     66 
     67     @staticmethod
     68     def _unguarded_remove(key, attachment_id):
     69         work_items = db.get(key)
     70         if attachment_id in work_items.item_ids:
     71             # We should never have more than one entry for a work item, so we only need remove the first.
     72             work_items.item_ids.remove(attachment_id)
     73         work_items.put()
     74 
     75     # Because this uses .key() self.is_saved() must be True or this will throw NotSavedError.
     76     def remove_work_item(self, attachment_id):
     77         db.run_in_transaction(self._unguarded_remove, self.key(), attachment_id)
     78