~shikharkohli/sahana-eden/gsoc1

« back to all changes in this revision

Viewing changes to models/irs.py

  • Committer: Shikhar Kohli
  • Date: 2010-08-11 18:40:09 UTC
  • mfrom: (802.1.156 eden)
  • Revision ID: shikharkohli@gmail.com-20100811184009-yy8zag5dowlam0ip
Merge trunk and update (a bit) of JS

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
 
 
3
"""
 
4
    Incident Reporting System - Model
 
5
"""
 
6
 
 
7
module = "irs"
 
8
if deployment_settings.has_module(module):
 
9
 
 
10
    # Settings
 
11
    resource = "setting"
 
12
    tablename = "%s_%s" % (module, resource)
 
13
    table = db.define_table(tablename,
 
14
                            Field("audit_read", "boolean"),
 
15
                            Field("audit_write", "boolean"),
 
16
                            migrate=migrate)
 
17
 
 
18
    irs_incident_type_opts = {
 
19
        1:T("animalHealth.animalDieOff"),
 
20
        2:T("animalHealth.animalFeed"),
 
21
        3:T("aviation.aircraftCrash"),
 
22
        4:T("aviation.aircraftHijacking"),
 
23
        5:T("aviation.airportClosure"),
 
24
        6:T("aviation.airspaceClosure"),
 
25
        7:T("aviation.noticeToAirmen"),
 
26
        8:T("aviation.spaceDebris"),
 
27
        9:T("civil.demonstrations"),
 
28
        10:T("civil.dignitaryVisit"),
 
29
        11:T("civil.displacedPopulations"),
 
30
        12:T("civil.emergency"),
 
31
        13:T("civil.looting"),
 
32
        14:T("civil.publicEvent"),
 
33
        15:T("civil.riot"),
 
34
        16:T("civil.volunteerRequest"),
 
35
        17:T("crime.bomb"),
 
36
        18:T("crime.bombExplosion"),
 
37
        19:T("crime.bombThreat"),
 
38
        20:T("crime.dangerousPerson"),
 
39
        21:T("crime.drugs"),
 
40
        22:T("crime.homeCrime"),
 
41
        23:T("crime.illegalImmigrant"),
 
42
        24:T("crime.industrialCrime"),
 
43
        25:T("crime.poisoning"),
 
44
        26:T("crime.retailCrime"),
 
45
        27:T("crime.shooting"),
 
46
        28:T("crime.stowaway"),
 
47
        29:T("crime.terrorism"),
 
48
        30:T("crime.vehicleCrime"),
 
49
        31:T("fire.forestFire"),
 
50
        32:T("fire.hotSpot"),
 
51
        33:T("fire.industryFire"),
 
52
        34:T("fire.smoke"),
 
53
        35:T("fire.urbanFire"),
 
54
        36:T("fire.wildFire"),
 
55
        37:T("flood.damOverflow"),
 
56
        38:T("flood.flashFlood"),
 
57
        39:T("flood.highWater"),
 
58
        40:T("flood.overlandFlowFlood"),
 
59
        41:T("flood.tsunami"),
 
60
        42:T("geophysical.avalanche"),
 
61
        43:T("geophysical.earthquake"),
 
62
        44:T("geophysical.lahar"),
 
63
        45:T("geophysical.landslide"),
 
64
        46:T("geophysical.magneticStorm"),
 
65
        47:T("geophysical.meteorite"),
 
66
        48:T("geophysical.pyroclasticFlow"),
 
67
        49:T("geophysical.pyroclasticSurge"),
 
68
        50:T("geophysical.volcanicAshCloud"),
 
69
        51:T("geophysical.volcanicEvent"),
 
70
        52:T("hazardousMaterial.biologicalHazard"),
 
71
        53:T("hazardousMaterial.chemicalHazard"),
 
72
        54:T("hazardousMaterial.explosiveHazard"),
 
73
        55:T("hazardousMaterial.fallingObjectHazard"),
 
74
        56:T("hazardousMaterial.infectiousDisease"),
 
75
        57:T("hazardousMaterial.poisonousGas"),
 
76
        58:T("hazardousMaterial.radiologicalHazard"),
 
77
        59:T("health.infectiousDisease"),
 
78
        60:T("health.infestation"),
 
79
        61:T("ice.iceberg"),
 
80
        62:T("ice.icePressure"),
 
81
        63:T("ice.rapidCloseLead"),
 
82
        64:T("ice.specialIce"),
 
83
        65:T("marine.marineSecurity"),
 
84
        66:T("marine.nauticalAccident"),
 
85
        67:T("marine.nauticalHijacking"),
 
86
        68:T("marine.portClosure"),
 
87
        69:T("marine.specialMarine"),
 
88
        70:T("meteorological.blizzard"),
 
89
        71:T("meteorological.blowingSnow"),
 
90
        72:T("meteorological.drought"),
 
91
        73:T("meteorological.dustStorm"),
 
92
        74:T("meteorological.fog"),
 
93
        75:T("meteorological.freezingDrizzle"),
 
94
        76:T("meteorological.freezingRain"),
 
95
        77:T("meteorological.freezingSpray"),
 
96
        78:T("meteorological.hail"),
 
97
        79:T("meteorological.hurricane"),
 
98
        80:T("meteorological.rainFall"),
 
99
        81:T("meteorological.snowFall"),
 
100
        82:T("meteorological.snowSquall"),
 
101
        83:T("meteorological.squall"),
 
102
        84:T("meteorological.stormSurge"),
 
103
        85:T("meteorological.thunderstorm"),
 
104
        86:T("meteorological.tornado"),
 
105
        87:T("meteorological.tropicalStorm"),
 
106
        88:T("meteorological.waterspout"),
 
107
        89:T("meteorological.winterStorm"),
 
108
        90:T("missingPerson.amberAlert"),
 
109
        91:T("missingPerson.missingVulnerablePerson"),
 
110
        92:T("missingPerson.silver"),
 
111
        93:T("publicService.emergencySupportFacility"),
 
112
        94:T("publicService.emergencySupportService"),
 
113
        95:T("publicService.schoolClosure"),
 
114
        96:T("publicService.schoolLockdown"),
 
115
        97:T("publicService.serviceOrFacility"),
 
116
        98:T("publicService.transit"),
 
117
        99:T("railway.railwayAccident"),
 
118
        100:T("railway.railwayHijacking"),
 
119
        101:T("roadway.bridgeClosure"),
 
120
        102:T("roadway.hazardousRoadConditions"),
 
121
        103:T("roadway.roadwayAccident"),
 
122
        104:T("roadway.roadwayClosure"),
 
123
        105:T("roadway.roadwayDelay"),
 
124
        106:T("roadway.roadwayHijacking"),
 
125
        107:T("roadway.roadwayUsageCondition"),
 
126
        108:T("roadway.trafficReport"),
 
127
        109:T("temperature.arcticOutflow"),
 
128
        110:T("temperature.coldWave"),
 
129
        111:T("temperature.flashFreeze"),
 
130
        112:T("temperature.frost"),
 
131
        113:T("temperature.heatAndHumidity"),
 
132
        114:T("temperature.heatWave"),
 
133
        115:T("temperature.windChill"),
 
134
        116:T("wind.galeWind"),
 
135
        117:T("wind.hurricaneForceWind"),
 
136
        118:T("wind.stormForceWind"),
 
137
        119:T("wind.strongWind")
 
138
    }
 
