def oauth_make_token(user, client, scope, user_session=None): # Look for an existing token if client.confidential: token = AuthToken.query.filter_by(user=user, client=client).first() elif user_session: token = AuthToken.query.filter_by(user_session=user_session, client=client).first() else: raise ValueError("user_session not provided") # If token exists, add to the existing scope if token: token.add_scope(scope) else: # If there's no existing token, create one if client.confidential: token = AuthToken(user=user, client=client, scope=scope, token_type='bearer') token = failsafe_add(db.session, token, user=user, client=client) elif user_session: token = AuthToken(user_session=user_session, client=client, scope=scope, token_type='bearer') token = failsafe_add(db.session, token, user_session=user_session, client=client) # TODO: Look up Resources for items in scope; look up their providing clients apps, # and notify each client app of this token return token
def oauth_make_token(user, auth_client, scope, user_session=None): # Look for an existing token token = auth_client.authtoken_for(user, user_session) # If token exists, add to the existing scope if token: token.add_scope(scope) else: # If there's no existing token, create one if auth_client.confidential: token = AuthToken(user=user, auth_client=auth_client, scope=scope, token_type='bearer') token = failsafe_add(db.session, token, user=user, auth_client=auth_client) elif user_session: token = AuthToken( user_session=user_session, auth_client=auth_client, scope=scope, token_type='bearer', ) token = failsafe_add(db.session, token, user_session=user_session, auth_client=auth_client) else: raise ValueError("user_session not provided") return token
def test_failsafe_add(self): """ failsafe_add gracefully handles IntegrityError from dupe entries """ d1 = NamedDocument(name=u'add_and_commit_test', title=u"Test") d1a = failsafe_add(self.session, d1, name=u'add_and_commit_test') self.assertTrue(d1a is d1) # We got back what we created, so the commit succeeded d2 = NamedDocument(name=u'add_and_commit_test', title=u"Test") d2a = failsafe_add(self.session, d2, name=u'add_and_commit_test') self.assertFalse(d2a is d2) # This time we got back d1 instead of d2 self.assertTrue(d2a is d1)
def test_failsafe_add_existing(self): """ failsafe_add doesn't fail if the item is already in the session """ d1 = NamedDocument(name=u'add_and_commit_test', title=u"Test") d1a = failsafe_add(self.session, d1, name=u'add_and_commit_test') self.assertTrue(d1a is d1) # We got back what we created, so the commit succeeded d2 = NamedDocument(name=u'add_and_commit_test', title=u"Test") self.session.add(d2) # Add to session before going to failsafe_add d2a = failsafe_add(self.session, d2, name=u'add_and_commit_test') self.assertFalse(d2a is d2) # This time we got back d1 instead of d2 self.assertTrue(d2a is d1)
def test_failsafe_add_silent_fail(self): """ failsafe_add does not raise IntegrityError with bad data when no filters are provided """ d1 = NamedDocument(name=u'missing_title') self.assertIsNone(failsafe_add(self.session, d1))
def load_user(self, userid, uuid=None, create=False): # TODO: How do we cache this? Connect to a cache manager if uuid and hasattr(self.usermodel, '__uuid_primary_key__') and self.usermodel.__uuid_primary_key__: user = self.usermodel.query.get(uuid) # This loads from SQLAlchemy session cache elif hasattr(self.usermodel, 'get'): user = self.usermodel.get(userid=userid, defercols=False) else: user = self.usermodel.query.filter_by( userid=userid).options(undefer('userinfo')).one_or_none() if user is None: if create: user = self.usermodel(userid=userid) if uuid and hasattr(self.usermodel, '__uuid_primary_key__') and self.usermodel.__uuid_primary_key__: user.id = uuid failsafe_add(self.db.session, user, userid=userid) return user
def save_jobview(event_session_id, jobpost_id, bgroup, viewed_time): """ Save a jobpost view as a background job to ensure this doesn't fire *before* the associated save_impressions job. Queue order is important for the cointoss flag in A/B testing. """ jvs = JobViewSession.get_by_ids(event_session_id=event_session_id, jobpost_id=jobpost_id) if jvs is None: jvs = JobViewSession( event_session_id=event_session_id, jobpost_id=jobpost_id, datetime=viewed_time, bgroup=bgroup, ) jvs = failsafe_add(db.session, jvs, event_session_id=event_session_id, jobpost_id=jobpost_id) # Since this is a new view, is there an existing job impression in the same session # which has a bgroup defined? If yes, this view has an associated coin toss. ji = JobImpression.get_by_ids(jobpost_id=jobpost_id, event_session_id=event_session_id) if ji: jvs.cointoss = True if ji.bgroup != jvs.bgroup and jvs.bgroup is not None: jvs.crosstoss = True else: jvs.cointoss = False db.session.commit()
def get(cls, tag, create=False): title = tag[:cls.__name_length__] name = make_name(title) ob = cls.query.filter_by(name=name).one_or_none() if create and not ob: ob = cls(name=name, title=title) ob = failsafe_add(db.session, ob, name=name) return ob
def add_email(self, email, primary=False, type=None, private=False): useremail = UserEmail(user=self, email=email, type=type, private=private) useremail = failsafe_add(db.session, useremail, user=self, email=email) if primary: self.primary_email = useremail return useremail
def oauth_make_token(user, client, scope, user_session=None): if client.confidential: token = AuthToken.query.filter_by(user=user, client=client).first() elif user_session: token = AuthToken.query.filter_by(user_session=user_session, client=client).first() if token: token.add_scope(scope) else: if client.confidential: token = AuthToken(user=user, client=client, scope=scope, token_type='bearer') token = failsafe_add(db.session, token, user=user, client=client) elif user_session: token = AuthToken(user_session=user_session, client=client, scope=scope, token_type='bearer') token = failsafe_add(db.session, token, user_session=user_session, client=client) else: raise ValueError("user_session not provided") # TODO: Look up Resources for items in scope; look up their providing clients apps, # and notify each client app of this token return token
def register_internal(username, fullname, password): user = User(username=username, fullname=fullname, password=password) if not username: user.username = None if user.username: # We can only use failsafe_add when a unique identifier like username is present user = failsafe_add(db.session, user, username=user.username) else: db.session.add(user) user_registered.send(user) return user
def add_email(self, email, primary=False, type=None, private=False): if primary: for emailob in self.emails: if emailob.primary: emailob.primary = False useremail = UserEmail(user=self, email=email, primary=primary, type=type, private=private) useremail = failsafe_add(db.session, useremail, user=self, email=email) with db.session.no_autoflush: for team in Team.query.filter_by(domain=useremail.domain): if self not in team.users: team.users.append(self) return useremail
def session_form(space, proposal=None, session=None): if session: form = SessionForm(obj=session, model=Session) else: form = SessionForm() form.venue_room_id.choices = rooms_list(space) if request.method == 'GET': if not (session or proposal): form.is_break.data = True if proposal: form.description.data = proposal.description form.speaker_bio.data = proposal.bio form.speaker.data = proposal.owner.fullname form.title.data = proposal.title return render_template('session_form.html', form=form, formid='session_form') if form.validate_on_submit(): new = False if not session: new = True session = Session() if proposal: session.proposal = proposal form.populate_obj(session) if new: session.parent = space session.make_id() # FIXME: This should not be required session.make_name() session = failsafe_add(db.session, session, proposal_space_id=space.id, url_id=session.url_id) db.session.commit() data = dict(id=session.url_id, title=session.title, room_scoped_name=session.venue_room.scoped_name if session.venue_room else None, is_break=session.is_break, modal_url=session.url_for('edit'), delete_url=session.url_for('delete'), proposal_id=session.proposal_id) return jsonify(status=True, data=data) return jsonify(status=False, form=render_template('session_form.html', form=form, formid='session_new'))
def session_form(project, proposal=None, session=None): # Look for any existing unscheduled session if proposal and not session: session = Session.for_proposal(proposal) if session: form = SessionForm(obj=session, model=Session) else: form = SessionForm() if proposal: form.description.data = proposal.description form.speaker_bio.data = proposal.bio form.speaker.data = proposal.owner.fullname form.title.data = proposal.title form.venue_room_id.choices = rooms_list(project) if request.method == 'GET': if not (session or proposal): form.is_break.data = True return render_template('session_form.html.jinja2', form=form, formid='session_form') if form.validate_on_submit(): new = False if not session: new = True session = Session() if proposal: session.proposal = proposal form.populate_obj(session) if new: session.parent = project session = failsafe_add(db.session, session, project_id=project.id, url_id=session.url_id) db.session.commit() data = dict( id=session.url_id, title=session.title, room_scoped_name=session.venue_room.scoped_name if session.venue_room else None, is_break=session.is_break, modal_url=session.url_for('edit'), delete_url=session.url_for('delete'), proposal_id=session.proposal_id) return jsonify(status=True, data=data) return jsonify( status=False, form=render_template('session_form.html.jinja2', form=form, formid='session_new'))
def save_jobview(event_session_id, jobpost_id, bgroup, viewed_time): """ Save a jobpost view as a background job to ensure this doesn't fire *before* the associated save_impressions job. Queue order is important for the cointoss flag in A/B testing. """ jvs = JobViewSession.get_by_ids(event_session_id=event_session_id, jobpost_id=jobpost_id) if jvs is None: jvs = JobViewSession(event_session_id=event_session_id, jobpost_id=jobpost_id, datetime=viewed_time, bgroup=bgroup) jvs = failsafe_add(db.session, jvs, event_session_id=event_session_id, jobpost_id=jobpost_id) # Since this is a new view, is there an existing job impression in the same session # which has a bgroup defined? If yes, this view has an associated coin toss. ji = JobImpression.get_by_ids(jobpost_id=jobpost_id, event_session_id=event_session_id) if ji: jvs.cointoss = True if ji.bgroup != jvs.bgroup and jvs.bgroup is not None: jvs.crosstoss = True else: jvs.cointoss = False db.session.commit()
def session_form(project, proposal=None, session=None): # Look for any existing unscheduled session if proposal and not session: session = Session.for_proposal(proposal) if session: form = SessionForm(obj=session, model=Session) else: form = SessionForm() if proposal: form.description.data = proposal.outline form.speaker_bio.data = proposal.bio form.speaker.data = proposal.owner.fullname form.title.data = proposal.title form.venue_room_id.choices = rooms_list(project) if not form.venue_room_id.choices: del form.venue_room_id if request.method == 'GET': return render_template( 'session_form.html.jinja2', form=form, formid='session_form', title=_("Edit session"), ) if form.validate_on_submit(): new = False if not session: new = True session = Session() if proposal: session.proposal = proposal form.populate_obj(session) if new: session.parent = project if session.proposal: session = failsafe_add( db.session, session, project_id=project.id, proposal_id=session.proposal_id, ) else: db.session.add(session) db.session.commit() if request_is_xhr(): data = { 'id': session.url_id, 'title': session.title, 'room_scoped_name': (session.venue_room.scoped_name if session.venue_room else None), 'is_break': session.is_break, 'modal_url': session.url_for('edit'), 'delete_url': session.url_for('delete'), 'proposal_id': session.proposal_id, # FIXME: Switch to UUID } return jsonify(status=True, data=data) else: return redirect(session.url_for('view')) return jsonify( status=False, form=render_template( 'session_form.html.jinja2', form=form, formid='session_new', title=_("Edit session"), ), )