From 8093d8288e80650af5109a171ed547d393c17eb3 Mon Sep 17 00:00:00 2001 From: Tony Date: Tue, 7 Jun 2022 19:50:46 -0500 Subject: [PATCH] clean up for linters --- .../0003_set_site_domain_and_name.py | 2 +- config/settings/base.py | 6 +- config/settings/local.py | 9 ++- docs/conf.py | 1 + instagen/utils/gen_image.py | 35 +--------- setup.cfg | 1 + teamsnap/migrations/0001_initial.py | 22 ++++-- teamsnap/urls.py | 15 ++-- teamsnap/views.py | 69 +++++++++++-------- 9 files changed, 78 insertions(+), 82 deletions(-) diff --git a/benchcoach/contrib/sites/migrations/0003_set_site_domain_and_name.py b/benchcoach/contrib/sites/migrations/0003_set_site_domain_and_name.py index f83474b..fefa49c 100644 --- a/benchcoach/contrib/sites/migrations/0003_set_site_domain_and_name.py +++ b/benchcoach/contrib/sites/migrations/0003_set_site_domain_and_name.py @@ -23,7 +23,7 @@ def _update_or_create_site_with_sequence(site_model, connection, domain, name): # site is created. # To avoid this, we need to manually update DB sequence and make sure it's # greater than the maximum value. - max_id = site_model.objects.order_by('-id').first().id + max_id = site_model.objects.order_by("-id").first().id with connection.cursor() as cursor: cursor.execute("SELECT last_value from django_site_id_seq") (current_id,) = cursor.fetchone() diff --git a/config/settings/base.py b/config/settings/base.py index d0da613..c1b3d3e 100644 --- a/config/settings/base.py +++ b/config/settings/base.py @@ -273,8 +273,4 @@ SOCIALACCOUNT_FORMS = {"signup": "benchcoach.users.forms.UserSocialSignupForm"} # Your stuff... # ------------------------------------------------------------------------------ INSTALLED_APPS += ["teamsnap", "instagen"] -SOCIALACCOUNT_PROVIDERS = { - 'teamsnap': { - 'SCOPE': ["read", "write"] - } -} +SOCIALACCOUNT_PROVIDERS = {"teamsnap": {"SCOPE": ["read", "write"]}} diff --git a/config/settings/local.py b/config/settings/local.py index 1d61629..6a5adb2 100644 --- a/config/settings/local.py +++ b/config/settings/local.py @@ -64,7 +64,14 @@ INSTALLED_APPS += ["django_extensions"] # noqa F405 # Your stuff... # ------------------------------------------------------------------------------ -ALLOWED_HOSTS += ["localhost", "0.0.0.0", "127.0.0.1", "asc-nas.local", "10.0.1.4", "benchcoach-dev.ascorrea.com"] +ALLOWED_HOSTS += [ + "localhost", + "0.0.0.0", + "127.0.0.1", + "asc-nas.local", + "10.0.1.4", + "benchcoach-dev.ascorrea.com", +] ACCOUNT_AUTHENTICATION_METHOD = "email" ACCOUNT_EMAIL_REQUIRED = True ACCOUNT_USERNAME_REQUIRED = False diff --git a/docs/conf.py b/docs/conf.py index 3d45fbb..610663e 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -12,6 +12,7 @@ import os import sys + import django if os.getenv("READTHEDOCS", default=False) == "True": diff --git a/instagen/utils/gen_image.py b/instagen/utils/gen_image.py index c8c0b67..dfa2f40 100644 --- a/instagen/utils/gen_image.py +++ b/instagen/utils/gen_image.py @@ -2,10 +2,8 @@ import os from dataclasses import dataclass from datetime import datetime from pathlib import Path -from typing import List -from zoneinfo import ZoneInfo -from PIL import Image, ImageDraw, ImageFilter, ImageFont +from PIL import Image, ImageDraw, ImageFont # image_directory = 'input/images/logos-bw/{filename}.{ext}' @@ -115,19 +113,6 @@ def gen_image( # transparent color. tmp = Image.new("RGBA", background_image.size, (0, 0, 0, 0)) - # Create a drawing context for it. - draw = ImageDraw.Draw(tmp) - - # section margin describes the margin of the section rectangles from the sides of the image - section_margin_pct = 0.05 - llx = int(section_margin_pct * background_image.size[0]) - urx = int((1 - section_margin_pct) * background_image.size[0]) - lly = int((1 - section_margin_pct) * background_image.size[1]) - ury = int(0.50 * background_image.size[1]) - - lly2 = int(0.49 * background_image.size[1]) - ury2 = int(0.05 * background_image.size[1]) - section_info = Image.open( Path( "instagen/static/instagen/graphics/{name}{ext}".format( @@ -150,7 +135,6 @@ def gen_image( font = ImageFont.truetype(font_regular_path, 62) text = f"{date:%a, %B %-d %-I:%M %p}".upper() # text = date - text_size = draw.textsize(text, font) loc = (1050, 280) section_info_draw.text(loc, text, (14, 42, 28), font=font, anchor="ra") @@ -160,7 +144,6 @@ def gen_image( text = location.name.upper() else: text = location_name.upper() - text_size = section_info_draw.textsize(text, font) loc = (1050, 355) section_info_draw.text(loc, text, (14, 42, 28), font=font, anchor="ra") @@ -169,7 +152,6 @@ def gen_image( text = "VS" else: text = "AT" - text_size = section_title_draw.textsize(text, font) loc = (540, 120) color = (255, 255, 255) section_title_draw.text(loc, text, color, font=font, anchor="mm") @@ -249,19 +231,6 @@ def gen_results_image( # transparent color. tmp = Image.new("RGBA", background_image.size, (0, 0, 0, 0)) - # Create a drawing context for it. - draw = ImageDraw.Draw(tmp) - - # section margin describes the margin of the section rectangles from the sides of the image - section_margin_pct = 0.05 - llx = int(section_margin_pct * background_image.size[0]) - urx = int((1 - section_margin_pct) * background_image.size[0]) - lly = int((1 - section_margin_pct) * background_image.size[1]) - ury = int(0.50 * background_image.size[1]) - - lly2 = int(0.49 * background_image.size[1]) - ury2 = int(0.05 * background_image.size[1]) - section_info = Image.open( Path( "instagen/static/instagen/graphics/{name}{ext}".format( @@ -301,7 +270,6 @@ def gen_results_image( text = f"{date:%a, %B %-d %-I:%M %p}".upper() # text = date font = ImageFont.truetype(font_condensed_path, 34) - text_size = section_info_draw.textsize(text, font) loc = (1050, 355) section_info_draw.text(loc, text, (14, 42, 28), font=font, anchor="ra") @@ -310,7 +278,6 @@ def gen_results_image( text = "VS" else: text = "AT" - text_size = section_title_draw.textsize(text, font) loc = (540, 120) color = (255, 255, 255) section_title_draw.text(loc, text, color, font=font, anchor="mm") diff --git a/setup.cfg b/setup.cfg index e1fcf64..0e936ed 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,6 +1,7 @@ [flake8] max-line-length = 120 exclude = .tox,.git,*/migrations/*,*/static/CACHE/*,docs,node_modules,venv +ignore = W503 [pycodestyle] max-line-length = 120 diff --git a/teamsnap/migrations/0001_initial.py b/teamsnap/migrations/0001_initial.py index c7ba542..6a5fc68 100644 --- a/teamsnap/migrations/0001_initial.py +++ b/teamsnap/migrations/0001_initial.py @@ -15,11 +15,25 @@ class Migration(migrations.Migration): operations = [ migrations.CreateModel( - name='Preferences', + name="Preferences", fields=[ - ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), - ('managed_team_id', models.IntegerField()), - ('user', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)), + ( + "id", + models.BigAutoField( + auto_created=True, + primary_key=True, + serialize=False, + verbose_name="ID", + ), + ), + ("managed_team_id", models.IntegerField()), + ( + "user", + models.OneToOneField( + on_delete=django.db.models.deletion.CASCADE, + to=settings.AUTH_USER_MODEL, + ), + ), ], ), ] diff --git a/teamsnap/urls.py b/teamsnap/urls.py index 55b08b6..cdfcbcb 100644 --- a/teamsnap/urls.py +++ b/teamsnap/urls.py @@ -7,8 +7,8 @@ from .views import ( dashboard, edit_lineup, schedule_view, + submit_lineup, view_event, - submit_lineup ) urlpatterns = default_urlpatterns(TeamsnapProvider) @@ -30,12 +30,13 @@ urlpatterns += [ name="teamsnap_edit_lineup", ), path( - '/event//submit_lineup/', + "/event//submit_lineup/", submit_lineup, - name='teamsnap_submit_lineup' + name="teamsnap_submit_lineup", + ), + path( + "/event//edit_lineup/", + edit_lineup, + name="teamsnap_edit_multiple_lineups", ), - path('/event//edit_lineup/', - edit_lineup, - name='teamsnap_edit_multiple_lineups' - ), ] diff --git a/teamsnap/views.py b/teamsnap/views.py index d4dadad..b086ac2 100644 --- a/teamsnap/views.py +++ b/teamsnap/views.py @@ -1,19 +1,20 @@ import datetime +import pyteamsnap.api import requests from allauth.socialaccount.providers.oauth2.views import ( OAuth2Adapter, OAuth2CallbackView, OAuth2LoginView, ) +from django.http import HttpResponseNotAllowed, HttpResponseServerError, JsonResponse from django.shortcuts import redirect, render from django.views.generic.edit import FormView -from django.http import HttpResponseNotAllowed, HttpResponse, JsonResponse, HttpResponseServerError from .forms import PreferencesForm from .models import Preferences from .provider import TeamsnapProvider -import pyteamsnap.api + class TeamsnapAdapter(OAuth2Adapter): provider_id = TeamsnapProvider.id @@ -46,6 +47,7 @@ class TeamsnapAdapter(OAuth2Adapter): oauth2_login = OAuth2LoginView.adapter_view(TeamsnapAdapter) oauth2_callback = OAuth2CallbackView.adapter_view(TeamsnapAdapter) + def get_teamsnap_client(request): request.user.socialaccount_set.filter(provider="teamsnap").first() current_teamsnap_user = request.user.socialaccount_set.filter( @@ -58,6 +60,7 @@ def get_teamsnap_client(request): return pyteamsnap.api.TeamSnap(token=ts_token) + class PreferencesFormView(FormView): template_name = "preferences.html" form_class = PreferencesForm @@ -190,8 +193,6 @@ def view_event(request, event_id, team_id=None): def edit_lineup(request, event_ids, team_id): import re - from teamsnap.forms import LineupEntryFormset - from pyteamsnap.api import ( Availability, AvailabilitySummary, @@ -199,9 +200,10 @@ def edit_lineup(request, event_ids, team_id): EventLineup, EventLineupEntry, Member, - TeamSnap, ) + from teamsnap.forms import LineupEntryFormset + client = get_teamsnap_client(request) event_ids = str(event_ids).split(",") @@ -231,28 +233,34 @@ def edit_lineup(request, event_ids, team_id): for lineup_entry in ts_lineup_entries: members.append( { - "member": getattr(ts_member_lookup[lineup_entry.data['member_id']],'data'), + "member": getattr( + ts_member_lookup[lineup_entry.data["member_id"]], "data" + ), "availability": getattr( - ts_availability_lookup.get(lineup_entry.data['member_id'], {}), "data", {} - ), - "lineup_entry": getattr( - lineup_entry, "data", {} + ts_availability_lookup.get(lineup_entry.data["member_id"], {}), + "data", + {}, ), + "lineup_entry": getattr(lineup_entry, "data", {}), } ) - in_lineup_already = [m['member'] for m in members] + in_lineup_already = [m["member"] for m in members] for member in ts_members: - if not member.data in in_lineup_already: + if member.data not in in_lineup_already: members.append( { "member": getattr(member, "data"), "availability": getattr( - ts_availability_lookup.get(member.data["id"], {}), "data", {} + ts_availability_lookup.get(member.data["id"], {}), + "data", + {}, ), "lineup_entry": getattr( - ts_lineup_entries_lookup.get(member.data["id"], {}), "data", {} + ts_lineup_entries_lookup.get(member.data["id"], {}), + "data", + {}, ), } ) @@ -346,9 +354,7 @@ def edit_lineup(request, event_ids, team_id): } ) - return render( - request, "lineup/multiple_edit.html", context={"contexts": contexts} - ) + return render(request, "lineup/multiple_edit.html", context={"contexts": contexts}) def dashboard(request, team_id=None): @@ -389,14 +395,14 @@ def dashboard(request, team_id=None): def submit_lineup(request, team_id, event_id): + from pyteamsnap.api import Event, EventLineup, EventLineupEntry + from teamsnap.forms import LineupEntryFormset - from pyteamsnap.api import EventLineup, TeamSnap, EventLineupEntry, Event - client = get_teamsnap_client(request) - ts_event = Event.get(client,event_id) + ts_event = Event.get(client, event_id) ts_lineup = EventLineup.search(client, event_id=event_id) - event_lineup_id = ts_lineup[0].data['id'] + event_lineup_id = ts_lineup[0].data["id"] if request.GET: return HttpResponseNotAllowed() if request.POST: @@ -405,12 +411,14 @@ def submit_lineup(request, team_id, event_id): r = [] for form in formset: data = form.cleaned_data - if data.get('event_lineup_entry_id'): - event_lineup_entry = EventLineupEntry.get(client, id=data.get('event_lineup_entry_id')) - if data.get('position_only'): - data['label'] = data['label'] + ' [PO]' + if data.get("event_lineup_entry_id"): + event_lineup_entry = EventLineupEntry.get( + client, id=data.get("event_lineup_entry_id") + ) + if data.get("position_only"): + data["label"] = data["label"] + " [PO]" event_lineup_entry.data.update(data) - if not data.get('sequence') and not data.get('label'): + if not data.get("sequence") and not data.get("label"): try: r.append(event_lineup_entry.delete()) except Exception as e: @@ -418,13 +426,14 @@ def submit_lineup(request, team_id, event_id): else: try: r.append(event_lineup_entry.put()) - except: + except Exception as e: + e pass pass - elif data.get('sequence') is not None and data.get('label'): + elif data.get("sequence") is not None and data.get("label"): event_lineup_entry = EventLineupEntry.new(client) - if data.get('position_only'): - data['label'] = data['label'] + ' [PO]' + if data.get("position_only"): + data["label"] = data["label"] + " [PO]" event_lineup_entry.data.update(data) event_lineup_entry.data.update({"event_lineup_id": event_lineup_id}) try: