Пример #1
0
  def _Check(self):
    """Gathers pre-mutation information:
       1. List of requested episodes and photos to save.
       2. Checkpoints list of episode and post ids that need to be (re)created.
       3. Acquires locks for all source viewpoints.

       Validates the following:
       1. Permissions to share from source episodes.
    """
    # Create save episode dicts from episodes and viewpoints in the request.
    self._save_ep_dicts = yield self._CreateSaveEpisodeDicts()

    # Validate episodes and posts to save.
    source_ep_posts_list = yield ViewfinderOperation._CheckCopySources('save',
                                                                       self._client,
                                                                       self._user.user_id,
                                                                       self._save_ep_dicts)

    # Get dicts describing the target episodes and posts.
    target_ep_ids = [ep_dict['new_episode_id'] for ep_dict in self._save_ep_dicts]
    self._target_ep_dicts = ViewfinderOperation._CreateCopyTargetDicts(self._op.timestamp,
                                                                       self._user.user_id,
                                                                       self._user.private_vp_id,
                                                                       source_ep_posts_list,
                                                                       target_ep_ids)

    # Lock all source viewpoints.
    for source_vp_id in set(episode.viewpoint_id for episode, posts in source_ep_posts_list):
      yield self._lock_tracker.AcquireViewpointLock(source_vp_id)

    # Start populating the checkpoint if this the first time the operation has been run.
    if self._op.checkpoint is None:
      # Get subset of target episodes and posts that need to be saved.
      self._new_ids = yield ViewfinderOperation._CheckCopyTargets('save',
                                                                  self._client,
                                                                  self._user.user_id,
                                                                  self._user.private_vp_id,
                                                                  self._target_ep_dicts)

      # Set checkpoint.
      # List of new episode/post ids need to be check-pointed because they may change in the
      # UPDATE phase. If we fail after UPDATE, but before NOTIFY, we would not send correct
      # notifications on retry.
      checkpoint = {'new': list(self._new_ids)}
      yield self._op.SetCheckpoint(self._client, checkpoint)
    else:
      # Restore state from checkpoint.
      self._new_ids = set(self._op.checkpoint['new'])

    raise gen.Return(True)