Python google.appengine.api.taskqueue.add() Examples

The following are code examples for showing how to use google.appengine.api.taskqueue.add(). They are from open source Python projects. You can vote up the examples you like or vote down the ones you don't like.

Example 1
Project: Utmost_Bot   Author: jiachen247   File: main.py    GNU General Public License v3.0 6 votes vote down vote up
def post(self):
        self.response.write("mass message")

        # send_message(CREATOR_ID, weeksary_message,markdown=True)

        # def queue_photo(user, uid):
        #     taskqueue.add(url='/photo', payload=uid)
        # #     logging.info(LOG_ENQUEUED.format_to_message('photo', uid, user.get_description()))
        # try:
        #     query = User.all()
        #     for user in query.run(batch_size=3000):
        #         send_message(user,)
        #
        # except Exception as e:
        #     logging.error(e)
        #     send_message(CREATOR_ID, "MASS MESSAGE FAILURE:: " + str(e))
        # else:
        #     send_message(CREATOR_ID, "MASS MESSAGE SUCCESS::") 
Example 2
Project: loaner   Author: google   File: base_model.py    Apache License 2.0 6 votes vote down vote up
def stream_to_bq(self, user, summary, timestamp=None):
    """Creates a task to stream an update to BigQuery.

    Args:
      user: string user email of the acting user.
      summary: string summary of the action being performed.
      timestamp: datetime, if not provided current time will be used.
    """
    if not timestamp:
      timestamp = datetime.datetime.utcnow()
    calling_function = inspect.stack()[1][3]
    task_params = {
        'model_instance': self,
        'timestamp': timestamp,
        'actor': user,
        'method': calling_function,
        'summary': summary,
    }
    taskqueue.add(
        queue_name='stream-bq',
        payload=pickle.dumps(task_params),
        target='default') 
Example 3
Project: loaner   Author: google   File: base_model.py    Apache License 2.0 6 votes vote down vote up
def add_docs_to_index(cls, documents):
    """Adds a list of documents to a particular index.

    Args:
      documents: a list of search.Documents to add to the class' index.
    """
    index = cls.get_index()
    for doc in documents:
      try:
        index.put(doc)
      except search.PutError as err:
        result = err.results[0]
        if result.code == search.OperationResult.TRANSIENT_ERROR:
          index.put(doc)
      except (search.Error, apiproxy_errors.OverQuotaError):
        logging.error(_PUT_DOC_ERR_MSG, doc, index) 
Example 4
Project: loaner   Author: google   File: send_email.py    Apache License 2.0 6 votes vote down vote up
def _send_email(**kwargs):
  """Sends email using App Engine's email API.

  Args:
    **kwargs: kwargs for the email api.
  """
  kwargs['sender'] = constants.SEND_EMAIL_AS
  if not constants.ON_PROD:
    if constants.ON_DEV:
      kwargs['subject'] = '[dev] ' + kwargs['subject']
    elif constants.ON_LOCAL:
      kwargs['subject'] = '[local] ' + kwargs['subject']
    elif constants.ON_QA:
      kwargs['subject'] = '[qa] ' + kwargs['subject']

  taskqueue.add(queue_name='send-email', params=kwargs, target='default') 
Example 5
Project: loaner   Author: google   File: events.py    Apache License 2.0 6 votes vote down vote up
def get_actions_for_event(event_name):
  """Gets all Action mappings for a given Event.

  Args:
    event_name: str, the name of the Event.

  Returns:
    A list of actions for the Event, or None.

  Raises:
    NoEventsError: if there are no Events to be found in datastore.
  """
  all_mappings = get_all_event_action_mappings()
  if not all_mappings:
    raise NoEventsError(
        'There are no events; run bootstrap to add the default ones.')

  return all_mappings.get(event_name) 
Example 6
Project: sndlatr   Author: Schibum   File: models.py    Apache License 2.0 6 votes vote down vote up
def add_to_queue(self, url=None, target_state='queued', countdown=0):
        """
        Adds job to task queue and transactionally updates state to 'queued'
        and saves job.
        Does nothing if state is not 'scheduled'.
        """
        if self.state != 'scheduled':
            logging.warn('tried to add job {} with state {}, to queue, '
                         'doing nothing'.format(self.key, self.state))
            return
        if url is None:
            url = self.queue_url
        logging.debug(u'scheduling job {} for {}'.format(self.key,
                                                        self.user_email))
        taskqueue.add(url=url,
                      payload=json.dumps({'key': self.key.urlsafe()}),
                      queue_name=self.queue_name,
                      countdown=countdown,
                      transactional=True)
        self.state = target_state
        self.put() 
Example 7
Project: experts-app-backend   Author: GoogleDeveloperExperts   File: update_gplus.py    Apache License 2.0 6 votes vote down vote up
def get(self):
        """Create tasks."""
        logging.info('crons/new_gplus')

        accounts = Account.query()
        user_count = 0
        for account in accounts:
            # only process valid account types
            if account.type not in VALID_ACCOUNT_TYPES:
                continue
            # uncomment this for testing against Patrick's account
            # if account.gplus_id != "117346385807218227082":
            #     continue

            user_count += 1
            taskqueue.add(queue_name='gplus',
                          url='/tasks/new_gplus',
                          params={'gplus_id': account.gplus_id})

        logging.info('crons/new_gplus created tasks for %s users' % user_count) 
Example 8
Project: experts-app-backend   Author: GoogleDeveloperExperts   File: update_gplus.py    Apache License 2.0 6 votes vote down vote up
def get(self):
        """Create tasks."""
        logging.info('crons/new_gplus')

        accounts = Account.query()
        user_count = 0
        for account in accounts:
            # only process valid account types
            if account.type not in VALID_ACCOUNT_TYPES:
                continue
            # uncomment this for testing against a USER's account
            if account.gplus_id != "100280111838016730110":
                continue

            user_count += 1
            taskqueue.add(queue_name='gplus',
                          url='/tasks/new_gplus',
                          params={'gplus_id': account.gplus_id})

        logging.info('crons/new_gplus created tasks for %s users' % user_count) 
Example 9
Project: experts-app-backend   Author: GoogleDeveloperExperts   File: update_gplus.py    Apache License 2.0 6 votes vote down vote up
def get(self):
        """create tasks."""
        logging.info('crons/upd_gplus')

        # taskqueue.add(queue_name='gplus',
        #               url='/tasks/upd_gplus',
        #               params={'gplus_id':111820256548303113275})

        accounts = Account.query()
        user_count = 0
        for account in accounts:
            # only process valid account types
            if account.type not in VALID_ACCOUNT_TYPES:
                continue
            # uncomment this for testing against Patrick's account
            # if account.gplus_id != "117346385807218227082":
            #     continue

            user_count += 1
            taskqueue.add(queue_name='gplus',
                          url='/tasks/upd_gplus',
                          params={'gplus_id': account.gplus_id})

        logging.info('crons/upd_gplus created tasks for %s users' % user_count) 
Example 10
Project: conf   Author: XonqNopp   File: e101_example.py    GNU General Public License v3.0 6 votes vote down vote up
def get(self, user_id, encoded_email, token):
        verify = models.User.get_by_auth_token(int(user_id), token)
        email = utils.decode(encoded_email)
        if verify[0] is None:
            self.add_message('There was an error or the link is outdated. Please copy and paste the link from your email.', 'warning')
            self.redirect_to('home')

        else:
            # save new email
            user = verify[0]
            user.email = email
            user.put()
            # delete token
            models.User.delete_auth_token(int(user_id), token)
            # add successful message and redirect
            self.add_message("Your email has been successfully updated!", "success")
            self.redirect_to('edit-profile') 
Example 11
Project: roger-api   Author: rogertalk   File: jobs.py    MIT License 6 votes vote down vote up
def delete_chunks():
    cursor = datastore_query.Cursor(urlsafe=request.args.get('cursor'))
    delete_before = int(request.args['delete_before'])
    threshold = datetime.utcfromtimestamp(delete_before)
    query = models.Chunk.query(models.Chunk.end < threshold,
                               models.Chunk.persist == False)
    keys, next_cursor, more = query.fetch_page(1000, keys_only=True, start_cursor=cursor)
    if next_cursor and more:
        # There are still more chunks to delete after this batch.
        taskqueue.add(
            method='GET',
            url='/_ah/jobs/delete_chunks',
            params={'cursor': next_cursor.urlsafe(), 'delete_before': delete_before},
            queue_name=config.DELETE_CHUNKS_QUEUE_NAME)
    # Delete the chunks.
    ndb.delete_multi(keys)
    logging.info('Deleted %d expired chunks', len(keys))
    return '' 
Example 12
Project: roger-api   Author: rogertalk   File: api.py    MIT License 6 votes vote down vote up
def post_profile_me_requests(session):
    try:
        delay = int(flask_extras.get_parameter('delay'))
        assert delay > 0
    except:
        raise errors.InvalidArgument('Specify a delay greater than zero')
    content_key, content = utils.get_or_create_content_from_request()
    if not content_key:
        raise errors.ResourceNotFound('That content does not exist')
    if not content:
        content = content_key.get()
    if not content or not content.is_public:
        raise errors.ResourceNotFound('That content does not exist')
    taskqueue.add(url='/_ah/jobs/content_request_send',
                  countdown=delay,
                  params={'requester_id': config.REACTION_CAM_ID,
                          'account_id': session.account_id,
                          'content_id': content.key.id()},
                  queue_name=config.INTERNAL_QUEUE)
    return {'success': True} 
Example 13
Project: roger-api   Author: rogertalk   File: api.py    MIT License 6 votes vote down vote up
def post_requests(session):
    # Note: This endpoint sends content requests as notifications.
    identifier_list = flask_extras.get_parameter_list('identifier')
    account_keys = models.Account.resolve_keys(identifier_list)
    if not all(account_keys):
        raise errors.InvalidArgument('One or more "identifier" arguments were invalid')
    account_keys = filter(lambda k: k not in session.account.blocked_by, account_keys)
    if not account_keys:
        return {'success': True}
    content_key, content = utils.get_or_create_content_from_request()
    if not content_key:
        raise errors.ResourceNotFound('That content does not exist')
    taskqueue.add(url='/_ah/jobs/content_request_send',
                  params={'requester_id': session.account_id,
                          'account_id': [k.id() for k in account_keys],
                          'comment': flask_extras.get_parameter('comment') or '',
                          'content_id': content_key.id()},
                  queue_name=config.INTERNAL_QUEUE)
    return {'success': True} 
