Example #1
0
  def wrapper(request, *args, **kwargs):
    """Decorator wrapper method
    """

    try:
      return func(request, *args, **kwargs)
    except task_responses.FatalTaskError as error:
      logging.exception(error)
      return task_responses.terminateTask()
    except Exception as exception:
      logging.exception(exception)
      return task_responses.repeatTask()
Example #2
0
    def wrapper(request, *args, **kwargs):
        """Decorator wrapper method
    """

        try:
            return func(request, *args, **kwargs)
        except task_responses.FatalTaskError as error:
            logging.exception(error)
            return task_responses.terminateTask()
        except Exception as exception:
            logging.exception(exception)
            return task_responses.repeatTask()
Example #3
0
    def wrapper(func):
        def iterative_wrapped(request, *args, **kwargs):
            """Decorator wrapper method

      Args:
        request: Django HTTP Request object

      request.POST usage:
        fields: a JSON dict for the properties that the entities should have.
          This updates values from the task_default entry.
        start_key: the key of the next entity to fetch

      Returns:
        Standard HTTP Django response
      """

            post_dict = request.POST

            fields = task_default.get('fields', {})
            if 'fields' in post_dict:
                fields.update(pickle.loads(str(post_dict['fields'])))

            start_key = task_default.get('start_key', None)
            if 'start_key' in post_dict:
                # get the key where to start this iteration
                start_key = post_dict['start_key']
            if start_key:
                start_key = db.Key(start_key)

            # get the entities for this iteration
            entities, next_start_key = logic.getBatchOfData(
                filter=fields, start_key=start_key)

            # copy the post_dict so that the wrapped function can edit what it needs
            context = post_dict.copy()

            try:
                func(request,
                     entities=entities,
                     context=context,
                     *args,
                     **kwargs)
            except task_responses.FatalTaskError, error:
                logging.debug(post_dict)
                logging.error(error)
                return task_responses.terminateTask()
            except Exception, exception:
                logging.debug(post_dict)
                logging.error(exception)
                return task_responses.repeatTask()
Example #4
0
def iterative_task(logic, repeat_in=None, **task_default):
    """Iterative wrapper method

  Args:
    logic: the Logic instance to get entities for
    repeat_in: the task will be executed again t seconds after completion
    task_default: keyword arguments which can contain the following options:
      fields: dictionary to filter the entities on
      start_key: the default key where to start this iterative task
  """
    def wrapper(func):
        def iterative_wrapped(request, *args, **kwargs):
            """Decorator wrapper method

      Args:
        request: Django HTTP Request object

      request.POST usage:
        fields: a JSON dict for the properties that the entities should have.
          This updates values from the task_default entry.
        start_key: the key of the next entity to fetch

      Returns:
        Standard HTTP Django response
      """

            post_dict = request.POST

            fields = task_default.get('fields', {})
            if 'fields' in post_dict:
                fields.update(pickle.loads(str(post_dict['fields'])))

            start_key = task_default.get('start_key', None)
            if 'start_key' in post_dict:
                # get the key where to start this iteration
                start_key = post_dict['start_key']
            if start_key:
                start_key = db.Key(start_key)

            # get the entities for this iteration
            entities, next_start_key = logic.getBatchOfData(
                filter=fields, start_key=start_key)

            # copy the post_dict so that the wrapped function can edit what it needs
            context = post_dict.copy()

            # when true, the iterative task will not be repeated when completed
            do_not_repeat = False

            try:
                func(request,
                     entities=entities,
                     context=context,
                     *args,
                     **kwargs)
            except task_responses.DoNotRepeatException, exception:
                do_not_repeat = True
            except task_responses.FatalTaskError, error:
                logging.debug(post_dict)
                logging.error(error)
                return task_responses.terminateTask()
            except Exception, exception:
                logging.debug(post_dict)
                logging.error(exception)
                return task_responses.repeatTask()
Example #5
0
    # strings.
    context[str(key)] = value

  logging.info('Sending %s' %context)
  message = mail.EmailMessage(**context)

  try:
    message.check_initialized()
  except:
    logging.error('This message was not properly initialized')
    mail_entity.delete()
    return responses.terminateTask()

  def txn():
    """Transaction that ensures the deletion of the Email entity only if
    the mail has been successfully sent.
    """
    mail_entity.delete()
    message.send()

  try:
    db.RunInTransaction(txn)
  except mail.Error, exception:
    # shouldn't happen because validate has been called, keeping the Email
    # entity for study purposes.
    return error_handler.logErrorAndReturnOK(exception)
  except OverQuotaError:
    return responses.repeatTask()

  # mail successfully sent
  return responses.terminateTask()
