app/soc/logic/lists.py
changeset 625 ec867361b5ba
parent 572 1b3e7280743a
child 1307 091a21cf3627
equal deleted inserted replaced
624:811f50717bea 625:ec867361b5ba
    12 # distributed under the License is distributed on an "AS IS" BASIS,
    12 # distributed under the License is distributed on an "AS IS" BASIS,
    13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14 # See the License for the specific language governing permissions and
    14 # See the License for the specific language governing permissions and
    15 # limitations under the License.
    15 # limitations under the License.
    16 
    16 
    17 """List generation logic
    17 """List generation logic.
    18 """
    18 """
    19 
    19 
    20 __authors__ = [
    20 __authors__ = [
    21   '"Sverre Rabbelier" <sverre@rabbelier.nl>',
    21   '"Sverre Rabbelier" <sverre@rabbelier.nl>',
    22   ]
    22   ]
    23 
    23 
    24 
    24 
    25 class Lists(object):
    25 class Lists(object):
    26   """List array suitable for enumerating over with just 'for in'
    26   """List array suitable for enumerating over with just 'for in'.
    27   """
    27   """
    28 
    28 
    29   DEF_PASSTHROUGH_FIELDS = [
    29   DEF_PASSTHROUGH_FIELDS = [
    30       'pagination',
    30       'pagination',
    31       'pagination_form',
    31       'pagination_form',
    39       'first',
    39       'first',
    40       'last',
    40       'last',
    41       ]
    41       ]
    42 
    42 
    43   def __init__(self, contents):
    43   def __init__(self, contents):
    44     """Constructs a new Lists object with the specified contents
    44     """Constructs a new Lists object with the specified contents.
    45     """
    45     """
    46 
    46 
    47     # All the contents of all the lists
    47     # All the contents of all the lists
    48     self.contents = contents
    48     self._contents = contents
    49     self.content = {}
    49     self._content = {}
    50 
    50 
    51     # For iterating over all the lists
    51     # For iterating over all the lists
    52     self.lists = range(len(contents))
    52     self._lists = range(len(contents))
    53     self.list_data = []
    53     self._list_data = []
    54 
    54 
    55     # For iterating over the rows
    55     # For iterating over the rows
    56     self.rows = []
    56     self._rows = []
    57     self.row_data = []
    57     self._row_data = []
    58 
    58 
    59   def __getattr__(self, attr):
    59   def __getattr__(self, attr):
    60     """Delegate field lookup to the current list if appropriate
    60     """Delegate field lookup to the current list if appropriate.
    61 
    61 
    62     If, and only if, a lookup is done on one of the fields defined in
    62     If, and only if, a lookup is done on one of the fields defined in
    63     DEF_PASSTHROUGH_FIELDS, and the current list defines this field,
    63     DEF_PASSTHROUGH_FIELDS, and the current list defines this field,
    64     the value from the current list is returned.
    64     the value from the current list is returned.
    65     """
    65     """
    66 
    66 
    67     if attr not in self.DEF_PASSTHROUGH_FIELDS:
    67     if attr not in self.DEF_PASSTHROUGH_FIELDS:
    68       raise AttributeError()
    68       raise AttributeError()
    69 
    69 
    70     if attr not in self.content:
    70     if attr not in self._content:
    71       raise AttributeError()
    71       raise AttributeError()
    72 
    72 
    73     return self.get(attr)
    73     return self.get(attr)
    74 
    74 
    75   def get(self, item):
    75   def get(self, item):
    76     """Returns the item for the current list data
    76     """Returns the item for the current list data.
    77     """
    77     """
    78 
    78 
    79     return self.content[item]
    79     return self._content[item]
    80 
    80 
    81   def next_list(self):
    81   def nextList(self):
    82     """Shifts out the current list content
    82     """Shifts out the current list content.
    83 
    83 
    84     The main content of the next list is returned for further processing.
    84     The main content of the next list is returned for further processing.
    85     """
    85     """
    86 
    86 
    87     # Advance the list data once
    87     # Advance the list data once
    88     self.content = self.contents[0]
    88     self._content = self._contents[0]
    89     self.contents = self.contents[1:]
    89     self._contents = self._contents[1:]
    90 
    90 
    91     # Update internal 'iterators'
    91     # Update internal 'iterators'
    92     self.list_data = self.get('data')
    92     self._list_data = self.get('data')
    93     self.rows = range(len(self.list_data))
    93     self._rows = range(len(self._list_data))
    94 
    94 
    95     return self.get('main')
    95     return self.get('main')
    96 
    96 
    97   def next_row(self):
    97   def nextRow(self):
    98     """Returns the next list row for the current list
    98     """Returns the next list row for the current list.
    99 
    99 
   100     Before calling this method, next_list should be called at least once.
   100     Before calling this method, nextList should be called at least once.
   101     """
   101     """
   102 
   102 
   103     # Update internal 'iterators'
   103     # Update internal 'iterators'
   104     self.row_data =  self.list_data[0]
   104     self._row_data =  self._list_data[0]
   105 
   105 
   106     # Advance the row data once
   106     # Advance the row data once
   107     self.list_data = self.list_data[1:]
   107     self._list_data = self._list_data[1:]
   108 
   108 
   109     return self.get('row')
   109     return self.get('row')
   110 
   110 
   111   def lists(self):
   111   def lists(self):
   112     """Returns a list of numbers the size of the amount of lists.
   112     """Returns a list of numbers the size of the amount of lists.
   113 
   113 
   114     This method can be used to iterate over all lists with shift,
   114     This method can be used to iterate over all lists with shift,
   115     without using a while loop.
   115     without using a while loop.
   116     """
   116     """
   117 
   117 
   118     return self.lists
   118     return self._lists
   119 
   119 
   120   def rows(self):
   120   def rows(self):
   121     """Returns a list of numbers the size of the amount of items.
   121     """Returns a list of numbers the size of the amount of items.
   122 
   122 
   123     This method can be used to iterate over all items with next for
   123     This method can be used to iterate over all items with next for
   124     the current list, without using a while loop.
   124     the current list, without using a while loop.
   125     """
   125     """
   126 
   126 
   127     return self.rows
   127     return self._rows
   128 
   128 
   129   def item(self):
   129   def item(self):
   130     """Returns the current row item for the current list
   130     """Returns the current row item for the current list.
   131 
   131 
   132     Before calling this method, next_row should be called at least once.
   132     Before calling this method, nextRow should be called at least once.
   133     """
   133     """
   134 
   134 
   135     return self.row_data
   135     return self._row_data
   136 
   136 
   137   def redirect(self):
   137   def redirect(self):
   138     """Returns the redirect for the current row item in the current list.
   138     """Returns the redirect for the current row item in the current list.
   139     """
   139     """
   140 
   140 
   141     action, args = self.get('action')
   141     action, args = self.get('action')
   142     result = action(self.row_data, args)
   142     return action(self._row_data, args)
   143     return result