diff --git a/composer/report/base.py b/composer/report/base.py index 0cd8052..62f2a16 100644 --- a/composer/report/base.py +++ b/composer/report/base.py @@ -73,13 +73,14 @@ class Report(Template): fields = Jeep((field.label,)) defaultOutputFields = (field.label,) + defaultSortCriteria = (field.label,) presentationFormat = None renderers = () - sortSpec = () - outputFields = () - queryCriteria = None + outputFields = () + sortCriteria = () + def __init__(self, name): self.name = name @@ -87,9 +88,6 @@ class Report(Template): def getQueryFields(self): return [f for f in self.fields if 'query' in f.executionSteps] - def getSortFields(self): - return [f for f in self.fields if 'sort' in f.executionSteps] - def getOutputFields(self): return [f for f in self.fields if 'output' in f.executionSteps] @@ -105,6 +103,20 @@ class Report(Template): return [f for f in self.getOutputFields() if f.name not in activeNames] + def getSortFields(self): + return [f for f in self.fields if 'sort' in f.executionSteps] + + def getSortCriteria(self): + if not self.sortCriteria: + fieldNames = [f.name for f in self.getSortFields()] + return [f for f in self.defaultSortCriteria + if f.name in fieldNames] + return self.sortCriteria + + def getAvailableSortFields(self): + sortCriteria = [f.name for f in self.getSortCriteria()] + return [f for f in self.getSortFields() if f.name not in sortCriteria] + def getPresentationFormats(self): return [dict(renderer='default', title='Default')] diff --git a/composer/report/field.py b/composer/report/field.py index fd67078..09eaf08 100644 --- a/composer/report/field.py +++ b/composer/report/field.py @@ -59,6 +59,10 @@ class Field(Component): def name(self): return self.__name__ + def getSortValue(self, row): + # TODO: consider 'descending' flag, use raw value instead of formatted one + return getattr(row, self.name) + label = Field('label', u'Label', u'A short text that identifies a row for humans.') diff --git a/composer/report/result.py b/composer/report/result.py new file mode 100644 index 0000000..673f4c5 --- /dev/null +++ b/composer/report/result.py @@ -0,0 +1,51 @@ +# +# Copyright (c) 2010 Helmut Merz helmutm@cy55.de +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +# + +""" +Report result sets and related classes. + +$Id$ +""" + + +class Row(object): + + def __init__(self, context): + self.context = context + + def __getattr__(self, attr): + return getattr(self.context, attr) + + +class ResultSet(object): + + def __init__(self, context, data, rowFactory=Row, sortCriteria=None): + self.context = context + self.data = data + self.rowFactory = rowFactory + self.sortCriteria = sortCriteria + + def getResult(self): + result = [self.rowFactory(item) for item in self.data] + if self.sortCriteria: + result.sort(key=lambda x: [f.getSortValue(x) for f in self.sortCriteria]) + return result + + def __iter__(self): + return iter(self.getResult()) + diff --git a/reporter/resultset.py b/reporter/resultset.py index f8eca7e..b6fcd8e 100644 --- a/reporter/resultset.py +++ b/reporter/resultset.py @@ -1,5 +1,5 @@ # -# Copyright (c) 2008 Helmut Merz helmutm@cy55.de +# Copyright (c) 2010 Helmut Merz helmutm@cy55.de # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by @@ -17,8 +17,10 @@ # """ -Example classes for the cybertools.reporter package. These use the -cybertools.contact package +Result set and related classes for reporting. + +Now obsolete (but still used in some projects), +use cybertools.composer.report.result instead. $Id$ """