Skip to content

Commit

Permalink
style: format code with black
Browse files Browse the repository at this point in the history
  • Loading branch information
eldersantoss committed Jun 21, 2024
1 parent aa9c575 commit 0149d13
Showing 1 changed file with 20 additions and 70 deletions.
90 changes: 20 additions & 70 deletions core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,7 @@ def __str__(self) -> str:
return self.name

def logo_url(self):
return (
f"https://media.api-sports.io/football/teams/{self.data_source_id}.png"
if self.data_source_id
else ""
)
return f"https://media.api-sports.io/football/teams/{self.data_source_id}.png" if self.data_source_id else ""


class Competition(models.Model):
Expand Down Expand Up @@ -75,11 +71,7 @@ def get_teams(self):
params = {"league": self.real_data_source_id, "season": self.season}

# TODO: tratar requests.exceptions.ConnectionError:
response = (
requests.get(source_url, headers=headers, params=params)
.json()
.get("response")
)
response = requests.get(source_url, headers=headers, params=params).json().get("response")
sleep(settings.FOOTBALL_API_RATE_LIMIT_TIME)

teams = []
Expand Down Expand Up @@ -114,9 +106,7 @@ def create_and_update_matches(self, days_from: int | None, days_ahead: int | Non
"season": self.season,
"from": str(from_),
"to": str(to),
"status": "-".join(
[Match.NOT_STARTED, *Match.IN_PROGRESS_AND_FINISHED_STATUS]
),
"status": "-".join([Match.NOT_STARTED, *Match.IN_PROGRESS_AND_FINISHED_STATUS]),
}

# TODO: tratar requests.exceptions.ConnectionError:
Expand Down Expand Up @@ -245,9 +235,7 @@ def update_matches(self, days_from: int | None, days_ahead: int | None):
away_goals = data["goals"]["away"]

match = (
Match.objects.exclude(status__in=Match.FINISHED_STATUS)
.filter(data_source_id=data_source_id)
.first()
Match.objects.exclude(status__in=Match.FINISHED_STATUS).filter(data_source_id=data_source_id).first()
)

if match is None:
Expand All @@ -266,9 +254,7 @@ def get_with_matches_on_period(cls, from_: date, to: date):
"""Returns competitions which that have matches on period"""

matches_on_period = Match.get_happen_on_period(from_, to)
competitions_with_matches_on_period = matches_on_period.values(
"competition"
).distinct()
competitions_with_matches_on_period = matches_on_period.values("competition").distinct()

return cls.objects.filter(id__in=competitions_with_matches_on_period)

Expand Down Expand Up @@ -371,18 +357,12 @@ def update_status(
):
self.status = new_status

match_time_limit = self.date_time + timezone.timedelta(
minutes=match_time_limit_minutes
)
match_time_limit = self.date_time + timezone.timedelta(minutes=match_time_limit_minutes)
match_broke_limit_time = timezone.now() >= match_time_limit

REGULAR_MATCH_DURATION = 90

if (
match_broke_limit_time
and elapsed_time >= REGULAR_MATCH_DURATION
and self.status == Match.SECOND_HALF
):
if match_broke_limit_time and elapsed_time >= REGULAR_MATCH_DURATION and self.status == Match.SECOND_HALF:
self.status = Match.FINISHED_STATUS