Example #6
0
    def sendMail(self, request):
        """Sends out an email that is stored in the datastore.

    The POST request should contain the following entries:
      mail_key: Datastore key for an Email entity.
    """
        post_dict = request.POST

        mail_key = post_dict.get('mail_key', None)

        if not mail_key:
            return error_handler.logErrorAndReturnOK('No email key specified')

        # TODO(daniel): so ugly...
        try:
            mail_entity = db.get(mail_key)
        except datastore_errors.BadKeyError:
            mail_entity = ndb.Key(urlsafe=mail_key).get()

        if not mail_entity:
            return error_handler.logErrorAndReturnOK(
                'No email entity found for key %s' % mail_key)

        # construct the EmailMessage from the given context
        loaded_context = json.loads(mail_entity.context)

        context = {}
        for key, value in loaded_context.iteritems():
            # If we don't do this python will complain about kwargs not being
            # strings.
            context[str(key)] = value

        logging.info('Sending %s', context)
        message = mail.EmailMessage(**context)

        try:
            message.check_initialized()
        except Exception as e:
            logging.exception(e)
            context['body'] = context.get('body', '')[:10]
            logging.error('This message was not properly initialized: "%s"',
                          context)
            mail_entity.delete()
            return responses.terminateTask()

        def txn():
            """Transaction that ensures the deletion of the Email entity only if
      the mail has been successfully sent.
      """
            mail_entity.delete()
            message.send()

        try:
            db.RunInTransaction(txn)
        except mail.Error as exception:
            # shouldn't happen because validate has been called, keeping the Email
            # entity for study purposes.
            return error_handler.logErrorAndReturnOK(exception)
        except (OverQuotaError, DeadlineExceededError) as e:
            return responses.repeatTask()

        # mail successfully sent
        return responses.terminateTask()
Example #7
0
    def iterative_wrapped(request, *args, **kwargs):
      """Decorator wrapper method

      Args:
        request: Django HTTP Request object

      request.POST usage:
        fields: a JSON dict for the properties that the entities should have.
          This updates values from the task_default entry.
        start_key: the key of the next entity to fetch

      Returns:
        Standard HTTP Django response
      """

      post_dict = request.POST

      fields = task_default.get('fields', {})
      if 'fields' in post_dict:
        fields.update(pickle.loads(str(post_dict['fields'])))

      start_key = task_default.get('start_key', None)
      if 'start_key' in post_dict:
        # get the key where to start this iteration
        start_key = post_dict['start_key']
      if start_key:
        start_key = db.Key(start_key)

      # get the entities for this iteration
      entities, next_start_key = logic.getBatchOfData(filter=fields,
                                                      start_key=start_key)

      # copy the post_dict so that the wrapped function can edit what it needs
      context = post_dict.copy()

      # when true, the iterative task will not be repeated when completed
      do_not_repeat = False

      try:
        func(request, entities=entities, context=context, *args, **kwargs)
      except task_responses.DoNotRepeatException as exception:
        do_not_repeat = True
      except task_responses.FatalTaskError as error:
        logging.debug(post_dict)
        logging.error(error)
        return task_responses.terminateTask()
      except Exception as exception:
        logging.debug(post_dict)
        logging.error(exception)
        return task_responses.repeatTask()

      if next_start_key:
        # set the key to use for the next iteration
        context.update({'start_key': next_start_key})

        task_responses.startTask(url=request.path, context=context)
      elif not do_not_repeat and repeat_in is not None:
        # the task will be executed again after repeat_in seconds
        if 'start_key' in context:
          del context['start_key']

        task_responses.startTask(url=request.path, countdown=repeat_in,
            context=context)

      return task_responses.terminateTask()
