def put(self, id): assert request.json["id"] == id transaction = transaction_schema.load(request.json, partial=True) assert transaction.owner_id == current_user.id current_session.add(transaction.data) current_session.commit() return transaction_schema.dump(transaction.data).data, 201
def add(cls, match, player, team, number, on, off, reds, yellows, session=session): firstname = player.split()[0] surname = " ".join(player.split()[1:]) player = Player.from_query(firstname=firstname, surname=surname, session=session) team = Team.from_query(shortname=team, session=session) try: position = session.query(cls).filter_by(match=match, team=team.id, player=player.id, number=number).one() except NoResultFound: position = cls(match=match, team=team.id, player=player.id, number=number, on=on, off=off, reds=reds, yellows=yellows) session.add(position) session.commit() return position
def add(self, match): """ Add a match object to the database. """ tournament = session.query(Tournament).filter_by( name=match.tournament).one() season = session.query(Season).filter_by(tournament=tournament.id, name=match.season).one() home = Team.from_query(match.teams['home'].short_name) away = Team.from_query(match.teams['away'].short_name) try: match = session.query(Match).filter_by(date=match.date, home=home.id, away=away.id, season=season.id).one() except NoResultFound: match = Match( date=match.date, season=season.id, home=home.id, away=away.id, home_score=match.score['home'], away_score=match.score['away'], ) session.add(match) session.commit() return match
def post(self): parse_args = video_parser.parse_args() new_video = Video() new_video.title = parse_args['title'] new_video.type = 'LIVE' new_video.status = 'EMPTY' new_video.created_at = datetime.now() new_video.segment_count = -1 new_video.segment_duration = 3000 new_video.repr_1 = Reprs.HIGH new_video.repr_2 = Reprs.MEDIUM new_video.repr_3 = Reprs.LOW new_video.uri_mpd = None new_video.uri_m3u8 = None try: session.add(new_video) session.commit() except: session.rollback() raise return new_video, 201
def add_dict(self, match): """ Add a match object to the database. """ tournament = session.query(Tournament).filter_by( name=match['tournament']).one() season = session.query(Season).filter_by(tournament=tournament.id, name=match['season']).one() home = Team.from_query(match['home_team']) away = Team.from_query(match['away_team']) try: match = session.query(Match).filter_by(date=match['date'], home=home.id, away=away.id, season=season.id).one() except NoResultFound: date = datetime.strptime(match['date'], "%Y-%m-%d") match = Match( date=date, season=season.id, home=home.id, away=away.id, home_score=match['home_score'], away_score=match['away_score'], ) session.add(match) session.commit() return match
def post(self, video_id): parse_args = video_end_parser.parse_args() last_segment_id = parse_args['last_segment_id'] if not last_segment_id: abort(400, message="Expecting last_segment_id") return None # check the video video = session \ .query(Video) \ .filter(Video.video_id == video_id) \ .first() if not video: abort(404, message="Video (%s) doesn't exist" % video_id) return None video.segment_count = last_segment_id + 1 video.status = 'OK' # generate the thumbnail thumbnail_segment_id = int(video.segment_count / 2) enqueue_segment_task('thumbnail', video.video_id, thumbnail_segment_id) try: session.add(video) session.commit() except: session.rollback() logger.error("Error persistent data: %s" % traceback.format_exc()) raise return video
def when_user_is_registered(app, user, confirm_token): """ we log their ip address so as to try and block spammers. """ remote_addr = request.remote_addr or None user.registered_ip = remote_addr current_session.add(user) current_session.commit()
def edit(link): """ the wiki page. """ # TODO: we need to add users_id, parents, and keywords page = Wiki.for_link(current_session, link) if page is None: # we create a new empty wiki page! page = Wiki(link=link, title=link, latest=1) if page.locked and not current_user.has_role('admin'): flash('Wiki page locked.') return current_app.login_manager.unauthorized() form = WikiForm(obj=page) if request.method == 'GET': # we want people to enter in new data for this field. form.changes.data = '' elif request.method == 'POST': if form.validate_on_submit(): page.new_version(current_session) page.content = form.content.data page.changes = form.changes.data page.users_id = current_user.id current_session.add(page) current_session.commit() delete_view_wiki_cache(link) return redirect(url_for('wiki.index', link=page.link)) return render_template('wiki/edit.html', form=form, wiki=page)
def _create_proxy_group(user_id, username): """ Create a proxy group for the given user Args: user_id (int): unique integer id for user username (str): unique name for user Return: userdatamodel.user.GoogleProxyGroup: the newly created proxy group """ with GoogleCloudManager() as g_cloud: prefix = get_prefix_for_google_proxy_groups() new_proxy_group = g_cloud.create_proxy_group_for_user(user_id, username, prefix=prefix) proxy_group = GoogleProxyGroup(id=new_proxy_group["id"], email=new_proxy_group["email"]) # link proxy group to user user = current_session.query(User).filter_by(id=user_id).first() user.google_proxy_group_id = proxy_group.id current_session.add(proxy_group) current_session.commit() logger.info("Created proxy group {} for user {} with id {}.".format( new_proxy_group["email"], username, user_id)) return proxy_group
def post_login(self, user, token_result): user.id_from_idp = token_result["sub"] user.email = token_result["email"] user.display_name = "{given_name} {family_name}".format(**token_result) info = {} if user.additional_info is not None: info.update(user.additional_info) info.update(token_result) info.pop("fence_username", None) info.pop("exp", None) user.additional_info = info current_session.add(user) current_session.commit() with flask.current_app.arborist.context(authz_provider="synapse"): if config["DREAM_CHALLENGE_TEAM"] in token_result.get("team", []): # make sure the user exists in Arborist flask.current_app.arborist.create_user( dict(name=user.username)) flask.current_app.arborist.add_user_to_group( user.username, config["DREAM_CHALLENGE_GROUP"], datetime.now(timezone.utc) + timedelta(seconds=config["SYNAPSE_AUTHZ_TTL"]), ) else: flask.current_app.arborist.remove_user_from_group( user.username, config["DREAM_CHALLENGE_GROUP"])
def add(cls, name, score=None, session=session): try: event_type = session.query(cls).filter_by(name=name).one() except NoResultFound: event_type = EventType(name=name, score=score) session.add(event_type) session.commit() return event_type
def save_task(self): try: current_session.add(self) current_session.commit() except (Exception, ) as e: current_session.rollback() raise Exception(e) return True
def login_user(username, provider, fence_idp=None, shib_idp=None, email=None): """ Login a user with the given username and provider. Set values in Flask session to indicate the user being logged in. In addition, commit the user and associated idp information to the db. Args: username (str): specific username of user to be logged in provider (str): specfic idp of user to be logged in fence_idp (str, optional): Downstreawm fence IdP shib_idp (str, optional): Downstreawm shibboleth IdP email (str, optional): email of user (may or may not match username depending on the IdP) """ def set_flask_session_values(user): """ Helper fuction to set user values in the session. Args: user (User): User object """ flask.session["username"] = user.username flask.session["user_id"] = str(user.id) flask.session["provider"] = user.identity_provider.name if fence_idp: flask.session["fence_idp"] = fence_idp if shib_idp: flask.session["shib_idp"] = shib_idp flask.g.user = user flask.g.scopes = ["_all"] flask.g.token = None user = query_for_user(session=current_session, username=username) if user: _update_users_email(user, email) # This expression is relevant to those users who already have user and # idp info persisted to the database. We return early to avoid # unnecessarily re-saving that user and idp info. if user.identity_provider and user.identity_provider.name == provider: set_flask_session_values(user) return else: if email: user = User(username=username, email=email) else: user = User(username=username) idp = (current_session.query(IdentityProvider).filter( IdentityProvider.name == provider).first()) if not idp: idp = IdentityProvider(name=provider) user.identity_provider = idp current_session.add(user) current_session.commit() set_flask_session_values(user)
def new_comment(project_id): """ Post a comment on this project. """ form = ProjectCommentForm() if form.validate_on_submit(): project = project_for(project_id) author = CommentAuthor.from_user(current_session, current_user) parent_id = int(form.parent_id.data) if form.parent_id.data else None thread_id = int(form.thread_id.data) if form.thread_id.data else None # we have proxy fix for remote_addr. ip_address = request.remote_addr created_at = datetime.datetime.now() # hardcoded pygame forum id. category = '796386' forum = 'pygame' title = project.title link = f'https://pygame.org/project/{project_id}/' id_text = f'pygame_project_{project_id}' message = form.message.data message = message if '<p>' not in message else f'<p>{message}</p>' if not thread_id: thread = CommentThread( id_text=id_text, forum=forum, category=category, link=link, title=title, ip_address=ip_address, author=author, created_at=created_at, is_closed=False, is_deleted=False, ) current_session.add(thread) post = CommentPost(author=author, parent_id=parent_id, message=sanitize_html(message), ip_address=ip_address, created_at=created_at, is_deleted=False, is_spam=False) if classify_comment(post) == 'spam': post.is_spam = True if thread_id is None: post.thread = thread else: post.thread_id = thread_id current_session.add(post) current_session.commit() return redirect(url_for('project.view', project_id=project_id))
def put(self, id): assert request.json["id"] == id account = account_schema.load(request.json, partial=True) assert account.owner_id == current_user.id current_session.add(account.data) current_session.commit() return account_schema.dump(account.data).data, 201
def new_project(): """ This adds both a project, and a release. """ form = FirstReleaseForm() if form.validate_on_submit(): now = datetime.datetime.now() user = current_user project = Project( title=form.title.data, summary=form.summary.data, description=form.description.data, uri=form.uri.data, datetimeon=now, user=user, youtube_trailer=form.youtube_trailer.data, github_repo=form.github_repo.data, patreon=form.patreon.data, ) tags = [t.lstrip().rstrip() for t in form.tags.data.split(',')] if '' in tags: tags.remove('') for value in tags: tag = Tags(project=project, value=value) current_session.add(tag) release = Release(datetimeon=now, description=form.description.data, srcuri=form.srcuri.data, winuri=form.winuri.data, macuri=form.macuri.data, version=form.version.data) project.releases.append(release) if form.image.data is not None: www = Path(current_app.config['WWW']) sec_fname = secure_filename(form.image.data.filename) extension = os.path.splitext(sec_fname)[-1] current_session.commit() image_fname = f'{project.id}{extension}' project.image = image_fname image_path = str(www / 'shots' / image_fname) save_image(form.image, image_path) current_session.add(project) current_session.commit() return redirect(url_for('project.view', project_id=project.id)) return render_template('project/newproject.html', form=form)
def list_player_simulations(): simulations = ( current_session.query(models.PlayerSimulation) .order_by(models.PlayerSimulation.run_time.desc()) ) load_all = flask.request.args.get('all', False) if not load_all: simulations = simulations.limit(50) form = PlayerSimulationForm() form.profile_id.choices = [ (profile.id, str(profile)) for profile in current_session.query(models.Profile) ] if form.validate_on_submit(): form_data = form.data.copy() form_data.pop('csrf_token', None) profile = ( current_session.query(models.Profile) .filter(models.Profile.id == form_data['profile_id']) .one() ) lookups = sim.load_lookups(current_session) simulation = models.PlayerSimulation( profile=profile, iterations=form_data['iterations'], ) current_session.add(simulation) current_session.commit() flask.g.q.enqueue_call( jobs.run_one_player_sim, kwargs=dict( sim_id=simulation.id, profile=profile, score_shot_types=lookups[0], score_points=lookups[1], iterations=form_data['iterations'] ), ) return flask.redirect( flask.url_for('.view_player_simulation', id=simulation.id) ) return flask.render_template( 'list_player_simulations.html', form=form, simulations=simulations, show_modal=flask.request.method == 'POST', ), 200 if flask.request.method == 'GET' else 400
def put(self, id): assert request.json["id"] == id if id != current_user.id: abort(401) user = user_schema.load(request.json) current_session.add(user.data) current_session.commit() return user_schema.dump(user.data).data, 201
def add(self, firstname, surname, country=None, session=session): try: player = session.query(Player).filter_by(firstname=firstname, surname=surname).one() except NoResultFound: player = Player(firstname=firstname, surname=surname, country=country) session.add(player) session.commit() return player
def _update_users_email(user, email): """ Update email if provided and doesn't match db entry. """ if email and user.email != email: logger.info( f"Updating username {user.username}'s email from {user.email} to {email}" ) user.email = email current_session.add(user) current_session.commit()
def _update_users_id_from_idp(user, id_from_idp): """ Update id_from_idp if provided and doesn't match db entry. """ if id_from_idp and user.id_from_idp != id_from_idp: logger.info( f"Updating username {user.username}'s id_from_idp from {user.id_from_idp} to {id_from_idp}" ) user.id_from_idp = id_from_idp current_session.add(user) current_session.commit()
def add_user_to_system(): try: username = request.values['username'] password = request.values['password'] deviceid = request.values['deviceid'] except KeyError: return "invalid request", 404 pwhash = hashlib.sha512(password).hexdigest() userrecord = User(username=username, pwhash=pwhash, clientid=deviceid) current_session.add(userrecord) current_session.commit() return '', 200
def add(self, tournament, session=session): try: if isinstance(tournament, rugby.data.Tournament): tournament = session.query(Tournament).filter_by( name=tournament.name).one() else: tournament = session.query(Tournament).filter_by( name=tournament['name']).one() except NoResultFound: tournament = Tournament(name=tournament['name']) session.add(tournament) session.commit() return tournament
def create_user(cls, name, username, password, session=session): """Create a new user in the database.""" try: user = cls.find_user(username, session) except NoResultFound: salt = bcrypt.gensalt() user = cls(name=name, username=username, password=bcrypt.hashpw(password.encode(), salt), registered_on=datetime.now()) session.add(user) session.commit() return user
def add_custom_service_account_key_expiration( key_id, service_account_id, expires, private_key=None ): """ Add db entry of user service account key and its custom expiration. """ sa_key = GoogleServiceAccountKey( key_id=key_id, service_account_id=service_account_id, expires=expires, private_key=private_key, ) current_session.add(sa_key) current_session.commit()
def register(): if current_user.is_authenticated: return redirect(url_for('main.main')) form = RegistrationForm() if form.validate_on_submit(): user = User(username=form.username.data, email=form.email.data) user.set_password(form.password.data) db.add(user) newuser = db.query(User).filter_by(username=form.username.data).first() fav = Favourite(user_id=newuser.id, coins='') db.add(fav) db.commit() flash(f'Congratulations, you are now a registered user! {user.id}') return redirect(url_for('author.login')) return render_template('register.html', title='Register', form=form)
def post(): form = BlogPostForm(request.form) if request.method == 'POST': if form.validate(): post = Post(user=form.author.data, title=form.title.data, content=form.content.data) current_session.add(post) current_session.commit() flash('Post created!') return redirect(url_for('post')) else: flash('Form is not valid! Post was not created.') posts = current_session.query(Post).all() return render_template('post.html', form=form, posts=posts)
def add(self, tournament, session=session): tournament_id = Tournament.add(tournament).id try: if isinstance(tournament, rugby.data.Tournament): season = session.query(Season).filter_by( name=tournament.season, tournament=tournament_id).one() else: season = session.query(Season).filter_by( name=tournament['season'], tournament=tournament_id).one() except NoResultFound: season = Season(name=tournament['season'], tournament=tournament_id) session.add(season) session.commit() return season
def add(cls, tournament, season, mapping): """ Add a dictionary of teams and conferences to the model. """ tournament_id = session.query(Tournament).filter_by( name=tournament).one().id season_id = session.query(Season).filter_by( name=season, tournament=tournament_id).one().id for team, conference in mapping.items(): team_id = session.query(Team).filter_by(shortname=team).one().id mapping = cls(season=season_id, team=team_id, conference=conference) session.add(mapping) session.commit()
def ajax(): form = BlogPostForm() if request.method == 'POST': # and form.validate(): post = Post(user=current_user, title=form.title.data, content=form.content.data) print(post) current_session.add(post) current_session.commit() return jsonify( post.title, post.content, current_user.name ) #{'success':True}), 200, {'ContentType':'application/json'} #, 'Post created!' if request.method == 'GET': return jsonify(current_user.name) return 'Invalid form'
def put(self, video_id): logger.info("Updating video [%s]" % video_id) parsed_args = video_parser.parse_args() video = session \ .query(Video) \ .filter(Video.video_id == video_id) \ .first() video.title = parsed_args['title'] try: session.add(video) session.commit() logger.info("Updated video [%s]" % video_id) except: session.rollback() logger.error("Error persistent data: %s" % traceback.format_exc()) raise return video, 201
def post(self, video_id): parse_args = segment_parser.parse_args() segment = VideoSegment() segment.video_id = video_id segment.segment_id = parse_args['segment_id'] segment.status = 'NIL' segment.repr_1_status = 'NIL' segment.repr_2_status = 'NIL' segment.repr_3_status = 'NIL' # check the video ID video = session \ .query(Video) \ .filter(Video.video_id == video_id) \ .first() if not video: abort(404, message="Video (%s) doesn't exist" % segment.video_id) return segment.uri_mpd = None segment.uri_m3u8 = None segment.original_extension = parse_args["original_extension"] segment.original_path = "%s/%s/%s.%s" % ( upload_path, segment.video_id, segment.segment_id, segment.original_extension ) upload_success = True try: # processing the uploaded file # creating the directory dir_path = os.path.dirname(segment.original_path) if not os.path.exists(dir_path) \ or not os.path.isdir(dir_path): os.mkdir(dir_path) uploaded_file = parse_args['data'] uploaded_file.save(dst=segment.original_path, buffer_size=524288) except: upload_success = False logger.error("Error processing segment upload: %r" % traceback.format_exc()) segment.repr_1_status = 'ERROR' segment.repr_2_status = 'ERROR' segment.repr_3_status = 'ERROR' try: session.add(segment) session.commit() except: session.rollback() # clean up the uploaded file if os.path.exists(segment.original_path): os.remove(segment.original_path) logger.error("Error persistent data: %s" % traceback.format_exc()) raise if upload_success: # generate the thumbnail for the first segment, for live streamings if (segment.segment_id == 0): enqueue_segment_task('thumbnail', video.video_id, 0) enqueue_segment_task('transcode', segment.video_id, segment.segment_id) return segment, 201