app/soc/tasks/helper/decorators.py
author Lennard de Rijk <ljvderijk@gmail.com>
Sun, 25 Oct 2009 18:41:56 -0700
changeset 3049 45b5738619d3
parent 2995 5931e6d6056f
permissions -rw-r--r--
Redone the request View to work with the new model. Also added a TODO to fix editing your Request message.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2990
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
     1
#!/usr/bin/python2.5
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
     2
#
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
     3
# Copyright 2009 the Melange authors.
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
     4
#
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
     5
# Licensed under the Apache License, Version 2.0 (the "License");
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
     6
# you may not use this file except in compliance with the License.
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
     7
# You may obtain a copy of the License at
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
     8
#
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
     9
#   http://www.apache.org/licenses/LICENSE-2.0
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    10
#
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    11
# Unless required by applicable law or agreed to in writing, software
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    12
# distributed under the License is distributed on an "AS IS" BASIS,
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    13
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    14
# See the License for the specific language governing permissions and
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    15
# limitations under the License.
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    16
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    17
"""Decorators for the Task API.
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    18
"""
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    19
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    20
__authors__ = [
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    21
  '"Daniel Hans" <daniel.m.hans@gmail.com>',
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    22
  '"Lennard de Rijk" <ljvderijk@gmail.com>',
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    23
  ]
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    24
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    25
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    26
import logging
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    27
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    28
from functools import wraps
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    29
2995
5931e6d6056f Changed the working of the iterative_task decorator.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2993
diff changeset
    30
from google.appengine.ext import db
5931e6d6056f Changed the working of the iterative_task decorator.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2993
diff changeset
    31
2990
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    32
from soc.tasks import responses as task_responses
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    33
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    34
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    35
def task(func):
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    36
  """Task decorator wrapper method
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    37
  """
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    38
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    39
  @wraps(func)
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    40
  def wrapper(request, *args, **kwargs):
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    41
    """Decorator wrapper method
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    42
    """
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    43
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    44
    try:
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    45
      return func(request, *args, **kwargs)
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    46
    except task_responses.FatalTaskError, error:
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    47
      logging.exception(error)
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    48
      return task_responses.terminateTask()
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    49
    except Exception, exception:
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    50
      logging.exception(exception)
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    51
      return task_responses.repeatTask()
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    52
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    53
  return wrapper
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    54
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    55
2995
5931e6d6056f Changed the working of the iterative_task decorator.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2993
diff changeset
    56
def iterative_task(logic, **task_default):
2990
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    57
  """Iterative wrapper method
2995
5931e6d6056f Changed the working of the iterative_task decorator.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2993
diff changeset
    58
5931e6d6056f Changed the working of the iterative_task decorator.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2993
diff changeset
    59
  Args:
5931e6d6056f Changed the working of the iterative_task decorator.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2993
diff changeset
    60
    logic: the Logic instance to get entities for
5931e6d6056f Changed the working of the iterative_task decorator.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2993
diff changeset
    61
    task_default: keyword arguments which can contain the following options:
5931e6d6056f Changed the working of the iterative_task decorator.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2993
diff changeset
    62
      fields: dictionary to filter the entities on
5931e6d6056f Changed the working of the iterative_task decorator.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2993
diff changeset
    63
      start_key: the default key where to start this iterative task
2990
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    64
  """
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    65
2995
5931e6d6056f Changed the working of the iterative_task decorator.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2993
diff changeset
    66
  def wrapper(func):
5931e6d6056f Changed the working of the iterative_task decorator.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2993
diff changeset
    67
    def iterative_wrapped(request, *args, **kwargs):
5931e6d6056f Changed the working of the iterative_task decorator.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2993
diff changeset
    68
      """Decorator wrapper method
2990
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    69
2995
5931e6d6056f Changed the working of the iterative_task decorator.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2993
diff changeset
    70
      Args:
5931e6d6056f Changed the working of the iterative_task decorator.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2993
diff changeset
    71
        request: Django HTTP Request object
2990
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    72
2995
5931e6d6056f Changed the working of the iterative_task decorator.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2993
diff changeset
    73
      request.POST usage:
5931e6d6056f Changed the working of the iterative_task decorator.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2993
diff changeset
    74
        fields: a JSON dict for the properties that the entities should have.
5931e6d6056f Changed the working of the iterative_task decorator.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2993
diff changeset
    75
          This updates values from the task_default entry.
5931e6d6056f Changed the working of the iterative_task decorator.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2993
diff changeset
    76
        start_key: the key of the next entity to fetch
2990
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    77
2995
5931e6d6056f Changed the working of the iterative_task decorator.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2993
diff changeset
    78
      Returns:
5931e6d6056f Changed the working of the iterative_task decorator.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2993
diff changeset
    79
        Standard HTTP Django response
5931e6d6056f Changed the working of the iterative_task decorator.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2993
diff changeset
    80
      """
