Example #1
0
    def __Verify(self):
        """Determines if the bug is valid.

    Raises:
      db.Error: Raised if any bug property is invalid.
    """
        if not self.title:
            raise db.Error('Missing title; required.')
Example #2
0
 def txn():
     calls = memcache.incr('calls')
     result = ResultModel.get_by_key_name('test')
     if not result:
         result = ResultModel(key_name='test', total=0)
     result.total += len(keys)
     result.put()
     if memcache.get('raise') and (calls % 2):
         raise db.Error()
Example #3
0
 def put(self,*args,**kw):
     self.slug = unicode(slugify(self.name))
     existing = db.Query(Session).filter('slug =',self.slug).get()
     if existing and existing.key() != self.key():
         raise db.Error('A session already exists with a similar name: "%s".' % existing.name)
     days = {
         'Monday'    : 1,
         'Tuesday'   : 2,
         'Wednesday' : 3,
         'Thursday'  : 4,
         'Friday'    : 5,
         'Saturday'  : 6,
         'Sunday'    : 7,
     }
     self.ordering = days[self.day_of_week]
     self.update_coordinates()
     return db.Model.put(self,*args,**kw)    
Example #4
0
    def create(cls,
               blob_info=None,
               data=None,
               filename=None,
               url=None,
               mime_type=None,
               **kwargs):
        """
        Create an ``Image``. Use this class method rather than creating an image with the constructor. You must provide one
        of the following parameters ``blob_info``, ``data``, or ``url`` to specify the image data to use.

        :param blob_info: The `Blobstore`_ data to use as the image data. If this parameter is not ``None``, all
            other parameters will be ignored as they are not needed.
        :param data: The image data that should be put in the `Blobstore`_ and used as the image data.
        :param filename: The filename of the image data. If not provided, the filename will be guessed from the URL
            or, if there is no URL, it will be set to the stringified `Key`_ of the image entity.
        :param url: The URL to fetch the image data from and then place in the `Blobstore`_ to be used as the image data.
        :param mime_type: The `mime type`_ to use for the `Blobstore`_ image data.
            If ``None``, it will attempt to guess the mime type from the url fetch response headers or the filename.
        :param parent:  Inherited from `Model`_. The `Model`_ instance or `Key`_ instance for the entity that is the new
            image's parent.
        :param key_name: Inherited from `Model`_. The name for the new entity. The name becomes part of the primary key.
        :param key: Inherited from `Model`_. The explicit `Key`_ instance for the new entity.
            Cannot be used with ``key_name`` or ``parent``. If ``None``, falls back on the behavior for ``key_name`` and
            ``parent``.
        :param kwargs: Initial values for the instance's properties, as keyword arguments.  Useful if subclassing.
        :return: An instance of the ``Image`` class.
        """
        if filename is not None:
            filename = filename.encode('ascii', 'ignore')
        if url is not None:
            url = url.encode('ascii', 'ignore')
        if blob_info is not None:
            kwargs['blob_info'] = blob_info
            return cls.create_new_entity(**kwargs)
        if data is None:
            if url is not None:
                response = urlfetch.fetch(url)
                data = response.content
                mime_type = mime_type or response.headers.get(
                    'Content-Type', None)
                if filename is None:
                    path = urlparse.urlsplit(url)[2]
                    filename = path[path.rfind('/') + 1:]
        if data is None:
            raise db.Error("No image data")
        image = cls.create_new_entity(source_url=url, **kwargs)
        filename = filename or str(image.key())
        mime_type = mime_type or mimetypes.guess_type(
            filename)[0] or 'application/octet-stream'
        if mime_type not in config.VALID_MIME_TYPES:
            message = "The image mime type (%s) isn't valid" % mime_type
            logging.warning(message)
            image.delete()
            raise images.BadImageError(message)
        blob_file_name = files.blobstore.create(
            mime_type=mime_type, _blobinfo_uploaded_filename=filename)
        with files.open(blob_file_name, 'a') as f:
            f.write(data)
        files.finalize(blob_file_name)
        image.blob_info = files.blobstore.get_blob_key(blob_file_name)
        image.put()
        image = cls.get(str(image.key()))
        if image is not None and image.blob_info is None:
            logging.error("Failed to create image: %s" % filename)
            image.delete()
            image = None
        return image