Example 14
Project: roger-api   Author: rogertalk   File: api.py    MIT License 6 votes vote down vote up
def post_streams_stream_id_participants(session, stream_id):
    """
    Adds one or more participants to the specified stream.

    Request:
        POST /v1/streams/12345678/participants?participant=87654321
        Authorization: Bearer 5ZVhC41gMAqjpno3ELKc5pLtcAbQAPO1bWJf6hCMFW8T20s

    Response:
        {...}

    """
    stream = streams.get_by_id(session.account, stream_id)
    participants = map(accounts.Resolver.parse, flask_extras.get_parameter_list('participant'))
    account_keys = set()
    for participant in participants:
        account_key = participant.get_or_create_account_key()
        if not account_key:
            # TODO: Create temporary accounts for participants without accounts.
            raise errors.NotSupported('Cannot add external users to a stream (yet)')
        account_keys.add(account_key)
    stream.invite(account_keys)
    return stream 
Example 15
Project: hashtagtodo   Author: slackpad   File: admin_user.py    MIT License 6 votes vote down vote up
def freemium():
    if request.method == 'POST':
        operation = request.form.get('operation')
        if operation == "add":
            addresses = [email.strip() for email in request.form.get('addresses', '').split(',')]
            for email in addresses:
                Freemium.create_or_update(email)
        elif operation == 'remove':
            prefix = 'remove-'
            for key in request.form:
                if key.startswith(prefix):
                    email = key[len(prefix):]
                    entry = Freemium.get_by_email(email)
                    if entry is not None:
                        entry.key.delete()
        else:
            flash('Unknown operation')

    entries = Freemium.get_all()
    entries = [entry.key.id() for entry in entries]
    return render_template('admin-freemium.html', entries=entries) 
Example 16
Project: python-docs-samples   Author: GoogleCloudPlatform   File: taskqueue.py    Apache License 2.0 6 votes vote down vote up
def get(self, namespace='default'):
        # Queue task to update global counter.
        current_global_count = get_count('counter')
        taskqueue.add(
            url='/tasks/counter',
            params={'counter_name': 'counter'})

        # Queue task to update counter in specified namespace.
        previous_namespace = namespace_manager.get_namespace()
        try:
            namespace_manager.set_namespace(namespace)
            current_namespace_count = get_count('counter')
            taskqueue.add(
                url='/tasks/counter',
                params={'counter_name': 'counter'})
        finally:
            namespace_manager.set_namespace(previous_namespace)

        self.response.write(
            'Counters will be updated asyncronously.'
            'Current values: Global: {}, Namespace {}: {}'.format(
                current_global_count, namespace, current_namespace_count)) 
Example 17
Project: python-docs-samples   Author: GoogleCloudPlatform   File: application.py    Apache License 2.0 6 votes vote down vote up
def post(self):
        amount = int(self.request.get('amount'))

        task = taskqueue.add(
            url='/update_counter',
            target='worker',
            params={'amount': amount})

        self.response.write(
            'Task {} enqueued, ETA {}.'.format(task.name, task.eta))


# AsyncEnqueueTaskHandler behaves the same as EnqueueTaskHandler, but shows
# how to queue the task using the asyncronous API. This is not wired up by
# default. To use this, change the MainPageHandler's form action to
# /enqueue_async 
Example 18
Project: luci-py   Author: luci   File: change_log.py    Apache License 2.0 6 votes vote down vote up
def enqueue_process_change_task(auth_db_rev):
  """Transactionally adds a call to 'process_change' to the task queue.

  Pins the task to currently executing version of BACKEND_MODULE module
  (defined in config.py).

  Added as AuthDB commit callback in get_backend_routes() below.
  """
  assert ndb.in_transaction()
  conf = config.ensure_configured()
  try:
    # Pin the task to the module and version.
    taskqueue.add(
        url='/internal/auth/taskqueue/process-change/%d' % auth_db_rev,
        queue_name=conf.PROCESS_CHANGE_TASK_QUEUE,
        headers={'Host': modules.get_hostname(module=conf.BACKEND_MODULE)},
        transactional=True)
  except Exception as e:
    logging.error(
        'Problem adding "process-change" task to the task queue (%s): %s',
        e.__class__.__name__, e)
    raise 
Example 19
Project: luci-py   Author: luci   File: change_log.py    Apache License 2.0 6 votes vote down vote up
def enqueue_process_change_task(auth_db_rev):
  """Transactionally adds a call to 'process_change' to the task queue.

  Pins the task to currently executing version of BACKEND_MODULE module
  (defined in config.py).

  Added as AuthDB commit callback in get_backend_routes() below.
  """
  assert ndb.in_transaction()
  conf = config.ensure_configured()
  try:
    # Pin the task to the module and version.
    taskqueue.add(
        url='/internal/auth/taskqueue/process-change/%d' % auth_db_rev,
        queue_name=conf.PROCESS_CHANGE_TASK_QUEUE,
        headers={'Host': modules.get_hostname(module=conf.BACKEND_MODULE)},
        transactional=True)
  except Exception as e:
    logging.error(
        'Problem adding "process-change" task to the task queue (%s): %s',
        e.__class__.__name__, e)
    raise 
Example 20
Project: luci-py   Author: luci   File: change_log.py    Apache License 2.0 6 votes vote down vote up
def enqueue_process_change_task(auth_db_rev):
  """Transactionally adds a call to 'process_change' to the task queue.

  Pins the task to currently executing version of BACKEND_MODULE module
  (defined in config.py).

  Added as AuthDB commit callback in get_backend_routes() below.
  """
  assert ndb.in_transaction()
  conf = config.ensure_configured()
  try:
    # Pin the task to the module and version.
    taskqueue.add(
        url='/internal/auth/taskqueue/process-change/%d' % auth_db_rev,
        queue_name=conf.PROCESS_CHANGE_TASK_QUEUE,
        headers={'Host': modules.get_hostname(module=conf.BACKEND_MODULE)},
        transactional=True)
  except Exception as e:
    logging.error(
        'Problem adding "process-change" task to the task queue (%s): %s',
        e.__class__.__name__, e)
    raise 
Example 21
Project: luci-py   Author: luci   File: change_log.py    Apache License 2.0 6 votes vote down vote up
def enqueue_process_change_task(auth_db_rev):
  """Transactionally adds a call to 'process_change' to the task queue.

  Pins the task to currently executing version of BACKEND_MODULE module
  (defined in config.py).

  Added as AuthDB commit callback in get_backend_routes() below.
  """
  assert ndb.in_transaction()
  conf = config.ensure_configured()
  try:
    # Pin the task to the module and version.
    taskqueue.add(
        url='/internal/auth/taskqueue/process-change/%d' % auth_db_rev,
        queue_name=conf.PROCESS_CHANGE_TASK_QUEUE,
        headers={'Host': modules.get_hostname(module=conf.BACKEND_MODULE)},
        transactional=True)
  except Exception as e:
    logging.error(
        'Problem adding "process-change" task to the task queue (%s): %s',
        e.__class__.__name__, e)
    raise 
Example 22
Project: Flask-Python-GAE-Login-Registration   Author: orymeyer   File: main.py    Apache License 2.0 5 votes vote down vote up
def queue():
    taskqueue.add(url='/fetch')
    return "Queued." 
Example 23
Project: iris   Author: doitintl   File: main.py    MIT License 5 votes vote down vote up
def schedule():
    """
    Checks if it's time to run a schedule.
    Returns:
    """
    logging.info("Nothing here")
    projects = gcp.get_all_projetcs()
    for project in sorted(projects, key=lambda x: x['name']):
        project_id = str(project['projectId'])
        service_list = gcp.list_services(project_id)
        logging.debug("Creating deferred task for %s", project_id)
        for plugin in Plugin.plugins:
            if utils.is_service_enbaled(service_list, plugin.api_name()):
                store(plugin.__class__.__name__, plugin)
                task = taskqueue.add(queue_name='iris-tasks',
                                     url="/tasks/do_tag",
                                     method='GET',
                                     params={
                                         'project_id': project_id,
                                         'plugin': plugin.__class__.__name__,
                                     })
                logging.debug('Task %s for %s enqueued, ETA %s.', task.name,
                              plugin.__class__.__name__, task.eta)
            else:
                logging.debug("Service %s is not enabled", plugin.api_name())
    return 'ok', 200 
Example 24
Project: Utmost_Bot   Author: jiachen247   File: main.py    GNU General Public License v3.0 5 votes vote down vote up
def get(self):
        if not self.run():
            taskqueue.add(url='/send') 
Example 25
Project: Utmost_Bot   Author: jiachen247   File: main.py    GNU General Public License v3.0 5 votes vote down vote up
def get(self):
        taskqueue.add(url='/promo', method="POST") 
Example 26
Project: Utmost_Bot   Author: jiachen247   File: main.py    GNU General Public License v3.0 5 votes vote down vote up
def get(self):
        taskqueue.add(url='/mass') 
Example 27
Project: Utmost_Bot   Author: jiachen247   File: main.py    GNU General Public License v3.0 5 votes vote down vote up
def get(self):
        try:
            query = User.all()
            query.filter('active =', False)
            for user in query.run(batch_size=3000):
                uid = str(user.get_uid())
                taskqueue.add(url='/verify', payload=uid)
            self.response.headers['Content-Type'] = 'text/plain'
            self.response.write('Cleanup in progress\n')
        except Exception as e:
            logging.error(e) 
Example 28
Project: loaner   Author: google   File: process_action.py    Apache License 2.0 5 votes vote down vote up
def post(self):
    """Process an async Action task with the correct Action class."""
    payload = pickle.loads(self.request.body)
    async_actions = payload.pop('async_actions')
    action_name = async_actions.pop(0)
    action_instance = self.actions['async'].get(action_name)
    if action_instance:
      try:
        action_instance.run(**payload)
      # pylint: disable=broad-except, because this logic, in which tasks are
      # responsible for spawning subsequent tasks, creates a chain that could be
      # interrupted by any conceivable exception in an action's run method. This
      # handling ensures any further tasks will run.
      except Exception as error:
        logging.exception(
            'Failed to run async Action %r due to error: %r',
            action_name, str(error))
      # pylint: enable=broad-except
    else:
      logging.error('No async Action named %s found.', action_name)

    if async_actions:
      payload['async_actions'] = async_actions
      taskqueue.add(
          queue_name='process-action',
          payload=pickle.dumps(payload),
          target='default') 
