def validate_registration(self, reg_id): reg = Registration.objects.get(pk=reg_id) fail_reason = None warnings = [] if reg.already_in_slack_group: slack_user = slackapi.get_user(reg.slack_username.lower()) or slackapi.get_user(reg.lichess_username.lower()) if slack_user == None: reg.already_in_slack_group = False 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.has_played_20_games = player.games_played_for(reg.season.league) >= 20 if player.account_status != 'normal': fail_reason = 'The lichess user "%s" has the "%s" mark.' % (reg.lichess_username, player.account_status) 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 not played 20 games.') 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 notify_slack_link(self, lichess_username): player = Player.get_or_create(lichess_username) email = slackapi.get_user(player.slack_user_id).email msg = 'Your lichess account has been successfully linked with the Slack account "%s".' % email lichessapi.send_mail(lichess_username, 'Slack Account Linked', msg)