Example #8
0
def iterative_task(logic, repeat_in=None, **task_default):
  """Iterative wrapper method

  Args:
    logic: the Logic instance to get entities for
    repeat_in: the task will be executed again t seconds after completion
    task_default: keyword arguments which can contain the following options:
      fields: dictionary to filter the entities on
      start_key: the default key where to start this iterative task
  """

  def wrapper(func):
    def iterative_wrapped(request, *args, **kwargs):
      """Decorator wrapper method

      Args:
        request: Django HTTP Request object

      request.POST usage:
        fields: a JSON dict for the properties that the entities should have.
          This updates values from the task_default entry.
        start_key: the key of the next entity to fetch

      Returns:
        Standard HTTP Django response
      """

      post_dict = request.POST

      fields = task_default.get('fields', {})
      if 'fields' in post_dict:
        fields.update(pickle.loads(str(post_dict['fields'])))

      start_key = task_default.get('start_key', None)
      if 'start_key' in post_dict:
        # get the key where to start this iteration
        start_key = post_dict['start_key']
      if start_key:
        start_key = db.Key(start_key)

      # get the entities for this iteration
      entities, next_start_key = logic.getBatchOfData(filter=fields,
                                                      start_key=start_key)

      # copy the post_dict so that the wrapped function can edit what it needs
      context = post_dict.copy()

      # when true, the iterative task will not be repeated when completed
      do_not_repeat = False

      try:
        func(request, entities=entities, context=context, *args, **kwargs)
      except task_responses.DoNotRepeatException, exception:
        do_not_repeat = True
      except task_responses.FatalTaskError, error:
        logging.debug(post_dict)
        logging.error(error)
        return task_responses.terminateTask()
      except Exception, exception:
        logging.debug(post_dict)
        logging.error(exception)
        return task_responses.repeatTask()
Example #9
0
        def iterative_wrapped(request, *args, **kwargs):
            """Decorator wrapper method

      Args:
        request: Django HTTP Request object

      request.POST usage:
        fields: a JSON dict for the properties that the entities should have.
          This updates values from the task_default entry.
        start_key: the key of the next entity to fetch

      Returns:
        Standard HTTP Django response
      """

            post_dict = request.POST

            fields = task_default.get("fields", {})
            if "fields" in post_dict:
                fields.update(pickle.loads(str(post_dict["fields"])))

            start_key = task_default.get("start_key", None)
            if "start_key" in post_dict:
                # get the key where to start this iteration
                start_key = post_dict["start_key"]
            if start_key:
                start_key = db.Key(start_key)

            # get the entities for this iteration
            entities, next_start_key = logic.getBatchOfData(filter=fields, start_key=start_key)

            # copy the post_dict so that the wrapped function can edit what it needs
            context = post_dict.copy()

            # when true, the iterative task will not be repeated when completed
            do_not_repeat = False

            try:
                func(request, entities=entities, context=context, *args, **kwargs)
            except task_responses.DoNotRepeatException as exception:
                do_not_repeat = True
            except task_responses.FatalTaskError as error:
                logging.debug(post_dict)
                logging.error(error)
                return task_responses.terminateTask()
            except Exception as exception:
                logging.debug(post_dict)
                logging.error(exception)
                return task_responses.repeatTask()

            if next_start_key:
                # set the key to use for the next iteration
                context.update({"start_key": next_start_key})

                task_responses.startTask(url=request.path, context=context)
            elif not do_not_repeat and repeat_in is not None:
                # the task will be executed again after repeat_in seconds
                if "start_key" in context:
                    del context["start_key"]

                task_responses.startTask(url=request.path, countdown=repeat_in, context=context)

            return task_responses.terminateTask()
Example #10
0
  def sendMail(self, request):
    """Sends out an email that is stored in the datastore.

    The POST request should contain the following entries:
      mail_key: Datastore key for an Email entity.
    """
    post_dict = request.POST

    mail_key = post_dict.get('mail_key', None)

    if not mail_key:
      return error_handler.logErrorAndReturnOK('No email key specified')

    # TODO(daniel): so ugly...
    try:
      mail_entity = db.get(mail_key)
    except datastore_errors.BadKeyError:
      mail_entity = ndb.Key(urlsafe=mail_key).get()

    if not mail_entity:
      return error_handler.logErrorAndReturnOK(
          'No email entity found for key %s' % mail_key)

    # construct the EmailMessage from the given context
    loaded_context = json.loads(mail_entity.context)

    context = {}
    for key, value in loaded_context.iteritems():
      # If we don't do this python will complain about kwargs not being
      # strings.
      context[str(key)] = value

    logging.info('Sending %s', context)
    message = mail.EmailMessage(**context)

    try:
      message.check_initialized()
    except Exception as e:
      logging.exception(e)
      context['body'] = context.get('body', '')[:10]
      logging.error('This message was not properly initialized: "%s"', context)
      mail_entity.delete()
      return responses.terminateTask()

    def txn():
      """Transaction that ensures the deletion of the Email entity only if
      the mail has been successfully sent.
      """
      mail_entity.delete()
      message.send()

    try:
      db.RunInTransaction(txn)
    except mail.Error as exception:
      # shouldn't happen because validate has been called, keeping the Email
      # entity for study purposes.
      return error_handler.logErrorAndReturnOK(exception)
    except (OverQuotaError, DeadlineExceededError) as e:
      return responses.repeatTask()

    # mail successfully sent
    return responses.terminateTask()