Example 29
Project: kay-template   Author: yosukesuzuki   File: models.py    MIT License 5 votes vote down vote up
def create_inactive_user(cls, user_name, password, email, send_email=True,
                           do_registration=True):
    import datetime
    from kay.registration.models import RegistrationProfile
    from google.appengine.api import taskqueue
    def txn():
      key_name = cls.get_key_name(user_name)
      user = cls.get_by_key_name(key_name)
      if user:
        from kay.auth import DuplicateKeyError
        raise DuplicateKeyError(_(u"This user name is already taken."
                                  " Please choose another user name."))
      if do_registration:
        salt = crypto.gen_salt()
        activation_key = crypto.sha1(salt+user_name).hexdigest()
        profile_key = db.Key.from_path(cls.kind(), key_name,
                                       RegistrationProfile.kind(),
                                       activation_key)

        expiration_date = datetime.datetime.now() + \
            datetime.timedelta(seconds=settings.ACCOUNT_ACTIVATION_DURATION)
        taskqueue.add(url=url_for('_internal/expire_registration',
                                  registration_key=str(profile_key)),
                      eta=expiration_date, transactional=True)
        taskqueue.add(url=url_for('_internal/send_registration_confirm',
                                  registration_key=str(profile_key)),
                      transactional=True)
        user = cls(key_name=key_name, activated=False, user_name=user_name,
                   password=crypto.gen_pwhash(password), email=email)
        profile = RegistrationProfile(user=user, parent=user,
                                      key_name=activation_key)
        db.put([profile, user])
      else:
        user = cls(key_name=key_name, activated=False, user_name=user_name,
                   password=crypto.gen_pwhash(password), email=email)
        db.put(user)
      return user
    user = db.run_in_transaction(txn)
    return user 
Example 30
Project: kay-template   Author: yosukesuzuki   File: models.py    MIT License 5 votes vote down vote up
def get_new_session(cls, user, data=None, auto_delete=True,
                      countdown=settings.TEMPORARY_SESSION_LIFETIME,
                      additional_tq_url=None, tq_kwargs={}):
    from kay.utils import crypto
    from google.appengine.api import taskqueue
    def txn(id):
      key_name = cls.get_key_name(id)
      if additional_tq_url is not None:
        tq_kwargs.update({'session_key': db.Key.from_path(cls.kind(),
                                                          key_name)})
        taskqueue.add(url=url_for(additional_tq_url, **tq_kwargs),
                      transactional=True)
      taskqueue.add(url=url_for("_internal/expire_temporary_session",
                                session_key=db.Key.from_path(cls.kind(),
                                                             key_name)),
                    countdown=countdown, transactional=True)
      session = cls.get_by_key_name(key_name)
      if session is None:
        if data:
          session = cls(key_name=key_name, user=user, data=data)
        else:
          session = cls(key_name=key_name, user=user)
        session.put()
        return session
      else:
        raise db.Rollback("The specified id already exists.")

    id = crypto.new_iid()
    session = db.run_in_transaction(txn, id)
    while session is None:
      id = crypto.new_iid()
      session = db.run_in_transaction(txn, id)
    return session 
Example 31
Project: experts-app-backend   Author: GoogleDeveloperExperts   File: so_tasks.py    Apache License 2.0 5 votes vote down vote up
def get(self):
        """Create tasks."""
        logging.info('crons/harvest_so')

        # harvest a period i.e. from January 1st INITIAL RUN
        # num_months = 1
        # start_date = date(2015, 7, 1)

        # normal monthly harvest >> HAS TO RUN ON THE FIRST OF THE MONTH
        if date.today().day != 1:
            logging.info(
                'Stack Overflow Harvesting MUST be run on first of the month : EXITING')
            return

        num_months = 1
        start_date = date.today() - monthdelta(1)

        accounts = Account.query(Account.so_id != None)
        user_count = 0
        for account in accounts:
            # only process valid account types
            if account.type not in VALID_ACCOUNT_TYPES:
                continue
            # uncomment this for testing against Patrick's account
            # if account.gplus_id != "117346385807218227082": 109290164663410811176
            # if account.gplus_id != "109290164663410811176":
            #     continue

            user_count += 1

            for i in range(0, num_months):
                start = start_date + monthdelta(i)
                end = start + monthdelta(1)
                taskqueue.add(queue_name='gplus',
                              url='/tasks/harvest_so',
                              params={'key': account.key.urlsafe(), 'from': str(start), 'to': str(end)})

        logging.info(
            'crons/harvest_so created tasks for {} users from {} to {}'.format(user_count, start, end)) 
Example 32
Project: RSSNewsGAE   Author: liantian-cn   File: background.py    Apache License 2.0 5 votes vote down vote up
def launch_fetch():
    feeds = get_quarterly_feed_key()
    now = datetime.now(tz=app.config["TIME_ZONE"])
    if now.minute / 15 == 3:
        feeds += get_hourly_feed_key()
    if now.hour == 9 and now.minute / 15 == 1:
        feeds += get_daily_feed_key()
    for feed in feeds:
        taskqueue.add(queue_name='fetch-queue',
                      url=url_for("fetch_one_feed"),
                      method='POST',
                      params={"key": feed.urlsafe()}
                      )
    return "Done", 200 
Example 33
Project: RSSNewsGAE   Author: liantian-cn   File: background.py    Apache License 2.0 5 votes vote down vote up
def fetch_one_feed():
    key = request.values.get('key')
    feed = ndb.Key(urlsafe=key).get()
    parser = url2parser(feed.url)
    if parser is not None:
        the_last_fetch = feed.latest_fetch
        feed.latest_fetch = datetime.now()
        list_of_news_entities = []
        ndb.put_multi(list_of_news_entities)
        for entry in parser.entries:
            if check_entry(entry):
                entry.published = datetime.fromtimestamp(mktime(entry.published_parsed))
                if entry.published > the_last_fetch:
                    news_entry = NewsEntry()
                    news_entry.published = entry.published
                    news_entry.title = entry.title
                    news_entry.link = entry.link
                    news_entry.summary = clean_html(entry.summary)
                    news_entry.feed = feed.title
                    list_of_news_entities.append(news_entry)
        feed.put()
        news_key_list = ndb.put_multi(list_of_news_entities)
        for news_key in news_key_list:
            taskqueue.add(queue_name='collect-queue',
                          url=url_for("collect_keyword_for_one_news"),
                          method='POST',
                          params={"key": news_key.urlsafe()}
                          )
        return "Done", 200
    else:
        return "parser is None", 200 
Example 34
Project: RSSNewsGAE   Author: liantian-cn   File: background.py    Apache License 2.0 5 votes vote down vote up
def collect_keyword_for_one_news():
    user_key_word = get_pure_keyword()
    key = request.values.get('key')
    news = ndb.Key(urlsafe=key).get()
    form_fields = {
        "text": news.summary.encode("utf-8"),
        "topK": app.config["TOP_KEYWORD"],
        "withWeight": 0
    }
    form_data = urllib.urlencode(form_fields)
    result = urlfetch.fetch(url=app.config["JIEBA_API"],
                            payload=form_data,
                            method=urlfetch.POST,
                            headers={'Content-Type': 'application/x-www-form-urlencoded'},
                            follow_redirects=False)
    json_content = json.loads(result.content)
    key_words = json_content["result"]
    del news.key_word[:]
    news.key_word = key_words
    tmp = [val for val in key_words if val in user_key_word]
    if tmp:
        news.important = True
    if tmp and app.config["PUSHOVER"]:
        taskqueue.add(queue_name='push-msg-queue',
                      url=url_for("push_important_news"),
                      method='POST',
                      params={"key": key})
    news.put()
    return "Done", 200 
Example 35
Project: TimeSync   Author: DanForever   File: base.py    Apache License 2.0 5 votes vote down vote up
def UpdateSubscription( self, sub ):
		pebbleToken = sub.key().name()
		
		logging.debug( "UpdateSubscription(): " + str( pebbleToken ) )
		
		# Grab the URL for updating the user's agenda
		url = self.GetUrl \
		(
			"mainhandler",
			{
				'handler'	: "tvshowtime",
				'branch'	: "agenda",
				'action'	: "update"
			},
			full = False,
			scheme = None
		)
		
		# Use this to make the name of the task vaguely unique, assuming we don't
		# want to allow the task to execute more frequently than once a minute
		nameDate = datetime.utcnow().strftime( "Date%Y-%m-%dT%H-%M-" )
		
		config = \
		{
			'name' : "TVST-Sub-" + nameDate + str( pebbleToken ),
			'url' : url,
			'method' : "POST",
			'headers' :
			{
				'X-User-Token' : pebbleToken
			}
		}
		
		logging.debug( "Creating Task: " + str( config ) )
		
		try:
			taskqueue.add( **config )
		except ( taskqueue.TaskAlreadyExistsError, taskqueue.TombstonedTaskError ) as e:
			logging.warning( "Couldn't create task due to name conflict with previously created task: " + str( e ) )
			return ( requests.codes.too_many_requests, { 'status' : "Too soon" } )
		return ( requests.codes.ok, { 'status' : "success" } ) 
Example 36
Project: roger-api   Author: rogertalk   File: streams.py    MIT License 5 votes vote down vote up
def join(self, account, reason='unknown', **kwargs):
        # TODO: Do something with reason parameter.
        # TODO: Decide on 1:1 restrictions.
        account = models.Account.resolve(account)
        self._account_map[account.key] = account
        # Create a stream object specifically for the account that joined.
        stream = MutableStream(account, self._stream, account_map=self._account_map,
                               chunks=self._chunks)
        if not stream.participant:
            stream._set_participants(add=[account], **kwargs)
        return stream 
Example 37
Project: roger-api   Author: rogertalk   File: streams.py    MIT License 5 votes vote down vote up
def __init__(self, me, stream, disable_autojoin=False, **kwargs):
        super(MutableStream, self).__init__(stream, **kwargs)
        self._autojoin_disabled = disable_autojoin
        self.account = models.Account.resolve(me)
        if not self.account:
            raise ValueError('Invalid account')
        if not self.participant and stream.service_content_id and not disable_autojoin:
            # The account is not a participant but may become one if they are whitelisted.
            s, t, content_id = models.Service.parse_identifier(stream.service_content_id)
            stream_team = (s, t)
            for identity in self.account.identifiers:
                try:
                    s, t, i = models.Service.parse_identifier(identity.id())
                except:
                    continue
                if (s, t) != stream_team:
                    continue
                # The special content id "*" means anyone can join the stream.
                if content_id != '*' and i not in stream.service_members:
                    continue
                # This account is allowed to join this stream.
                self._set_participants(add=[self.account])
                logging.debug('Auto-joined stream %d', stream.key.id())
                break
        if self.participant:
            self._account_map[self.account.key] = self.account 
