コード例 #1
0
  def _handle_timeline_notification(self, data):
    """Handle timeline notification.

    This method handles when a user chooses to perform a custom menu optio
    (increment, decrement, reset).
    """
    # TODO: Race conditions occur if a user rapidly selects a menu item many
    #       times (concurrently reading and writing the same timeline item).
    #       Resolve this issue
    for user_action in data.get('userActions', []):
      logging.info(user_action)
      option = user_action.get('payload')
      if user_action.get('type') == 'CUSTOM' and option in PROCESS_OPTIONS:
        data = json.loads(self.request.body)
        item = self.mirror_service.timeline().get(id=data['itemId']).execute()
        num = util.get_num(custom_item_fields.get(item, 'num'))
        # possible actions are functions listed in PROCESS_OPTIONS
        custom_item_fields.set(
            item, 'num',
            PROCESS_OPTIONS[option](num), TIMELINE_ITEM_TEMPLATE_URL)
        if 'notification' in item:
          item.pop('notification')
        self.mirror_service.timeline().update(
            id=data['itemId'], body=item).execute()
	# Only handle the first successful action.
        break
      else:
        logging.info(
            "I don't know what to do with this notification: %s", user_action)
コード例 #2
0
  def _reset_counter(self):
    """Resets the counter for given timeline item."""
    item = self.mirror_service.timeline().get(
        id=self.request.get('itemId')).execute()

    item = custom_item_fields.set(
        item, 'num', 0, TIMELINE_ITEM_TEMPLATE_URL)

    if 'notification' in item:
      item.pop('notification')
    self.mirror_service.timeline().update(
        id=self.request.get('itemId'), body=item).execute()
    return 'Counter Reset'