| @@ 76-120 (lines=45) @@ | ||
| 73 | registerType(Preservation, PROJECTNAME) |
|
| 74 | ||
| 75 | ||
| 76 | class ajaxGetPreservations: |
|
| 77 | ||
| 78 | catalog_name='senaite_catalog_setup' |
|
| 79 | contentFilter = {'portal_type': 'Preservation',
|
|
| 80 | 'is_active': True} |
|
| 81 | ||
| 82 | def __init__(self, context, request): |
|
| 83 | self.context = context |
|
| 84 | self.request = request |
|
| 85 | ||
| 86 | def __call__(self): |
|
| 87 | ||
| 88 | plone.protect.CheckAuthenticator(self.request) |
|
| 89 | searchTerm = 'searchTerm' in self.request and self.request[ |
|
| 90 | 'searchTerm'].lower() or '' |
|
| 91 | page = self.request['page'] |
|
| 92 | nr_rows = self.request['rows'] |
|
| 93 | sord = self.request['sord'] |
|
| 94 | sidx = self.request['sidx'] |
|
| 95 | rows = [] |
|
| 96 | ||
| 97 | # lookup objects from ZODB |
|
| 98 | catalog = getToolByName(self.context, self.catalog_name) |
|
| 99 | brains = catalog(self.contentFilter) |
|
| 100 | brains = searchTerm and \ |
|
| 101 | [p for p in brains if p.Title.lower().find(searchTerm) > -1] \ |
|
| 102 | or brains |
|
| 103 | ||
| 104 | rows = [{'UID': p.UID,
|
|
| 105 | 'preservation_uid': p.UID, |
|
| 106 | 'Preservation': p.Title, |
|
| 107 | 'Description': p.Description} |
|
| 108 | for p in brains] |
|
| 109 | ||
| 110 | rows = sorted(rows, cmp=lambda x, y: cmp(x.lower( |
|
| 111 | ), y.lower()), key=itemgetter(sidx and sidx or 'Preservation')) |
|
| 112 | if sord == 'desc': |
|
| 113 | rows.reverse() |
|
| 114 | pages = len(rows) / int(nr_rows) |
|
| 115 | pages += divmod(len(rows), int(nr_rows))[1] and 1 or 0 |
|
| 116 | ret = {'page': page,
|
|
| 117 | 'total': pages, |
|
| 118 | 'records': len(rows), |
|
| 119 | 'rows': rows[(int(page) - 1) * int(nr_rows): int(page) * int(nr_rows)]} |
|
| 120 | return json.dumps(ret) |
|
| 121 | ||
| @@ 29-71 (lines=43) @@ | ||
| 26 | from senaite.core.p3compat import cmp |
|
| 27 | ||
| 28 | ||
| 29 | class get_sampletypes(object): |
|
| 30 | ||
| 31 | catalog_name='senaite_catalog_setup' |
|
| 32 | contentFilter = {'portal_type': 'SampleType', |
|
| 33 | 'is_active': True} |
|
| 34 | ||
| 35 | def __init__(self, context, request): |
|
| 36 | self.context = context |
|
| 37 | self.request = request |
|
| 38 | ||
| 39 | def __call__(self): |
|
| 40 | plone.protect.CheckAuthenticator(self.request) |
|
| 41 | searchTerm = 'searchTerm' in self.request and self.request[ |
|
| 42 | 'searchTerm'].lower() or '' |
|
| 43 | page = self.request['page'] |
|
| 44 | nr_rows = self.request['rows'] |
|
| 45 | sord = self.request['sord'] |
|
| 46 | sidx = self.request['sidx'] |
|
| 47 | ||
| 48 | # lookup objects from ZODB |
|
| 49 | catalog = getToolByName(self.context, self.catalog_name) |
|
| 50 | brains = catalog(self.contentFilter) |
|
| 51 | brains = searchTerm and \ |
|
| 52 | [p for p in brains if p.Title.lower().find(searchTerm) > -1] \ |
|
| 53 | or brains |
|
| 54 | ||
| 55 | rows = [{'UID': p.UID, |
|
| 56 | 'sampletype_uid': p.UID, |
|
| 57 | 'SampleType': p.Title, |
|
| 58 | 'Description': p.Description} |
|
| 59 | for p in brains] |
|
| 60 | ||
| 61 | rows = sorted(rows, cmp=lambda x, y: cmp(x.lower( |
|
| 62 | ), y.lower()), key=itemgetter(sidx and sidx or 'SampleType')) |
|
| 63 | if sord == 'desc': |
|
| 64 | rows.reverse() |
|
| 65 | pages = len(rows) / int(nr_rows) |
|
| 66 | pages += divmod(len(rows), int(nr_rows))[1] and 1 or 0 |
|
| 67 | ret = {'page': page, |
|
| 68 | 'total': pages, |
|
| 69 | 'records': len(rows), |
|
| 70 | 'rows': rows[(int(page) - 1) * int(nr_rows): int(page) * int(nr_rows)]} |
|
| 71 | return json.dumps(ret) |
|
| 72 | ||
| @@ 149-193 (lines=45) @@ | ||
| 146 | # TODO: Refactor! |
|
| 147 | # This class is registered as `getcontainers` and used in artemplate to |
|
| 148 | # populate the combogrid field |
|
| 149 | class ajaxGetContainers: |
|
| 150 | ||
| 151 | catalog_name='senaite_catalog_setup' |
|
| 152 | contentFilter = {'portal_type': 'SampleContainer',
|
|
| 153 | 'is_active': True} |
|
| 154 | ||
| 155 | def __init__(self, context, request): |
|
| 156 | self.context = context |
|
| 157 | self.request = request |
|
| 158 | ||
| 159 | def __call__(self): |
|
| 160 | ||
| 161 | plone.protect.CheckAuthenticator(self.request) |
|
| 162 | searchTerm = 'searchTerm' in self.request and self.request[ |
|
| 163 | 'searchTerm'].lower() or '' |
|
| 164 | page = self.request['page'] |
|
| 165 | nr_rows = self.request['rows'] |
|
| 166 | sord = self.request['sord'] |
|
| 167 | sidx = self.request['sidx'] |
|
| 168 | rows = [] |
|
| 169 | ||
| 170 | # lookup objects from ZODB |
|
| 171 | catalog = getToolByName(self.context, self.catalog_name) |
|
| 172 | brains = catalog(self.contentFilter) |
|
| 173 | brains = searchTerm and \ |
|
| 174 | [p for p in brains if p.Title.lower().find(searchTerm) > -1] \ |
|
| 175 | or brains |
|
| 176 | ||
| 177 | rows = [{'UID': p.UID,
|
|
| 178 | 'container_uid': p.UID, |
|
| 179 | 'Container': p.Title, |
|
| 180 | 'Description': p.Description} |
|
| 181 | for p in brains] |
|
| 182 | ||
| 183 | rows = sorted(rows, cmp=lambda x, y: cmp(x.lower( |
|
| 184 | ), y.lower()), key=itemgetter(sidx and sidx or 'Container')) |
|
| 185 | if sord == 'desc': |
|
| 186 | rows.reverse() |
|
| 187 | pages = len(rows) / int(nr_rows) |
|
| 188 | pages += divmod(len(rows), int(nr_rows))[1] and 1 or 0 |
|
| 189 | ret = {'page': page,
|
|
| 190 | 'total': pages, |
|
| 191 | 'records': len(rows), |
|
| 192 | 'rows': rows[(int(page) - 1) * int(nr_rows): int(page) * int(nr_rows)]} |
|
| 193 | return json.dumps(ret) |
|
| 194 | ||