Example 38
Project: roger-api   Author: rogertalk   File: streams.py    MIT License 5 votes vote down vote up
def invite(self, participants, **kwargs):
        # TODO: Decide on 1:1 restrictions.
        # TODO: We should probably send a request to the user instead of adding them.
        if not isinstance(participants, (list, set)):
            participants = [participants]
        participants = models.Account.resolve_keys(participants)
        # TODO: Blocking shouldn't stop the stream from existing, only prevent it from
        #       being visible to the blocker.
        for key in participants:
            if key in self.account.blocked_by:
                logging.info('Blocked Stream.invite from %s to %s', self.account.key, key)
                return
        self._set_participants(add=participants, **kwargs) 
Example 39
Project: roger-api   Author: rogertalk   File: streams.py    MIT License 5 votes vote down vote up
def send(self, payload, duration, client_id=None, export=False, mute_notification=False, text=None, **kwargs):
        if 'allow_duplicate' not in kwargs and self.account.status == 'bot':
            # Bots get to send duplicate content.
            kwargs['allow_duplicate'] = True
        if text:
            assert 'text_segments' not in kwargs
            segment = models.TextSegment(start=0, duration=duration, text=text)
            kwargs['text_segments'] = [segment]
        if self.account.location_info:
            kwargs['location'] = self.account.location_info.location
            kwargs['timezone'] = self.account.location_info.timezone
        if export and 'external_content_id' not in kwargs:
            # Create random content id first so it can be stored with the chunk.
            kwargs['external_content_id'] = models.ExportedContent.random_id()
        self._tx(models.Stream.add_chunk, self.account.key, payload, duration, **kwargs)
        chunk = self.chunks[-1]
        self.notify_change(notifs.ON_STREAM_CHUNK, chunk=chunk,
                           mute_notification=mute_notification)
        self._report('sent', duration=chunk.duration / 1000.0)
        if export or (self.service_content_id and not self.export_content_disabled):
            params = {
                'account_id': self.account.key.id(),
                'account_image_url': self.account.image_url or '',
                'account_name': self.account.display_name or '',
                'attachments': convert.to_json(chunk.attachments),
                'chunk_id': chunk.chunk_id,
                'client_id': client_id or '',
                'content_id': chunk.external_content_id or '',
                'destination': self.service_content_id or '',
                'duration': chunk.duration,
                'stream_id': self.key.id(),
                'stream_image_url': self.image_url or '',
                'stream_title': self.title or '',
                'text': convert.to_json(chunk.text_segments),
                'timestamp': convert.unix_timestamp_ms(chunk.start),
                'url': chunk.url,
            }
            taskqueue.add(url='/_ah/jobs/export_content', params=params,
                          queue_name=config.SERVICE_QUEUE_NAME) 
Example 40
Project: roger-api   Author: rogertalk   File: streams.py    MIT License 5 votes vote down vote up
def _set_participants(self, add=None, remove=None, **kwargs):
        params = dict(kwargs, remove=remove)
        if add:
            add_accounts = self._lookup_accounts(add)
            params['add'] = add_accounts
            params['owners'] = {}
            # All new bots will be owned by the user who added them.
            owner = self.participant.owner if self.account.is_bot else self.account.key
            for account in add_accounts:
                if not account.is_bot:
                    continue
                params['owners'][account.key] = owner
        # Update participants list and create a diff based on the changes.
        before = models.Account.resolve_keys(self.participants)
        self._tx(models.Stream.set_participants, **params)
        after = models.Account.resolve_keys(self.participants)
        # TODO: Make a difference of leaving vs. kicking and inviting vs. joining?
        # Notify added accounts that they joined.
        added = after - before
        for key in added:
            self._notify_one(key, notifs.ON_STREAM_JOIN, stream_id=self.key.id())
            self._report('joined' if key == self.account.key else 'added-participant')
        # Notify removed accounts that they left.
        removed = before - after
        for key in removed:
            # TODO: Report removed participants.
            self._notify_one(key, notifs.ON_STREAM_LEAVE, stream_id=self.key.id(),
                             ignore_visibility=True)
        # Notify everyone else of the changes.
        added_ids, removed_ids = [k.id() for k in added], [k.id() for k in removed]
        if added_ids or removed_ids:
            for key in after & before:
                # TODO: Include participants in this notif.
                self._notify_one(key, notifs.ON_STREAM_PARTICIPANTS,
                                 added=added_ids, removed=removed_ids,
                                 stream_id=self.key.id())
        if self.account.key not in after:
            # The current account was removed from the stream.
            self.account = None
            self._stream = None
            self._chunks = None 
Example 41
Project: roger-api   Author: rogertalk   File: accounts.py    MIT License 5 votes vote down vote up
def get_or_create(*args, **kwargs):
    if not args:
        raise ValueError('Need at least one identifier')
    args = map(identifiers.clean, args)
    notify_change = kwargs.pop('notify_change', True)
    # Find the first account matching an identifier and attempt to add the remaining
    # identifiers to it. If no account was found, create a new one with the available
    # identifiers added to it. Identifiers belonging to other accounts are ignored.
    identity_keys = [ndb.Key('Identity', identifier) for identifier in args]
    account_key = None
    claimables = []
    for identifier, identity in zip(args, ndb.get_multi(identity_keys)):
        if not identity or not identity.account:
            claimables.append(identifier)
            continue
        if account_key and identity.account != account_key:
            logging.warning('%r does not belong to account %d',
                            identifier, account_key.id())
            continue
        account_key = identity.account
    # TODO: Transaction?
    if account_key:
        logging.debug('Found account %d for %r', account_key.id(), args)
        handler = get_handler(account_key)
        if not handler.image_url and 'image' in kwargs:
            logging.debug('Updating image for %d', account_key.id())
            handler.set_image(kwargs['image'])
    else:
        handler = create(claimables.pop(0), **kwargs)
        logging.debug('Created account %d for %r', handler.account_id, args)
    for identifier in claimables:
        # TODO: Decide if/when to notify on connect for additional identifiers.
        handler.add_identifier(identifier, notify_change=False, notify_connect=False)
    # Only trigger one change notif, and only for existing accounts.
    if account_key and claimables and notify_change:
        handler._notify_account_change()
    return handler


# Runs additional logic when an account receives a chunk. 
Example 42
Project: roger-api   Author: rogertalk   File: accounts.py    MIT License 5 votes vote down vote up
def add_identifier(self, identifier, notify_change=True, notify_connect=True, **kwargs):
        identifier, identifier_type = identifiers.parse(identifier)
        identity, account = models.Identity.add(identifier, self.account.key, **kwargs)
        if not identity:
            if self.has_identifier(identifier):
                # Just assume the identifier is already owned by the current account.
                return
            raise errors.AlreadyExists('That identifier is already in use')
        # Brazil can use two formats for one phone number.
        equivalent = self._get_alternate_identifier(identity.key.id())
        if equivalent:
            i, a = models.Identity.add(equivalent, self.account.key)
            if i and a:
                identity, account = i, a
            else:
                logging.warning('Failed to reserve %r (based on %r) for %d',
                                equivalent, identifier, self.account.key.id())
        # Update in-memory instance to reflect reality.
        self.account.populate(**account.to_dict())
        if identifier_type == identifiers.EMAIL and self.account.is_activated:
            # Connect the email "service".
            _, team, user = identifiers.parse_service(identifier)
            self.connect_service('email', team, user, notify=notify_connect)
        elif identifier_type == identifiers.SERVICE_ID:
            service, team, user = identifiers.parse_service(identifier)
            if service == 'fika':
                # fika.io "service" always gets connected.
                self.connect_service('fika', team, user, notify=notify_connect)
        if notify_change:
            self._notify_account_change() 
Example 43
Project: roger-api   Author: rogertalk   File: accounts.py    MIT License 5 votes vote down vote up
def generate_username(self, base=None):
        """Generates a new username based on display name."""
        identifier = None
        new_username = base or random.username_generator()
        while True:
            identifier, account = models.Identity.add(new_username, self.account.key,
                                                      primary=bool(base))
            if identifier:
                logging.debug('Successfully added username %r', new_username)
                self.account.populate(**account.to_dict())
                self._notify_account_change()
                return
            logging.debug('Could not add username %r', new_username)
            new_username = random.username_generator(base) 
Example 44
Project: roger-api   Author: rogertalk   File: accounts.py    MIT License 5 votes vote down vote up
def on_new_service(self, auth, client_key, notify=True):
        params = {
            'account_id': self.account_id,
            'client_id': client_key.id(),
            'notify': 'true' if notify else 'false',
            'service_id': auth.service.id(),
            'team_id': auth.service_team.id() if auth.service_team else '',
            'resource': auth.service_identifier,
        }
        logging.debug('Queueing job to set up %s for %d', auth.key.id(), self.account_id)
        taskqueue.add(method='GET', url='/_ah/jobs/set_up_new_service', params=params,
                      queue_name=config.SERVICE_QUEUE_NAME) 
Example 45
Project: roger-api   Author: rogertalk   File: accounts.py    MIT License 5 votes vote down vote up
def set_username(self, new_username):
        new_username, identifier_type = identifiers.parse(new_username)
        if identifier_type != identifiers.USERNAME:
            # A user may not use this endpoint to add a phone number/e-mail.
            raise errors.InvalidArgument('A valid username must be provided')
        # Switch out the old username if it exists, otherwise just add the new one.
        old_username = self.username
        if old_username and self.account.primary_set:
            self.change_identifier(old_username, new_username, primary=True)
        else:
            self.add_identifier(new_username, primary=True) 
Example 46
Project: roger-api   Author: rogertalk   File: cron.py    MIT License 5 votes vote down vote up
def report_to_bigquery():
    """Flush all pending events of a certain type to BigQuery."""
    # Schedule multiple flush jobs per minute for some events.
    if request.method == 'GET':
        tasks = []
        for delay in xrange(0, 60, 5):
            tasks.append(taskqueue.Task(method='POST', url=request.path,
                                        countdown=delay,
                                        params={'event_name': 'content_vote_v1'}))
        tasks.append(taskqueue.Task(method='POST', url=request.path))
        taskqueue.Queue(config.BIGQUERY_CRON_QUEUE_NAME).add(tasks)
        return ''
    # Retrieve pending events from pull queue.
    try:
        q = taskqueue.Queue(config.BIGQUERY_QUEUE_NAME)
        tasks = q.lease_tasks_by_tag(config.BIGQUERY_LEASE_TIME.total_seconds(),
                                     config.BIGQUERY_LEASE_AMOUNT,
                                     tag=flask_extras.get_parameter('event_name'))
        logging.debug('Leased %d event(s) from %s', len(tasks), config.BIGQUERY_QUEUE_NAME)
    except taskqueue.TransientError:
        logging.warning('Could not lease events due to transient error')
        return '', 503
    if not tasks:
        return ''
    # Insert the events into BigQuery.
    table_id = tasks[0].tag
    rows = [json.loads(t.payload) for t in tasks]
    bigquery_client.insert_rows(table_id, rows)
    # Delete the tasks now that we're done with them.
    q.delete_tasks(tasks)
    return '' 
