Skip to content

Commit

Permalink
chore: Fix E501
Browse files Browse the repository at this point in the history
  • Loading branch information
jpmckinney committed Dec 10, 2023
1 parent ebd5ddc commit cef4ac8
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 20 deletions.
10 changes: 6 additions & 4 deletions representatives/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,17 @@ def update_from_data_source(self, request, queryset):
try:
count = individual_set.update_from_data_source()
except Exception:
messages.error(request, "Couldn't update individuals in {}: {}".format(individual_set, traceback.format_exc()))
messages.error(request, f"Couldn't update individuals in {individual_set}: {traceback.format_exc()}")
continue
if count is False:
messages.error(request, "Couldn't update individuals in %s." % individual_set)
messages.error(request, f"Couldn't update individuals in {individual_set}.")
else:
message = "Updated {} individuals in {}.".format(count, individual_set)
message = f"Updated {count} individuals in {individual_set}."
no_boundaries = individual_set.individuals.filter(boundary='').values_list('name', flat=True)
if no_boundaries:
messages.warning(request, message + " %d match no boundary (%s)." % (len(no_boundaries), ', '.join(no_boundaries)))
messages.warning(
request, message + f" {len(no_boundaries)} match no boundary ({', '.join(no_boundaries)})."
)
else:
messages.success(request, message)

Expand Down
8 changes: 6 additions & 2 deletions representatives/management/commands/updaterepresentatives.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,13 @@ class Command(BaseCommand):
help = 'Updates representatives from sources.'

def handle(self, *args, **options):
for representative_set in itertools.chain(RepresentativeSet.objects.filter(enabled=True), Election.objects.filter(enabled=True)):
for representative_set in itertools.chain(
RepresentativeSet.objects.filter(enabled=True), Election.objects.filter(enabled=True)
):
try:
representative_set.update_from_data_source()
except Exception:
log.error("Couldn't update representatives in %s." % representative_set)
representative_set.__class__.objects.filter(pk=representative_set.pk).update(last_import_successful=False)
representative_set.__class__.objects.filter(pk=representative_set.pk).update(
last_import_successful=False
)
11 changes: 6 additions & 5 deletions representatives/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ def update_from_data_source(self):
try:
setattr(rep, json_fieldname, json.loads(source_rep.get(json_fieldname)))
except ValueError:
raise Exception("Invalid JSON in {}: {}".format(json_fieldname, source_rep.get(json_fieldname)))
raise Exception(f"Invalid JSON in {json_fieldname}: {source_rep.get(json_fieldname)}")
if isinstance(getattr(rep, json_fieldname), list):
for d in getattr(rep, json_fieldname):
if isinstance(d, dict):
Expand All @@ -175,7 +175,7 @@ def update_from_data_source(self):
rep.incumbent = False

if not source_rep.get('name'):
rep.name = ' '.join([component for component in [source_rep.get('first_name'), source_rep.get('last_name')] if component])
rep.name = ' '.join([c for c in [source_rep.get('first_name'), source_rep.get('last_name')] if c])
if not source_rep.get('first_name') and not source_rep.get('last_name'):
(rep.first_name, rep.last_name) = split_name(rep.name)

Expand All @@ -190,7 +190,9 @@ def update_from_data_source(self):
boundary_url = boundary_names.get(get_comparison_string(rep.district_name))

if not boundary_url:
logger.warning("{}: Couldn't find district boundary {} in {}".format(self.slug, rep.district_name, self.boundary_set))
logger.warning(
"%s: Couldn't find district boundary %s in %s", self.slug, rep.district_name, self.boundary_set
)
else:
rep.boundary = boundary_url_to_name(boundary_url)
if not rep.district_name:
Expand Down Expand Up @@ -277,8 +279,7 @@ class Meta:
abstract = True

def __str__(self):
return "{} ({} for {})".format(
self.name, self.elected_office, self.district_name)
return f"{self.name} ({self.elected_office} for {self.district_name})"

@property
def boundary_url(self):
Expand Down
18 changes: 14 additions & 4 deletions representatives/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,33 @@

urlpatterns = [
path('representatives/', RepresentativeListView.as_view()),
re_path(r'^representatives/(?P<set_slug>[\w_-]+)/$', RepresentativeListView.as_view(), name='representatives_representative_list'),
re_path(
r'^representatives/(?P<set_slug>[\w_-]+)/$',
RepresentativeListView.as_view(),
name='representatives_representative_list',
),
re_path(r'^boundaries/(?P<slug>[\w_-]+/[\w_-]+)/representatives/', RepresentativeListView.as_view()),
path('representative-sets/', RepresentativeSetListView.as_view()),
re_path(
r'^representative-sets/(?P<slug>[\w_-]+)/$', RepresentativeSetDetailView.as_view(),
r'^representative-sets/(?P<slug>[\w_-]+)/$',
RepresentativeSetDetailView.as_view(),
name='representatives_representative_set_detail'
),
]

if app_settings.ENABLE_CANDIDATES:
urlpatterns += [
path('candidates/', CandidateListView.as_view()),
re_path(r'^candidates/(?P<set_slug>[\w_-]+)/$', CandidateListView.as_view(), name='representatives_candidate_list'),
re_path(
r'^candidates/(?P<set_slug>[\w_-]+)/$',
CandidateListView.as_view(),
name='representatives_candidate_list',
),
re_path(r'^boundaries/(?P<slug>[\w_-]+/[\w_-]+)/candidates/$', CandidateListView.as_view()),
path('elections/', ElectionListView.as_view()),
re_path(
r'^elections/(?P<slug>[\w_-]+)/$', ElectionDetailView.as_view(),
r'^elections/(?P<slug>[\w_-]+)/$',
ElectionDetailView.as_view(),
name='representatives_election_detail'
),
]
7 changes: 5 additions & 2 deletions representatives/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,14 @@ def filter(self, request, qs):
if 'point' in request.GET:
if app_settings.RESOLVE_POINT_REQUESTS_OVER_HTTP:
url = app_settings.BOUNDARYSERVICE_URL + 'boundaries/?' + urlencode({'contains': request.GET['point']})
boundaries = [boundary_url_to_name(boundary['url']) for boundary in json.loads(urlopen(url).read().decode())['objects']]
boundaries = [
boundary_url_to_name(boundary['url'])
for boundary in json.loads(urlopen(url).read().decode())['objects']
]
else:
try:
latitude, longitude = re.sub(r'[^\d.,-]', '', request.GET['point']).split(',')
wkt = 'POINT({} {})'.format(longitude, latitude)
wkt = f'POINT({longitude} {latitude})'
boundaries = Boundary.objects.filter(shape__contains=wkt).values_list('set_id', 'slug')
except ValueError:
raise BadRequest("Invalid latitude,longitude '%s' provided." % request.GET['point'])
Expand Down
4 changes: 1 addition & 3 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,5 @@ install_requires =
profile = black

[flake8]
max-line-length = 119
exclude = representatives/migrations
extend-ignore =
# E501 line too long (X > 79 characters)
E501

0 comments on commit cef4ac8

Please sign in to comment.