コード例 #1
0
  def _Update(self):
    """Updates the database:
       1. Creates episode if it did not exist, or sets episode's location/placemark.
       2. Creates posts that did not previously exist.
       3. Creates photos that did not previously exist.
       4. Updates photo MD5 values if they were given in a re-upload.
    """
    # Set episode location/placemark.
    if self._set_location or self._set_placemark:
      for ph_dict in self._ph_dicts:
        if 'location' not in self._ep_dict and 'location' in ph_dict:
          self._ep_dict['location'] = ph_dict['location']
        if 'placemark' not in self._ep_dict and 'placemark' in ph_dict:
          self._ep_dict['placemark'] = ph_dict['placemark']

    # Create new episode if it did not exist at the beginning of the operation.
    if self._episode_id in self._new_ids:
      yield gen.Task(Episode.CreateNew, self._client, **self._ep_dict)
    # Update existing episode's location/placemark.
    elif self._set_location or self._set_placemark:
      yield gen.Task(self._episode.UpdateExisting,
                     self._client,
                     location=self._ep_dict.get('location', None),
                     placemark=self._ep_dict.get('placemark', None))

    # Create posts and photos that did not exist at the beginning of the operation.
    tasks = []
    for ph_dict in self._ph_dicts:
      # Only create post, user_photo and photo if photo did not exist at the beginning of the operation.
      if ph_dict['photo_id'] in self._new_ids:
        # Create user photo record if asset keys were specified.
        asset_keys = ph_dict.pop('asset_keys', None)
        if asset_keys is not None:
          tasks.append(UserPhoto.CreateNew(self._client,
                                           user_id=self._user.user_id,
                                           photo_id=ph_dict['photo_id'],
                                           asset_keys=asset_keys))

        tasks.append(Photo.CreateNew(self._client, **ph_dict))
        tasks.append(Post.CreateNew(self._client, episode_id=self._episode_id, photo_id=ph_dict['photo_id']))
      else:
        # Update the photo if any MD5 attributes need to be overwritten. This is allowed if the photo image
        # has not yet been uploaded. This can happen if the MD5 value has changed on the client due to an IOS
        # upgrade.
        md5_dict = {'photo_id': ph_dict['photo_id']}
        util.SetIfNotNone(md5_dict, 'tn_md5', ph_dict['tn_md5'])
        util.SetIfNotNone(md5_dict, 'med_md5', ph_dict['med_md5'])
        util.SetIfNotNone(md5_dict, 'full_md5', ph_dict['full_md5'])
        util.SetIfNotNone(md5_dict, 'orig_md5', ph_dict['orig_md5'])
        if md5_dict:
          yield Photo.UpdateExisting(self._client, **md5_dict)

    yield tasks
コード例 #2
0
ファイル: photo_test.py プロジェクト: zorro0799/viewfinder
    def testQuery(self):
        """Verify photo creation and query by photo id."""
        def _OnQuery(p, p2):
            self.assertEqual(p2.caption, p.caption)
            self.assertEqual(p2.photo_id, p.photo_id)
            self.stop()

        def _OnCreatePhoto(p):
            Photo.Query(self._client, p.photo_id, None, partial(_OnQuery, p))

        photo_id = Photo.ConstructPhotoId(time.time(),
                                          self._mobile_dev.device_id, 1)
        episode_id = Episode.ConstructEpisodeId(time.time(),
                                                self._mobile_dev.device_id, 2)
        p_dict = {
            'photo_id': photo_id,
            'episode_id': episode_id,
            'user_id': self._user.user_id,
            'caption': 'a photo'
        }
        Photo.CreateNew(self._client, callback=_OnCreatePhoto, **p_dict)
コード例 #3
0
ファイル: photo_test.py プロジェクト: zorro0799/viewfinder
    def testUpdateAttribute(self):
        """Verify update of a photo attribute."""
        def _OnUpdate(p):
            p.aspect_ratio = None
            p.Update(self._client, self.stop)

        def _OnQuery(p):
            p.content_type = 'image/png'
            p.Update(self._client, partial(_OnUpdate, p))

        def _OnCreatePhoto(p):
            Photo.Query(self._client, p.photo_id, None, _OnQuery)

        photo_id = Photo.ConstructPhotoId(time.time(),
                                          self._mobile_dev.device_id, 1)
        episode_id = Episode.ConstructEpisodeId(time.time(),
                                                self._mobile_dev.device_id, 2)
        p_dict = {
            'photo_id': photo_id,
            'episode_id': episode_id,
            'user_id': self._user.user_id,
            'caption': 'A Photo'
        }
        Photo.CreateNew(self._client, callback=_OnCreatePhoto, **p_dict)