from django.core.management.base import BaseCommand, CommandError

from datetime import datetime
import re, sys
from django.db import connection, transaction

import logging
logger = logging.getLogger(__name__)

from django.contrib.auth.models import User

# application import
from team.models import Player


class Command(BaseCommand):
    """
    reset password by team
    """

    help = 'Import the sessions for school board, the file should have one field on each line, e.g. 2016-12-03'


    def add_arguments(self, parser):
        parser.add_argument(
            '-t',
            
            type=str,
            dest='team-id',
            default=False,
            help='team id',
        )
        
        parser.add_argument(
            '-p',
            type=str,
            dest='password',
            default=False,
            help='new password to update',
        )

    def handle(self, *args, **options):
        team_id = options['team-id']
        if not team_id:
            raise CommandError('team id is not defined')
        else:
            self.team_id = team_id
            
        
        password = options['password']
        if not password:
            raise CommandError('password is not defined')
        else:
            self.password = password
            
        self.run(file)

    def run(self, file_path):
        players = Player.objects.filter(team__id=self.team_id, status='a')

        i = 1
        for p in players:
            user = p.user
            user.set_password(self.password)
            user.save()
            print i, user.first_name, user.last_name, p.status
            i+=1
        logger.info("Hello {0}".format(file))
