first init of this template.

This commit is contained in:
2022-05-14 07:16:31 -05:00
parent d6ee55a050
commit 4632926a34
40 changed files with 1854 additions and 857 deletions

331
teamsnap/utils/gen_image.py Normal file
View File

@@ -0,0 +1,331 @@
from PIL import Image
from PIL import ImageDraw
from PIL import ImageFilter, ImageFont
from pathlib import Path
import os
from datetime import datetime
from zoneinfo import ZoneInfo
from typing import List
from dataclasses import dataclass
# image_directory = 'input/images/logos-bw/{filename}.{ext}'
# font_regular_path = "input/fonts/DINAlternate-Bold.ttf"
# font_condensed_path = "input/fonts/DINCondensed-Bold.ttf"
font_regular_path = "benchcoachproject/static/teamsnap/ig/fonts/ScalaSans-BoldLF.otf"
font_condensed_path = "benchcoachproject/static/teamsnap/ig/fonts/ScalaSans-BoldLF.otf"
@dataclass
class Team:
name: str
winlosstie: List[int] = None
image_directory: str = '../input/images/logos-bw/{filename}.{ext}'
@property
def id(self):
return self.name.lower().replace(' ', '-')
@property
def image(self):
path = self.image_directory.format(filename=self.id, ext="png")
if os.path.isfile(path):
return path
else:
return None
@dataclass
class Location:
name: str
address1: str = ""
address2: str = ""
image_directory: str = 'benchcoachproject/static/teamsnap/ig/locations/{filename}.{ext}'
@property
def id(self):
return self.name.lower().replace(' ', '-')
@property
def image(self):
path = self.image_directory.format(filename=self.id, ext="png")
if os.path.isfile(path):
return path
else:
return None
@property
def address(self):
return ",".join([self.address1,self.address2])
args = {
"team_fave" : Team("Hounds"),
"team_opponent" : Team("Trojans"),
"home": False,
"date" : "2021-05-08 12:30 pm",
"location" : Location("Maywood", image_directory="benchcoachproject/static/teamsnap/ig/locations/maywood.{ext}"),
"runs_for": 8,
"runs_against": 9
}
def gen_image (team_fave, team_opponent, date, location=None,
location_name = None,
home=False,
background='location',
address = None,
width = 1080,
height = 1080,
*kwargs,
**args
):
if not isinstance(date, datetime):
# date = parser.parse(date)
# date = date.astimezone(ZoneInfo("America/Chicago"))
pass
if location.image and background == 'location':
background_image = Image.open(location.image).copy()
background_image = background_image.resize((width, height))
# background_image = background_image.filter(ImageFilter.GaussianBlur(radius=5))
background_image = background_image.convert("RGBA")
elif background == 'transparent':
background_image = Image.new('RGBA', (width, height), (0, 0, 0, 0))
else:
background_image = Image.new('RGBA', (width, height), (50, 55, 102))
title_images = []
for team in [team_fave, team_opponent]:
if team.image:
title_images.append(Image.open(team.image).copy())
else:
title_images.append(Image.new('RGBA', (1080, 1080)))
title_image_left = title_images[0]
title_image_right = title_images[1]
# Make a blank image for the rectangle, initialized to a completely
# 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 = .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(.50 * background_image.size[1])
lly2 = int(.49 * background_image.size[1])
ury2 = int(.05 * background_image.size[1])
section_info = Image.open(Path('benchcoachproject/static/teamsnap/ig/graphics/{name}{ext}'.format(name="sign-tan", ext=".png")))
section_info_draw = ImageDraw.Draw(section_info)
section_title = Image.open(Path('benchcoachproject/static/teamsnap/ig/graphics/{name}{ext}'.format(name="sign-green", ext=".png")))
section_title_draw = ImageDraw.Draw(section_title)
# First line: Date
font = ImageFont.truetype(font_regular_path, 62)
text = "{:%a, %B %-d %-I:%M %p}".format(date).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")
# Second line: Venue
font = ImageFont.truetype(font_condensed_path, 34)
if not location_name:
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")
font = ImageFont.truetype(font_regular_path, 80)
if home:
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")
# Alpha composite the two images together.
background_image = Image.alpha_composite(background_image, tmp)
# Title Image Left
title_image_left.thumbnail([350, 350])
loc = (
50, -50
)
section_title.paste(title_image_left, loc, title_image_left)
# Title Image Right
title_image_right.thumbnail([350, 350])
loc = (
650, -50
)
section_title.paste(title_image_right, loc, title_image_right)
# background_image.paste(section_info, (llx, ury), section_info)
# background_image.paste(section_title, (llx, ury2), section_title)
section_title.paste(section_info,(0,0),section_info)
section_title.thumbnail([800, 800])
if background=="badge":
return section_title
background_image.paste(section_title,(
int((background_image.size[0]-section_title.size[0])/2),
height - 360
),section_title)
return background_image
def gen_results_image (team_fave, team_opponent, date,
location=None,
location_name = None,
home=False,
background='location',
address = None,
width = 1080,
height = 1080,
runs_for=0,
runs_against=0,
*kwargs,
**args
):
if not isinstance(date, datetime):
# date = parser.parse(date)
# date = date.astimezone(ZoneInfo("America/Chicago"))
pass
if location.image and background == 'location':
background_image = Image.open(location.image).copy()
background_image = background_image.resize((width, height))
# background_image = background_image.filter(ImageFilter.GaussianBlur(radius=5))
background_image = background_image.convert("RGBA")
elif background == 'transparent':
background_image = Image.new('RGBA', (width, height), (0, 0, 0, 0))
else:
background_image = Image.new('RGBA', (width, height), (50, 55, 102))
title_images = []
for team in [team_fave, team_opponent]:
if team.image:
title_images.append(Image.open(team.image).copy())
else:
title_images.append(Image.new('RGBA', (1080, 1080)))
title_image_left = title_images[0]
title_image_right = title_images[1]
# Make a blank image for the rectangle, initialized to a completely
# 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 = .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(.50 * background_image.size[1])
lly2 = int(.49 * background_image.size[1])
ury2 = int(.05 * background_image.size[1])
#todo fix path
section_info = Image.open(Path('benchcoachproject/static/teamsnap/ig/graphics/{name}{ext}'.format(name="sign-tan", ext=".png")))
section_info_draw = ImageDraw.Draw(section_info)
section_title = Image.open(Path('benchcoachproject/static/teamsnap/ig/graphics/{name}{ext}'.format(name="sign-green", ext=".png")))
section_title_draw = ImageDraw.Draw(section_title)
# First line: Results
loc = (
1050,
265
)
if runs_for > runs_against:
result_letter = "W"
elif runs_for < runs_against:
result_letter = "L"
elif runs_for == runs_against:
result_letter = "T"
font = ImageFont.truetype(font_regular_path, 100)
section_info_draw.text(loc, f"FINAL: {result_letter} {runs_for}-{runs_against}", (14,42,28), font=font, anchor="ra")
# Second line: Date
text = "{:%a, %B %-d %-I:%M %p}".format(date).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")
font = ImageFont.truetype(font_regular_path, 80)
if home:
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")
# Alpha composite the two images together.
background_image = Image.alpha_composite(background_image, tmp)
# Title Image Left
title_image_left.thumbnail([350, 350])
loc = (
50, -50
)
section_title.paste(title_image_left, loc, title_image_left)
# Title Image Right
title_image_right.thumbnail([350, 350])
loc = (
650, -50
)
section_title.paste(title_image_right, loc, title_image_right)
# background_image.paste(section_info, (llx, ury), section_info)
# background_image.paste(section_title, (llx, ury2), section_title)
section_title.paste(section_info,(0,0),section_info)
section_title.thumbnail([800, 800])
if background=="badge":
return section_title
background_image.paste(section_title,(
int((background_image.size[0]-section_title.size[0])/2),
height - 360
),section_title)
# background_image.show()
return background_image
# gen_results_image(**args)

