Skip to content

Commit

Permalink
Component hooks: allow filtering by multiple fields, DRK: use this to
Browse files Browse the repository at this point in the history
include only dates of completed appointments in case list XLS exports,
include UUID for automatic updates
  • Loading branch information
nursix committed Feb 23, 2016
1 parent 7e25b2d commit da6ea57
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 29 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
550f323 (2016-02-22 19:23:54)
nursix-1.1.0-devel-3437-g7e25b2d (2016-02-23 08:02:49)
25 changes: 24 additions & 1 deletion modules/s3/s3resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,30 @@ def _attach(self, alias, hook):
component.multiple = hook.multiple
component.defaults = hook.defaults

if not filterby:
if isinstance(filterby, dict):
# Filter by multiple criteria
query = None
table = hook.table
for k, v in filterby.items():
is_list = isinstance(v, (tuple, list))
if is_list and len(v) == 1:
filterfor = v[0]
is_list = False
else:
filterfor = v
if not is_list:
subquery = (table[k] == filterfor)
elif filterfor:
subquery = (table[hook.filterby].belongs(filterfor))
else:
subquery = None
if subquery:
if query is None:
query = subquery
else:
query &= subquery
component.filter = query
elif not filterby:
# Can use filterby=False to enforce table aliasing yet
# suppress component filtering (useful e.g. with two
# foreign key links from the same table)
Expand Down
58 changes: 31 additions & 27 deletions modules/templates/DRK/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -875,38 +875,41 @@ def custom_prep(r):
]
if absence_field:
list_fields.append(absence_field)

if r.representation == "xls":

# Extra list_fields for XLS export
atypes = {"GU": None,
"X-Ray": None,
}
attable = s3db.dvr_case_appointment_type
appointment_type = db(attable.name == "GU").select(attable.id,
limitby=(0, 1)
).first()
try:
gu = appointment_type.id
except:
# Prepop not done
gu = None
appointment_type = db(attable.name == "X-Ray").select(attable.id,
limitby=(0, 1)
).first()
try:
xray = appointment_type.id
except:
# Prepop not done
xray = None

query = attable.name.belongs(atypes.keys())
rows = db(query).select(attable.id,
attable.name,
)
for row in rows:
atypes[row.name] = row.id

# Filtered Components
COMPLETED = 4
s3db.add_components("pr_person",
dvr_case_appointment = ({"name": "gu",
"joinby": "person_id",
"filterby": "type_id",
"filterfor": (gu,),
},
{"name": "xray",
"joinby": "person_id",
"filterby": "type_id",
"filterfor": (xray,),
},
),
dvr_case_appointment = (
{"name": "gu",
"joinby": "person_id",
"filterby": {"type_id": (atypes["GU"],),
"status": COMPLETED,
},
},
{"name": "xray",
"joinby": "person_id",
"filterby": {"type_id": (atypes["X-Ray"],),
"status": COMPLETED,
}
},
),
)

list_fields += [# Date of the GU (GU = Health Screening, case appointments)
(T("GU"), "gu.date"),
# Date of the X-Ray (case appointments)
Expand All @@ -918,6 +921,7 @@ def custom_prep(r):
"shelter_registration.check_in_date",
# Last Check-out (if checked-out)
"shelter_registration.check_out_date",
("UUID", "uuid"),
]
configure(list_fields = list_fields)

Expand Down

0 comments on commit da6ea57

Please sign in to comment.