def get_team(self): """See docstring in class TeamLoader.""" if not os.path.exists(os.path.join(os.path.dirname(self.path), "contest.yaml")): logger.critical("File missing: \"contest.yaml\"") return None team_code = os.path.basename(self.path) logger.info("Loading parameters for team %s.", team_code) conf = load_yaml_from_path( os.path.join(os.path.dirname(self.path), "contest.yaml")) args = {} conf = load(conf, None, "teams") for team in conf: if team["code"] == team_code: conf = team break else: logger.critical("The specified team cannot be found.") return None load(conf, args, "code") load(conf, args, "name") logger.info("Team parameters loaded.") return Team(**args)
def post(self): fallback_page = self.url("teams", "add") try: attrs = dict() self.get_string(attrs, "code") self.get_string(attrs, "name") assert attrs.get("code") is not None, \ "No team code specified." # Create the team. team = Team(**attrs) self.sql_session.add(team) except Exception as error: self.service.add_notification( make_datetime(), "Invalid field(s)", repr(error)) self.redirect(fallback_page) return if self.try_commit(): # Create the team on RWS. self.service.proxy_service.reinitialize() # In case other teams need to be added. self.redirect(fallback_page)
def _maketeam(self, teamname): team = self.teams[teamname] teamdb = Team(code=team.code, name=team.name) return teamdb
def get_team(self): # due to the terrible AddUser script conf_path = os.path.dirname(self.path) name = os.path.basename(self.path) if not exists(conf_path): logger.critical("cannot find config file") return None logger.info("loading parameters for team \"%s\"", name) conf = load_yaml(conf_path) candidates = [u for u in conf['teams'] if u['code'] == name] if len(candidates) == 0: logger.critical("cannot find specified team") return None if len(candidates) > 1: logger.critical("multiple teams found with the same code") return None item = candidates[0] # default values team = {} assign(team, item, 'code') assign(team, item, 'name') return Team(**item)
def get_team(cls, **kwargs): """Create a team""" args = { "code": unique_unicode_id(), "name": unique_unicode_id(), } args.update(kwargs) team = Team(**args) return team
def add_team(code, name): logger.info("Creating the team in the database.") team = Team(code=code, name=name) try: with SessionGen() as session: session.add(team) session.commit() except IntegrityError: logger.error("A team with the given code already exists.") return False logger.info("Team added.") return True
def load_participations(path): logger.info("Loading...") with open(path, 'r') as io: data = json.load(io) participations = data['participations'] with SessionGen() as session: for entry in participations: logger.info('Loading: %s' % (entry)) contest = Contest.get_from_id(entry['contest_id'], session) if contest is None: logger.error(" Contest ID %d not found" % (entry['contest_id'])) session.rollback() return False userdata = entry['user'] user = session.query(User).filter( User.username == userdata['username']).first() if user is None: user = User(username=userdata['username'], first_name=userdata['first_name'], last_name=userdata['last_name'], password=build_password( generate_random_password())) logger.info(' Creating new user: %s' % (user.username)) session.add(user) else: logger.info(' Using existing user: %s (id=%d)' % (user.username, user.id)) if 'plaintext_password' in userdata: logger.info(' * password') user.password = build_password(userdata['plaintext_password'], 'plaintext') if 'first_name' in userdata: logger.info(' * first_name: %s' % (userdata['first_name'])) user.first_name = userdata['first_name'] if 'last_name' in userdata: logger.info(' * last_name: %s' % (userdata['last_name'])) user.last_name = userdata['last_name'] participation = session.query(Participation).join( Participation.user).filter( Participation.contest == contest).filter( User.username == user.username).first() if participation is None: participation = Participation(user=user, contest=contest) logger.info( ' Creating new participation for contest_id=%d user=%s' % (contest.id, user.username)) session.add(participation) else: logger.info( ' Updating participation: id=%d contest_id=%d user=%s' % (participation.id, participation.contest_id, participation.user.username)) if 'plaintext_password' in entry: logger.info(' * plaintext_password') participation.password = build_password( entry['plaintext_password'], 'plaintext') if 'ip' in entry: logger.info(' * ip: %s' % (entry['ip'])) participation.ip = [ipaddress.ip_network(entry['ip'])] if 'delay_time' in entry: logger.info(' * delay_time: %d' % (entry['delay_time'])) participation.delay_time = datetime.timedelta( seconds=entry['delay_time']) if 'extra_time' in entry: logger.info(' * extra_time: %d' % (entry['extra_time'])) participation.extra_time = datetime.timedelta( seconds=entry['extra_time']) if 'hidden' in entry: logger.info(' * hidden: %s' % (entry['hidden'])) participation.hidden = entry['hidden'] if 'unrestricted' in entry: logger.info(' * unrestricted: %s' % (entry['unrestricted'])) participation.unrestricted = entry['unrestricted'] if 'team' in userdata: team = session.query(Team).filter( Team.code == userdata['team']['code']).first() if team is None: team = Team(code=userdata['team']['code'], name=userdata['team']['name']) logger.info(' Creating new team: %s' % (team.code)) session.add(team) else: logger.info(' Using existing team: %s' % (team.code)) if 'name' in userdata['team']: logger.info(' * name: %s' % (userdata['team']['name'])) team.name = userdata['team']['name'] participation.team = team session.commit() logger.info("Done.") return True
def post(self, contest_id): fallback_page = self.url("contest", contest_id, "users", "import") self.contest = self.safe_get_item(Contest, contest_id) r_params = self.render_params() action = self.get_body_argument('action', 'upload') if action == 'upload': ignore_existing = self.get_body_argument('ignore_existing', False) load_passwords = self.get_body_argument('load_passwords', False) try: user_csv = self.request.files["users_csv"][0] users = CsvUserLoader(None, None, user_csv['body']).read_users() processed_users = [] some_participants_exist = False for user in users: username = user['username'] result = { 'participant': False, 'username': username } db_user = self.sql_session.query(User).filter_by( username=username).first() if not db_user: self.application.service.add_notification( make_datetime(), 'User missing', '"%s" doesn\'t exist. Import users first.' % username) self.redirect(fallback_page) return result['user_id'] = db_user.id result['team'] = user.get('team') participation = self.sql_session.query(Participation) \ .filter(Participation.user == db_user) \ .filter(Participation.contest == self.contest) \ .first() if participation: result['participant'] = True if not ignore_existing and not some_participants_exist: some_participants_exist = True self.application.service.add_notification( make_datetime(), 'User exists', 'Some participants already exist') if load_passwords: result['password'] = \ cmscommon.crypto.hash_password(user.get('password'), method='plaintext') else: result['password'] = None processed_users.append(result) r_params['users'] = processed_users r_params['has_errors'] = \ (some_participants_exist and not ignore_existing) r_params['load_passwords'] = load_passwords self.render('participation_preview.html', **r_params) return except Exception as error: self.application.service.add_notification( make_datetime(), "Bad CSV file", repr(error)) self.redirect(fallback_page) return elif action == 'save': user_id = self.get_body_arguments('user_id', False) teams = self.get_body_arguments('team', False) passwords = self.get_body_arguments('password', False) for i in range(len(user_id)): user = self.safe_get_item(User, user_id[i]) team = None if teams[i]: team_code = ImportParticipantsHandler \ .prepare_team_code(teams[i]) team = self.sql_session.query(Team) \ .filter_by(code=team_code).first() if not team: team = Team(code=team_code, name=teams[i]) self.sql_session.add(team) password = passwords[i] if passwords else None participation = Participation(user=user, contest=self.contest, team=team, password=password) self.sql_session.add(participation) if self.try_commit(): # Create the user on RWS. self.application.service.proxy_service.reinitialize() self.redirect(self.url("contest", contest_id, "users")) return self.redirect(fallback_page)