139
 
 
140
    # Incidents
 
141
    # This is the current status of an Incident
 
142
    resource = "incident"
 
143
    tablename = "%s_%s" % (module, resource)
 
144
    table = db.define_table(tablename, timestamp, uuidstamp, authorstamp, deletion_status,
 
145
            Field("name"),
 
146
            Field("category", "integer"),
 
147
            Field("contact"),
 
148
            location_id,
 
149
            Field("time", "datetime"),
 
150
            Field("persons_affected", "integer"),
 
151
            Field("persons_injured", "integer"),
 
152
            Field("persons_deceased", "integer"),
 
153
            comments,
 
154
            )
 
155
    table.name.requires = IS_NOT_EMPTY()
 
156
    table.category.requires = IS_NULL_OR(IS_IN_SET(irs_incident_type_opts))
 
157
    table.category.represent = lambda opt: irs_incident_type_opts.get(opt, opt)
 
158
 
 
159
    # CRUD strings
 
160
    ADD_INCIDENT = T("Add Incident")
 
161
    LIST_INCIDENTS = T("List Incidents")
 
162
    s3.crud_strings[tablename] = Storage(
 
163
        title_create = ADD_INCIDENT,
 
164
        title_display = T("Incident Details"),
 
165
        title_list = LIST_INCIDENTS,
 
166
        title_update = T("Edit Incident"),
 
167
        title_search = T("Search Incidents"),
 
168
        subtitle_create = T("Add New Incident"),
 
169
        subtitle_list = T("Incidents"),
 
170
        label_list_button = LIST_INCIDENTS,
 
171
        label_create_button = ADD_INCIDENT,
 
172
        msg_record_created = T("Incident added"),
 
173
        msg_record_modified = T("Incident updated"),
 
174
        msg_record_deleted = T("Incident deleted"),
 
175
        msg_list_empty = T("No Incidents currently registered"))
 
