From 37cb41af1b61ac910e6b0c3ef073c8cbd4757def Mon Sep 17 00:00:00 2001 From: Adam Goldsmith Date: Thu, 18 Jan 2024 14:00:36 -0500 Subject: [PATCH] Enable ruff's "pylint" rules --- doorcontrol/hid/DoorController.py | 4 +-- membershipworks/membershipworks_api.py | 35 ++++++++++++++------------ membershipworks/routers.py | 5 +--- membershipworks/tasks/scrape.py | 12 +++++---- pyproject.toml | 2 +- 5 files changed, 30 insertions(+), 28 deletions(-) diff --git a/doorcontrol/hid/DoorController.py b/doorcontrol/hid/DoorController.py index 6183592..b51b21a 100644 --- a/doorcontrol/hid/DoorController.py +++ b/doorcontrol/hid/DoorController.py @@ -50,7 +50,7 @@ class DoorController: ) # ignore insecure SSL xml = etree.XML(r.content) if ( - r.status_code != 200 + r.status_code != requests.codes.ok or len(xml.findall("{http://www.hidglobal.com/VertX}Error")) > 0 ): raise RemoteError(r) @@ -75,7 +75,7 @@ class DoorController: ) resp_xml = etree.XML(r.content) # probably meed to be more sane about this - if r.status_code != 200 or len(resp_xml.findall("{*}Error")) > 0: + if r.status_code != requests.codes.ok or len(resp_xml.findall("{*}Error")) > 0: raise RemoteError(r) return resp_xml diff --git a/membershipworks/membershipworks_api.py b/membershipworks/membershipworks_api.py index 2097023..5dfa673 100644 --- a/membershipworks/membershipworks_api.py +++ b/membershipworks/membershipworks_api.py @@ -1,5 +1,6 @@ import csv import datetime +from enum import Enum from io import StringIO import requests @@ -32,18 +33,20 @@ CRM = { 27: "Payment", } -# Types of fields, extracted from a html snippet in all.js + some guessing -typ = { - 1: "Text input", - 2: "Password", # inferred from data - 3: "Simple text area", - 4: "Rich text area", - 7: "Address", - 8: "Check box", - 9: "Select", - 11: "Display value stored in field (ie. read only)", - 12: "Required waiver/terms", -} + +# Types of fields ("typ"), extracted from a html snippet in all.js + some guessing +class FieldType(Enum): + TEXT_INPUT = 1 + PASSWORD = 2 # inferred from data + SIMPLE_TEXT_AREA = 3 + RICH_TEXT_AREA = 4 + ADDRESS = 7 + CHECKBOX = 8 + SELECT = 9 + # Display value stored in field + READ_ONLY = 11 + REQUIRED_WAIVER_TERMS = 12 + # more constants, this time extracted from the members csv export in all.js staticFlags = { @@ -85,7 +88,7 @@ class MembershipWorks: data={"eml": username, "pwd": password}, headers={"X-Org": "10000"}, ) - if r.status_code != 200 or "SF" not in r.json(): + if r.status_code != requests.codes.ok or "SF" not in r.json(): raise MembershipWorksRemoteError("login", r) self.org_info = r.json() self.auth_token = self.org_info["SF"] @@ -168,7 +171,7 @@ class MembershipWorks: BASE_URL + "/v2/accounts", params={"dek": ",".join([folder_map[f] for f in folders])}, ) - if r.status_code != 200 or "usr" not in r.json(): + if r.status_code != requests.codes.ok or "usr" not in r.json(): raise MembershipWorksRemoteError("user listing", r) # get list of member ID matching the search @@ -196,7 +199,7 @@ class MembershipWorks: "var": columns, }, ) - if r.status_code != 200: + if r.status_code != requests.codes.ok: raise MembershipWorksRemoteError("csv generation", r) if r.text[0] == "\ufeff": @@ -221,7 +224,7 @@ class MembershipWorks: "edp": end_date.strftime("%s"), }, ) - if r.status_code != 200: + if r.status_code != requests.codes.ok: raise MembershipWorksRemoteError("csv generation", r) if json: return r.json() diff --git a/membershipworks/routers.py b/membershipworks/routers.py index 6d2677a..aae1652 100644 --- a/membershipworks/routers.py +++ b/membershipworks/routers.py @@ -18,9 +18,6 @@ class MembershipWorksRouter: return None def allow_relation(self, obj1, obj2, **hints): - if ( - obj1._meta.app_label == self.app_label - or obj2._meta.app_label == self.app_label - ): + if self.app_label in (obj1._meta.app_label, obj2._meta.app_label): return True return None diff --git a/membershipworks/tasks/scrape.py b/membershipworks/tasks/scrape.py index 023ae3a..dd101c8 100644 --- a/membershipworks/tasks/scrape.py +++ b/membershipworks/tasks/scrape.py @@ -4,7 +4,7 @@ from datetime import datetime, timedelta from django.conf import settings from django.db import transaction -from membershipworks.membershipworks_api import MembershipWorks +from membershipworks.membershipworks_api import FieldType, MembershipWorks from membershipworks.models import ( Event, EventCategory, @@ -24,9 +24,8 @@ def flags_for_member(csv_member, all_flags, folders): if flag.type == "folder": if csv_member["Account ID"] in folders[flag.id]: yield flag - else: - if csv_member[flag.name] == flag.name: - yield flag + elif csv_member[flag.name] == flag.name: + yield flag def update_flags(mw_flags) -> list[Flag]: @@ -52,7 +51,10 @@ def scrape_members(membershipworks: MembershipWorks): for csv_member in members: for field in membershipworks._all_fields().values(): # convert checkboxes to real booleans - if field.get("typ") == 8 and field["lbl"] in csv_member: + if ( + field.get("typ") == FieldType.CHECKBOX.value + and field["lbl"] in csv_member + ): csv_member[field["lbl"]] = ( True if csv_member[field["lbl"]] == "Y" else False ) diff --git a/pyproject.toml b/pyproject.toml index da75590..7878065 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -43,7 +43,7 @@ admin_email = "cmsmanage.django_q2_admin_email_reporter:AdminEmailReporter" [tool.ruff] line-length = 88 -select = ["E4", "E7", "E9", "F", "I", "C4", "UP", "PERF"] +select = ["E4", "E7", "E9", "F", "I", "C4", "UP", "PERF", "PL"] [tool.ruff.lint.isort] known-first-party = [