-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path00_tables.py
584 lines (519 loc) · 23.1 KB
/
00_tables.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
# -*- coding: utf-8 -*-
"""
Global tables and re-usable fields
"""
# =============================================================================
# Import models
#
from s3.s3model import S3Model
import eden as models
current.models = models
current.s3db = s3db = S3Model()
# Explicit import statements to have them reload automatically in debug mode
import eden.asset
import eden.auth
import eden.cms
import eden.delphi
import eden.doc
import eden.dvi
import eden.event
import eden.fire
import eden.gis
import eden.hms
import eden.hrm
import eden.inv
import eden.irs
import eden.msg
import eden.ocr
import eden.org
import eden.patient
import eden.pr
import eden.sit
import eden.proc
import eden.project
import eden.req
import eden.scenario
import eden.supply
import eden.support
import eden.survey
import eden.sync
import eden.vehicle
# =============================================================================
# Import S3 meta fields into global namespace
#
from s3.s3fields import *
# =============================================================================
# Record authorship meta-fields
# Author of a record
s3_meta_created_by = S3ReusableField("created_by", db.auth_user,
readable=False,
writable=False,
requires=None,
default=session.auth.user.id
if auth.is_logged_in()
else None,
represent=s3_user_represent,
ondelete="RESTRICT")
# Last author of a record
s3_meta_modified_by = S3ReusableField("modified_by", db.auth_user,
readable=False,
writable=False,
requires=None,
default=session.auth.user.id
if auth.is_logged_in()
else None,
update=session.auth.user.id
if auth.is_logged_in()
else None,
represent=s3_user_represent,
ondelete="RESTRICT")
def s3_authorstamp():
return (s3_meta_created_by(),
s3_meta_modified_by())
# =============================================================================
# Record ownership meta-fields
# Individual user who owns the record
s3_meta_owned_by_user = S3ReusableField("owned_by_user", db.auth_user,
readable=False,
writable=False,
requires=None,
default=session.auth.user.id
if auth.is_logged_in()
else None,
represent=lambda id: \
id and s3_user_represent(id) or UNKNOWN_OPT,
ondelete="RESTRICT")
# Role of users who collectively own the record
s3_meta_owned_by_role = S3ReusableField("owned_by_role", "integer",
readable=False,
writable=False,
requires=None,
default=None,
represent=s3_role_represent)
# Role of the Organisation the record belongs to
s3_meta_owned_by_organisation = S3ReusableField("owned_by_organisation", "integer",
readable=False,
writable=False,
requires=None,
default=None,
represent=s3_role_represent)
# Role of the Facility the record belongs to
s3_meta_owned_by_facility = S3ReusableField("owned_by_facility", "integer",
readable=False,
writable=False,
requires=None,
default=None,
represent=s3_role_represent)
def s3_ownerstamp():
return (s3_meta_owned_by_user(),
s3_meta_owned_by_role(),
s3_meta_owned_by_organisation(),
s3_meta_owned_by_facility())
# Make available for S3Models
s3.ownerstamp = s3_ownerstamp
# =============================================================================
def s3_timestamp():
return (s3_meta_created_on(),
s3_meta_modified_on(),
)
# Make available for S3Models
s3.timestamp = s3_timestamp
# =============================================================================
# Common meta-fields
# @todo: can this be moved into s3fields.py?
#
def s3_meta_fields():
fields = (s3_meta_uuid(),
s3_meta_mci(),
s3_meta_deletion_status(),
s3_meta_deletion_fk(),
s3_meta_created_on(),
s3_meta_modified_on(),
s3_meta_created_by(),
s3_meta_modified_by(),
s3_meta_owned_by_user(),
s3_meta_owned_by_role(),
s3_meta_owned_by_organisation(),
s3_meta_owned_by_facility())
return fields
# Make available for S3Models
s3.meta_fields = s3_meta_fields
# =============================================================================
response.s3.all_meta_field_names = [field.name for field in
[s3_meta_uuid(),
s3_meta_mci(),
s3_meta_deletion_status(),
s3_meta_deletion_fk(),
s3_meta_created_on(),
s3_meta_modified_on(),
s3_meta_created_by(),
s3_meta_modified_by(),
s3_meta_owned_by_user(),
s3_meta_owned_by_role(),
s3_meta_owned_by_organisation(),
s3_meta_owned_by_facility()
]]
# =============================================================================
# Reusable field for scheduler task links
#
scheduler_task_id = S3ReusableField("scheduler_task_id",
"reference %s" % s3base.S3Task.TASK_TABLENAME,
ondelete="CASCADE")
s3.scheduler_task_id = scheduler_task_id
# =============================================================================
# Reusable roles fields for map layer permissions management (GIS)
role_required = S3ReusableField("role_required", db.auth_group,
sortby="role",
requires = IS_NULL_OR(IS_ONE_OF(db,
"auth_group.id",
"%(role)s",
zero=T("Public"))),
widget = S3AutocompleteWidget(
"auth",
"group",
fieldname="role"),
represent = s3_role_represent,
label = T("Role Required"),
comment = DIV(_class="tooltip",
_title="%s|%s" % (T("Role Required"),
T("If this record should be restricted then select which role is required to access the record here."))),
ondelete = "RESTRICT")
roles_permitted = S3ReusableField("roles_permitted", 'list:reference auth_group',
sortby="role",
requires = IS_NULL_OR(IS_ONE_OF(db,
"auth_group.id",
"%(role)s",
multiple=True)),
# @ToDo
#widget = S3CheckboxesWidget(lookup_table_name = "auth_group",
# lookup_field_name = "role",
# multiple = True),
represent = s3_role_represent,
label = T("Roles Permitted"),
comment = DIV(_class="tooltip",
_title="%s|%s" % (T("Roles Permitted"),
T("If this record should be restricted then select which role(s) are permitted to access the record here."))),
ondelete = "RESTRICT")
# Make available for S3Models
s3.role_required = role_required
s3.roles_permitted = roles_permitted
# =============================================================================
# Other reusable fields
# -----------------------------------------------------------------------------
# Reusable comments field to include in other table definitions
s3_comments = S3ReusableField("comments", "text",
label = T("Comments"),
widget = s3_comments_widget,
comment = DIV(_class="tooltip",
_title="%s|%s" % (T("Comments"),
T("Please use this field to record any additional information, including a history of the record if it is updated."))))
s3.comments = s3_comments
# -----------------------------------------------------------------------------
# Reusable currency field to include in other table definitions
#
# @ToDo: Move to a Finance module
#
currency_type_opts = deployment_settings.get_fin_currencies()
default_currency = deployment_settings.get_fin_currency_default()
currency_type = S3ReusableField("currency_type", "string",
length = 3,
#notnull=True,
requires = IS_IN_SET(currency_type_opts.keys(),
zero=None),
default = default_currency,
label = T("Currency"),
#represent = lambda opt: \
# currency_type_opts.get(opt, UNKNOWN_OPT),
writable = deployment_settings.get_fin_currency_writable())
response.s3.currency_type = currency_type
# =============================================================================
# Lx
#
# These fields are populated onaccept from location_id
# - for many reads to fewer writes, this is faster than Virtual Fields
#
# Labels that vary by country are set by gis.update_table_hierarchy_labels()
#
address_L4 = S3ReusableField("L4",
#label=gis.get_location_hierarchy("L4"),
readable=False,
writable=False)
address_L3 = S3ReusableField("L3",
#label=gis.get_location_hierarchy("L3"),
readable=False,
writable=False)
address_L2 = S3ReusableField("L2",
#label=gis.get_location_hierarchy("L2"),
readable=False,
writable=False)
address_L1 = S3ReusableField("L1",
#label=gis.get_location_hierarchy("L1"),
readable=False,
writable=False)
address_L0 = S3ReusableField("L0",
# L0 Location Name never varies except with a Translation
label=T("Country"),
readable=False,
writable=False)
# -----------------------------------------------------------------------------
def lx_fields():
# return multiple reusable fields
fields = (
address_L4(),
address_L3(),
address_L2(),
address_L1(),
address_L0(),
)
return fields
s3.lx_fields = lx_fields
# -----------------------------------------------------------------------------
# Hide Lx fields in Create forms
# inc list_create (list_fields over-rides)
def lx_hide(table):
table.L4.readable = False
table.L3.readable = False
table.L2.readable = False
table.L1.readable = False
table.L0.readable = False
return
s3.lx_hide = lx_hide
# -----------------------------------------------------------------------------
def lx_onvalidation(form):
"""
Write the Lx fields from the Location
- used by pr_person, hrm_training, irs_ireport
@ToDo: Allow the reverse operation.
If these fields are populated then create/update the location
"""
vars = form.vars
if "location_id" in vars and vars.location_id:
table = s3db.gis_location
query = (table.id == vars.location_id)
location = db(query).select(table.name,
table.level,
table.parent,
table.path,
limitby=(0, 1)).first()
if location:
if location.level == "L0":
vars.L0 = location.name
elif location.level == "L1":
vars.L1 = location.name
if location.parent:
query = (table.id == location.parent)
country = db(query).select(table.name,
limitby=(0, 1)).first()
if country:
vars.L0 = country.name
else:
# Get Names of ancestors at each level
vars = gis.get_parent_per_level(vars,
vars.location_id,
feature=location,
ids=False,
names=True)
s3.lx_onvalidation = lx_onvalidation
# -----------------------------------------------------------------------------
def lx_update(table, record_id):
"""
Write the Lx fields from the Location
- used by hrm_human_resource & pr_address
@ToDo: Allow the reverse operation.
If these fields are populated then create/update the location
"""
if "location_id" in table:
ltable = s3db.gis_location
query = (table.id == record_id) & \
(ltable.id == table.location_id)
location = db(query).select(ltable.id,
ltable.name,
ltable.level,
ltable.parent,
ltable.path,
limitby=(0, 1)).first()
if location:
vars = Storage()
if location.level == "L0":
vars.L0 = location.name
elif location.level == "L1":
vars.L1 = location.name
if location.parent:
query = (ltable.id == location.parent)
country = db(query).select(ltable.name,
limitby=(0, 1)).first()
if country:
vars.L0 = country.name
else:
# Get Names of ancestors at each level
vars = gis.get_parent_per_level(vars,
location.id,
feature=location,
ids=False,
names=True)
# Update record
db(table.id == record_id).update(**vars)
s3.lx_update = lx_update
# =============================================================================
# Addresses
#
# These fields are populated onaccept from location_id
#
# @ToDo: Add Postcode to gis.update_table_hierarchy_labels()
#
address_building_name = S3ReusableField("building_name",
label=T("Building Name"),
readable=False,
writable=False)
address_address = S3ReusableField("address",
label=T("Address"),
readable=False,
writable=False)
address_postcode = S3ReusableField("postcode",
label=deployment_settings.get_ui_label_postcode(),
readable=False,
writable=False)
# -----------------------------------------------------------------------------
def address_fields():
# return multiple reusable fields
fields = (
address_building_name(),
address_address(),
address_postcode(),
address_L4(),
address_L3(),
address_L2(),
address_L1(),
address_L0(),
)
return fields
s3.address_fields = address_fields
# -----------------------------------------------------------------------------
# Hide Address fields in Create forms
# inc list_create (list_fields over-rides)
def address_hide(table):
table.building_name.readable = False
table.address.readable = False
table.L4.readable = False
table.L3.readable = False
table.L2.readable = False
table.L1.readable = False
table.L0.readable = False
table.postcode.readable = False
return
s3.address_hide = address_hide
# -----------------------------------------------------------------------------
def address_onvalidation(form):
"""
Write the Address fields from the Location
- used by pr_address, org_office & cr_shelter
@ToDo: Allow the reverse operation.
If these fields are populated then create/update the location
"""
vars = form.vars
if "location_id" in vars and vars.location_id:
table = s3db.gis_location
# Read Postcode & Street Address
query = (table.id == vars.location_id)
location = db(query).select(table.addr_street,
table.addr_postcode,
table.name,
table.level,
table.parent,
table.path,
limitby=(0, 1)).first()
if location:
vars.address = location.addr_street
vars.postcode = location.addr_postcode
if location.level == "L0":
vars.L0 = location.name
elif location.level == "L1":
vars.L1 = location.name
if location.parent:
query = (table.id == location.parent)
country = db(query).select(table.name,
limitby=(0, 1)).first()
if country:
vars.L0 = country.name
else:
if location.level is None:
vars.building_name = location.name
# Get Names of ancestors at each level
vars = gis.get_parent_per_level(vars,
vars.location_id,
feature=location,
ids=False,
names=True)
s3.address_onvalidation = address_onvalidation
# -----------------------------------------------------------------------------
def address_update(table, record_id):
"""
Write the Address fields from the Location
- used by asset_asset
@ToDo: Allow the reverse operation.
If these fields are populated then create/update the location
"""
if "location_id" in table:
ltable = s3db.gis_location
# Read Postcode & Street Address
query = (table.id == record_id) & \
(ltable.id == table.location_id)
location = db(query).select(ltable.id,
ltable.addr_street,
ltable.addr_postcode,
ltable.name,
ltable.level,
ltable.parent,
ltable.path,
limitby=(0, 1)).first()
if location:
vars = Storage()
vars.address = location.addr_street
vars.postcode = location.addr_postcode
if location.level == "L0":
vars.L0 = location.name
elif location.level == "L1":
vars.L1 = location.name
if location.parent:
query = (ltable.id == location.parent)
country = db(query).select(ltable.name,
limitby=(0, 1)).first()
if country:
vars.L0 = country.name
else:
if location.level is None:
vars.building_name = location.name
# Get Names of ancestors at each level
vars = gis.get_parent_per_level(vars,
location.id,
feature=location,
ids=False,
names=True)
# Update record
db(table.id == record_id).update(**vars)
s3.address_update = address_update
# =============================================================================
# Default CRUD strings
#
ADD_RECORD = T("Add Record")
LIST_RECORDS = T("List Records")
s3.crud_strings = Storage(
title_create = ADD_RECORD,
title_display = T("Record Details"),
title_list = LIST_RECORDS,
title_update = T("Edit Record"),
title_search = T("Search Records"),
subtitle_create = T("Add New Record"),
subtitle_list = T("Available Records"),
label_list_button = LIST_RECORDS,
label_create_button = ADD_RECORD,
label_delete_button = T("Delete Record"),
msg_record_created = T("Record added"),
msg_record_modified = T("Record updated"),
msg_record_deleted = T("Record deleted"),
msg_list_empty = T("No Records currently available"),
msg_match = T("Matching Records"),
msg_no_match = T("No Matching Records"),
name_nice = T("Record"),
name_nice_plural = T("Records"))
# END =========================================================================