176
 
 
177
    incident_id = db.Table(None, "incident_id",
 
178
                           Field("incident_id", table,
 
179
                                 requires = IS_NULL_OR(IS_ONE_OF(db, "irs_incident.id", "%(id)s")),
 
180
                                 represent = lambda id: id,
 
181
                                 label = T("Incident"),
 
182
                                 ondelete = "RESTRICT"))
 
183
    s3xrc.model.configure(table,
 
184
                        list_fields = [
 
185
                            "id",
 
186
                            "category",
 
187
                            "time",
 
188
                            "location_id"
 
189
                        ])
 
190
    # -----------------------------------------------------------------------------
 
191
    # Reports
 
192
    # This is a report of an Incident
 
193
    # A single incident may generate many reports
 
194
    resource = "ireport"
 
195
    tablename = "%s_%s" % (module, resource)
 
196
    table = db.define_table(tablename, timestamp, uuidstamp, authorstamp, deletion_status,
 
197
            incident_id,
 
198
            Field("name"),
 
199
            Field("category", "integer"),
 
200
            person_id,
 
201
            Field("contact"),
 
202
            location_id,
 
203
            Field("time", "datetime"),
 
204
            Field("persons_affected", "integer"),
 
205
            Field("persons_injured", "integer"),
 
206
            Field("persons_deceased", "integer"),
 
207
            comments,
 
208
            )
 
209
    table.name.requires = IS_NOT_EMPTY()
 
210
    table.category.requires = IS_NULL_OR(IS_IN_SET(irs_incident_type_opts))
 
211
    table.category.represent = lambda opt: irs_incident_type_opts.get(opt, opt)
 
212
    table.person_id.default = session.auth.user.id if auth.is_logged_in() else None
 
213
 
 
214
    table.name.label = T("Short Description")
 
215
    table.name.comment = SPAN("*", _class="req")
 
216
    table.person_id.label = T("Reporter Name")
 
217
    table.persons_affected.label = T("Number of People Affected")
 
218
    table.persons_injured.label = T("Number of People Injured")
 
219
    table.persons_deceased.label = T("Number of People Deceased")
 
220
 
 
221
    # CRUD strings
 
222
    ADD_REPORT = T("Add Report")
 
223
    LIST_REPORTS = T("List Reports")
 
224
    s3.crud_strings[tablename] = Storage(
 
225
        title_create = ADD_REPORT,
 
226
        title_display = T("Report Details"),
 
227
        title_list = LIST_REPORTS,
 
228
        title_update = T("Edit Report"),
 
229
        title_search = T("Search Reports"),
 
230
        subtitle_create = T("Add New Report"),
 
231
        subtitle_list = T("Reports"),
 
232
        label_list_button = LIST_REPORTS,
 
233
        label_create_button = ADD_REPORT,
 
234
        msg_record_created = T("Report added"),
 
235
        msg_record_modified = T("Report updated"),
 
236
        msg_record_deleted = T("Report deleted"),
 
237
        msg_list_empty = T("No Reports currently registered"))
 
238
 
 
239
    s3xrc.model.add_component(module, resource,
 
240
                              multiple = True,
 
241
                              joinby = dict(irs_incident="incident_id"),
 
242
                              deletable = True,
 
243
                              editable = True)
 
244
 
 
245
    # -----------------------------------------------------------------------------
 