def is_finished(self) -> bool:
Expand Down Expand Up @@ -412,14 +392,8 @@ def result_str(self):
description="Aberta para palpites?",
)
def open_to_guesses(self):
return (
self.date_time
> timezone.now()
+ timezone.timedelta(minutes=self.MINUTES_BEFORE_START_MATCH)
) and (
self.date_time
<= timezone.now()
+ timezone.timedelta(hours=self.HOURS_BEFORE_OPEN_TO_GUESSES)
return (self.date_time > timezone.now() + timezone.timedelta(minutes=self.MINUTES_BEFORE_START_MATCH)) and (
self.date_time <= timezone.now() + timezone.timedelta(hours=self.HOURS_BEFORE_OPEN_TO_GUESSES)
)

def get_pools(self):
Expand Down Expand Up @@ -475,11 +449,7 @@ def get_involved_pools(self):
def get_who_should_be_notified_by_email(cls):
"""Returns guessers that should be notified by email"""

return (
cls.objects.exclude(user__email="")
.exclude(pools__isnull=True)
.exclude(receive_notifications=False)
)
return cls.objects.exclude(user__email="").exclude(pools__isnull=True).exclude(receive_notifications=False)

def get_involved_pools_with_new_matches(self):
"""Returns pools with new matches that this guesser is involved
Expand Down Expand Up @@ -529,9 +499,7 @@ class Meta:

def __str__(self) -> str:
return (
f"{self.match.home_team.name} {self.home_goals}"
+ " x "
+ f"{self.away_goals} {self.match.away_team.name}"
f"{self.match.home_team.name} {self.home_goals}" + " x " + f"{self.away_goals} {self.match.away_team.name}"
)

def get_score(self) -> int:
Expand Down Expand Up @@ -563,16 +531,10 @@ def _evaluate(self):
ACERTO_VISITANTE_VENCEDOR = (self.home_goals < self.away_goals) and (
self.match.home_goals < self.match.away_goals
)
ACERTO_EMPATE = (self.home_goals == self.away_goals) and (
self.match.home_goals == self.match.away_goals
)
ACERTO_EMPATE = (self.home_goals == self.away_goals) and (self.match.home_goals == self.match.away_goals)
ACERTO_PARCIAL = ACERTO_MANDANTE_VENCEDOR or ACERTO_VISITANTE_VENCEDOR
ACERTO_PARCIAL_COM_GOLS = ACERTO_PARCIAL and (
ACERTO_DE_GOLS_MANDANTE or ACERTO_DE_GOLS_VISITANTE
)
ACERTO_SOMENTE_GOLS = (
ACERTO_DE_GOLS_MANDANTE or ACERTO_DE_GOLS_VISITANTE
) and not ACERTO_PARCIAL
ACERTO_PARCIAL_COM_GOLS = ACERTO_PARCIAL and (ACERTO_DE_GOLS_MANDANTE or ACERTO_DE_GOLS_VISITANTE)
ACERTO_SOMENTE_GOLS = (ACERTO_DE_GOLS_MANDANTE or ACERTO_DE_GOLS_VISITANTE) and not ACERTO_PARCIAL
ACERTO_CRAVADO = ACERTO_DE_GOLS_MANDANTE and ACERTO_DE_GOLS_VISITANTE

PONTUACAO_ACERTO_CRAVADO = 10
Expand Down Expand Up @@ -705,10 +667,8 @@ def get_open_matches(self):
"""Returns matches open to guesses"""

return self.get_matches().filter(
date_time__gt=timezone.now()
+ timezone.timedelta(minutes=self.minutes_before_start_match),
date_time__lte=timezone.now()
+ timezone.timedelta(hours=self.hours_before_open_to_guesses),
date_time__gt=timezone.now() + timezone.timedelta(minutes=self.minutes_before_start_match),
date_time__lte=timezone.now() + timezone.timedelta(hours=self.hours_before_open_to_guesses),
)

@classmethod
Expand Down Expand Up @@ -738,9 +698,7 @@ def add_guess_to_pools(self, guess: Guess, for_all_pools: bool):
if for_all_pools:
pools_with_the_match = match.get_pools()
pools_with_the_guesser = guesser.pools.all()
pools_with_match_and_guesser = pools_with_the_match.intersection(
pools_with_the_guesser
)
pools_with_match_and_guesser = pools_with_the_match.intersection(pools_with_the_guesser)

for pool in pools_with_match_and_guesser:
self._replace_guess_in_pool(guess, pool)
Expand Down Expand Up @@ -804,9 +762,7 @@ def get_guessers_with_score_and_guesses(
guessers = self.get_guessers_with_match_scores(matches)

for guesser in guessers:
guesser.matches_and_guesses = self._get_guesses_per_matches(
guesser, matches
)
guesser.matches_and_guesses = self._get_guesses_per_matches(guesser, matches)

return guessers

Expand Down Expand Up @@ -838,11 +794,7 @@ def _assemble_datetime_period(self, month: int, year: int, week: int):
else:
# período semanal (ano, mes e semanas recebidos)
start = timezone.now().fromisocalendar(year, week, 1)
end = (
timezone.now()
.fromisocalendar(year, week, 7)
.replace(hour=23, minute=59, second=59)
)
end = timezone.now().fromisocalendar(year, week, 7).replace(hour=23, minute=59, second=59)

return start, end

Expand All @@ -862,9 +814,7 @@ def get_finished_or_in_progress_matches_on_period(
)

def get_guessers_with_match_scores(self, matches: Iterable[Match]):
guesses_of_this_pool_in_the_period = Q(guesses__match__in=matches) & Q(
guesses__in=self.guesses.all()
)
guesses_of_this_pool_in_the_period = Q(guesses__match__in=matches) & Q(guesses__in=self.guesses.all())
sum_expr = Sum(
"guesses__score",
filter=guesses_of_this_pool_in_the_period,
Expand Down

0 comments on commit 0149d13

Please sign in to comment.