def create_competition(): data = request.form if current_user.admin == 0: return serve_error('Only admins can create competitions', 401) try: competition = Competition( name=data['name'], start=int(data['start_time']), stop=(int(data['start_time']) + int(data['length'])), closed=1 if bool(data['closed']) else 0 ) competition.commit_to_session() comp_problems = loads(data['problems']) except KeyError as err: return serve_error('You must specify name, startTime, length, and' ' problem attributes. ' + err[0] + ' not found.', response_code=400) except ValueError: return serve_error('JSON data for \'problems\' not properly formatted', response_code=400) for problem in comp_problems: session.add(CompProblem( label=problem['label'][:2], cid=competition.cid, pid=problem['pid'] )) session.flush() session.commit() return serve_response(competition.to_dict())
def commit_to_session(self, session=session): """Commit this problem to the database as a new problem.""" session.add(self) session.flush() session.commit() session.refresh(self) return self.pid
def unregister_for_competition(cid): """ Called when a user wants to unregister for a competition. All the user has to do is submit a post to this url with no form data. From their logged-in status, we'll go ahead and remove them from the competiton. Similar to the <code>/register</code> endpoint, an admin can post a list of users to unregister from the competition. """ if session.query(Competition).filter(Competition.cid == cid).first() \ is None: return serve_error('Competition does not exist', response_code=404) if current_user.admin == 1 and 'users' in request.data: try: registrants = loads(request.data['users']) except ValueError: return serve_error('JSON data for \'users\' not properly formatted', response_code=400) else: registrants = [current_user.username] for user in registrants: (session.query(CompUser) .filter(CompUser.username == user, CompUser.cid == cid) .delete()) session.flush() session.commit() return serve_response({})
def commit_to_session(self, session=session): """Commit this blog post to the database""" session.add(self) session.flush() session.commit() session.refresh(self) return self.id
def put_competition_teams(cid): """ Update the teams for a competition If a user is an admin, they can update the competition's users, doing a PUT. This will take the JSON data in the 'teams' part of the request form and store it to the database. Any teams or users not included in the JSON data will not be a part of the competition and will have to re-register; however it should not be used for the solely purpose of de-registering participants. """ try: teams = loads(request.form['teams']) except KeyError as err: return serve_error('You must include the parameter \'teams\'.', response_code=400) except ValueError: return serve_error('JSON data for \'teams\' not properly formatted', response_code=400) # Delete all of the old CompUser rows for this competition session.query(CompUser).filter(CompUser.cid == cid).delete() for team in teams: for user in teams[team]: session.add(CompUser( cid=cid, username=user, team=team )) session.flush() session.commit() return serve_response({})
def commit_to_session(self): """Commit this CompUser to the database. This is useful for adding a newly-created CompUser to the database. """ session.add(self) session.flush() session.commit() session.refresh(self) self._problem = None
def change_password(): oldPassword = request.form['oldPassword'] newPassword = request.form['newPassword'] if bcrypt.check_password_hash(current_user.passw, oldPassword): hashed = bcrypt.generate_password_hash(newPassword) current_user.passw = hashed session.add(current_user) session.flush() session.commit() return serve_response({}) return serve_error('old password does not match', 401)
def update_status(self, status): '''Updates status in the database. :param status: the status of the submission :return: None ''' self.result = status dblock.acquire() session.flush() session.commit() dblock.release()
def commit_to_session(self, session=session): """Commit this Competition to the database. This is useful for adding a newly-created Competition to the database. """ session.add(self) session.flush() session.commit() session.refresh(self) self._problem = None return self.cid
def commit_to_session(self): '''Commit this Submission to the database. This is useful for adding a newly-created Submission to the database. ''' dblock.acquire() session.add(self) session.flush() session.commit() session.refresh(self) dblock.release() self._problem = None
def register_for_competition(cid): """ Called when a user wants to register for a competition. All the user has to do is submit a post to this url with no form data. From their logged-in status, we'll go ahead and add them to the competiton as an individual (team name is default their display name). A 400 error will be returned if the user is already registered for the competition. If the user that is submitting this is an admin, they can optionally supply a json array of usernames to register for the competition. Specifying this will not register the admin, but it will register all users that are listed. A 400 error will be returned if any of the users are already registered for the competition. """ if session.query(Competition).filter(Competition.cid == cid).first() \ is None: return serve_error('Competition does not exist', response_code=404) if current_user.admin == 1 and 'users' in request.data: try: registrants = loads(request.data['users']) except ValueError: return serve_error('JSON data for \'users\' not properly formatted', response_code=400) else: registrants = [current_user.username] for user in registrants: if session.query(CompUser).filter(CompUser.cid == cid, CompUser.username == user).first() is not None: return serve_error('User ' + user + ' already registered for ' 'competition', response_code=400) for username in registrants: user = session.query(User).filter(User.username == user).first() session.add(CompUser( cid=cid, username=user.username, team=user.display )) socketio.emit('new_user', { 'cid': cid, 'user': { 'display': user.display, 'username': user.username } }, namespace='/register') session.flush() session.commit() return serve_response({})
def update_competition_data(cid): """ Adds problems to a competition Doing a POST request adds that problem to the competition whereas a PUT request will remove all problems that were previously associated with that competition and add all of the ones in the form body. TODO: form validation to make sure that no duplicates are added. """ if current_user.admin == 0: # admins only return serve_error('Only admins can modify competitions', 401) data = request.form try: competition = session.query(Competition).filter(Competition.cid == cid)\ .first() competition.name = data['name'] competition.start=int(data['start_time']) competition.stop=(int(data['start_time']) + int(data['length'])) competition.closed = 0 if bool(data['closed']) else 0 competition.commit_to_session() # If the client sends a PUT request, we need to delete all of the old # problems associated with this competition session.query(CompProblem).filter(CompProblem.cid == cid).delete() comp_problems = loads(data['problems']) except KeyError as err: return serve_error('You must specify name, startTime, length, and' ' and problem attributes. ' + err[0] + ' not found.', response_code=400) except ValueError: return serve_error('JSON data for \'problems\' not properly formatted', response_code=400) for problem in comp_problems: session.add(CompProblem( label=problem['label'], cid=competition.cid, pid=problem['pid'] )) session.flush() session.commit() return serve_response(competition.to_dict())
def create_blog_post(): if not current_user.admin == 1: return serve_error('You must be an admin to submit blog posts', response_code=401) if not request.form['title'] or not request.form['subtitle'] or not request.form['body']: return serve_error('Must include title, subtitle, and body with request', response_code=400) post = BlogPost(title=request.form['title'], subtitle=request.form['subtitle'], post_time=int(time()), body=request.form['body'], username=current_user.username) session.add(post) session.flush() session.commit() session.refresh(post) return serve_response(create_blog_object(post))
def update_status(self, status): '''Updates status in the database. :param status: the status of the submission :return: None ''' self.result = status dblock.acquire() # Add to problem_solved if solved for first time if status == 'good' and not (session.query(ProblemSolved) .filter(ProblemSolved.pid == self.pid) .filter(ProblemSolved.username == self.username).all()): session.add(ProblemSolved(username=self.username, pid=self.pid, submit_time=self.submit_time)) session.flush() session.commit() dblock.release()
def create_user(): # Verify that the poster is an admin if current_user.admin == 0: return server_error('Must be admin to create users', 401) # Get form contents username = request.form['username'] password = request.form['password'] display = request.form['display'] # Create the user if doesn't already exist user = load_user(username) if user is None: hashed = bcrypt.generate_password_hash(password) user = User(username=username, passw=hashed, display=display, admin=0) session.add(user) session.flush() session.commit() return serve_response({}) return serve_error('username already exists', 401)
def insert_into_db(session, model, args_list): """ Insert a number of ORM objects into the session for testing. The number of objects inserted is equal to the length of the args_list parameter. :param session: the database session to insert into :param model: the model class of the objects to be added :param args: a list of the arguments to pass to the model constructor :param num: the number of ORM objects to create and insert :returns: the list of new ORM objects """ results = list() for args in args_list: model_object = model(**args) session.add(model_object) session.flush() session.commit(self) session.refresh(model_object) results.append(model_object) return results
def commit_to_session(self): session.add(self) session.flush() session.commit() session.refresh(self)
def commit_to_session(self): """Commit this problem to the database as a new template.""" session.add(self) session.flush() session.commit() session.refresh(self)
def commit_to_session(self, session=session): """Commit this problem data object to the database.""" session.add(self) session.flush() session.commit() session.refresh(self)
def commit_to_session(self, session=session): """Commit this sample case to the database.""" session.add(self) session.flush() session.commit() session.refresh(self)