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 datetime import datetime
     30 import logging
     31 
     32 from google.appengine.ext import db
     33 
     34 from model.datastorefile import DataStoreFile
     35 
     36 
     37 class TestFile(DataStoreFile):
     38     master = db.StringProperty()
     39     builder = db.StringProperty()
     40     test_type = db.StringProperty()
     41 
     42     @classmethod
     43     def delete_file(cls, key, master, builder, test_type, name, limit):
     44         if key:
     45             file = db.get(key)
     46             if not file:
     47                 logging.warning("File not found, key: %s.", key)
     48                 return False
     49 
     50             file._delete_all()
     51         else:
     52             files = cls.get_files(master, builder, test_type, name, limit)
     53             if not files:
     54                 logging.warning(
     55                     "File not found, master: %s, builder: %s, test_type:%s, name: %s.",
     56                     builder, test_type, name)
     57                 return False
     58 
     59             for file in files:
     60                 file._delete_all()
     61 
     62         return True
     63 
     64     @classmethod
     65     def get_files(cls, master, builder, test_type, name, load_data=True, limit=1):
     66         query = TestFile.all()
     67         if master:
     68             query = query.filter("master =", master)
     69         if builder:
     70             query = query.filter("builder =", builder)
     71         if test_type:
     72             query = query.filter("test_type =", test_type)
     73         if name:
     74             query = query.filter("name =", name)
     75 
     76         files = query.order("-date").fetch(limit)
     77         if load_data:
     78             for file in files:
     79                 file.load_data()
     80 
     81         return files
     82 
     83     @classmethod
     84     def add_file(cls, master, builder, test_type, name, data):
     85         file = TestFile()
     86         file.master = master
     87         file.builder = builder
     88         file.test_type = test_type
     89         file.name = name
     90 
     91         if not file.save(data):
     92             return None
     93 
     94         logging.info(
     95             "File saved, master: %s, builder: %s, test_type: %s, name: %s, key: %s.",
     96             master, builder, test_type, file.name, str(file.data_keys))
     97 
     98         return file
     99 
    100     @classmethod
    101     def update(cls, master, builder, test_type, name, data):
    102         files = cls.get_files(master, builder, test_type, name)
    103         if not files:
    104             return cls.add_file(master, builder, test_type, name, data)
    105 
    106         file = files[0]
    107         if not file.save(data):
    108             return None
    109 
    110         logging.info(
    111             "File replaced, master: %s, builder: %s, test_type: %s, name: %s, data key: %s.",
    112             master, builder, test_type, file.name, str(file.data_keys))
    113 
    114         return file
    115 
    116     def save(self, data):
    117         if not self.save_data(data):
    118             return False
    119 
    120         self.date = datetime.now()
    121         self.put()
    122 
    123         return True
    124 
    125     def _delete_all(self):
    126         self.delete_data()
    127         self.delete()
    128