def testCanSetMessageState(self):
     self.assertEqual(MESSAGE_UNKNOWN,
                      _GetDomainUserEntity(self._user_key).message_state)
     state_updater = EntityStateUpdater(task_key_id=self._task_key.id(),
                                        user_key_id=self._user_key.id())
     state_updater.SetMessageState(MESSAGE_FOUND)
     self.assertEqual(MESSAGE_FOUND,
                      _GetDomainUserEntity(self._user_key).message_state)
 def testCanSetTaskState(self):
     self.assertEqual(TASK_STARTED,
                      _GetRecallTaskEntity(self._task_key).task_state)
     state_updater = EntityStateUpdater(task_key_id=self._task_key.id(),
                                        user_key_id=self._user_key.id())
     state_updater.SetTaskState(TASK_GETTING_USERS)
     self.assertEqual(TASK_GETTING_USERS,
                      _GetRecallTaskEntity(self._task_key).task_state)
Beispiel #3
0
    def __init__(self, task_key_id, user_key_id, user_email, message_criteria):
        """Creates useful state updater.

    Args:
      task_key_id: Int unique id of the parent task.
      user_key_id: Int unique id of the user entity to update state.
      user_email: String reflecting the user email being accessed.
      message_criteria: String criteria (message-id) to recall.
    """
        self._state_updater = EntityStateUpdater(task_key_id=task_key_id,
                                                 user_key_id=user_key_id)
        self._state_updater.SetUserState(new_state=USER_RECALLING)
        self._gmail = GmailInterface()
        self._user_email = user_email
        self._message_criteria = message_criteria
Beispiel #4
0
class GmailHelper(object):
    """Abstracts Gmail operations for page handlers.

  Implemented as a context manager to support 'with ...' semantics.

  Uses GmailInterface and updates user state in the data store to reflect the
  success of the Gmail operations.
  """
    def __init__(self, task_key_id, user_key_id, user_email, message_criteria):
        """Creates useful state updater.

    Args:
      task_key_id: Int unique id of the parent task.
      user_key_id: Int unique id of the user entity to update state.
      user_email: String reflecting the user email being accessed.
      message_criteria: String criteria (message-id) to recall.
    """
        self._state_updater = EntityStateUpdater(task_key_id=task_key_id,
                                                 user_key_id=user_key_id)
        self._state_updater.SetUserState(new_state=USER_RECALLING)
        self._gmail = GmailInterface()
        self._user_email = user_email
        self._message_criteria = message_criteria

    def __enter__(self):
        """Wraps GmailInterface.Connect() with state updates.

    Returns:
      True if success else False.
    """
        if self._gmail.Connect(self._user_email):
            return self

        gmail_error_string = str(self.GetLastError())
        if _IMAP_DISABLED_STRING in gmail_error_string:
            new_state = USER_IMAP_DISABLED
        else:
            new_state = USER_CONNECT_FAILED
        self._state_updater.SetUserState(new_state=new_state)
        raise MessageRecallGmailError('Connection Error: %s.' %
                                      gmail_error_string)

    def CheckIfMessageExists(self):
        """Wraps GmailInterface.CheckIfMessageExists() with state updates.

    Returns:
      True if the search found at least one matching message.
    """
        result = self._gmail.CheckIfMessageExists(self._message_criteria)
        new_state = MESSAGE_FOUND if result else MESSAGE_NOT_FOUND
        self._state_updater.SetMessageState(new_state=new_state)
        return result

    def DeleteMessage(self):
        """Wraps GmailInterface.DeleteMessage() with state updates.

    Returns:
      True if message successfully purged and verified else False.
    """
        new_state = MESSAGE_DELETE_FAILED
        if self._gmail.DeleteMessage(self._message_criteria):
            self._state_updater.SetMessageState(new_state=MESSAGE_PURGED)
            if self._gmail.CheckIfMessageExists(self._message_criteria):
                new_state = MESSAGE_VERIFY_FAILED
            else:
                new_state = MESSAGE_VERIFIED_PURGED
        self._state_updater.SetMessageState(new_state=new_state)
        return new_state == MESSAGE_VERIFIED_PURGED

    def GetLastError(self):
        """Helper to retrieve last error info if any."""
        return self._gmail.GetLastError()

    def __exit__(self, exc_type, exc_value, exc_traceback):
        """Wraps GmailInterface.Disconnect() with state updates.

    Supplies exception information in case Exception suppression is desired.

    Args:
      exc_type: Type of Exception if an Exception to be raised.
      exc_value: Value of Exception if an Exception to be raised.
      exc_traceback: To be used in Exception processing.
    """
        self._gmail.Disconnect()
        self._state_updater.SetUserState(new_state=USER_DONE)