def validate_registration(self, reg_id): reg = Registration.objects.get(pk=reg_id) fail_reason = None warnings = [] try: user_meta = lichessapi.get_user_meta(reg.lichess_username, 1) player, _ = Player.objects.get_or_create( lichess_username__iexact=reg.lichess_username, defaults={'lichess_username': reg.lichess_username}) player.update_profile(user_meta) reg.classical_rating = player.rating_for(reg.season.league) reg.peak_classical_rating = lichessapi.get_peak_rating( reg.lichess_username, reg.season.league.rating_type) reg.has_played_20_games = not player.provisional_for(reg.season.league) if player.account_status != 'normal': fail_reason = 'The lichess user "%s" has the "%s" mark.' % ( reg.lichess_username, player.account_status) if reg.already_in_slack_group and not player.slack_user_id: reg.already_in_slack_group = False except lichessapi.ApiWorkerError: fail_reason = 'The lichess user "%s" could not be found.' % reg.lichess_username if not reg.has_played_20_games: warnings.append('Has a provisional rating.') if not reg.can_commit and (reg.season.league.competitor_type != 'team' or reg.alternate_preference != 'alternate'): warnings.append('Can\'t commit to a game per week.') if not reg.agreed_to_rules: warnings.append('Didn\'t agree to rules.') if fail_reason: reg.validation_ok = False reg.validation_warning = False comment_text = 'Validation error: %s' % fail_reason elif warnings: reg.validation_ok = True reg.validation_warning = True comment_text = 'Validation warning: %s' % ' '.join(warnings) else: reg.validation_ok = True reg.validation_warning = False comment_text = 'Validated.' add_system_comment(reg, comment_text) with reversion.create_revision(): reversion.set_comment('Validated registration.') reg.save()
def _ensure_profile_present(player): if not player.profile: user_meta = lichessapi.get_user_meta(player.lichess_username, priority=100) player.update_profile(user_meta)