246
    irs_assessment_type_opts = {
 
247
        1:T("initial assessment"),
 
248
        2:T("follow-up assessment"),
 
249
        3:T("final report"),
 
250
        99:T("other")
 
251
    }
 
252
 
 
253
    irs_event_type_opts = {
 
254
        1:T("primary incident"),
 
255
        2:T("secondary effect"),
 
256
        3:T("collateral event"),
 
257
        99:T("other")
 
258
    }
 
259
 
 
260
    irs_cause_type_opts = {
 
261
        1:T("natural hazard"),
 
262
        2:T("technical failure"),
 
263
        3:T("human error"),
 
264
        4:T("criminal intent"),
 
265
        5:T("operational intent"),
 
266
        99:T("other")
 
267
    }
 
268
 
 
269
    # Assessments
 
270
    # This is a follow-up assessment of an Incident
 
271
    resource = "iassessment"
 
272
    tablename = "%s_%s" % (module, resource)
 
273
    table = db.define_table(tablename,
 
274
                            timestamp, uuidstamp, authorstamp, deletion_status,
 
275
                            incident_id,
 
276
                            Field("datetime", "datetime"),
 
277
                            Field("itype", "integer",
 
278
                                requires = IS_IN_SET(irs_assessment_type_opts, zero=None),
 
279
                                default = 1,
 
280
                                label = T("Report Type"),
 
281
                                represent = lambda opt: irs_assessment_type_opts.get(opt, UNKNOWN_OPT)),
 
282
                            Field("event_type", "integer",
 
283
                                requires = IS_IN_SET(irs_event_type_opts, zero=None),
 
284
                                default = 1,
 
285
                                label = T("Event type"),
 
286
                                represent = lambda opt: irs_event_type_opts.get(opt, UNKNOWN_OPT)),
 
287
                            Field("cause_type", "integer",
 
288
                                requires = IS_IN_SET(irs_cause_type_opts, zero=None),
 
289
                                default = 1,
 
290
                                label = T("Type of cause"),
 
291
                                represent = lambda opt: irs_cause_type_opts.get(opt, UNKNOWN_OPT)),
 
292
                            Field("report", "text"),
 
293
                            Field("persons_affected", "integer"),
 
294
                            Field("persons_injured", "integer"),
 
295
                            Field("persons_deceased", "integer"),
 
296
                            migrate=migrate)
 
297
 
 
298
    table.modified_by.label = T("Reporter")
 
299
    table.modified_by.readable = True
 
300
 
 
301
    table.datetime.label = T("Date/Time")
 
302
 
 
303
    # CRUD strings
 
304
    ADD_ASSESSMENT = T("Add Assessment")
 
305
    LIST_ASSESSMENTS = T("List Assessments")
 
306
    s3.crud_strings[tablename] = Storage(
 
307
        title_create = ADD_ASSESSMENT,
 
308
        title_display = T("Assessment Details"),
 
309
        title_list = LIST_ASSESSMENTS,
 
310
        title_update = T("Edit Assessment"),
 
311
        title_search = T("Search Assessments"),
 
312
        subtitle_create = T("Add New Assessment"),
 
313
        subtitle_list = T("Assessments"),
 
314
        label_list_button = LIST_ASSESSMENTS,
 
315
        label_create_button = ADD_ASSESSMENT,
 
316
        msg_record_created = T("Assessment added"),
 
317
        msg_record_modified = T("Assessment updated"),
 
318
        msg_record_deleted = T("Assessment deleted"),
 
319
        msg_list_empty = T("No Assessments currently registered"))
 
320
 
 
321
    s3xrc.model.configure(table,
 
322
        list_fields = [
 
323
            "id",
 
324
            "datetime",
 
325
            "itype",
 
326
            "modified_by"
 
327
        ])
 
328
 
 
329
    s3xrc.model.add_component(module, resource,
 
330
                              multiple = True,
 
331
                              joinby = dict(irs_incident="incident_id"),
 
332
                              deletable = True,
 
333
                              editable = True)
 
334
 
 
335
    # -----------------------------------------------------------------------------
 
336
    irs_image_type_opts = {
 
337
        1:T("Photograph"),
 
338
        2:T("Map"),
 
339
        3:T("Document Scan"),
 
340
        99:T("other")
 
341
    }
 
342
 
 
343
    resource = "iimage"
 
