LeafletMap

Below is an example configuration of a panel using the LeafletMap module:

{
    "full_row": True,
    "title": "World Map",
    "url_name": "frame_leaflet_map",
    "url_action_name": "panel_edb_map",
    "type": "Map",
    "width": 12,
    "height": 500,
    "content": {
        "view": custom_views.LeafletMapEdb,
        "view_params": {
            "model": custom_models.EdbCalculation,
            "order_by": "supplier__location__country_code_a2",
        },
        "url_params_list": [
            None,
            {"category": "str"},
        ],
    },
    "footer": {
        "select_name": "category",
        "select_values": {
            "self": "Total Risk Exposure Score",
            "social_and_labor": "Social & Labor Score",
            "health_and_safety": "Health & Safety Score",
            "environment": "Environment Score",
        },
        "col_classes": "col-md-6 col-sm-6 col-xs-6",
    },
},

The view_params are:

  • model the model this map is showing

  • order_by the field in the model

The custom_views can be defined as followed:

class LeafletMapEdb(api_views.LeafletMap):

    category = "total_risk_exposure_score"

    def get_country_code_a2_map(self):

        def get_background_color(value):
            if value < 3:
                return "#FF5733"
            elif value < 6:
                return "#FFE4B3"
            else:
                return "#4CE64C"

        country_code_a2_map = {}

        queryset = self.get_queryset()
        if queryset.exists():
            country_risks = list(
                queryset
                .values(
                    "supplier__location__country_code_a2",
                    "social_and_labor",
                    "health_and_safety",
                    "environment",
                    "total_risk_exposure_score",
                )
                .distinct()
            )

            for country_risk in country_risks:
                country_code_a2 = country_risk["supplier__location__country_code_a2"].upper()
                country_code_a2_map[country_code_a2] = {
                    "background_color": get_background_color(country_risk.get(self.category, 0)),
                    "attributes": {
                        "Social & Labor Risk": round(
                            country_risk["social_and_labor"], 2
                        ),
                        "Health & Safety Risk": round(
                            country_risk["health_and_safety"], 2
                        ),
                        "Environment Risk": round(country_risk["environment"], 2),
                        "Total Risk Exposure": round(
                            country_risk["total_risk_exposure_score"], 2
                        ),
                    },
                    "image_url": None,
                    "url": None,
                    "custom_html": None,
                }

            return country_code_a2_map

    def get_marker_map(self):
        marker_map = {}
        queryset = self.get_queryset(
            exclude_filters={
                "supplier__latitude__isnull": True,
            }
        )
        if queryset.exists():
            edbcalculations = list(
                queryset.values(
                    "supplier__id",
                    "supplier__name", # title
                    "supplier__latitude",
                    "supplier__longitude",
                    "supplier__company_website",
                    "supplier__business_registration_number",
                    "supplier__factory_ffc",
                    "supplier__facility_street",
                    "supplier__facility_city",
                    "supplier__facility_state",
                    "social_and_labor",
                    "health_and_safety",
                    "environment",
                    "total_risk_exposure_score",
                    "fob_spend",
                ).distinct()
            )
            for edbcalculation in edbcalculations:
                marker_map[edbcalculation["supplier__id"]] = {
                    "coordinates": {
                        "latitude": edbcalculation["supplier__latitude"],
                        "longitude": edbcalculation["supplier__longitude"],
                    },
                    "title": '<a current_page_name="grid_search" class="menu_control_link" name="grid_search_result_{search_reference}{value}" menu_item="grid_search_group_container" href="#custom_web/grid_search_result_{search_reference}/{value}" target="{target}">{label}<i class="mdi mdi-open-in-new"></i></a>'.format(
                        value=edbcalculation["supplier__id"],
                        label=edbcalculation["supplier__name"],
                        search_reference="supplier",
                        target="_blank",
                    ),
                    "attributes": {
                        "BR#": edbcalculation["supplier__business_registration_number"],
                        "FFC#": edbcalculation["supplier__factory_ffc"],
                        "Province": edbcalculation["supplier__facility_state"],
                        "City": edbcalculation["supplier__facility_city"],
                        "Street": edbcalculation["supplier__facility_street"],
                        "Website": "<a href='{url}' target='_blank'>{url}</a>".format(
                            url=edbcalculation["supplier__company_website"]
                        )
                        if edbcalculation["supplier__company_website"]
                        else None,
                        "FOB Spend": str_utils.millions_formatter(edbcalculation["fob_spend"]),
                        # "Social & Labor Risk": round(edbcalculation["social_and_labor"], 2),
                        # "Health & Safety Risk": round(edbcalculation["health_and_safety"], 2),
                        # "Environment Risk": round(edbcalculation["environment"], 2),
                        # "Total Risk Exposure": round(
                        #     edbcalculation["total_risk_exposure_score"], 2
                        # ),
                    },
                    # "image_url": None,
                    # "custom_html": None,
                }

        return marker_map

Function get_marker_map can have all of the information about the supplier and show them on the map.

Last updated