forked from sahana/eden
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcap.py
691 lines (591 loc) · 29.4 KB
/
cap.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
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
# -*- coding: utf-8 -*-
"""
CAP Module - Controllers
"""
module = request.controller
resourcename = request.function
if not settings.has_module(module):
raise HTTP(404, body="Module disabled: %s" % module)
# -----------------------------------------------------------------------------
def index():
""" Module's Home Page """
module_name = settings.modules[module].name_nice
response.title = module_name
return dict(module_name=module_name)
# -----------------------------------------------------------------------------
def info_prep(r):
"""
Preprocessor for CAP Info segments
- whether accessed via /eden/info or /eden/alert/x/info
"""
if s3.debug:
s3.scripts.append("/%s/static/scripts/S3/s3.cap.js" % appname)
else:
s3.scripts.append("/%s/static/scripts/S3/s3.cap.min.js" % appname)
s3.stylesheets.append("S3/cap.css")
table = db.cap_info
if r.representation == "html":
if r.component:
if r.component_name == "info":
info_fields_comments()
elif r.component_name == "area":
area_fields_comments()
elif r.component_name == "resource":
resource_fields_comments()
elif r.tablename == "cap_info":
info_fields_comments()
item = r.record
if item and r.tablename == "cap_info" and \
s3db.cap_alert_is_template(item.alert_id):
for f in ["urgency", "certainty",
"effective", "onset", "expires",
"priority", "severity"]:
field = table[f]
field.writable = False
field.readable = False
field.required = False
for f in ["category", "event"]:
table[f].required = False
post_vars = request.post_vars
template_id = None
if post_vars.get("language", False):
if r.tablename == "cap_info":
# cap/info controller
try:
template_id = db(table.id == r.id).select(table.template_info_id,
limitby=(0, 1)
).first().template_info_id
except AttributeError, KeyError:
pass
elif r.component_name == "info":
# cap/x/info component tab
try:
template_id = r.component.get_id()
# this will error out if component is not yet saved
except:
pass
if template_id:
# Read template and copy locked fields to post_vars
template = db(table.id == template_id).select(limitby=(0, 1)).first()
settings = json.loads(template.template_settings)
if isinstance(settings.get("locked", False), dict):
locked_fields = [lf for lf in settings["locked"] if settings["locked"]]
for lf in locked_fields:
post_vars[lf] = template[lf]
return True
# -----------------------------------------------------------------------------
def alert():
""" REST controller for CAP Alerts and Components """
def prep(r):
if r.id:
if r.record.is_template:
redirect(URL(c="cap", f="template",
args = request.args,
vars = request.vars))
else:
s3.filter = (r.table.is_template == False)
s3.formats["cap"] = r.url() # .have added by JS
if r.interactive:
alert_fields_comments()
if not r.component:
if r.method != "import":
s3.crud.submit_style = "hide"
s3.crud.custom_submit = (("edit_info",
T("Save and edit information"),
"",
),)
elif r.component_name in ("area", "resource"):
# Limit to those for this Alert
r.component.table.info_id.requires = IS_EMPTY_OR(
IS_ONE_OF(current.db, "cap_info.id",
s3db.cap_info_represent,
filterby="alert_id",
filter_opts=(r.id,),
))
elif r.component_name == "location":
# Limit to those for this Alert
r.component.table.area_id.requires = IS_EMPTY_OR(
IS_ONE_OF(current.db, "cap_area.id",
s3db.cap_area_represent,
filterby="alert_id",
filter_opts=(r.id,),
))
#elif r.representation == "cap":
# # This is either importing from or exporting to cap format. Set both
# # postprocessing hooks so we don't have to enumerate methods.
# s3db.configure("gis_location",
# xml_post_parse = s3db.cap_gis_location_xml_post_parse,
# xml_post_render = s3db.cap_gis_location_xml_post_render,
# )
post_vars = request.post_vars
if post_vars.get("edit_info", False):
tid = post_vars["template_id"]
if tid:
# Read template and copy locked fields to post_vars
table = db.cap_alert
template = db(table.id == tid).select(limitby=(0, 1)).first()
try:
tsettings = json.loads(template.template_settings)
except ValueError:
tsettings = dict()
if isinstance(tsettings.get("locked", False), dict):
locked_fields = [lf for lf in tsettings["locked"] if tsettings["locked"]]
for lf in locked_fields:
post_vars[lf] = template[lf]
info_prep(r)
return True
s3.prep = prep
def postp(r, output):
"""
REST post-processor:
- check to see if "Save and add information" was pressed
"""
lastid = r.resource.lastid
if lastid and request.post_vars.get("edit_info", False):
table = db.cap_alert
alert = db(table.id == lastid).select(table.template_id,
limitby=(0, 1)).first()
if alert:
# Clone all cap_info entries from the alert template
itable = s3db.cap_info
unwanted_fields = set(("deleted_rb",
"owned_by_user",
"approved_by",
"mci",
"deleted",
"modified_on",
"realm_entity",
"uuid",
"created_on",
"deleted_fk",
# Don't copy this: make an
# Ajax call instead
"template_settings",
))
fields = [itable[f] for f in itable.fields
if f not in unwanted_fields]
rows = db(itable.alert_id == alert.template_id).select(*fields)
for row in rows:
row_clone = row.as_dict()
del row_clone["id"]
row_clone["alert_id"] = lastid
row_clone["template_info_id"] = row.id
row_clone["is_template"] = False
itable.insert(**row_clone)
r.next = URL(c="cap", f="alert", args=[lastid, "info"])
if r.interactive:
#if r.component_name == "info":
# update_url = URL(f="info", args=["[id]"])
# s3_action_buttons(r, update_url=update_url)
#if r.component_name == "area":
# update_url = URL(f="area", args=["[id]"])
# s3_action_buttons(r, update_url=update_url)
if isinstance(output, dict) and "form" in output:
if not r.component and \
r.method not in ("import", "import_feed"):
fields = s3db.cap_info_labels()
jsobj = []
for f in fields:
jsobj.append("'%s': '%s'" % (f, fields[f].replace("'", "\\'")))
s3.js_global.append('''i18n.cap_info_labels={%s}''' % ", ".join(jsobj))
form = output["form"]
form.update(_class="cap_alert_form")
set_priority_js()
return output
s3.postp = postp
output = s3_rest_controller(rheader = s3db.cap_rheader)
return output
# -----------------------------------------------------------------------------
def info():
"""
REST controller for CAP info segments
- shouldn't ever be called
"""
def prep(r):
result = info_prep(r)
if result:
if not r.component and r.representation == "html":
s3.crud.custom_submit = (("add_language",
T("Save and add another language..."),
"",
),)
return result
s3.prep = prep
def postp(r, output):
if r.representation == "html":
if r.component_name == "area":
update_url = URL(f="area", args=["[id]"])
s3_action_buttons(r, update_url=update_url)
if not r.component and "form" in output:
set_priority_js()
return output
s3.postp = postp
output = s3_rest_controller(rheader = s3db.cap_rheader)
return output
# -----------------------------------------------------------------------------
def template():
""" REST controller for CAP templates """
atable = s3db.cap_alert
s3.filter = (atable.is_template == True)
viewing = request.vars["viewing"]
if viewing:
table, _id = viewing.strip().split(".")
if table == "cap_alert":
redirect(URL(c="cap", f="template", args=[_id]))
def prep(r):
for f in ["identifier", "msg_type"]:
field = atable[f]
field.writable = False
field.readable = False
field.requires = None
for f in ["status", "scope"]:
atable[f].requires = None
atable.template_title.required = True
atable.status.readable = atable.status.writable = False
itable = db.cap_info
for f in ["urgency", "certainty",
"priority", "severity",
"effective", "onset", "expires"]:
field = itable[f]
field.writable = False
field.readable = False
field.required = False
for f in ["category", "event"]:
itable[f].required = False
ADD_ALERT_TPL = T("Create Template")
s3.crud_strings["cap_template"] = Storage(
label_create = ADD_ALERT_TPL,
title_display = T("Template"),
title_list = T("Templates"),
title_update = T("Edit Template"), # If already-published, this should create a new "Update" alert instead of modifying the original
title_upload = T("Import Templates"),
label_list_button = T("List Templates"),
label_delete_button = T("Delete Template"),
msg_record_created = T("Template created"),
msg_record_modified = T("Template modified"),
msg_record_deleted = T("Template deleted"),
msg_list_empty = T("No templates to show"))
if r.representation == "html":
alert_fields_comments()
s3.scripts.append("/%s/static/scripts/json2.min.js" % appname)
if s3.debug:
s3.scripts.append("/%s/static/scripts/S3/s3.cap.js" % appname)
else:
s3.scripts.append("/%s/static/scripts/S3/s3.cap.min.js" % appname)
s3.stylesheets.append("S3/cap.css")
return True
s3.prep = prep
def postp(r,output):
if r.interactive and "form" in output:
s3.js_global.append('''i18n.cap_locked="%s"''' % T("Locked"))
tablename = r.tablename
if tablename == "cap_alert":
output["form"].add_class("cap_template_form")
elif tablename == "cap_info":
output["form"].add_class("cap_info_template_form")
return output
s3.postp = postp
output = s3_rest_controller("cap", "alert",
rheader = s3db.cap_rheader)
return output
# -----------------------------------------------------------------------------
def area():
"""
REST controller for CAP area
- shouldn't ever be called
"""
def postp(r, output):
if r.interactive and r.component and r.component_name == "area_location":
# Modify action button to open cap/area_location directly.
#read_url = URL(c="cap", f="area_location", args=["[id]"])
update_url = URL(c="cap", f="area_location", args=["[id]", "update"])
delete_url = URL(c="cap", f="area_location", args=["[id]", "delete"])
s3_action_buttons(r,
update_url=update_url,
delete_url=delete_url,
)
return output
s3.postp = postp
output = s3_rest_controller("cap", "area",
rheader = s3db.cap_rheader)
return output
# -----------------------------------------------------------------------------
def area_location():
"""
REST controller for CAP area location
- shouldn't ever be called
"""
def prep(r):
if r.interactive:
# Don't allow changing the area_id.
altable = s3db.cap_area_location
afield = altable.area_id
afield.readable = False
afield.writable = False
# Hide the location hierarchy fields in the location widget.
#ltable = s3db.gis_location
#for f in ["L0", "L1", "L2", "L3", "L4", "L5"]:
# field = ltable[f]
# field.readable = False
# field.writable = False
# field.requires = None
return True
s3.prep = prep
output = s3_rest_controller("cap", "area_location",
rheader = s3db.cap_rheader)
return output
# -----------------------------------------------------------------------------
def alert_fields_comments():
"""
Add comments to Alert fields
"""
table = db.cap_alert
table.identifier.comment = DIV(
_class="tooltip",
_title="%s|%s" % (
T("A unique identifier of the alert message"),
T("A number or string uniquely identifying this message, assigned by the sender. Must notnclude spaces, commas or restricted characters (< and &).")))
table.sender.comment = DIV(
_class="tooltip",
_title="%s|%s" % (
T("The identifier of the sender of the alert message"),
T("This is guaranteed by assigner to be unique globally; e.g., may be based on an Internet domain name. Must not include spaces, commas or restricted characters (< and &).")))
table.status.comment = DIV(
_class="tooltip",
_title="%s|%s" % (
T("Denotes the appropriate handling of the alert message"),
T("See options.")))
table.msg_type.comment = DIV(
_class="tooltip",
_title="%s|%s" % (
T("The nature of the alert message"),
T("See options.")))
table.source.comment = DIV(
_class="tooltip",
_title="%s|%s" % (
T("The text identifying the source of the alert message"),
T("The particular source of this alert; e.g., an operator or a specific device.")))
table.scope.comment = DIV(
_class="tooltip",
_title="%s|%s" % (
T("Denotes the intended distribution of the alert message"),
T("Who is this alert for?")))
table.restriction.comment = DIV(
_class="tooltip",
_title="%s|%s" % (
T("The text describing the rule for limiting distribution of the restricted alert message"),
T("Used when scope is 'Restricted'.")))
table.addresses.comment = DIV(
_class="tooltip",
_title="%s|%s" % (
T("The group listing of intended recipients of the alert message"),
T("Required when scope is 'Private', optional when scope is 'Public' or 'Restricted'. Each recipient shall be identified by an identifier or an address.")))
table.codes.comment = DIV(
_class="tooltip",
_title="%s|%s" % (
T("Codes for special handling of the message"),
T("Any user-defined flags or special codes used to flag the alert message for special handling.")))
table.note.comment = DIV(
_class="tooltip",
_title="%s|%s" % (
T("The text describing the purpose or significance of the alert message"),
T("The message note is primarily intended for use with status 'Exercise' and message type 'Error'")))
table.reference.comment = DIV(
_class="tooltip",
_title="%s|%s" % (
T("The group listing identifying earlier message(s) referenced by the alert message"),
T("The extended message identifier(s) (in the form sender,identifier,sent) of an earlier CAP message or messages referenced by this one.")))
table.incidents.comment = DIV(
_class="tooltip",
_title="%s|%s" % (
T("A list of incident(s) referenced by the alert message"),
T("Used to collate multiple messages referring to different aspects of the same incident. If multiple incident identifiers are referenced, they SHALL be separated by whitespace. Incident names including whitespace SHALL be surrounded by double-quotes.")))
# -----------------------------------------------------------------------------
def area_fields_comments():
"""
Add comments to Area fields
"""
table = db.cap_area
table.name.comment = DIV(
_class="tooltip",
_title="%s|%s" % (
T("The affected area of the alert message"),
T("A text description of the affected area.")))
# table.circle.comment = DIV(
# _class="tooltip",
# _title="%s|%s" % (
# T("A point and radius delineating the affected area"),
# T("The circular area is represented by a central point given as a coordinate pair followed by a radius value in kilometers.")))
# table.geocode.comment = DIV(
# _class="tooltip",
# _title="%s|%s" % (
# T("The geographic code delineating the affected area"),
# T("Any geographically-based code to describe a message target area, in the form. The key is a user-assigned string designating the domain of the code, and the content of value is a string (which may represent a number) denoting the value itself (e.g., name='ZIP' and value='54321'). This should be used in concert with an equivalent description in the more universally understood polygon and circle forms whenever possible.")))
table.altitude.comment = DIV(
_class="tooltip",
_title="%s|%s" % (
T("The specific or minimum altitude of the affected area"),
T("If used with the ceiling element this value is the lower limit of a range. Otherwise, this value specifies a specific altitude. The altitude measure is in feet above mean sea level.")))
table.ceiling.comment = DIV(
_class="tooltip",
_title="%s|%s" % (
T("The maximum altitude of the affected area"),
T("must not be used except in combination with the 'altitude' element. The ceiling measure is in feet above mean sea level.")))
# -----------------------------------------------------------------------------
def info_fields_comments():
"""
Add comments to Information segment fields
"""
table = db.cap_info
table.language.comment = DIV(
_class="tooltip",
_title="%s|%s" % (
T("Denotes the language of the information"),
T("Code Values: Natural language identifier per [RFC 3066]. If not present, an implicit default value of 'en-US' will be assumed. Edit settings.cap.languages in 000_config.py to add more languages. See <a href=\"%s\">here</a> for a full list.") % "http://www.i18nguy.com/unicode/language-identifiers.html"))
table.category.comment = DIV(
_class="tooltip",
_title="%s|%s" % (
T("Denotes the category of the subject event of the alert message"),
T("You may select multiple categories by holding down control and then selecting the items.")))
table.event.comment = DIV(
_class="tooltip",
_title="%s|%s" % (
T("The text denoting the type of the subject event of the alert message"),
T("")))
table.response_type.comment = DIV(
_class="tooltip",
_title="%s|%s" % (
T("Denotes the type of action recommended for the target audience"),
T("Multiple response types can be selected by holding down control and then selecting the items")))
table.urgency.comment = DIV(
_class="tooltip",
_title="%s|%s" % (
T("Denotes the urgency of the subject event of the alert message"),
T("The urgency, severity, and certainty of the information collectively distinguish less emphatic from more emphatic messages." +
"'Immediate' - Responsive action should be taken immediately" +
"'Expected' - Responsive action should be taken soon (within next hour)" +
"'Future' - Responsive action should be taken in the near future" +
"'Past' - Responsive action is no longer required" +
"'Unknown' - Urgency not known")))
table.severity.comment = DIV(
_class="tooltip",
_title="%s|%s" % (
T("Denotes the severity of the subject event of the alert message"),
T("The urgency, severity, and certainty elements collectively distinguish less emphatic from more emphatic messages." +
"'Extreme' - Extraordinary threat to life or property" +
"'Severe' - Significant threat to life or property" +
"'Moderate' - Possible threat to life or property" +
"'Minor' - Minimal to no known threat to life or property" +
"'Unknown' - Severity unknown")))
table.certainty.comment = DIV(
_class="tooltip",
_title="%s|%s" % (
T("Denotes the certainty of the subject event of the alert message"),
T("The urgency, severity, and certainty elements collectively distinguish less emphatic from more emphatic messages." +
"'Observed' - Determined to have occurred or to be ongoing" +
"'Likely' - Likely (p > ~50%)" +
"'Possible' - Possible but not likely (p <= ~50%)" +
"'Unlikely' - Not expected to occur (p ~ 0)" +
"'Unknown' - Certainty unknown")))
table.audience.comment = DIV(
_class="tooltip",
_title="%s|%s" % (
T("The intended audience of the alert message"),
T("")))
table.event_code.comment = DIV(
_class="tooltip",
_title="%s|%s" % (
T("A system-specific code identifying the event type of the alert message"),
T("Any system-specific code for events, in the form of key-value pairs. (e.g., SAME, FIPS, ZIP).")))
table.effective.comment = DIV(
_class="tooltip",
_title="%s|%s" % (
T("The effective time of the information of the alert message"),
T("If not specified, the effective time shall be assumed to be the same the time the alert was sent.")))
table.onset.comment = DIV(
_class="tooltip",
_title="%s|%s" % (
T("The expected time of the beginning of the subject event of the alert message"),
T("")))
table.expires.comment = DIV(
_class="tooltip",
_title="%s|%s" % (
T("The expiry time of the information of the alert message"),
T("If this item is not provided, each recipient is free to enforce its own policy as to when the message is no longer in effect.")))
table.sender_name.comment = DIV(
_class="tooltip",
_title="%s|%s" % (
T("The text naming the originator of the alert message"),
T("The human-readable name of the agency or authority issuing this alert.")))
table.headline.comment = DIV(
_class="tooltip",
_title="%s|%s" % (
T("The text headline of the alert message"),
T("A brief human-readable headline. Note that some displays (for example, short messaging service devices) may only present this headline; it should be made as direct and actionable as possible while remaining short. 160 characters may be a useful target limit for headline length.")))
table.description.comment = DIV(
_class="tooltip",
_title="%s|%s" % (
T("The subject event of the alert message"),
T("An extended human readable description of the hazard or event that occasioned this message.")))
table.instruction.comment = DIV(
_class="tooltip",
_title="%s|%s" % (
T("The recommended action to be taken by recipients of the alert message"),
T("An extended human readable instruction to targeted recipients. If different instructions are intended for different recipients, they should be represented by use of multiple information blocks. You can use a different information block also to specify this information in a different language.")))
table.web.comment = DIV(
_class="tooltip",
_title="%s|%s" % (
T("A URL associating additional information with the alert message"),
T("A full, absolute URI for an HTML page or other text resource with additional or reference information regarding this alert.")))
table.contact.comment = DIV(
_class="tooltip",
_title="%s|%s" % (
T("The contact for follow-up and confirmation of the alert message"),
T("")))
table.parameter.comment = DIV(
_class="tooltip",
_title="%s|%s" % (
T("A system-specific additional parameter associated with the alert message"),
T("Any system-specific datum, in the form of key-value pairs.")))
# -----------------------------------------------------------------------------
def resource_fields_comments():
"""
Add comments to Resource fields
"""
table = db.cap_resource
table.resource_desc.comment = DIV(
_class="tooltip",
_title="%s|%s" % (
T("The type and content of the resource file"),
T("The human-readable text describing the type and content, such as 'map' or 'photo', of the resource file.")))
table.mime_type.comment = DIV(
_class="tooltip",
_title="%s|%s" % (
T("The identifier of the MIME content type and sub-type describing the resource file"),
T("MIME content type and sub-type as described in [RFC 2046]. (As of this document, the current IANA registered MIME types are listed at http://www.iana.org/assignments/media-types/)")))
table.size.comment = DIV(
_class="tooltip",
_title="%s|%s" % (
T("The integer indicating the size of the resource file"),
T("Approximate size of the resource file in bytes.")))
# @ToDo: This should be handled under the hood
table.uri.comment = DIV(
_class="tooltip",
_title="%s|%s" % (
T("The identifier of the hyperlink for the resource file"),
T("A full absolute URI, typically a Uniform Resource Locator that can be used to retrieve the resource over the Internet.")))
table.digest.comment = DIV(
_class="tooltip",
_title="%s|%s" % (
T("The code representing the digital digest ('hash') computed from the resource file"),
T("Calculated using the Secure Hash Algorithm (SHA-1).")))
# -----------------------------------------------------------------------------
def set_priority_js():
""" Output json for priority field """
p_settings = [f[0:1] + f[2:] for f in settings.get_cap_priorities()]
priority_conf = '''S3.cap_priorities=%s''' % json.dumps(p_settings)
js_global = s3.js_global
if not priority_conf in js_global:
js_global.append(priority_conf)
return
# END =========================================================================