Example 47
Project: roger-api   Author: rogertalk   File: jobs.py    MIT License 5 votes vote down vote up
def set_up_new_service():
    # Extract all parameters from the request.
    account_id = int(request.args['account_id'])
    handler = accounts.get_handler(account_id)
    notify = flask_extras.get_flag('notify')
    service_key = models.Service.resolve_key(request.args['service_id'])
    team_id = request.args['team_id'] or None
    if team_id:
        team_key = models.ServiceTeam.resolve_key((service_key, team_id))
    else:
        team_key = None
    auth_key = models.ServiceAuth.resolve_key((account_id, service_key, team_key))
    client_id = request.args['client_id']
    resource = request.args['resource']
    # Perform team specific logic.
    if not team_key:
        # No team joined, so don't do anything.
        return ''
    if service_key.id() == 'email':
        # Don't run any team logic on public email domains.
        if team_key.id() in config.PUBLIC_EMAIL_DOMAINS:
            logging.debug('Not setting up public email domain %s', team_key.id())
            return ''
    if not notify:
        logging.debug('Not notifying team join because notify=False')
        return ''
    if client_id == 'fikaio':
        # Don't notify about users that log in on the fika.io website.
        # TODO: Don't hardcode it like this.
        logging.debug('Not notifying team join for fika.io login')
        return ''
    # Also kick off a job which notifies all team members.
    logging.debug('Queueing job to announce team join')
    params = request.args.to_dict()
    taskqueue.add(method='GET', url='/_ah/jobs/announce_team_join', params=params,
                  queue_name=config.SERVICE_QUEUE_NAME)
    return '' 
Example 48
Project: roger-api   Author: rogertalk   File: api.py    MIT License 5 votes vote down vote up
def post_device(session):
    """
    Add a user's device token so that they can receive push notifications.

    Request:
        POST /v1/device?device_token=abcdef&platform=ios
        Authorization: Bearer 5ZVhC41gMAqjpno3ELKc5pLtcAbQAPO1bWJf6hCMFW8T20s

    Response:
        {"success": true}

    """
    client_id, _ = auth.get_client_details()
    if client_id == 'fika':
        app = 'io.fika.Fika'
    elif client_id == 'reactioncam':
        app = 'cam.reaction.ReactionCam'
    else:
        raise errors.InvalidArgument('Unsupported client')
    user_agent = request.headers.get('User-Agent', '')
    models.Device.add(session.account,
                      app=app,
                      device_id=flask_extras.get_parameter('device_id'),
                      device_info=user_agent,
                      environment=flask_extras.get_parameter('environment'),
                      platform=flask_extras.get_parameter('platform'),
                      token=flask_extras.get_parameter('device_token'),
                      api_version=g.api_version)
    return {'success': True} 
Example 49
Project: roger-api   Author: rogertalk   File: api.py    MIT License 5 votes vote down vote up
def post_facebook_auth():
    client_id, _ = auth.get_client_details()
    access_token = flask_extras.get_parameter('access_token')
    if not access_token:
        raise errors.MissingArgument('A Facebook token is required')
    try:
        url = ('https://graph.facebook.com/v2.10/me?access_token=%s'
               '&fields=id,email,name,picture.type(large){url}')
        result = urlfetch.fetch(method=urlfetch.GET, url=url % (access_token,))
        data = json.loads(result.content)
        try:
            identifier = 'facebook:%s' % (data['id'],)
        except KeyError:
            logging.error('Unexpected data (HTTP %d): %r', result.status_code, data)
            raise
    except Exception as e:
        logging.exception('Fetching Facebook profile failed.')
        raise errors.InvalidCredentials('Invalid Facebook access token')
    session = auth.get_session()
    if session:
        handler = accounts.get_handler(session.account)
        handler.add_identifier(identifier)
    else:
        handler = accounts.get_or_create(identifier,
            image=data['picture']['data']['url'] if 'picture' in data else None)
        session = handler.create_session()
        taskqueue.add(url='/_ah/jobs/track_login',
                      countdown=20,
                      params={'account_id': handler.account_id,
                              'client_id': client_id,
                              'fb_token': access_token},
                      queue_name=config.INTERNAL_QUEUE)
    email = data.get('email')
    if email:
        try:
            handler.add_identifier(email)
        except:
            logging.exception('Could not add Facebook email:')
    g.public_options['include_extras'] = True
    g.public_options['view_account'] = session.account
    return session 
Example 50
Project: roger-api   Author: rogertalk   File: api.py    MIT License 5 votes vote down vote up
def post_profile_me_chat_announce(session):
    text = flask_extras.get_parameter('text')
    if not text:
        raise errors.InvalidArgument('Missing text')
    taskqueue.add(url='/_ah/jobs/chat_announce',
                  params={'owner_id': session.account_id,
                          'channel_id': str(session.account_id),
                          'text': text},
                  queue_name=config.INTERNAL_QUEUE,
                  retry_options=taskqueue.TaskRetryOptions(task_retry_limit=0))
    return {'success': True} 
Example 51
Project: roger-api   Author: rogertalk   File: api.py    MIT License 5 votes vote down vote up
def _handle_content_was_deleted(creator, content, related_to):
    task = taskqueue.Task(
        countdown=5,
        url='/_ah/jobs/content_was_deleted',
        params={'content_id': content.key.id(),
                'creator_id': creator.key.id(),
                'original_id': related_to.key.id() if related_to else ''})
    task.add(queue_name=config.INTERNAL_QUEUE) 
Example 52
Project: Conference-Organization-APP-UDACITY   Author: kongling893   File: conference.py    Apache License 2.0 5 votes vote down vote up
def addSessionToWishlist(self, request):
        """Add the session to the user's list of sessions they are interested in attending"""
        #get session key
        sessionKey = request.sessionKey
        # get session object
        session = ndb.Key(urlsafe=sessionKey).get()
        # check that session exists or not
        if not session:
            raise endpoints.NotFoundException(
                'No session found with key: %s' % sessionKey)

        user = endpoints.get_current_user()
        if not user:
            raise endpoints.UnauthorizedException('Authorization required')

        profile = self._getProfileFromUser()
        if not profile:
            raise endpoints.BadRequestException('Profile does not exist for user')
        # check if key and Session
        if not type(ndb.Key(urlsafe=sessionKey).get()) == Session:
            raise endpoints.NotFoundException('This key is not a Session instance')
        # add session to wishlist
        if sessionKey not in profile.sessionKeysInWishlist:
            try:
                profile.sessionKeysInWishlist.append(sessionKey)
                profile.put()
            except Exception:
                raise endpoints.InternalServerErrorException('Error in storing the wishlist')
        return self._copySessionToForm(session) 
Example 53
Project: hashtagtodo   Author: slackpad   File: jobs_api.py    MIT License 5 votes vote down vote up
def users_job():
    task_url = '/api/v1/queues/stats/rollup'
    taskqueue.add(url=task_url)
    return make_response() 
Example 54
Project: hashtagtodo   Author: slackpad   File: queues_api.py    MIT License 5 votes vote down vote up
def force_job():
    task_url = '/api/v1/queues/sync/user'
    for key in User.get_all_keys():
        params = {
            'user_id': key.urlsafe(),
            'force': 1,
        }
        taskqueue.add(url=task_url, params=params)

    return make_response() 
Example 55
Project: hashtagtodo   Author: slackpad   File: app_user.py    MIT License 5 votes vote down vote up
def todos():
    task_url = '/api/v1/queues/sync/user'
    params = {
        'user_id': g.user.key.urlsafe(),
        'force': 1,
    }
    taskqueue.add(url=task_url, params=params)
    return render_template('todos.html') 
Example 56
Project: hashtagtodo   Author: slackpad   File: app_user.py    MIT License 5 votes vote down vote up
def calendars():
    calendars = Calendar.get_all(g.user.key)
    if request.method == 'POST':
        if g.user.is_premium:
            enable_todolist = 'enable-todolist' in request.form
            if g.user.enable_todolist != enable_todolist:
                g.user.enable_todolist = enable_todolist
                g.user.put()
                if enable_todolist:
                    task_url = '/api/v1/queues/sync/user'
                    params = {
                        'user_id': g.user.key.urlsafe(),
                        'force': 1,
                    }
                    taskqueue.add(url=task_url, params=params)
                else:
                    try:
                        delete_todolist(g.user, g.client)
                    except:
                        pass

        for calendar in calendars:
            active_key = 'active-' + calendar.key.urlsafe()
            calendar.active = active_key in request.form

            todolist_key = 'todolist-' + calendar.key.urlsafe()
            calendar.show_in_todolist = todolist_key in request.form

            calendar.put()

        flash('Calendar settings saved.')

    return render_template('calendars.html', calendars=calendars) 
Example 57
Project: hashtagtodo   Author: slackpad   File: pipelines.py    MIT License 5 votes vote down vote up
def map(user):
        task_url = '/api/v1/queues/sync/user'
        params = {
            'user_id': user.key.urlsafe()
        }

        try:
            if user.synced is not None:
                taskqueue.add(url=task_url, params=params)
        except AttributeError:
            pass 
Example 58
Project: emdec-gtfs   Author: paulo-raca   File: emdec2gtfs.py    Do What The F*ck You Want To Public License 5 votes vote down vote up
def async_refresh(self):
        all_routes = self.get_route_list(nocache=True)
        for route in all_routes:
            taskqueue.add(url='/route/%s/json?nocache' % route, method='GET')
            taskqueue.add(url='/route/%s/gtfs?nocache' % route, method='GET')
        taskqueue.add(url='/route/all/gtfs?nocache', method='GET')
        return "Queued everything uncached" 
