Skip to content

Commit

Permalink
Merge branch 'main' into james/rm_pgadmin
Browse files Browse the repository at this point in the history
  • Loading branch information
james-otten authored Oct 9, 2024
2 parents 41d3353 + 89e90cb commit da8a0de
Show file tree
Hide file tree
Showing 5 changed files with 154 additions and 11 deletions.
12 changes: 11 additions & 1 deletion src/meshapi/serializers/nested_key_object_related_field.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import typing
from typing import Any, Dict, Tuple, cast
from uuid import UUID

from django.core.exceptions import ObjectDoesNotExist
from django.db.models import Model
Expand Down Expand Up @@ -42,7 +43,16 @@ def _get_key_fields(self) -> Tuple[str, ...]:
return ("id",) + self.additional_keys

def to_representation(self, value: Model) -> dict[str, Any]:
return {key: getattr(value, key) for key in self._get_key_fields()}
output = {}
for key in self._get_key_fields():
output[key] = getattr(value, key)

# Convert UUID objects to str so that the resulting data
# is trivially JSON serializable
if isinstance(output[key], UUID):
output[key] = str(output[key])

return output

def to_internal_value(self, data: dict) -> Model:
queryset = self.get_queryset()
Expand Down
80 changes: 80 additions & 0 deletions src/meshapi/tests/test_map_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,54 @@ def test_install_data(self):
)
)

nodes.append(
Node(
network_number=9823,
status=Node.NodeStatus.ACTIVE,
latitude=40.724868,
longitude=-73.987881,
)
)
installs.append(
Install(
install_number=12381924,
status=Install.InstallStatus.PENDING,
request_date=datetime.date(2024, 1, 27),
roof_access=True,
building=buildings[-1],
node=nodes[-1],
member=member,
)
)

nodes.append(
Node(
network_number=9821,
status=Node.NodeStatus.ACTIVE,
latitude=40.724868,
longitude=-73.987881,
)
)

buildings.append(
Building(
address_truth_sources=[],
latitude=40.6962265,
longitude=-73.9917741,
altitude=66,
primary_node=nodes[-1],
)
)

nodes.append(
Node(
network_number=9820,
status=Node.NodeStatus.ACTIVE,
latitude=40.724868,
longitude=-73.987881,
)
)

for node in nodes:
node.save()

Expand Down Expand Up @@ -391,6 +439,30 @@ def test_install_data(self):
"roofAccess": True,
"panoramas": [],
},
{
"coordinates": [-73.987881, 40.724868, None],
"id": 9820,
"panoramas": [],
"requestDate": None,
"roofAccess": True,
"status": "NN assigned",
},
{
"coordinates": [-73.987881, 40.724868, None],
"id": 9821,
"panoramas": [],
"requestDate": None,
"roofAccess": True,
"status": "NN assigned",
},
{
"coordinates": [-73.987881, 40.724868, None],
"id": 9823,
"panoramas": [],
"requestDate": 1706331600000,
"roofAccess": True,
"status": "NN assigned",
},
{
"id": 9999,
"status": "Installed",
Expand Down Expand Up @@ -438,6 +510,14 @@ def test_install_data(self):
"panoramas": [],
"roofAccess": True,
},
{
"coordinates": [-73.9917741, 40.6962265, 66.0],
"id": 12381924,
"panoramas": [],
"requestDate": 1706331600000,
"roofAccess": True,
"status": "Interested",
},
{
"id": 1123456,
"name": "Northwest AP",
Expand Down
26 changes: 24 additions & 2 deletions src/meshapi/tests/test_slack_notification.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,26 @@
from django.test import RequestFactory, TestCase
from requests import RequestException

from meshapi.models import Member
from meshapi.models import Building, Install, Member
from meshapi.serializers import MemberSerializer
from meshapi.tests.sample_data import sample_building, sample_install, sample_member
from meshapi.util.admin_notifications import notify_administrators_of_data_issue


class TestSlackNotification(TestCase):
def setUp(self):
self.sample_install_copy = sample_install.copy()
self.building_1 = Building(**sample_building)
self.building_1.save()
self.sample_install_copy["building"] = self.building_1

self.sample_member = Member(**sample_member)
self.sample_member.save()
self.sample_install_copy["member"] = self.sample_member

self.install = Install(**self.sample_install_copy)
self.install.save()

@requests_mock.Mocker()
@patch("meshapi.util.admin_notifications.SLACK_ADMIN_NOTIFICATIONS_WEBHOOK_URL", "https://mock-slack-url")
def test_slack_notification_for_name_change(self, requests_mocker):
Expand All @@ -23,6 +37,9 @@ def test_slack_notification_for_name_change(self, requests_mocker):
)
member.save()

self.install.member = member
self.install.save()

rf = RequestFactory()
mock_join_form_request = rf.post("https://mock-meshdb-url.example/join-form/")

Expand Down Expand Up @@ -56,7 +73,12 @@ def test_slack_notification_for_name_change(self, requests_mocker):
' "all_phone_numbers": [\n'
' "+1 212-555-5555"\n'
" ],\n"
' "installs": [],\n'
' "installs": [\n'
" {\n"
f' "id": "{self.install.id}",\n'
f' "install_number": {self.install.install_number}\n'
" }\n"
" ],\n"
' "name": "Stacy Maidenname",\n'
' "primary_email_address": "[email protected]",\n'
' "stripe_email_address": null,\n'
Expand Down
36 changes: 28 additions & 8 deletions src/meshapi/views/map.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from rest_framework.response import Response
from rest_framework.views import APIView

from meshapi.models import LOS, AccessPoint, Device, Install, Link, Node, Sector
from meshapi.models import LOS, AccessPoint, Building, Device, Install, Link, Node, Sector
from meshapi.serializers import (
EXCLUDED_INSTALL_STATUSES,
MapDataInstallSerializer,
Expand Down Expand Up @@ -71,6 +71,7 @@ def get_queryset(self) -> List[Install]: # type: ignore[override]
Node.objects.filter(~Q(status=Node.NodeStatus.INACTIVE))
.prefetch_related("devices")
.prefetch_related("installs")
.prefetch_related("buildings")
.prefetch_related(
Prefetch(
"installs",
Expand All @@ -89,10 +90,27 @@ def get_queryset(self) -> List[Install]: # type: ignore[override]
if node.network_number and node.network_number not in covered_nns:
# Arbitrarily pick a representative install for the details of the "Fake" node,
# preferring active installs if possible
representative_install = (
node.active_installs # type: ignore[attr-defined]
or node.prefetched_installs # type: ignore[attr-defined]
)[0]
try:
representative_install = (
node.active_installs # type: ignore[attr-defined]
or node.prefetched_installs # type: ignore[attr-defined]
)[0]
except IndexError:
representative_install = None

if representative_install:
building = representative_install.building
else:
building = node.buildings.first()

if not building:
# If we couldn't get a building from the install or node,
# make a faux one instead, to carry the lat/lon info into the serializer
building = Building(
latitude=node.latitude,
longitude=node.longitude,
altitude=node.altitude,
)

all_installs.append(
Install(
Expand All @@ -101,9 +119,11 @@ def get_queryset(self) -> List[Install]: # type: ignore[override]
status=Install.InstallStatus.NN_REASSIGNED
if node.status == node.NodeStatus.ACTIVE
else Install.InstallStatus.REQUEST_RECEIVED,
building=representative_install.building,
request_date=representative_install.request_date,
roof_access=representative_install.roof_access,
building=building,
request_date=representative_install.request_date
if representative_install
else node.install_date,
roof_access=representative_install.roof_access if representative_install else True,
),
)
covered_nns.add(node.network_number)
Expand Down
11 changes: 11 additions & 0 deletions src/meshdb/utils/spreadsheet_import/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,17 @@ def main():
building = get_or_create_building(row, addr_parser, dropped_modifications.append)
if not building:
skipped[row.id] = "Unable to parse address"
if (
new
and not member.all_email_addresses
and not member.all_phone_numbers
and not member.name
and not member.notes
):
# If this member object stores no contact information, and is not going to be
# used for an install because of an invalid address, remove the member object
# to avoid cluttering the DB with "husk" members that are entirely blank
member.delete()
continue

node = get_or_create_node(row)
Expand Down

0 comments on commit da8a0de

Please sign in to comment.