climu / openstudyroom

Website for the Open Study Room online go/baduk/weiqi community.
https://openstudyroom.org
GNU General Public License v3.0
67 stars 33 forks source link

Populating a league can raise an error #442

Closed climu closed 3 years ago

climu commented 3 years ago

If a player already is in a league and an admin try to add it again via the populate tool we have a 500 because:

IntegrityError: duplicate key value violates unique  constraint"league_leagueplayer_user_id_5c067d1d_uniq"
DETAIL:  Key (user_id, division_id)=(2240, 344) already exists.

happens here: https://github.com/climu/openstudyroom/blob/f0a4c6a23f7ff8feeb085c13a135c23a052ffcee/league/views.py#L1560-L1564

We should detect that and warn the admins in populate view that render a preview.

climu commented 3 years ago

Hum this happens even without a player in it. This error is also raised: ValueError: invalid literal for int() with base 10: '' at https://github.com/climu/openstudyroom/blob/f0a4c6a23f7ff8feeb085c13a135c23a052ffcee/league/views.py#L1559

with player being <LeaguePlayer: 7636: lyra33, The Shell #4> properly defined.

climu commented 3 years ago

We introduced some "wontplay" sgf so there were a difference between https://github.com/climu/openstudyroom/blob/46115b7915caa962f2903b93194514aede198d5a/league/views.py#L1499 and https://github.com/climu/openstudyroom/blob/46115b7915caa962f2903b93194514aede198d5a/league/views.py#L1557

LucasBertrand commented 3 years ago

Should we just change the nb_games method ? event.sgf_set.filter(Q(black=user) | Q(white=user)).count() to something like this event.sgf_set.filter(Q(Q(black=user) | Q(white=user)) & ~Q(result='WontPlay')).count()

climu commented 3 years ago

I fixed it in 46115b7 like that:

    def get_sgfs(self):
        """Return a queryset of all valid player SGF"""
        user = self.user
        event = self.event
        return event.sgf_set.exclude(winner__isnull=True).filter(
            Q(black=user) | Q(white=user))

then we use this in nb_games, nb_win and nb_loss.

If we ever want to change that we just have to update the get_sgfs methods.