Example 59
Project: python-docs-samples   Author: GoogleCloudPlatform   File: main.py    Apache License 2.0 5 votes vote down vote up
def main_page():
    page_name = flask.request.args.get('page_name', 'default')
    response = """
        <html><body>
            <h2>Permanent note page: %s</h2>""" % cgi.escape(page_name)

    parent = parent_key(page_name)
    notes = Note.query(ancestor=parent).fetch(20)
    for note in notes:
        response += '<h3>%s</h3>' % cgi.escape(note.key.id())
        response += '<blockquote>%s</blockquote>' % cgi.escape(note.content)

    response += (
        """<hr>
           <form action="/add?%s" method="post">
           Submit Note: <input value="Title" name="note_title"><br>
           <textarea value="Note" name="note_text" rows="4" cols="60">
           </textarea>
           <input type="submit" value="Etch in stone"></form>"""
        % urllib.urlencode({'page_name': page_name}))
    response += """
            <hr>
            <form>Switch page: <input value="%s" name="page_name">
            <input type="submit" value="Switch"></form>
            </body>
        </html>""" % cgi.escape(page_name, quote=True)

    return response


# [START standard] 
Example 60
Project: python-docs-samples   Author: GoogleCloudPlatform   File: main.py    Apache License 2.0 5 votes vote down vote up
def insert_if_absent_taskq(note_key, note):
    taskqueue.add(url=flask.url_for('taskq_worker'), transactional=True)
    # do insert
    # [END taskq]
    fetch = note_key.get()
    if fetch is None:
        note.put()
        return True
    return False 
Example 61
Project: luci-py   Author: luci   File: handlers_endpoints_v1.py    Apache License 2.0 5 votes vote down vote up
def store_and_enqueue_verify_task(entry, task_queue_host):
  entry.put()
  taskqueue.add(
      url='/internal/taskqueue/verify/%s' % entry.key.id(),
      params={'req': os.environ['REQUEST_LOG_ID']},
      queue_name='verify',
      headers={'Host': task_queue_host},
      transactional=True,
  ) 
Example 62
Project: luci-py   Author: luci   File: handlers_endpoints_v1.py    Apache License 2.0 5 votes vote down vote up
def partition_collection(cls, entries):
    """Create sets of existent and new digests."""
    seen_unseen = [set(), set()]
    for digest, obj in cls.check_entries_exist(entries):
      if obj and obj.expanded_size != digest.size:
        # It is important to note that when a file is uploaded to GCS,
        # ContentEntry is only stored in the finalize call, which is (supposed)
        # to be called only after the GCS upload completed successfully.
        #
        # There is 3 cases:
        # - Race condition flow:
        #   - Client 1 preuploads with cache miss.
        #   - Client 2 preuploads with cache miss.
        #   - Client 1 uploads to GCS.
        #   - Client 2 uploads to GCS; note that both write to the same file.
        #   - Client 1 finalizes, stores ContentEntry and initiates the task
        #     queue.
        #   - Client 2 finalizes, overwrites ContentEntry and initiates a second
        #     task queue.
        # - Object was not yet verified. It can be either because of an almost
        #   race condition, as observed by second client after the first client
        #   finished uploading but before the task queue completed or because
        #   there is an issue with the task queue. In this case,
        #   expanded_size == -1.
        # - Two objects have the same digest but different content and it happen
        #   that the content has different size. In this case,
        #   expanded_size != -1.
        #
        # TODO(maruel): Using logging.error to catch these in the short term.
        logging.error(
            'Upload race.\n%s is not yet fully uploaded.', digest.digest)
        # TODO(maruel): Force the client to upload.
        #obj = None
      seen_unseen[bool(obj)].add(digest)
    logging.debug(
        'Hit:%s',
        ''.join(sorted('\n%s' % d.digest for d in seen_unseen[True])))
    logging.debug(
        'Missing:%s',
        ''.join(sorted('\n%s' % d.digest for d in seen_unseen[False])))
    return seen_unseen 
