~shikharkohli/sahana-eden/gsoc1

« back to all changes in this revision

Viewing changes to controllers/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 - Controllers
 
5
"""
 
6
 
 
7
module = request.controller
 
8
 
 
9
if module not in deployment_settings.modules:
 
10
    session.error = T("Module disabled!")
 
11
    redirect(URL(r=request, c="default", f="index"))
 
12
 
 
13
# Options Menu (available in all Functions' Views)
 
14
response.menu_options = [
 
15
    [T("Reports"), False, URL(r=request, f="ireport"),[
 
16
        [T("List"), False, URL(r=request, f="ireport")],
 
17
        [T("Add"), False, URL(r=request, f="ireport", args="create")],
 
18
        #[T("Search"), False, URL(r=request, f="report", args="search")]
 
19
    ]],
 
20
    [T("Incidents"), False, URL(r=request, f="incident"),[
 
21
        [T("List"), False, URL(r=request, f="incident")],
 
22
        [T("Add"), False, URL(r=request, f="incident", args="create")],
 
23
        #[T("Search"), False, URL(r=request, f="incident", args="search")]
 
24
    ]],
 
25
    [T("Assessments"), False, URL(r=request, f="iassessment"),[
 
26
        [T("List"), False, URL(r=request, f="iassessment")],
 
27
        [T("Add"), False, URL(r=request, f="iassessment", args="create")],
 
28
        #[T("Search"), False, URL(r=request, f="assessment", args="search")]
 
29
    ]],
 
30
    [T("Map"), False, URL(r=request, f="maps")],
 
31
]
 
32
 
 
33
def index():
 
34
    "Custom View"
 
35
    module_name = deployment_settings.modules[module].name_nice
 
36
    return dict(module_name=module_name)
 
37
 
 
38
def maps():
 
39
    "Show a Map of all Reports"
 
40
 
 
41
    feature_class_id = db(db.gis_feature_class.name == "Incident").select(db.gis_feature_class.id, limitby=(0, 1)).first().id
 
42
    reports = db(db.gis_location.feature_class_id == feature_class_id).select()
 
43
    popup_url = URL(r=request, f="report", args="read.popup?report.location_id=")
 
44
    map = gis.show_map(feature_queries = [{"name":Tstr("Reports"), "query":reports, "active":True, "popup_url": popup_url}], window=True)
 
45
 
 
46
    return dict(map=map)
 
47
 
 
48
def incident():
 
49
    "REST Controller"
 
50
    resource = request.function
 
51
 
 
52
    response.s3.pagination = True
 
53
 
 
54
    db.irs_iimage.assessment_id.readable = db.irs_iimage.assessment_id.writable = False
 
55
    db.irs_iimage.incident_id.readable = db.irs_iimage.incident_id.writable = False
 
56
    db.irs_iimage.report_id.readable = db.irs_iimage.report_id.writable = False
 
57
 
 
58
    output = shn_rest_controller(module, resource,
 
59
                                 rheader=lambda r: shn_irs_rheader(r,
 
60
                                                                    tabs = [(T("Incident Details"), None),
 
61
                                                                            (T("Reports"), "ireport"),
 
62
                                                                            (T("Images"), "iimage"),
 
63
                                                                            (T("Assessments"), "iassessment"),
 
64
                                                                            (T("Response"), "iresponse")]),
 
65
                                                                    sticky=True)
 
66
    return output
 
67
 
 
68
def ireport():
 
69
    "REST Controller"
 
70
    resource = request.function
 
71
 
 
72
    db.irs_iimage.assessment_id.readable = db.irs_iimage.assessment_id.writable = False
 
73
    db.irs_iimage.report_id.readable = db.irs_iimage.report_id.writable = False
 
74
 
 
75
    response.s3.pagination = True
 
76
 
 
77
    output = shn_rest_controller(module, resource,
 
78
                                 rheader=lambda r: shn_irs_rheader(r,
 
79
                                                                   tabs = [(T("Report Details"), None),
 
80
                                                                           (T("Images"), "iimage")  ]),
 
81
                                                                   sticky=True)
 
82
    return output
 
83
 
 
84
def iassessment():
 
85
    "REST Controller"
 
86
    resource = request.function
 
87
 
 
88
    db.irs_iimage.assessment_id.readable = db.irs_iimage.assessment_id.writable = False
 
89
    db.irs_iimage.report_id.readable = db.irs_iimage.report_id.writable = False
 
90
 
 
91
    response.s3.pagination = True
 
92
 
 
93
    output = shn_rest_controller(module, resource,
 
94
                                 rheader=lambda r: shn_irs_rheader(r,
 
95
                                                                   tabs = [(T("Assessment Details"), None),
 
96
                                                                           (T("Images"), "iimage")  ]),
 
97
                                                                   sticky=True)
 
98
    return output
 
99
 
 
100
def shn_irs_rheader(r, tabs=[]):
 
101
 
 
102
    if r.representation == "html":
 
103
        rheader_tabs = shn_rheader_tabs(r, tabs)
 
104
 
 
105
        if r.name == "ireport":
 
106
            report = r.record
 
107
            reporter = report.person_id
 
108
            if reporter:
 
109
                reporter = shn_pr_person_represent(reporter)
 
110
            location = report.location_id
 
111
            if location:
 
112
                location = shn_gis_location_represent(location)
 
113
            rheader = DIV(TABLE(
 
114
                            TR(
 
115
                                TH(T("Short Description: ")), report.name,
 
116
                                TH(T("Reporter: ")), reporter),
 
117
                            TR(
 
118
                                TH(T("Contacts: ")), report.contact,
 
119
                                TH(T("Location: ")), location)
 
120
                            ),
 
121
                        rheader_tabs)
 
122
 
 
123
        elif r.name == "incident":
 
124
            incident = r.record
 
125
            location = incident.location_id
 
126
            if location:
 
127
                location = shn_gis_location_represent(location)
 
128
            category = irs_incident_type_opts.get(incident.category, incident.category)
 
129
            rheader = DIV(TABLE(
 
130
                            TR(
 
131
                                TH(T("Short Description: ")), incident.name,
 
132
                                TH(T("Category: ")), category),
 
133
                            TR(
 
134
                                TH(T("Contacts: ")), incident.contact,
 
135
                                TH(T("Location: ")), location)
 
136
                            ),
 
137
                      rheader_tabs)
 
138
 
 
139
        elif r.name == "iassessment":
 
140
            assessment = r.record
 
141
            author = shn_pr_person_represent(assessment.created_by)
 
142
            itype = irs_assessment_type_opts.get(assessment.itype, UNKNOWN_OPT)
 
143
            etype = irs_event_type_opts.get(assessment.event_type, UNKNOWN_OPT)
 
144
            rheader = DIV(TABLE(
 
145
                            TR(
 
146
                                TH(T("Assessment Type: ")), itype,
 
147
                                TH(T("Author: ")), author),
 
148
                            TR(
 
149
                                TH("Event type: "), etype,
 
150
                                TH(T("Date: ")), assessment.datetime)
 
151
                            ),
 
152
                      rheader_tabs)
 
153
 
 
154
        return rheader
 
155
    else:
 
156
        return None