1
|
|
|
# -*- coding: utf-8 -*- |
2
|
|
|
# |
3
|
|
|
# This file is part of SENAITE.HEALTH. |
4
|
|
|
# |
5
|
|
|
# SENAITE.HEALTH is free software: you can redistribute it and/or modify it |
6
|
|
|
# under the terms of the GNU General Public License as published by the Free |
7
|
|
|
# Software Foundation, version 2. |
8
|
|
|
# |
9
|
|
|
# This program is distributed in the hope that it will be useful, but WITHOUT |
10
|
|
|
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
11
|
|
|
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
12
|
|
|
# details. |
13
|
|
|
# |
14
|
|
|
# You should have received a copy of the GNU General Public License along with |
15
|
|
|
# this program; if not, write to the Free Software Foundation, Inc., 51 |
16
|
|
|
# Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
17
|
|
|
# |
18
|
|
|
# Copyright 2018-2019 by it's authors. |
19
|
|
|
# Some rights reserved, see README and LICENSE. |
20
|
|
|
|
21
|
|
|
import json |
22
|
|
|
from operator import itemgetter |
23
|
|
|
|
24
|
|
|
import plone |
25
|
|
|
from bika.lims.browser import BrowserView |
26
|
|
|
|
27
|
|
|
|
28
|
|
View Code Duplication |
class ajaxGetAetiologicAgentSubtypes(BrowserView): |
|
|
|
|
29
|
|
|
""" Aetologic Agent Subtypes for a specified Aetiologic Agent from site setup |
30
|
|
|
""" |
31
|
|
|
def __call__(self): |
32
|
|
|
plone.protect.CheckAuthenticator(self.request) |
33
|
|
|
title = 'title' in self.request and self.request['title'] or '' |
34
|
|
|
auid = 'auid' in self.request and self.request['auid'] or '' |
35
|
|
|
searchTerm = (len(title) == 0 and 'searchTerm' in self.request) and self.request['searchTerm'].lower() or title.lower() |
36
|
|
|
page = 'page' in self.request and self.request['page'] or 1 |
37
|
|
|
nr_rows = 'rows' in self.request and self.request['rows'] or 10 |
38
|
|
|
sord = 'sord' in self.request and self.request['sord'] or 'asc' |
39
|
|
|
sidx = 'sidx' in self.request and self.request['sidx'] or 'Subtype' |
40
|
|
|
rows = [] |
41
|
|
|
|
42
|
|
|
agents = self.bika_setup_catalog(portal_type='AetiologicAgent', UID=auid) |
43
|
|
|
if agents and len(agents) == 1: |
44
|
|
|
agent = agents[0].getObject() |
45
|
|
|
subtypes = agent.getAetiologicAgentSubtypes() |
46
|
|
|
if subtypes and searchTerm: |
47
|
|
|
subtypes = [subtype for subtype in subtypes if subtype['Subtype'].lower().find(searchTerm) > -1] |
48
|
|
|
|
49
|
|
|
for subtype in subtypes: |
|
|
|
|
50
|
|
|
if (len(title) > 0 and subtype['Subtype'] == title): |
51
|
|
|
rows.append({'Subtype': subtype['Subtype'], |
52
|
|
|
'SubtypeRemarks': subtype.get('SubtypeRemarks', '')}) |
53
|
|
|
elif len(title) == 0: |
54
|
|
|
rows.append({'Subtype': subtype['Subtype'], |
55
|
|
|
'SubtypeRemarks': subtype.get('SubtypeRemarks', '')}) |
56
|
|
|
|
57
|
|
|
rows = sorted(rows, cmp=lambda x, y: cmp(x.lower(), y.lower()), key=itemgetter(sidx and sidx or 'Subtype')) |
|
|
|
|
58
|
|
|
if sord == 'desc': |
59
|
|
|
rows.reverse() |
60
|
|
|
pages = len(rows) / int(nr_rows) |
61
|
|
|
pages += divmod(len(rows), int(nr_rows))[1] and 1 or 0 |
62
|
|
|
ret = {'page': page, |
63
|
|
|
'total': pages, |
64
|
|
|
'records': len(rows), |
65
|
|
|
'rows': rows[(int(page) - 1) * int(nr_rows): int(page) * int(nr_rows)]} |
66
|
|
|
|
67
|
|
|
return json.dumps(ret) |
68
|
|
|
|