View File

@@ -3,7 +3,8 @@ from typing import List, Tuple
import benchcoach.models
from benchcoach.models import BenchcoachModel, Availability, Player, Team, Positioning, Event, Venue
from teamsnap.teamsnap.api import TeamSnap
from pyteamsnap.api import TeamSnap
import pyteamsnap
import teamsnap.models
from django.db.models import QuerySet
@@ -50,14 +51,14 @@ class TeamsnapSyncEngine(AbstractSyncEngine):
}
teamsnapmodel_to_apiobject = {
teamsnap.models.Availability: teamsnap.teamsnap.api.Availability,
teamsnap.models.Event: teamsnap.teamsnap.api.Event,
# teamsnap.models.LineupEntry:teamsnap.teamsnap.api.LineupEntry, # Not implemented Yet
teamsnap.models.Location: teamsnap.teamsnap.api.Location,
teamsnap.models.Member: teamsnap.teamsnap.api.Member,
teamsnap.models.Opponent: teamsnap.teamsnap.api.Opponent,
teamsnap.models.Team: teamsnap.teamsnap.api.Team,
# teamsnap.models.User:teamsnap.teamsnap.api.User # Not implemented yet
teamsnap.models.Availability: pyteamsnap.api.Availability,
teamsnap.models.Event: pyteamsnap.api.Event,
# teamsnap.models.LineupEntry:pyteamsnap.api.LineupEntry, # Not implemented Yet
teamsnap.models.Location: pyteamsnap.api.Location,
teamsnap.models.Member: pyteamsnap.api.Member,
teamsnap.models.Opponent: pyteamsnap.api.Opponent,
teamsnap.models.Team: pyteamsnap.api.Team,
# teamsnap.models.User:pyteamsnap.api.User # Not implemented yet
}
apiobject_to_teamsnapmodel = {v:k for k,v in teamsnapmodel_to_apiobject.items()}
@@ -104,14 +105,14 @@ class TeamsnapSyncEngine(AbstractSyncEngine):
}
teamsnapmodel_to_apiobject = {
teamsnap.models.Availability: teamsnap.teamsnap.api.Availability,
teamsnap.models.Event: teamsnap.teamsnap.api.Event,
# teamsnap.models.LineupEntry:teamsnap.teamsnap.api.LineupEntry, # Not implemented Yet
teamsnap.models.Location: teamsnap.teamsnap.api.Location,
teamsnap.models.Member: teamsnap.teamsnap.api.Member,
teamsnap.models.Opponent: teamsnap.teamsnap.api.Opponent,
teamsnap.models.Team: teamsnap.teamsnap.api.Team,
# teamsnap.models.User:teamsnap.teamsnap.api.User # Not implemented yet
teamsnap.models.Availability: pyteamsnap.api.Availability,
teamsnap.models.Event: pyteamsnap.api.Event,
# teamsnap.models.LineupEntry:pyteamsnap.api.LineupEntry, # Not implemented Yet
teamsnap.models.Location: pyteamsnap.api.Location,
teamsnap.models.Member: pyteamsnap.api.Member,
teamsnap.models.Opponent: pyteamsnap.api.Opponent,
teamsnap.models.Team: pyteamsnap.api.Team,
# teamsnap.models.User:pyteamsnap.api.User # Not implemented yet
}
if isinstance(benchcoach_instance, benchcoach.models.Team):
@@ -128,9 +129,9 @@ class TeamsnapSyncEngine(AbstractSyncEngine):
r = self._update_teamsnapdb_to_benchcoachdb(teamsnap_instance, benchcoach_instance)
return r
def _update_from_teamsnapdata(self, teamsnap_instance:teamsnap.models.TeamsnapBaseModel, teamsnap_data: teamsnap.teamsnap.api.ApiObject) -> teamsnap.models.TeamsnapBaseModel:
def _update_from_teamsnapdata(self, teamsnap_instance:teamsnap.models.TeamsnapBaseModel, teamsnap_data: pyteamsnap.api.ApiObject) -> teamsnap.models.TeamsnapBaseModel:
''''''
if isinstance(teamsnap_data, teamsnap.teamsnap.api.ApiObject):
if isinstance(teamsnap_data, pyteamsnap.api.ApiObject):
teamsnap_data = teamsnap_data.data
else:
raise TypeError
@@ -390,7 +391,7 @@ class TeamsnapSyncEngine(AbstractSyncEngine):
r['team'] = []
# Search API for objects belonging to currently managed team, and iterate
for teamsnap_data in teamsnap.teamsnap.api.Team.search(client=self.client, id=self.managed_teamsnap_team_id):
for teamsnap_data in pyteamsnap.api.Team.search(client=self.client, id=self.managed_teamsnap_team_id):
# check if TeamSnap ID already exists in the Teamsnap DB.
if teamsnap.models.Team.objects.filter(id=teamsnap_data.data['id']):
teamsnap_instance = teamsnap.models.Team.objects.filter(id=teamsnap_data.data['id']).first()
@@ -417,7 +418,7 @@ class TeamsnapSyncEngine(AbstractSyncEngine):
# Opponents from teamsnap go to the BenchCoach "Team" database.
# Dependent on Team. These objects need to be available to attach as related objects or the functions
# self._update_from teamsnapdata and self.update_teamsnapdb_to_benchcoachdb may fail.
for teamsnap_data in teamsnap.teamsnap.api.Opponent.search(**kwargs):
for teamsnap_data in pyteamsnap.api.Opponent.search(**kwargs):
if teamsnap.models.Opponent.objects.filter(id=teamsnap_data.data['id']):
teamsnap_instance = teamsnap.models.Opponent.objects.filter(id=teamsnap_data.data['id']).first()
benchcoach_instance = teamsnap_instance.benchcoach_object
@@ -435,7 +436,7 @@ class TeamsnapSyncEngine(AbstractSyncEngine):
# Dependent on Team. These objects need to be available to attach as related objects or the functions
# self._update_from teamsnapdata and self.update_teamsnapdb_to_benchcoachdb may fail.
r['location'] = []
for teamsnap_data in teamsnap.teamsnap.api.Location.search(**kwargs):
for teamsnap_data in pyteamsnap.api.Location.search(**kwargs):
if teamsnap.models.Location.objects.filter(id=teamsnap_data.data['id']):
teamsnap_instance = teamsnap.models.Location.objects.filter(id=teamsnap_data.data['id']).first()
benchcoach_instance = teamsnap_instance.benchcoach_object
@@ -455,7 +456,7 @@ class TeamsnapSyncEngine(AbstractSyncEngine):
# self._update_from teamsnapdata and self.update_teamsnapdb_to_benchcoachdb may fail.
r['member'] = []
# Search API for members to import. Note: Non players are not included in sync.
for teamsnap_data in teamsnap.teamsnap.api.Member.search(**kwargs,
for teamsnap_data in pyteamsnap.api.Member.search(**kwargs,
is_non_player = False
):
if teamsnap_data.data['is_non_player'] == True:
@@ -478,7 +479,7 @@ class TeamsnapSyncEngine(AbstractSyncEngine):
# Dependent on Team, Opponent, Location. These objects need to be available to attach as related objects or the functions
# self._update_from teamsnapdata and self.update_teamsnapdb_to_benchcoachdb may fail.
r['event'] = []
for teamsnap_data in teamsnap.teamsnap.api.Event.search(**kwargs):
for teamsnap_data in pyteamsnap.api.Event.search(**kwargs):
if teamsnap.models.Event.objects.filter(id=teamsnap_data.data['id']):
teamsnap_instance = teamsnap.models.Event.objects.filter(id=teamsnap_data.data['id']).first()
benchcoach_instance = teamsnap_instance.benchcoach_object
@@ -504,7 +505,7 @@ class TeamsnapSyncEngine(AbstractSyncEngine):
# Search API for members to import. Note: Non players are not included in sync.
player_ids = [member.id for member in teamsnap.models.Member.objects.filter(is_non_player=False)]
for teamsnap_data in teamsnap.teamsnap.api.Availability.search(**kwargs,
for teamsnap_data in pyteamsnap.api.Availability.search(**kwargs,
member_id=",".join(player_ids)
):
if teamsnap.models.Availability.objects.filter(id=teamsnap_data.data['id']):