Skip to content

Commit

Permalink
Linting fixes (#272)
Browse files Browse the repository at this point in the history
* isort fixes via `isort .`

* black fixes via `black .`

* enable isort in `invoke lint`

* Add isort to `invoke format`

* resolve various flake8 findings

* format commented out code I should have removed to appease black

* nuke large commented out code block

---------

Co-authored-by: Andrew Dickinson <[email protected]>
  • Loading branch information
james-otten and Andrew-Dickinson authored Mar 31, 2024
1 parent afbfd9e commit 4ca9422
Show file tree
Hide file tree
Showing 42 changed files with 91 additions and 183 deletions.
2 changes: 1 addition & 1 deletion src/meshapi/admin.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from django.contrib import admin
from django.contrib.admin.options import forms
from django.db.models import Q, QuerySet
from django.db.models import Q
from django.utils.safestring import mark_safe
from nonrelated_inlines.admin import NonrelatedTabularInline

Expand Down
4 changes: 2 additions & 2 deletions src/meshapi/docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ def get(self, request, *args, **kwargs):
deepLinking: true,
onComplete: ui_onComplete,
plugins: [LogoutViaDjango],
}};
const logoutURL = "{reverse("rest_framework:logout")}";
}};
const logoutURL = "{reverse("rest_framework:logout")}";
"""
if session_id:
response.data["settings"] += 'const sessionID = "{session_id}";'
Expand Down
6 changes: 3 additions & 3 deletions src/meshapi/management/commands/scramble_members.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from django.contrib.auth.models import Group, Permission
from random import randrange

from django.core.management.base import BaseCommand
from faker import Faker
from random import randrange

from meshapi.models import Member, Install
from meshapi.models import Install, Member


# Uses faker to get fake names, emails, and phone numbers
Expand Down
2 changes: 1 addition & 1 deletion src/meshapi/migrations/0001_initial.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import django.contrib.postgres.fields
import django.core.validators
from django.db import migrations, models
import django.db.models.deletion
from django.db import migrations, models


class Migration(migrations.Migration):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Generated by Django 4.2.11 on 2024-03-22 01:45

from django.db import migrations, models
import django_jsonform.models.fields
from django.db import migrations, models


class Migration(migrations.Migration):
Expand Down
2 changes: 1 addition & 1 deletion src/meshapi/tests/group_helpers.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from django.contrib.auth.models import Group, Permission
from django.contrib.auth.models import Group
from django.core.management import call_command


Expand Down
2 changes: 1 addition & 1 deletion src/meshapi/tests/test_admin_change_view.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from django.contrib.auth.models import User
from django.test import Client, TestCase

from meshapi.models import Building, Device, Install, Link, Member, Node, Sector
from meshapi.models import Building, Device, Install, Link, Member, Node

from .sample_data import sample_building, sample_device, sample_install, sample_member, sample_node

Expand Down
2 changes: 1 addition & 1 deletion src/meshapi/tests/test_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from django.contrib.auth.models import User
from django.test import Client, TestCase

from ..models import Building, Device, Link, Node
from ..models import Device, Node
from .sample_data import sample_device, sample_node


Expand Down
4 changes: 2 additions & 2 deletions src/meshapi/tests/test_foreign_key_views_and_perms.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
from django.contrib.auth.models import Permission, User
from django.test import Client, TestCase

from meshapi.models import Building, Install, Link, Member, Node, Sector
from meshapi.tests.sample_data import sample_building, sample_install, sample_member, sample_node
from meshapi.models import Building, Install, Member
from meshapi.tests.sample_data import sample_building, sample_install, sample_member


def setup_objects():
Expand Down
2 changes: 1 addition & 1 deletion src/meshapi/tests/test_join_form.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ def test_bad_phone_join_form(self):

con = json.loads(response.content.decode("utf-8"))

self.assertEqual("555-555-5555 is not a valid phone number", con["detail"], f"Content is wrong")
self.assertEqual("555-555-5555 is not a valid phone number", con["detail"], "Content is wrong")

def test_bad_email_join_form(self):
# Name, email, phone, location, apt, rooftop, referral
Expand Down
6 changes: 1 addition & 5 deletions src/meshapi/tests/test_kml_endpoint.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
import datetime
import json

from django.contrib.auth.models import Group, User
from django.test import Client, TestCase
from fastkml import kml
from lxml import etree
from rest_framework.authtoken.models import Token

from meshapi.models import Building, Device, Install, Link, Member, Node, Sector
from meshapi.models import Building, Device, Install, Link, Member, Node


def create_building_install_node_and_device(member_ref, nn):
Expand Down
2 changes: 1 addition & 1 deletion src/meshapi/tests/test_link.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from django.contrib.auth.models import User
from django.test import Client, TestCase

from ..models import Building, Device, Link, Node
from ..models import Device, Link, Node
from .sample_data import sample_device, sample_node


Expand Down
2 changes: 1 addition & 1 deletion src/meshapi/tests/test_lookups.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from django.test import Client, TestCase

from ..models import Building, Device, Install, Link, Member, Node, Sector
from .sample_data import sample_building, sample_device, sample_member
from .sample_data import sample_building, sample_member


class TestMemberLookups(TestCase):
Expand Down
4 changes: 1 addition & 3 deletions src/meshapi/tests/test_map_endpoints.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import datetime
import json

from django.contrib.auth.models import Group, User
from django.test import Client, TestCase
from rest_framework.authtoken.models import Token

from meshapi.models import Building, Device, Install, Link, Member, Node, Sector

Expand Down Expand Up @@ -107,7 +105,7 @@ def test_install_data(self):
ap_device = Device(
id=123456,
node=nodes[-1],
name=f"Northwest AP",
name="Northwest AP",
model="Unknown",
type=Device.DeviceType.AP,
install_date=datetime.date(2024, 1, 27),
Expand Down
4 changes: 2 additions & 2 deletions src/meshapi/tests/test_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
from django.contrib.auth.models import User
from django.test import Client, TestCase

from ..models import Building, Device, Link, Node
from .sample_data import sample_device, sample_node
from ..models import Node
from .sample_data import sample_node


class TestNode(TestCase):
Expand Down
2 changes: 1 addition & 1 deletion src/meshapi/tests/test_sector.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from django.contrib.auth.models import User
from django.test import Client, TestCase

from ..models import Building, Device, Node, Sector
from ..models import Device, Node, Sector
from .sample_data import sample_node


Expand Down
2 changes: 1 addition & 1 deletion src/meshapi/tests/test_update_panos_github.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from django.contrib.auth.models import User
from django.test import Client, TestCase

from meshapi.models import Building, Install, Link, Member, Sector
from meshapi.models import Building, Install, Member
from meshapi.views import panoramas

from .sample_data import sample_building, sample_install, sample_member
Expand Down
12 changes: 6 additions & 6 deletions src/meshapi/tests/test_views_post_delete.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ def test_views_post_unauthenticated(self):
assert_correct_response(self, response, 403)

def test_views_delete_unauthenticated(self):
response = self.c.delete(f"/api/v1/installs/1/")
response = self.c.delete("/api/v1/installs/1/")
assert_correct_response(self, response, 403)

response = self.c.delete(f"/api/v1/members/1/")
response = self.c.delete("/api/v1/members/1/")
assert_correct_response(self, response, 403)

response = self.c.delete(f"/api/v1/buildings/1/")
response = self.c.delete("/api/v1/buildings/1/")
assert_correct_response(self, response, 403)


Expand Down Expand Up @@ -122,13 +122,13 @@ def test_views_post_put_installer(self):
assert install.notes.endswith("\n abcdef")

def test_views_delete_installer(self):
response = self.c.delete(f"/api/v1/installs/1/")
response = self.c.delete("/api/v1/installs/1/")
assert_correct_response(self, response, 403)

response = self.c.delete(f"/api/v1/members/1/")
response = self.c.delete("/api/v1/members/1/")
assert_correct_response(self, response, 403)

response = self.c.delete(f"/api/v1/buildings/1/")
response = self.c.delete("/api/v1/buildings/1/")
assert_correct_response(self, response, 403)


Expand Down
3 changes: 1 addition & 2 deletions src/meshapi/urls.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from django.urls import include, path
from rest_framework.urlpatterns import format_suffix_patterns
from django.urls import path

from meshapi import views

Expand Down
2 changes: 1 addition & 1 deletion src/meshapi/util/django_pglocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
@contextmanager
def advisory_lock(lock_id, shared=False, wait=True, using=None):
import six
from django.db import DEFAULT_DB_ALIAS, connections, transaction
from django.db import DEFAULT_DB_ALIAS, connections

if using is None:
using = DEFAULT_DB_ALIAS
Expand Down
15 changes: 7 additions & 8 deletions src/meshapi/validation.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import json
import time
import requests
from dataclasses import dataclass
from validate_email import validate_email

import phonenumbers
from geopy.geocoders import Nominatim
from meshapi.exceptions import AddressError, AddressAPIError
from meshapi.zips import NYCZipCodes
import requests
from validate_email import validate_email

from meshapi.exceptions import AddressAPIError, AddressError
from meshdb.utils.spreadsheet_import.building.constants import INVALID_BIN_NUMBERS
from meshdb.utils.spreadsheet_import.building.pelias import humanify_street_address

Expand Down Expand Up @@ -61,7 +60,7 @@ def __init__(self, street_address: str, city: str, state: str, zip: int):
"text": self.address,
"size": 1,
}
nyc_planning_req = requests.get(f"https://geosearch.planninglabs.nyc/v2/search", params=query_params)
nyc_planning_req = requests.get("https://geosearch.planninglabs.nyc/v2/search", params=query_params)
nyc_planning_resp = json.loads(nyc_planning_req.content.decode("utf-8"))
except Exception as e:
print(f"Got exception querying geosearch.planninglabs.nyc: {e}")
Expand Down Expand Up @@ -107,7 +106,7 @@ def __init__(self, street_address: str, city: str, state: str, zip: int):
"$select": "heightroof,groundelev",
"$limit": 1,
}
nyc_dataset_req = requests.get(f"https://data.cityofnewyork.us/resource/qb5r-6dgf.json", params=query_params)
nyc_dataset_req = requests.get("https://data.cityofnewyork.us/resource/qb5r-6dgf.json", params=query_params)
nyc_dataset_resp = json.loads(nyc_dataset_req.content.decode("utf-8"))

if len(nyc_dataset_resp) == 0:
Expand Down
2 changes: 1 addition & 1 deletion src/meshapi/views/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
from .lookups import *
from .map import *
from .model_api import *
from .query_api import *
from .panoramas import *
from .query_api import *
6 changes: 3 additions & 3 deletions src/meshapi/views/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import logging
import time
from dataclasses import dataclass
from datetime import date, datetime
from datetime import date
from json.decoder import JSONDecodeError
from typing import Optional

Expand Down Expand Up @@ -127,7 +127,7 @@ def join_form(request):
print("(NYC) Something went wrong validating the address. Re-trying...")
time.sleep(3)
# If we run out of tries, bail.
if nyc_addr_info == None:
if nyc_addr_info is None:
print(f"Could not parse address: {r.street_address}, {r.city}, {r.state}, {r.zip}")
return Response(
{"detail": "Your address could not be validated."}, status=status.HTTP_500_INTERNAL_SERVER_ERROR
Expand Down Expand Up @@ -401,7 +401,7 @@ def network_number_assignment(request):
return Response({"detail": "Install Number not found"}, status=status.HTTP_404_NOT_FOUND)

# Check if the install already has a network number
if nn_install.node != None:
if nn_install.node is not None:
message = f"This Install Number ({r.install_number}) already has a Network Number ({nn_install.node.network_number}) associated with it!"
print(message)
return Response(
Expand Down
2 changes: 1 addition & 1 deletion src/meshapi/views/geography.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from rest_framework.negotiation import BaseContentNegotiation
from rest_framework.views import APIView

from meshapi.models import Building, Install, Link, Node
from meshapi.models import Install, Link

KML_CONTENT_TYPE = "application/vnd.google-earth.kml+xml"
KML_CONTENT_TYPE_WITH_CHARSET = f"{KML_CONTENT_TYPE}; charset=utf-8"
Expand Down
2 changes: 1 addition & 1 deletion src/meshapi/views/map.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from drf_spectacular.utils import extend_schema, extend_schema_view
from rest_framework import generics, permissions

from meshapi.models import Building, Device, Install, Link, Node, Sector
from meshapi.models import Device, Install, Link, Node, Sector
from meshapi.serializers import (
ALLOWED_INSTALL_STATUSES,
EXCLUDED_INSTALL_STATUSES,
Expand Down
2 changes: 0 additions & 2 deletions src/meshapi/views/model_api.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
from django.contrib.auth.models import User
from drf_spectacular.types import OpenApiTypes
from drf_spectacular.utils import OpenApiExample, OpenApiResponse, extend_schema, extend_schema_view
from rest_framework import generics, permissions
from rest_framework.decorators import api_view, permission_classes
from rest_framework.response import Response

from meshapi.models import Building, Device, Install, Link, Member, Node, Sector
from meshapi.permissions import IsReadOnly
from meshapi.serializers import (
BuildingSerializer,
DeviceSerializer,
Expand Down
9 changes: 4 additions & 5 deletions src/meshapi/views/panoramas.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
import os
from pathlib import Path

import requests
from rest_framework import permissions
from celery.schedules import crontab
from rest_framework.decorators import api_view, permission_classes
from rest_framework.response import Response
from rest_framework.views import status
from meshapi.models import Building, Install
from meshapi.permissions import HasPanoramaUpdatePermission

from meshapi.models import Install
from meshapi.permissions import HasPanoramaUpdatePermission
from meshapi.util.django_pglocks import advisory_lock

from meshdb.celery import app as celery_app
from celery.schedules import crontab

# Config for gathering/generating panorama links
PANO_REPO_OWNER = "nycmeshnet"
Expand Down
20 changes: 3 additions & 17 deletions src/meshapi/views/query_api.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,12 @@
import os
from typing import Any, Dict, Optional

from drf_spectacular.utils import extend_schema, extend_schema_view
from meshapi.views.lookups import FilterRequiredListAPIView, InstallFilter
from rest_framework import generics
from rest_framework.views import models

from meshapi import permissions
from meshapi.docs import map_query_filters_to_param_annotations, query_form_password_param
from meshapi.models import Building, Install, Member
from meshapi.serializers.query_api import QueryFormSerializer
from django.db.models import Q
from django_filters import rest_framework as filters
from drf_spectacular.types import OpenApiTypes
from drf_spectacular.utils import OpenApiParameter, extend_schema, extend_schema_view
from rest_framework import generics
from rest_framework.response import Response

from rest_framework.decorators import api_view, permission_classes

from rest_framework import permissions
from meshapi.models import Install
from meshapi.permissions import LegacyMeshQueryPassword
from meshapi.serializers.query_api import QueryFormSerializer
from meshapi.views.lookups import FilterRequiredListAPIView

"""
Re-implements https://docs.nycmesh.net/installs/query/
Expand Down
4 changes: 2 additions & 2 deletions src/meshapi_hooks/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# Generated by Django 4.2.9 on 2024-02-06 02:26

from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
import drf_hooks.models
from django.conf import settings
from django.db import migrations, models


class Migration(migrations.Migration):
Expand Down
Loading

0 comments on commit 4ca9422

Please sign in to comment.