344
    tablename = "%s_%s" % (module, resource)
 
345
    table = db.define_table(tablename,
 
346
                            timestamp, uuidstamp, authorstamp, deletion_status,
 
347
                            Field("report_id", db.irs_ireport),
 
348
                            incident_id,
 
349
                            Field("assessment_id", db.irs_iassessment),
 
350
                            Field("type", "integer",
 
351
                                requires = IS_IN_SET(irs_image_type_opts, zero=None),
 
352
                                default = 1,
 
353
                                label = T("Image Type"),
 
354
                                represent = lambda opt: irs_image_type_opts.get(opt, UNKNOWN_OPT)),
 
355
                            Field("image", "upload", autodelete=True),
 
356
                            #Field("url"),
 
357
                            Field("description"),
 
358
                            #Field("tags"),
 
359
                            migrate=migrate)
 
360
 
 
361
    # CRUD strings
 
362
    ADD_IMAGE = T("Add Image")
 
363
    LIST_IMAGES = T("List Images")
 
364
    s3.crud_strings[tablename] = Storage(
 
365
        title_create = ADD_IMAGE,
 
366
        title_display = T("Image Details"),
 
367
        title_list = LIST_IMAGES,
 
368
        title_update = T("Edit Image"),
 
369
        title_search = T("Search Images"),
 
370
        subtitle_create = T("Add New Image"),
 
371
        subtitle_list = T("Images"),
 
372
        label_list_button = LIST_IMAGES,
 
373
        label_create_button = ADD_IMAGE,
 
374
        msg_record_created = T("Image added"),
 
375
        msg_record_modified = T("Image updated"),
 
376
        msg_record_deleted = T("Image deleted"),
 
377
        msg_list_empty = T("No Images currently registered"))
 
378
 
 
379
    s3xrc.model.add_component(module, resource,
 
380
                              multiple = True,
 
381
                              joinby = dict(irs_incident="incident_id",
 
382
                                            irs_ireport="report_id",
 
383
                                            irs_iassessment="assessment_id"),
 
384
                              deletable = True,
 
385
                              editable = True)
 
386
 
 
387
    # -----------------------------------------------------------------------------
 
388
    irs_response_type_opts = {
 
389
        1:T("Alert"),
 
390
        2:T("Intervention"),
 
391
        3:T("Closure"),
 
392
        99:T("other")
 
393
    }
 
394
 
 
395
    resource = "iresponse"
 
396
    tablename = "%s_%s" % (module, resource)
 
397
    table = db.define_table(tablename,
 
398
                            timestamp, uuidstamp, authorstamp, deletion_status,
 
399
                            incident_id,
 
400
                            Field("datetime", "datetime"),
 
401
                            Field("itype", "integer",
 
402
                                requires = IS_IN_SET(irs_response_type_opts, zero=None),
 
403
                                default = 1,
 
404
                                label = T("Type"),
 
405
                                represent = lambda opt: irs_response_type_opts.get(opt, UNKNOWN_OPT)),
 
406
                            Field("report", "text"),
 
407
                            migrate=migrate)
 
408
 
 
409
    # CRUD strings
 
410
    ADD_RESPONSE = T("Add Response")
 
411
    LIST_RESPONSES = T("List Responses")
 
412
    s3.crud_strings[tablename] = Storage(
 
413
        title_create = ADD_RESPONSE,
 
414
        title_display = T("Response Details"),
 
415
        title_list = LIST_RESPONSES,
 
416
        title_update = T("Edit Response"),
 
417
        title_search = T("Search Responses"),
 
418
        subtitle_create = T("Add New Response"),
 
419
        subtitle_list = T("Responses"),
 
420
        label_list_button = LIST_RESPONSES,
 
421
        label_create_button = ADD_RESPONSE,
 
422
        msg_record_created = T("Response added"),
 
423
        msg_record_modified = T("Response updated"),
 
424
        msg_record_deleted = T("Response deleted"),
 
425
        msg_list_empty = T("No Responses currently registered"))
 
426
 
 
427
    s3xrc.model.add_component(module, resource,
 
428
                              multiple = True,
 
429
                              joinby = dict(irs_incident="incident_id"),
 
430
                              deletable = True,
 
431
                              editable = True)
 
432
 
 
433
    # -----------------------------------------------------------------------------