Example 63
Project: cas-eval   Author: varepsilon   File: main.py    Apache License 2.0 5 votes vote down vote up
def export():
    user = users.get_current_user()
    total_shared = Session.query(Session.shared == True).count()
    if user and users.is_current_user_admin():
        bucket_size = max(1, total_shared // (NUM_TASKS - 1))
        for i in range(NUM_TASKS):
            # start a task with delay of 60*i seconds
            taskqueue.add(url='/tasks/process_export', method='GET',
                    params={'bucket':  i, 'bucket_size': bucket_size}, countdown=60*i)
        return 'Trigerred for %d queries' % total_shared, 200
    else:
        return 'Admin access only', 403 
Example 64
Project: love   Author: Yelp   File: web.py    MIT License 5 votes vote down vote up
def import_employees():
    flash('We started importing employee data in the background. Refresh the page to see it.', 'info')
    taskqueue.add(url='/tasks/employees/load/csv')
    return redirect(url_for('employees')) 
Example 65
Project: love   Author: Yelp   File: tasks.py    MIT License 5 votes vote down vote up
def load_employees_from_s3():
    logic.employee.load_employees()
    # we need to rebuild the love count index as the departments may have changed.
    taskqueue.add(url='/tasks/love_count/rebuild')
    return Response(status=200)


# This task has a web UI to trigger it, so let's use POST 
Example 66
Project: AppEngineConferenceCenter   Author: lolitschen   File: conference.py    Apache License 2.0 4 votes vote down vote up
def _createConferenceObject(self, request):
        """Create or update Conference object, returning ConferenceForm/request."""
        # preload necessary data items
        user = endpoints.get_current_user()
        if not user:
            raise endpoints.UnauthorizedException('Authorization required')
        user_id = getUserId(user)

        if not request.name:
            raise endpoints.BadRequestException("Conference 'name' field required")

        # copy ConferenceForm/ProtoRPC Message into dict
        data = {field.name: getattr(request, field.name) for field in request.all_fields()}
        del data['websafeKey']
        del data['organizerDisplayName']

        # add default values for those missing (both data model & outbound Message)
        for df in DEFAULTS:
            if data[df] in (None, []):
                data[df] = DEFAULTS[df]
                setattr(request, df, DEFAULTS[df])

        # if data['maxAttendees'] > 0:
        #     data['ratioAvailable'] = float(data['seatsAvailable'])/float(data['maxAttendees'])
        #     request.ratioAvailable = data['ratioAvailable']

        # convert dates from strings to Date objects; set month based on start_date
        if data['startDate']:
            data['startDate'] = datetime.strptime(data['startDate'][:10], "%Y-%m-%d").date()
            data['month'] = data['startDate'].month
        else:
            data['month'] = 0
        if data['endDate']:
            data['endDate'] = datetime.strptime(data['endDate'][:10], "%Y-%m-%d").date()

        # set seatsAvailable to be same as maxAttendees on creation
        if data["maxAttendees"] > 0:
            data["seatsAvailable"] = data["maxAttendees"]
        # generate Profile Key based on user ID and Conference
        # ID based on Profile key get Conference key from ID
        p_key = ndb.Key(Profile, user_id)
        c_id = Conference.allocate_ids(size=1, parent=p_key)[0]
        c_key = ndb.Key(Conference, c_id, parent=p_key)
        data['key'] = c_key
        data['organizerUserId'] = request.organizerUserId = user_id



        # create Conference, send email to organizer confirming
        # creation of Conference & return (modified) ConferenceForm
        Conference(**data).put()
        taskqueue.add(params={'email': user.email(),
            'conferenceInfo': repr(request)},
            url='/tasks/send_confirmation_email'
        )
        return request 
Example 67
Project: AppEngineConferenceCenter   Author: lolitschen   File: conference.py    Apache License 2.0 4 votes vote down vote up
def _conferenceRegistration(self, request, reg=True):
        """Register or unregister user for selected conference."""
        retval = None
        prof = self._getProfileFromUser() # get user Profile

        # check if conf exists given websafeConfKey
        # get conference; check that it exists
        wsck = request.websafeConferenceKey
        conf = ndb.Key(urlsafe=wsck).get()
        if not conf:
            raise endpoints.NotFoundException(
                'No conference found with key: %s' % wsck)

        # register
        if reg:
            # check if user already registered otherwise add
            if wsck in prof.conferenceKeysToAttend:
                raise ConflictException(
                    "You have already registered for this conference")

            # check if seats avail
            if conf.seatsAvailable <= 0:
                raise ConflictException(
                    "There are no seats available.")

            # register user, take away one seat
            prof.conferenceKeysToAttend.append(wsck)
            conf.seatsAvailable -= 1
            retval = True

        # unregister
        else:
            # check if user already registered
            if wsck in prof.conferenceKeysToAttend:

                # unregister user, add back one seat
                prof.conferenceKeysToAttend.remove(wsck)
                conf.seatsAvailable += 1
                retval = True
            else:
                retval = False

        # conf.ratioAvailable = float(conf.seatsAvailable)/float(conf.maxAttendees)

        # write things back to the datastore & return
        prof.put()
        conf.put()
        return BooleanMessage(data=retval) 
Example 68
Project: AppEngineConferenceCenter   Author: lolitschen   File: conference.py    Apache License 2.0 4 votes vote down vote up
def _createSessionObject(self, request):
        """Create or update _createSessionObject object, returning SessionForm/request."""
        # preload necessary data items
        user = endpoints.get_current_user()
        if not user:
            raise endpoints.UnauthorizedException('Authorization required')

        #check if authorized
        user_id = getUserId(user)
        conf = ndb.Key(urlsafe=request.websafeConferenceKey).get()
        if conf.organizerUserId != user_id:
            raise endpoints.UnauthorizedException('Incorrect Authorization')

        if not request.name:
            raise endpoints.BadRequestException("Session 'name' field required")

        #copy data into a dict
        data = {field.name: getattr(request, field.name) for field in request.all_fields()}
        del data['websafeConferenceKey']
        del data['websafeSessionKey']

        if not data['typeOfSession']:
            data['typeOfSession'] = str(SessionType.NOT_SPECIFIED)
        else: 
            data['typeOfSession']= str(data['typeOfSession'])

        for df in SESSION_DEFAULTS:
            if data[df] in (None, []):
                data[df] = SESSION_DEFAULTS[df]
                setattr(request, df, SESSION_DEFAULTS[df])

        # convert dates from strings to Date objects; set month based on start_date
        if data['date']:
            data['date'] = datetime.strptime(data['date'][:10], "%Y-%m-%d").date()
        else:
            data['date'] = datetime.now()

        if data['startTime']:
            data['startTime'] = datetime.strptime(data['startTime'], '%I:%M%p').time()
        else:
            data['startTime'] = datetime.strptime('12:00AM', '%I:%M%p').time()
            
        # generate session Key/ID based on conference key
        c_key = ndb.Key(urlsafe=request.websafeConferenceKey)
        s_id = Session.allocate_ids(size=1, parent=c_key)[0]
        s_key = ndb.Key(Session, s_id, parent=c_key)
        data['key'] = s_key

        #create session
        Session(**data).put()

        taskqueue.add(params={'websafeConferenceKey': c_key.urlsafe(),
            'speaker_name': data['speaker']},
            url='/tasks/set_featured_speaker'
        )

        print s_key.get();
        return self._copySessionToForm(s_key.get()) 
Example 69
Project: conf   Author: XonqNopp   File: e101_example.py    GNU General Public License v3.0 4 votes vote down vote up
def get(self, encoded_email):
        try:
            email = utils.decode(encoded_email)
            user = models.User.get_by_email(email)

            if (user.activated == False):
                # send email
                subject = config.app_name + " Account Verification Email"
                encoded_email = utils.encode(email)
                confirmation_url = self.uri_for("account-activation",
                    encoded_email = encoded_email,
                    _full = True)

                # load email's template
                template_val = {
                    "app_name": config.app_name,
                    "username": user.username,
                    "confirmation_url": confirmation_url,
                    "support_url": self.uri_for("contact", _full=True)
                }
                body_path = "emails/account_activation.txt"
                body = self.jinja2.render_template(body_path, **template_val)

                email_url = self.uri_for('taskqueue-send-email')
                taskqueue.add(url = email_url, params={
                    'to': str(email),
                    'subject' : subject,
                    'body' : body,
                    })

                message = _('The verification email has been resent to') + " " + str(email) + ". " +\
                    _('Please check your email to activate your account')
                self.add_message(message, "success")
                return self.redirect_to('home')
            else:
                message = _('Your account has been activated') + ". " +\
                    _('Please login to your account')
                self.add_message(message, "warning")
                return self.redirect_to('home')

        except (KeyError, AttributeError), e:
            message = _('Sorry') + ". " + _('Some error occurred') + "."
            self.add_message(message, "error")
            return self.redirect_to('home') 
Example 70
Project: conf   Author: XonqNopp   File: e101_example.py    GNU General Public License v3.0 4 votes vote down vote up
def post(self):
        """ validate contact form """

        if not self.form.validate():
            return self.get()
        remoteip  = self.request.remote_addr
        user_agent  = self.request.user_agent
        exception = self.request.POST.get('exception')
        name = self.form.name.data.strip()
        email = self.form.email.data.lower()
        message = self.form.message.data.strip()

        try:
            subject = _("Contact")
            body = ""
            # exceptions for error pages that redirect to contact
            if exception != "":
                body = "* Exception error: %s" % exception
            body = body + """
            * IP Address: %s
            * Web Browser: %s

            * Sender name: %s
            * Sender email: %s
            * Message: %s
            """ % (remoteip, user_agent, name, email, message)

            email_url = self.uri_for('taskqueue-send-email')
            taskqueue.add(url = email_url, params={
                'to': config.contact_recipient,
                'subject' : subject,
                'body' : body,
                'sender' : config.contact_sender,
                })

            message = _('Message sent successfully.')
            self.add_message(message, 'success')
            return self.redirect_to('contact')

        except (AttributeError, KeyError), e:
            message = _('Error sending the message. Please try again later.')
            self.add_message(message, 'error')
            return self.redirect_to('contact') 
Example 71
Project: conf   Author: XonqNopp   File: e101_example.py    GNU General Public License v3.0 4 votes vote down vote up
def post(self):
        """ Get fields from POST dict """

        if not self.form.validate():
            return self.get()
        current_password = self.form.current_password.data.strip()
        password = self.form.password.data.strip()

        try:
            user_info = models.User.get_by_id(long(self.user_id))
            auth_id = "own:%s" % user_info.username

            # Password to SHA512
            current_password = utils.encrypt(current_password, config.salt)
            try:
                user = models.User.get_by_auth_password(auth_id, current_password)
                # Password to SHA512
                password = utils.encrypt(password, config.salt)
                user.password = security.generate_password_hash(password, length=12)
                user.put()

                # send email
                subject = config.app_name + " Account Password Changed"

                # load email's template
                template_val = {
                    "app_name": config.app_name,
                    "first_name": user.name,
                    "username": user.username,
                    "email": user.email,
                    "reset_password_url": self.uri_for("password-reset", _full=True)
                }
                email_body_path = "emails/password_changed.txt"
                email_body = self.jinja2.render_template(email_body_path, **template_val)
                email_url = self.uri_for('taskqueue-send-email')
                taskqueue.add(url = email_url, params={
                    'to': user.email,
                    'subject' : subject,
                    'body' : email_body,
                    'sender' : config.contact_sender,
                    })

                # Login User
                self.auth.get_user_by_password(user.auth_ids[0], password)
                self.add_message(_('Password changed successfully'), 'success')
                return self.redirect_to('edit-profile')
            except (InvalidAuthIdError, InvalidPasswordError), e:
                # Returns error message to self.response.write in
                # the BaseHandler.dispatcher
                message = _("Your Current Password is wrong, please try again")
                self.add_message(message, 'error')
                return self.redirect_to('edit-password')
        except (AttributeError,TypeError), e:
            login_error_message = _('Sorry you are not logged in!')
            self.add_message(login_error_message,'error')
            self.redirect_to('login') 
Example 72
Project: conf   Author: XonqNopp   File: e101_example.py    GNU General Public License v3.0 4 votes vote down vote up
def post(self):
        # check captcha
        challenge = self.request.POST.get('recaptcha_challenge_field')
        response  = self.request.POST.get('recaptcha_response_field')
        remoteip  = self.request.remote_addr

        cResponse = captcha.submit(
            challenge,
            response,
            self.reCaptcha_private_key,
            remoteip)

        if cResponse.is_valid:
            # captcha was valid... carry on..nothing to see here
            pass
        else:
            logging.warning(cResponse.error_code)
            _message = _('Wrong image verification code. Please try again.')
            self.add_message(_message, 'error')
            return self.redirect_to('password-reset')
            # check if we got an email or username
        email_or_username = str(self.request.POST.get('email_or_username')).lower().strip()
        if utils.is_email_valid(email_or_username):
            user = models.User.get_by_email(email_or_username)
            _message = _("If the e-mail address you entered") + " (<strong>%s</strong>) " % email_or_username
        else:
            auth_id = "own:%s" % email_or_username
            user = models.User.get_by_auth_id(auth_id)
            _message = _("If the username you entered") + " (<strong>%s</strong>) " % email_or_username

        if user is not None:
            user_id = user.get_id()
            token = models.User.create_auth_token(user_id)
            email_url = self.uri_for('taskqueue-send-email')
            reset_url = self.uri_for('password-reset-check', user_id=user_id, token=token, _full=True)
            subject = _("Password reminder")
            body = _('Please click below to create a new password:') +\
                   """

                   %s
                   """ % reset_url
            taskqueue.add(url = email_url, params={
                'to': user.email,
                'subject' : subject,
                'body' : body,
                'sender' : config.contact_sender,
                })
            _message = _message + _("is associated with an account in our records, you will receive "\
                                    "an e-mail from us with instructions for resetting your password. "\
                                    "<br>If you don't receive this e-mail, please check your junk mail folder or ") +\
                       '<a href="' + self.uri_for('contact') + '">' + _('contact us') + '</a> ' +  _("for further assistance.")
            self.add_message(_message, 'success')
            return self.redirect_to('login')
        _message = _('Your email / username was not found. Please try another or ') + '<a href="' + self.uri_for('register') + '">' + _('create an account') + '</a>'
        self.add_message(_message, 'error')
        return self.redirect_to('password-reset') 
Example 73
Project: roger-api   Author: rogertalk   File: jobs.py    MIT License 4 votes vote down vote up
def content_comment_extras():
    # Extract all parameters from the request.
    content_id = int(request.form['content_id'])
    content_key = ndb.Key('Content', content_id)
    comment_id = request.form['comment_id']
    comment_key = ndb.Key('ContentComment', comment_id, parent=content_key)
    commenter_id = int(request.form['commenter_id'])
    commenter_key = ndb.Key('Account', commenter_id)
    creator_id = int(request.form['creator_id'])
    creator_key = ndb.Key('Account', creator_id)
    mentions = request.form['mentions'].split(',')
    if len(mentions) == 1 and not mentions[0]:
        mentions = []
    # Load relevant entities asynchronously.
    account_keys_futures = [models.Account.resolve_key_async(m) for m in mentions]
    commenter_future = commenter_key.get_async()
    comment_future = comment_key.get_async()
    content_future = content_key.get_async()
    commenter = commenter_future.get_result()
    comment = comment_future.get_result()
    if not comment:
        logging.warning('Failed to find comment with key %r', comment_key)
        return ''
    # Build a set of keys for the accounts to notify.
    interested_keys = set()
    # All mentioned users.
    for f in account_keys_futures:
        try:
            account_key = f.get_result()
        except:
            logging.exception('Failed to get key for a mentioned account.')
            continue
        interested_keys.add(account_key)
    # But not the creators of the comment or the content.
    interested_keys.discard(commenter_key)
    interested_keys.discard(creator_key)
    interested_keys.discard(None)
    # Notify away!
    content = content_future.get_result()
    futures = []
    for account_key in interested_keys:
        hub = notifs.Hub(account_key)
        futures.append(hub.emit_async(notifs.ON_CONTENT_COMMENT,
            comment=comment,
            commenter=commenter,
            content=content))
    for f in futures:
        try:
            f.get_result()
        except:
            logging.exception('Failed to notify an account.')
    return '' 
Example 74
Project: roger-api   Author: rogertalk   File: api.py    MIT License 4 votes vote down vote up
def post_register():
    """
    Registers a new user account.

    Request:
        POST /v1/register?username=bob&password=top_secret

    Response:
        {
            "access_token": "RLDvsbckw7tJJCiCPzU9bF",
            "refresh_token": "pArhTbEs8ex1f79vAqxR2",
            "token_type": "bearer",
            "expires_in": 3600,
            "status": "active",
            "account": {
                "id": 12345678,
                "display_name": "Bob Brown",
                "aliases": ["bob"]
            }
        }

    """
    client_id, _ = auth.get_client_details()
    username = flask_extras.get_parameter('username') or None
    if username:
        username, identifier_type = identifiers.parse(username)
        if identifier_type != identifiers.USERNAME:
            # A user may not use this endpoint to add a phone number/e-mail.
            raise errors.InvalidArgument('A valid username must be provided')
    birthday = flask_extras.get_parameter('birthday') or None
    if birthday:
        birthday = models.Account.parse_birthday(birthday)
    display_name = flask_extras.get_parameter('display_name') or None
    gender = flask_extras.get_parameter('gender') or None
    image = request.files.get('image') or None
    # Create the account with provided parameters.
    user = accounts.create(username,
        birthday=birthday,
        display_name=display_name,
        gender=gender,
        image=image,
        last_active_client=request.headers.get('User-Agent'))
    # Add password to the account if one was provided.
    challenge = 'none'
    password = flask_extras.get_parameter('password')
    if password:
        challenge = 'password'
        user.set_password(password)
    report.user_registered(user.account_id, auth_identifier=None, challenge=challenge,
                           status=user.account.status)
    # Track logins.
    taskqueue.add(url='/_ah/jobs/track_login',
                  countdown=20,
                  params={'account_id': user.account_id,
                          'client_id': client_id},
                  queue_name=config.INTERNAL_QUEUE)
    # Support adding participants in conjunction with registering.
    g.public_options['include_extras'] = True
    return utils.set_up_session(user, participants=flask_extras.get_parameter_list('stream_participant')) 
Example 75
Project: roger-api   Author: rogertalk   File: api.py    MIT License 4 votes vote down vote up
def post_youtube_auth(session):
    # TODO: Support logging in with this endpoint?
    code = flask_extras.get_parameter('code')
    if not code:
        raise errors.MissingArgument('Missing code parameter')
    data, profile = youtube.auth_async(code).get_result()
    user_id = profile['sub']
    handler = accounts.get_handler(session.account)
    email = profile.get('email')
    if email and profile['email_verified'] and not handler.has_identifier(email):
        try:
            handler.add_identifier(email, notify_change=False, notify_connect=False)
        except errors.AlreadyExists:
            logging.warning('Email %s already associated with another account', email)
        except:
            logging.exception('Failed to add email %s to account %d',
                email, session.account_key.id())
    identifier = models.Service.build_identifier('youtube', None, user_id)
    if not handler.has_identifier(identifier):
        handler.add_identifier(identifier, reclaim=True)
    refresh_token = data.get('refresh_token')
    handler.connect_service('youtube', None, user_id, notify=False,
        access_token=data.get('access_token'),
        expires_in=data.get('expires_in'),
        refresh_token=refresh_token,
        token_type=data.get('token_type'))
    try:
        channels_future = youtube.get_channels_async(refresh_token)
        channels = channels_future.get_result()
        logging.debug('Channel(s): %r', channels)
        channel = channels[0]
        needs_update = (channel['id'] != handler.youtube_channel_id or
                        channel['thumb_url'] != handler.youtube_channel_thumb_url or
                        channel['title'] != handler.youtube_channel_title or
                        channel['views'] != handler.youtube_channel_views or
                        channel['subs'] != handler.youtube_subs)
        if needs_update:
            handler.account.youtube_channel_id = channel['id']
            handler.account.youtube_channel_thumb_url = channel['thumb_url']
            handler.account.youtube_channel_title = channel['title']
            handler.account.youtube_channel_views = channel['views']
            handler.account.youtube_subs = channel['subs']
            handler.account.youtube_subs_updated = datetime.utcnow()
            handler.account.put()
    except:
        logging.exception('Failed to get YouTube channel.')
    g.public_options['include_extras'] = True
    return session 
Example 76
Project: roger-api   Author: rogertalk   File: oauth2.py    MIT License 4 votes vote down vote up
def request_token():
    """
    Attempts to log the user in with the specified grant type.

    Request:
        POST /oauth2/token?grant_type=password&client_id=ios
        username=bob&password=1234

    Response:
        {
            "access_token": "RLDvsbckw7tJJCiCPzU9bF",
            "refresh_token": "pArhTbEs8ex1f79vAqxR2",
            "token_type": "bearer",
            "expires_in": 3600,
            "account": {
                "id": 12345678,
                "username": "bob",
                "status": "active"
            }
        }

    """
    client_id, client_secret = auth.get_client_details()
    if not client_id:
        raise errors.MissingArgument('A client id is required')
    client = services.get_client(client_id)
    if client_secret != client.client_secret:
        # TODO: Enforce a valid client_secret.
        logging.warning('Client secret appears invalid (client_id=%s)', client_id)
    grant_type = flask_extras.get_parameter('grant_type')
    if grant_type == 'authorization_code':
        user = grant_type_authorization_code(client)
    elif grant_type == 'password':
        user = grant_type_password(client)
    elif grant_type == 'refresh_token':
        user = grant_type_refresh_token(client)
    else:
        raise errors.NotSupported('Unsupported grant type "{}"'.format(grant_type))
    # Report the successful token exchange.
    event = events.TokenExchangeV1(user.account.key.id(),
                                   client_id=client_id,
                                   grant_type=grant_type)
    event.report()
    # Track all fika.io logins.
    if grant_type != 'refresh_token':
        taskqueue.add(url='/_ah/jobs/track_login',
                      countdown=20,
                      params={'account_id': user.account_id, 'client_id': client_id},
                      queue_name=config.INTERNAL_QUEUE)
    # Return the session and some additional data about the user.
    data = utils.get_streams_data(user.account)
    session = user.create_session(extra_data=data)
    g.public_options['include_extras'] = True
    g.public_options['view_account'] = session.account
    return session 
Example 77
Project: Conference-Organization-APP-UDACITY   Author: kongling893   File: conference.py    Apache License 2.0 4 votes vote down vote up
def _createConferenceObject(self, request):
        """Create or update Conference object, returning ConferenceForm/request."""
        # preload necessary data items
        user = endpoints.get_current_user()
        if not user:
            raise endpoints.UnauthorizedException('Authorization required')
        user_id = getUserId(user)

        if not request.name:
            raise endpoints.BadRequestException("Conference 'name' field required")

        # copy ConferenceForm/ProtoRPC Message into dict
        data = {field.name: getattr(request, field.name) for field in request.all_fields()}
        del data['websafeKey']
        del data['organizerDisplayName']

        # add default values for those missing (both data model & outbound Message)
        for df in DEFAULTS:
            if data[df] in (None, []):
                data[df] = DEFAULTS[df]
                setattr(request, df, DEFAULTS[df])

        # convert dates from strings to Date objects; set month based on start_date
        if data['startDate']:
            data['startDate'] = datetime.strptime(data['startDate'][:10], "%Y-%m-%d").date()
            data['month'] = data['startDate'].month
        else:
            data['month'] = 0
        if data['endDate']:
            data['endDate'] = datetime.strptime(data['endDate'][:10], "%Y-%m-%d").date()

        # set seatsAvailable to be same as maxAttendees on creation
        # both for data model & outbound Message
        if data["maxAttendees"] > 0:
            data["seatsAvailable"] = data["maxAttendees"]
            setattr(request, "seatsAvailable", data["maxAttendees"])

        # make Profile Key from user ID
        p_key = ndb.Key(Profile, user_id)
        # allocate new Conference ID with Profile key as parent
        c_id = Conference.allocate_ids(size=1, parent=p_key)[0]
        # make Conference key from ID
        c_key = ndb.Key(Conference, c_id, parent=p_key)
        data['key'] = c_key
        data['organizerUserId'] = request.organizerUserId = user_id

        # create Conference, send email to organizer confirming
        # creation of Conference & return (modified) ConferenceForm
        Conference(**data).put()
        taskqueue.add(params={'email': user.email(),
            'conferenceInfo': repr(request)},
            url='/tasks/send_confirmation_email'
        )

        return request 
Example 78
Project: Conference-Organization-APP-UDACITY   Author: kongling893   File: conference.py    Apache License 2.0 4 votes vote down vote up
def _createSessionObject(self, request):
        """Create or update Conference object, returning SessionForm/request."""
        # preload necessary data items
        user = endpoints.get_current_user()
        if not user:
            raise endpoints.UnauthorizedException('Authorization required')
        user_id = getUserId(user)

        if not request.name:
            raise endpoints.BadRequestException("Session 'name' field required")

        #get cinference key
        wsck = request.websafeConferenceKey
        # get conference object
        c_key = ndb.Key(urlsafe=wsck)
        conf = c_key.get()
        # check that conference exists or not
        if not conf:
            raise endpoints.NotFoundException(
                'No conference found with key: %s' % wsck)
        # check that user is owner
        if conf.organizerUserId != getUserId(endpoints.get_current_user()):
            raise endpoints.ForbiddenException(
                'You must be the organizer to create a session.')

        # copy SessionForm/ProtoRPC Message into dict
        data = {field.name: getattr(request, field.name) for field in request.all_fields()}
        # convert date and time from strings to Date objects; 
        if data['date']:
            data['date'] = datetime.strptime(data['date'][:10], "%Y-%m-%d").date()
        if data['startTime']:
            data['startTime'] = datetime.strptime(data['startTime'][:10],  "%H, %M").time()
        # allocate new Session ID with Conference key as parent
        s_id = Session.allocate_ids(size=1, parent=c_key)[0]
        # make Session key from ID
        s_key = ndb.Key(Session, s_id, parent=c_key)
        data['key'] = s_key
        data['websafeConferenceKey'] = wsck
        del data['sessionSafeKey']

        #  save session into database
        Session(**data).put()
        # This task wil send a confirmation email to the owner 
        taskqueue.add(params={'email': user.email(),
            'conferenceInfo': repr(request)},
            url='/tasks/send_confirmation_session_email'
        )
        return request 
Example 79
Project: Conference-Organization-APP-UDACITY   Author: kongling893   File: conference.py    Apache License 2.0 4 votes vote down vote up
def _conferenceRegistration(self, request, reg=True):
        """Register or unregister user for selected conference."""
        retval = None
        prof = self._getProfileFromUser() # get user Profile

        # check if conf exists given websafeConfKey
        # get conference; check that it exists
        wsck = request.websafeConferenceKey
        conf = ndb.Key(urlsafe=wsck).get()
        if not conf:
            raise endpoints.NotFoundException(
                'No conference found with key: %s' % wsck)

        # register
        if reg:
            # check if user already registered otherwise add
            if wsck in prof.conferenceKeysToAttend:
                raise ConflictException(
                    "You have already registered for this conference")

            # check if seats avail
            if conf.seatsAvailable <= 0:
                raise ConflictException(
                    "There are no seats available.")

            # register user, take away one seat
            prof.conferenceKeysToAttend.append(wsck)
            conf.seatsAvailable -= 1
            retval = True

        # unregister
        else:
            # check if user already registered
            if wsck in prof.conferenceKeysToAttend:

                # unregister user, add back one seat
                prof.conferenceKeysToAttend.remove(wsck)
                conf.seatsAvailable += 1
                retval = True
            else:
                retval = False

        # write things back to the datastore & return
        prof.put()
        conf.put()
        return BooleanMessage(data=retval)