def __call__(self): T = current.T db = current.db s3db = current.s3db s3 = current.response.s3 session_s3 = current.session.s3 output = {} # Recent Updates etable = s3db.event_event stable = s3db.event_sitrep query = (stable.deleted == False) fields = [ etable.name, stable.id, stable.date, stable.name, stable.summary, ] left = [etable.on(etable.id == stable.event_id)] language = session_s3.language if language != current.deployment_settings.get_L10n_default_language(): ntable = s3db.event_event_name left.append(ntable.on((ntable.event_id == etable.id) & \ (ntable.language == language))) fields.append(ntable.name_l10n) use_local_event_name = True else: use_local_event_name = False sitreps = db(query).select(left=left, limitby=(0, 3), orderby=~stable.date, *fields) len_sitreps = len(sitreps) if len_sitreps == 0: from s3 import S3CRUD recent_updates = DIV(S3CRUD.crud_string("event_sitrep", "msg_list_empty"), _class="empty") else: recent_updates = DIV() rappend = recent_updates.append count = 0 for s in sitreps: count += 1 if use_local_event_name: event_name = s["event_event_name.name_l10n"] or s[ "event_event.name"] else: event_name = s["event_event.name"] if not event_name: event_name = s["event_sitrep.name"] rappend( H3( A( event_name, _href=URL( c="event", f="sitrep", args=[s["event_sitrep.id"]], ), ))) rappend(P(XML(s["event_sitrep.summary"]))) if count != len_sitreps: rappend(HR()) output["recent_updates"] = recent_updates map_btn = A( T("MAP OF CURRENT NEEDS"), #_href = URL(c="default", # f="index", # args="dashboard", # ), _href=URL( c="req", f="need_line", args="map", ), _class="small primary button", ) create_btn = A( T("CREATE A NEED"), _href=URL( c="req", f="need", args="create", ), _class="small primary button", ) output["needs_btn"] = DIV( SPAN(map_btn), SPAN(create_btn), _class="button-group radius", ) output["about_btn"] = A( "%s >" % T("Read More"), _href=URL( c="default", f="about", ), ) # Resources section if current.deployment_settings.has_module("cms"): system_roles = current.auth.get_system_roles() ADMIN = system_roles.ADMIN in session_s3.roles table = s3db.cms_post ltable = s3db.cms_post_module module = "default" resource = "index" query = (ltable.module == module) & \ ((ltable.resource == None) | \ (ltable.resource == resource)) & \ (ltable.post_id == table.id) & \ (table.deleted != True) item = current.db(query).select(table.body, table.id, limitby=(0, 1)).first() if item: if ADMIN: item = DIV( XML(item.body), BR(), A(T("Edit"), _href=URL(c="cms", f="post", args=[item.id, "update"]), _class="action-btn")) else: item = DIV(XML(item.body)) elif ADMIN: if s3.crud.formstyle == "bootstrap": _class = "btn" else: _class = "action-btn" item = A(T("Edit"), _href=URL(c="cms", f="post", args="create", vars={ "module": module, "resource": resource }), _class="%s cms-edit" % _class) else: item = "" else: item = "" output["item"] = item # Inject D3 scripts from s3 import S3Report S3Report.inject_d3() # Inject charts-script appname = current.request.application scripts = s3.scripts if s3.debug: script = "/%s/static/scripts/S3/s3.ui.charts.js" % appname if script not in scripts: scripts.append(script) else: script = "/%s/static/scripts/S3/s3.ui.charts.min.js" % appname if script not in scripts: scripts.append(script) # Instantiate charts scriptopts = { # Standard SHARE theme color set: "colors": [ '#0C9CD0', # blue '#E03158', # red '#FBA629', # amber '#8ABC3F', # green '#AFB8BF', # grey ], } script = '''$('.homepage-chart').uiChart(%s)''' % json.dumps( scriptopts) s3.jquery_ready.append(script) # Add last update time of chart data last_update = HomepageStatistics.last_update() if last_update: output["last_stats_update"] = T("Updated on %(date)s") % { "date": last_update } else: output["last_stats_update"] = None self._view(THEME, "index.html") return output
def __call__(self): T = current.T db = current.db s3db = current.s3db user = current.auth.user user_id = user.id # Huuricane Season lasts from 1/6 to 30/11 now = current.request.utcnow if 5 < now.month < 12: SEASON = T("this Season") SEASON_START = datetime.date(now.year, 6, 1) SEASON_END = None else: SEASON = T("last Season") last_year = now.year - 1 SEASON_START = datetime.date(last_year, 6, 1) SEASON_END = datetime.date(last_year, 12, 1) # Shipments stable = s3db.inv_send fields = [ "id", "send_ref", "date", "site_id", "to_site_id", "transport_type", "status", "filing_status", ] sresource = s3db.resource( "inv_send", filter=(stable.date != None), # Don't include Unsent Shipments ) srows = sresource.select( fields, as_rows=True, limit=5, orderby=~stable.date, ) rtable = s3db.inv_recv fields = [ "id", "recv_ref", "date", "site_id", "from_site_id", "transport_type", "status", "filing_status", ] rresource = s3db.resource( "inv_recv", filter=(rtable.date != None), # Don't include Unreceived Shipments ) rrows = rresource.select( fields, as_rows=True, limit=5, orderby=~rtable.date, ) rtotal = len(rrows) # Find the most recent 5 from both lists shipments = [] sappend = shipments.append rindex = 0 stotal = 0 for srow in srows: if rindex < rtotal: rrow = rrows[rindex] else: srow.type = "send" sappend(srow) if stotal == 4: break stotal += 1 continue send_date = srow.date recv_date = rrow.date if send_date > recv_date: srow.type = "send" sappend(srow) if stotal == 4: break stotal += 1 continue rrow.type = "recv" sappend(rrow) if stotal == 4: break stotal += 1 rindex += 1 if rindex < rtotal: rrow = rrows[rindex] else: srow.type = "send" sappend(srow) if stotal == 4: break stotal += 1 continue recv_date = rrow.date if send_date > recv_date: srow.type = "send" sappend(srow) if stotal == 4: break stotal += 1 continue rrow.type = "recv" sappend(rrow) if stotal == 4: break stotal += 1 rindex += 1 if rindex < rtotal: rrow = rrows[rindex] else: srow.type = "send" sappend(srow) if stotal == 4: break stotal += 1 continue recv_date = rrow.date if send_date > recv_date: srow.type = "send" sappend(srow) if stotal == 4: break stotal += 1 continue rrow.type = "recv" sappend(rrow) if stotal == 4: break stotal += 1 rindex += 1 if rindex < rtotal: rrow = rrows[rindex] else: srow.type = "send" sappend(srow) if stotal == 4: break stotal += 1 continue recv_date = rrow.date if send_date > recv_date: srow.type = "send" sappend(srow) if stotal == 4: break stotal += 1 continue rrow.type = "recv" sappend(rrow) if stotal == 4: break stotal += 1 rindex += 1 if rindex < rtotal: rrow = rrows[rindex] else: srow.type = "send" sappend(srow) if stotal == 4: break stotal += 1 continue recv_date = rrow.date if send_date > recv_date: srow.type = "send" sappend(srow) stotal += 1 break rrow.type = "recv" sappend(rrow) stotal += 1 break while stotal < 5: if rindex < rtotal: rrow = rrows[rindex] else: break rrow.type = "recv" sappend(rrow) rindex += 1 stotal += 1 date_represent = stable.date.represent status_represent = stable.status.represent site_ids = [] for row in shipments: if row.type == "send": site_ids += [ row.site_id, row.to_site_id, ] else: site_ids += [ row.site_id, row.from_site_id, ] #sites = org_SiteRepresent(show_type = False).bulk(list(set(site_ids))) sites = S3Represent(lookup="org_site").bulk(list(set(site_ids))) sites_get = sites.get transport_opts = { "Air": ICON("plane"), "Sea": ICON("ship"), "Road": ICON("truck"), "Hand": ICON("hand-grab"), } transport_opts_get = transport_opts.get filing_opts = { SHIP_DOC_PENDING: ICON("close"), SHIP_DOC_COMPLETE: ICON("check"), } filing_opts_get = filing_opts.get shipment_rows = [ DIV( DIV( T("Date"), _class="columns medium-2", ), DIV( T("in/out"), _class="columns medium-1", ), DIV( T("From"), _class="columns medium-1", ), DIV( T("To"), _class="columns medium-1", ), DIV( T("WB/GRN"), _class="columns medium-4", ), DIV( T("Trans."), _class="columns medium-1", ), DIV( T("Status"), _class="columns medium-1", ), DIV( ICON("briefcase"), _class="columns medium-1", ), _class="ship-card row", ), ] sappend = shipment_rows.append for row in shipments: if row.type == "send": in_out = ICON("arrow-right") from_site_id = row.site_id to_site_id = row.to_site_id shipment_ref = row.send_ref url = URL( c="inv", f="send", args=[row["inv_send.id"]], ) else: in_out = ICON("arrow-left") from_site_id = row.from_site_id to_site_id = row.site_id shipment_ref = row.recv_ref url = URL( c="inv", f="recv", args=[row.id], ) sappend( DIV( DIV( date_represent(row.date), _class="columns medium-2", ), DIV( in_out, _class="columns medium-1", ), DIV( sites_get(from_site_id), _class="columns medium-1", ), DIV( sites_get(to_site_id), _class="columns medium-1", ), DIV( A( shipment_ref, _href=url, ), _class="columns medium-4", ), DIV( transport_opts_get(row.transport_type), _class="columns medium-1", ), DIV( status_represent(row.status), _class="columns medium-1", ), DIV( filing_opts_get(row.filing_status), _class="columns medium-1", ), _class="ship-card row", )) shipments = DIV(*shipment_rows) # Alerts table = s3db.auth_user_notification query = (table.user_id == user_id) & \ (table.deleted == False) rows = db(query).select( table.name, table.url, orderby=~table.created_on, ) alert_rows = [] for row in rows: alert_rows.append( DIV( A( DIV(ICON("bell-o"), _class="columns medium-1"), DIV(row.name, _class="columns medium-11"), _href=row.url, _target="_blank", ), _class="alert-card row", )) alerts = DIV(*alert_rows) # Capacity # Define the Pivot Table r = s3_request("inv", "inv_item") r.customise_resource() resource = s3db.resource("inv_inv_item") report = S3Report() report.resource = resource capacity = report.widget( r, widget_id="capacity", ajaxurl=URL(c="inv", f="inv_item", args="report.json"), ) # KPI # Which Warehouses are we responsible for? wtable = s3db.inv_warehouse gtable = db.auth_group mtable = db.auth_membership query = (mtable.user_id == user_id) & \ (mtable.deleted == False) & \ (mtable.group_id == gtable.id) & \ (gtable.uuid.belongs("ORG_ADMIN", "logs_manager", "wh_operator", )) realms = db(query).select(mtable.pe_id) realms = list(set([row.pe_id for row in realms])) if None in realms: realms.remove(None) # Lookup Default Realm from s3db.pr import pr_default_realms default_realms = pr_default_realms(user.pe_id) realms = realms + default_realms from s3db.pr import pr_get_descendants child_pe_ids = pr_get_descendants(realms, entity_types=["inv_warehouse"]) warehouses = db(wtable.pe_id.belongs(realms + child_pe_ids)).select( wtable.site_id, wtable.name, wtable.free_capacity, ) wh_site_ids = [row.site_id for row in warehouses] itable = s3db.inv_inv_item fields = [ "site_id", "total_weight", "total_volume", "quantity", # extra_fields "item_pack_id$quantity", # extra_fields "item_id.weight", # extra_fields "item_id.volume", # extra_fields ] iresource = s3db.resource( "inv_inv_item", filter=(itable.site_id.belongs(wh_site_ids)), ) rows = iresource.select(fields, as_rows=True) stockpile_weight = 0 stockpile_volume = 0 for row in rows: stockpile_weight += row["inv_inv_item.total_weight"]() stockpile_volume += row["inv_inv_item.total_volume"]() fields = [ "id", "track_item.total_weight", "track_item.total_volume", "track_item.quantity", # extra_fields "track_item.item_pack_id$quantity", # extra_fields "track_item.item_id$weight", # extra_fields "track_item.item_id$volume", # extra_fields ] query = (stable.status.belongs([SHIP_STATUS_SENT, SHIP_STATUS_RECEIVED, SHIP_STATUS_RETURNING, ])) & \ (stable.date > SEASON_START) if SEASON_END: query &= (stable.date > SEASON_END) sresource = s3db.resource("inv_send", filter=query) srows = sresource.select(fields, as_rows=True) num_shipments = len(set([row["inv_send.id"] for row in srows])) shipments_weight = 0 shipments_volume = 0 for row in srows: weight = row["inv_track_item.total_weight"]() try: shipments_weight += weight except TypeError: # NONE returned: ignore pass volume = row["inv_track_item.total_volume"]() try: shipments_volume += volume except TypeError: # NONE returned: ignore pass float_represent = IS_FLOAT_AMOUNT.represent free_capacities = UL() for row in warehouses: free_capacities.append( LI("%s: %s m3" % (row.name, float_represent(row.free_capacity, precision=1)))) kpi = UL( LI("%s: %s kg" % (T("Total weight stockpiled"), float_represent(stockpile_weight, precision=1))), LI("%s: %s m3" % (T("Total volume stockpiled"), float_represent(stockpile_volume, precision=1))), LI("%s %s: %s" % (T("Number of Shipments sent"), SEASON, num_shipments)), LI("%s %s: %s kg" % (T("Total weight sent"), SEASON, float_represent(shipments_weight, precision=1))), LI("%s %s: %s m3" % (T("Total volume sent"), SEASON, float_represent(shipments_volume, precision=3))), LI("%s: %s" % (T("Number of warehouses"), len(warehouses))), LI("%s:" % T("Remaining stockpile capacities available"), free_capacities), ) # Preparedness Checklist #checklist = UL() output = { "title": T("Dashboard"), "shipments": shipments, "alerts": alerts, "capacity": capacity, "kpi": kpi, #"checklist": checklist, } # Custom view self._view(THEME, "inv_dashboard.html") return output
def __call__(self): T = current.T db = current.db s3db = current.s3db output = {} # Recent Updates etable = s3db.event_event stable = s3db.event_sitrep query = (stable.deleted == False) & \ (stable.event_id == etable.id) fields = [ etable.name, stable.date, stable.summary, ] language = current.session.s3.language if language != current.deployment_settings.get_L10n_default_language(): ntable = s3db.event_event_name left = ntable.on((ntable.event_id == etable.id) & \ (ntable.language == language)) fields.append(ntable.name_l10n) else: left = None sitreps = db(query).select(left=left, limitby=(0, 3), orderby=~stable.date, *fields) len_sitreps = len(sitreps) if len_sitreps == 0: from s3 import S3CRUD recent_updates = DIV(S3CRUD.crud_string("event_sitrep", "msg_list_empty"), _class="empty") else: recent_updates = DIV() rappend = recent_updates.append count = 0 for s in sitreps: count += 1 if left: event_name = s["event_event_name.name_l10n"] or s[ "event_event.name"] else: event_name = s["event_event.name"] rappend(H3(event_name)) rappend(P(s["event_sitrep.summary"])) if count != len_sitreps: rappend(HR()) output["recent_updates"] = recent_updates map_btn = A( T("MAP OF CURRENT NEEDS"), #_href = URL(c="default", # f="index", # args="dashboard", # ), _href=URL( c="req", f="need_line", args="map", ), _class="small primary button", ) create_btn = A( T("CREATE A NEED"), _href=URL( c="req", f="need", args="create", ), _class="small primary button", ) output["needs_btn"] = DIV( SPAN(map_btn), SPAN(create_btn), _class="button-group radius", ) output["about_btn"] = A( "%s >" % T("Read More"), _href=URL( c="default", f="about", ), ) self._view(THEME, "index.html") # Inject D3 scripts from s3 import S3Report S3Report.inject_d3() # Inject charts-script appname = current.request.application s3 = current.response.s3 scripts = s3.scripts if s3.debug: script = "/%s/static/scripts/S3/s3.ui.charts.js" % appname if script not in scripts: scripts.append(script) else: script = "/%s/static/scripts/S3/s3.ui.charts.min.js" % appname if script not in scripts: scripts.append(script) # Instantiate charts scriptopts = { # Standard SHARE theme color set: "colors": [ '#0C9CD0', # blue '#E03158', # red '#FBA629', # amber '#8ABC3F', # green '#AFB8BF', # grey ], } script = '''$('.homepage-chart').uiChart(%s)''' % json.dumps( scriptopts) s3.jquery_ready.append(script) return output
def vehicle_report(r, **attr): """ Custom method to provide a report on Vehicle Deployment Times - this is one of the main tools currently used to manage an Incident """ rheader = attr.get("rheader", None) if rheader: rheader = rheader(r) station_id = r.id if station_id: T = current.T s3db = current.s3db ftable = s3db.fire_station atable = s3db.asset_asset eatable = s3db.event_asset itable = s3db.event_incident query = (ftable.id == station_id) & \ (ftable.site_id == atable.site_id) & \ (atable.type == 1) & \ (atable.id == eatable.asset_id) & \ (eatable.start_date != None) & \ (eatable.end_date == None) current.response.s3.crud_strings["event_asset"] = Storage( title_report=T("Vehicle Deployment Times"), ) eatable.asset_id.label = T("Vehicle") # Add field method for minutes def minutes(row): if hasattr(row, "event_asset"): row = row.event_asset if hasattr(row, "start_date") and row.start_date: return int( (r.utcnow - row.start_date).total_seconds() / 60) else: return 0 from gluon import Field eatable.minutes = Field.Method( "minutes", minutes, ) s3db.configure( "event_asset", extra_fields=["start_date"], ) from s3 import S3Report req = r.factory( prefix="event", name="asset", args=["report"], vars=Storage( rows="asset_id", cols="incident_id", fact="sum(minutes)", ), ) req.set_handler("report", S3Report()) req.resource.add_filter(query) return req(rheader=rheader)
def __call__(self): T = current.T db = current.db s3db = current.s3db s3 = current.response.s3 session_s3 = current.session.s3 output = {} # Recent Updates etable = s3db.event_event stable = s3db.event_sitrep query = (stable.deleted == False) fields = [etable.name, stable.id, stable.date, stable.name, stable.summary, ] left = [etable.on(etable.id == stable.event_id)] language = session_s3.language if language != current.deployment_settings.get_L10n_default_language(): ntable = s3db.event_event_name left.append(ntable.on((ntable.event_id == etable.id) & \ (ntable.language == language))) fields.append(ntable.name_l10n) use_local_event_name = True else: use_local_event_name = False sitreps = db(query).select(left = left, limitby = (0, 3), orderby = ~stable.date, *fields ) len_sitreps = len(sitreps) if len_sitreps == 0: from s3 import S3CRUD recent_updates = DIV(S3CRUD.crud_string("event_sitrep", "msg_list_empty"), _class="empty") else: recent_updates = DIV() rappend = recent_updates.append count = 0 for s in sitreps: count += 1 if use_local_event_name: event_name = s["event_event_name.name_l10n"] or s["event_event.name"] else: event_name = s["event_event.name"] if not event_name: event_name = s["event_sitrep.name"] rappend(H3(A(event_name, _href = URL(c = "event", f = "sitrep", args = [s["event_sitrep.id"]], ), ))) rappend(P(XML(s["event_sitrep.summary"]))) if count != len_sitreps: rappend(HR()) output["recent_updates"] = recent_updates map_btn = A(T("MAP OF CURRENT NEEDS"), #_href = URL(c="default", # f="index", # args="dashboard", # ), _href = URL(c="req", f="need_line", args="map", ), _class = "small primary button", ) create_btn = A(T("CREATE A NEED"), _href = URL(c="req", f="need", args="create", ), _class = "small primary button", ) output["needs_btn"] = DIV(SPAN(map_btn), SPAN(create_btn), _class="button-group radius", ) output["about_btn"] = A("%s >" % T("Read More"), _href = URL(c="default", f="about", ), ) # Resources section if current.deployment_settings.has_module("cms"): system_roles = current.auth.get_system_roles() ADMIN = system_roles.ADMIN in session_s3.roles table = s3db.cms_post ltable = s3db.cms_post_module module = "default" resource = "index" query = (ltable.module == module) & \ ((ltable.resource == None) | \ (ltable.resource == resource)) & \ (ltable.post_id == table.id) & \ (table.deleted != True) item = current.db(query).select(table.body, table.id, limitby=(0, 1)).first() if item: if ADMIN: item = DIV(XML(item.body), BR(), A(T("Edit"), _href=URL(c="cms", f="post", args=[item.id, "update"]), _class="action-btn")) else: item = DIV(XML(item.body)) elif ADMIN: if s3.crud.formstyle == "bootstrap": _class = "btn" else: _class = "action-btn" item = A(T("Edit"), _href=URL(c="cms", f="post", args="create", vars={"module": module, "resource": resource }), _class="%s cms-edit" % _class) else: item = "" else: item = "" output["item"] = item # Inject D3 scripts from s3 import S3Report S3Report.inject_d3() # Inject charts-script appname = current.request.application scripts = s3.scripts if s3.debug: script = "/%s/static/scripts/S3/s3.ui.charts.js" % appname if script not in scripts: scripts.append(script) else: script = "/%s/static/scripts/S3/s3.ui.charts.min.js" % appname if script not in scripts: scripts.append(script) # Instantiate charts scriptopts = { # Standard SHARE theme color set: "colors": ['#0C9CD0', # blue '#E03158', # red '#FBA629', # amber '#8ABC3F', # green '#AFB8BF', # grey ], } script = '''$('.homepage-chart').uiChart(%s)''' % json.dumps(scriptopts) s3.jquery_ready.append(script) # Add last update time of chart data last_update = HomepageStatistics.last_update() if last_update: output["last_stats_update"] = T("Updated on %(date)s") % {"date": last_update} else: output["last_stats_update"] = None self._view(THEME, "index.html") return output
def __call__(self): T = current.T db = current.db s3db = current.s3db output = {} # Recent Updates etable = s3db.event_event stable = s3db.event_sitrep query = (stable.deleted == False) fields = [etable.name, stable.date, stable.name, stable.summary, ] left = [etable.on(etable.id == stable.event_id)] language = current.session.s3.language if language != current.deployment_settings.get_L10n_default_language(): ntable = s3db.event_event_name left.append(ntable.on((ntable.event_id == etable.id) & \ (ntable.language == language))) fields.append(ntable.name_l10n) use_local_event_name = True else: use_local_event_name = False sitreps = db(query).select(left = left, limitby = (0, 3), orderby = ~stable.date, *fields ) len_sitreps = len(sitreps) if len_sitreps == 0: from s3 import S3CRUD recent_updates = DIV(S3CRUD.crud_string("event_sitrep", "msg_list_empty"), _class="empty") else: recent_updates = DIV() rappend = recent_updates.append count = 0 for s in sitreps: count += 1 if use_local_event_name: event_name = s["event_event_name.name_l10n"] or s["event_event.name"] else: event_name = s["event_event.name"] if not event_name: event_name = s["event_sitrep.name"] rappend(H3(event_name)) rappend(P(XML(s["event_sitrep.summary"]))) if count != len_sitreps: rappend(HR()) output["recent_updates"] = recent_updates map_btn = A(T("MAP OF CURRENT NEEDS"), #_href = URL(c="default", # f="index", # args="dashboard", # ), _href = URL(c="req", f="need_line", args="map", ), _class = "small primary button", ) create_btn = A(T("CREATE A NEED"), _href = URL(c="req", f="need", args="create", ), _class = "small primary button", ) output["needs_btn"] = DIV(SPAN(map_btn), SPAN(create_btn), _class="button-group radius", ) output["about_btn"] = A("%s >" % T("Read More"), _href = URL(c="default", f="about", ), ) self._view(THEME, "index.html") # Inject D3 scripts from s3 import S3Report S3Report.inject_d3() # Inject charts-script appname = current.request.application s3 = current.response.s3 scripts = s3.scripts if s3.debug: script = "/%s/static/scripts/S3/s3.ui.charts.js" % appname if script not in scripts: scripts.append(script) else: script = "/%s/static/scripts/S3/s3.ui.charts.min.js" % appname if script not in scripts: scripts.append(script) # Instantiate charts scriptopts = { # Standard SHARE theme color set: "colors": ['#0C9CD0', # blue '#E03158', # red '#FBA629', # amber '#8ABC3F', # green '#AFB8BF', # grey ], } script = '''$('.homepage-chart').uiChart(%s)''' % json.dumps(scriptopts) s3.jquery_ready.append(script) # Add last update time of chart data last_update = HomepageStatistics.last_update() if last_update: output["last_stats_update"] = T("Updated on %(date)s") % {"date": last_update} else: output["last_stats_update"] = None return output