app/soc/tasks/helper/decorators.py
author Lennard de Rijk <ljvderijk@gmail.com>
Tue, 29 Sep 2009 19:56:58 +0200
changeset 2990 0b6a093c5c81
child 2991 d6efef2989ac
permissions -rw-r--r--
Moved the Tasks decorators to their own module.
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
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    30
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
    31
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    32
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    33
def task(func):
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    34
  """Task decorator wrapper method
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    35
  """
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    36
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    37
  @wraps(func)
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    38
  def wrapper(request, *args, **kwargs):
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    39
    """Decorator wrapper method
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    40
    """
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    41
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    42
    try:
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    43
      return func(request, *args, **kwargs)
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    44
    except task_responses.FatalTaskError, error:
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    45
      logging.exception(error)
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    46
      return task_responses.terminateTask()
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    47
    except Exception, exception:
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    48
      logging.exception(exception)
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    49
      return task_responses.repeatTask()
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    50
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    51
  return wrapper
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
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    54
def iterative_task(func):
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    55
  """Iterative wrapper method
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    56
  """
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    57
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    58
  @wraps(func)
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    59
  def wrapper(request, *args, **kwargs):
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    60
    """Decorator wrapper method
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    61
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    62
    Params usage:
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    63
      logic: name of the logic for the data model to iterate through
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    64
      filter: a dict for the properties that the entities should have
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    65
      order: a list with the sort order
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    66
      json: json object with additional parameters
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    67
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    68
    Returns:
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    69
      Standard http django response
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    70
    """
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    71
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    72
    post_dict = request.POST
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    73
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    74
    if 'logic' not in post_dict:
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    75
       return task_responses.terminateTask()
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    76
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    77
    _temp = __import__(post_dict['logic'], globals(), locals(), ['logic'], -1)
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    78
    logic = _temp.logic
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    79
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    80
    filter = None
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    81
    if 'filter' in post_dict:
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    82
      filter = simplejson.loads(post_dict['filter'])
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    83
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    84
    order = None
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    85
    if 'order' in post_dict:
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    86
      order = simplejson.loads(post_dict['order'])
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    87
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    88
    start_key = None
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    89
    if 'next_key' in post_dict:
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    90
      start_key = db.Key(post_dict['start_key'])
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    91
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    92
    json = None
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    93
    if 'json' in post_dict:
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    94
      json = post_dict['json']
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    95
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    96
    entities, start_key = logic.getBatchOfData(filter, order, start_key)
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    97
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    98
    try:
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    99
      new_json = func(request, entities=entities, json=json, *args, **kwargs)
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   100
    except task_responses.FatalTaskError, error:
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   101
      logging.error(error)
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   102
      return task_responses.terminateTask()
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   103
    except Exception, exception:
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   104
      logging.error(exception)
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   105
      return task_responses.repeatTask()
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   106
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   107
    if start_key is None:
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   108
      logging.debug('Task sucessfully completed')
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   109
    else:
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   110
      context = post_dict.copy()
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   111
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   112
      if 'json' in context:
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   113
        del context['json']
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   114
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   115
      context.update({'start_key': start_key})
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   116
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   117
      if new_json is not None:
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   118
        context.update({'json': new_json})
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   119
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   120
      task_responses.startTask(url=request.path, context=context)
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   121
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   122
    return task_responses.terminateTask()
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   123
0b6a093c5c81 Moved the Tasks decorators to their own module.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   124
  return wrapper