2990
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    81
2995
5931e6d6056f Changed the working of the iterative_task decorator.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2993
diff changeset
    82
      post_dict = request.POST
2990
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    83
2995
5931e6d6056f Changed the working of the iterative_task decorator.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2993
diff changeset
    84
      fields = task_default.get('fields', {})
5931e6d6056f Changed the working of the iterative_task decorator.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2993
diff changeset
    85
      if 'fields' in post_dict:
5931e6d6056f Changed the working of the iterative_task decorator.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2993
diff changeset
    86
        fields.update(simplejson.loads(post_dict['fields']))
2990
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    87
2995
5931e6d6056f Changed the working of the iterative_task decorator.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2993
diff changeset
    88
      start_key = task_default.get('start_key', None)
5931e6d6056f Changed the working of the iterative_task decorator.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2993
diff changeset
    89
      if 'start_key' in post_dict:
5931e6d6056f Changed the working of the iterative_task decorator.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2993
diff changeset
    90
        # get the key where to start this iteration
5931e6d6056f Changed the working of the iterative_task decorator.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2993
diff changeset
    91
        start_key = post_dict['start_key']
5931e6d6056f Changed the working of the iterative_task decorator.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2993
diff changeset
    92
      if start_key:
5931e6d6056f Changed the working of the iterative_task decorator.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2993
diff changeset
    93
        start_key = db.Key(start_key)
2990
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    94
2995
5931e6d6056f Changed the working of the iterative_task decorator.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2993
diff changeset
    95
      # get the entities for this iteration
5931e6d6056f Changed the working of the iterative_task decorator.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2993
diff changeset
    96
      entities, next_start_key = logic.getBatchOfData(filter=fields,
5931e6d6056f Changed the working of the iterative_task decorator.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2993
diff changeset
    97
                                                      start_key=start_key)
2990
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    98
2995
5931e6d6056f Changed the working of the iterative_task decorator.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2993
diff changeset
    99
      # copy the post_dict so that the wrapped function can edit what it needs
2990
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   100
      context = post_dict.copy()
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   101
2995
5931e6d6056f Changed the working of the iterative_task decorator.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2993
diff changeset
   102
      try:
5931e6d6056f Changed the working of the iterative_task decorator.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2993
diff changeset
   103
        func(request, entities=entities, context=context, *args, **kwargs)
5931e6d6056f Changed the working of the iterative_task decorator.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2993
diff changeset
   104
      except task_responses.FatalTaskError, error:
5931e6d6056f Changed the working of the iterative_task decorator.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2993
diff changeset
   105
        logging.error(error)
5931e6d6056f Changed the working of the iterative_task decorator.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2993
diff changeset
   106
        return task_responses.terminateTask()
5931e6d6056f Changed the working of the iterative_task decorator.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2993
diff changeset
   107
      except Exception, exception:
5931e6d6056f Changed the working of the iterative_task decorator.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2993
diff changeset
   108
        logging.error(exception)
5931e6d6056f Changed the working of the iterative_task decorator.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2993
diff changeset
   109
        return task_responses.repeatTask()
2990
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   110
2995
5931e6d6056f Changed the working of the iterative_task decorator.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2993
diff changeset
   111
      if next_start_key:
5931e6d6056f Changed the working of the iterative_task decorator.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2993
diff changeset
   112
        # set the key to use for the next iteration
5931e6d6056f Changed the working of the iterative_task decorator.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2993
diff changeset
   113
        context.update({'start_key': next_start_key})
2990
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   114
2995
5931e6d6056f Changed the working of the iterative_task decorator.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2993
diff changeset
   115
        task_responses.startTask(url=request.path, context=context)
2990
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   116
2995
5931e6d6056f Changed the working of the iterative_task decorator.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2993
diff changeset
   117
      return task_responses.terminateTask()
2990
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   118
2995
5931e6d6056f Changed the working of the iterative_task decorator.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2993
diff changeset
   119
    return iterative_wrapped
5931e6d6056f Changed the working of the iterative_task decorator.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2993
diff changeset
   120
  return wrapper