def twitter_authorized(response): next_url = request.args.get('next') or url_for('mod_feed.index') if not response: flash('You denied the request to sign in') return redirect(next_url) # Store the token so we can access it later from tokengetter session['twitter_oauth_token'] = ( response['oauth_token'], response['oauth_token_secret']) twitter_user_id = response['user_id'] twitter_screen_name = response['screen_name'] resp = twitter.get('users/show.json', data={'screen_name': twitter_screen_name}) twitter_profile_image_url = resp.data['profile_image_url'] # If the user is not registered, add him user = User.query.filter_by(email=twitter_screen_name).first() if not user: user = User(email=twitter_screen_name, register_with_provider=True) db.session.add(user) db.session.commit() # In any case we update the authentication token in the db # If the user has revoked access we will have new token here connection = Connection.query.filter_by( user_id=user.id, provider_id=provider_id['TWITTER']).first() if not connection: connection = Connection( user_id=user.id, provider_id=provider_id['TWITTER'], provider_user_id=twitter_user_id, display_name=twitter_screen_name, image_url=twitter_profile_image_url, user=user ) connection.oauth_token = response['oauth_token'], connection.oauth_secret = response['oauth_token_secret'], db.session.add(connection) login_user(user) flash('Logged in as id = %s, name = %s, redirect = %s, image_url = %s' % \ (connection.provider_user_id, connection.display_name, twitter_profile_image_url, request.args.get('next'))) return redirect(next_url)
def facebook_authorized(response): """Authorize the Facebook account. :param response: The response from Facebook """ next_url = request.args.get('next') or url_for('mod_feed.index') if not response: flash('You denied the request to sign in') return redirect(next_url) # Store the token so we can access it later from tokengetter session['facebook_access_token'] = (response['access_token'], '') fbme = facebook.get('/me') email = fbme.data['email'] # If the user is not registered, add him user = User.query.filter_by(email=email).first() if not user: user = User(email=email, register_with_provider=True, confirmed=True) db.session.add(user) db.session.commit() # In any case we update the authentication token in the db # If the user has revoked access we will have new token here connection = Connection.query.filter_by( user_id=user.id, provider_id=provider_id['FACEBOOK']).first() if not connection: connection = Connection( user_id=user.id, provider_id=provider_id['FACEBOOK'], provider_user_id=fbme.data['id'], display_name=fbme.data['name'], image_url='https://graph.facebook.com/%s/picture?type=large' % \ (fbme.data['id']), user=user) db.session.add(connection) connection.oauth_token = response['access_token'] # Create a `Saved` magazine if not Magazine.query.filter_by(name='Saved').first(): magazine = Magazine(name='Saved', public=False, user_id=user.id) db.session.add(magazine) db.session.commit() user.saved_magazine = magazine.id login_user(user) return redirect(next_url)
def add_friend(username): """Send a friend request to another user.""" user = User.query.filter_by(username=username).first() user_a_id = current_user.id user_b_id = user.id # print(user_b_id, "sss") # print(request.form) # Check connection status between user_a and user_b is_friends, is_pending = is_friends_or_pending(user_a_id, user_b_id) if user_a_id == user_b_id: flash("You cannot add yourself as a friend.") elif is_friends: flash("You are already friends.") elif is_pending: flash("Your friend request is pending.") else: requested_connection = Connection(user_a_id=user_a_id, user_b_id=user_b_id, status="Requested") db.session.add(requested_connection) db.session.commit() flash('You have sent a friend request to {}.'.format(username)) return redirect(url_for('user', username=username))
def generate_connections(session): """ Iterate through all users and generate connections. Each connection made should include users who have never been connected before. """ active_users = session.query(User).filter(User.active.is_(True)).all() active_user_ids = {user.id for user in active_users} chosen_user_ids = set() for user in active_users: if user.id in chosen_user_ids: # if a user has been chosen for a connection, skip them continue # this user will be chosen as user_1 for this connection, add them to the chosen list chosen_user_ids.update({user.id}) available_user_ids = get_available_connections( session, active_user_ids - chosen_user_ids, user) if len(available_user_ids) == 0: continue user_2_id = random.choice(list(available_user_ids)) chosen_user_ids.update({user_2_id}) cxn = Connection(user_1=user, user_2_id=user_2_id, created_at=datetime.datetime.utcnow()) session.add(cxn)
def new_connection(): connections = Connection.query.all( ) #filter(user_id.has(USER=current_user.username)) filter_con = [] new_connections = [] print(connections) x = 0 for connection in connections: if str(connection.USER)[5:] != "'" + current_user.username + "'": print(f'{str(connection.USER)[5:]} != {current_user.username}') else: print(f'{str(connection.USER)[5:]} == {current_user.username}') filter_con.append(x) x += 1 print(filter_con) for i in filter_con: new_connections.append(connections[i]) form = ConnectionForm() if form.validate_on_submit(): connection = Connection(name=form.name.data, key=form.key.data, secret=form.secret.data, USER=current_user) db.session.add(connection) db.session.commit() flash('Connection Established') return redirect(url_for('home')) return render_template('exemplum-new-connection.html', form=form, connections=new_connections)
def load_settings(config): xml = parse(config['SETTINGS_PATH']) camera_xml = parse(get_camera_settings_path(xml, config)) # TODO: camera settings tuio_tags = xml.getElementsByTagName('tuio') fiducial = xml.getElementsByTagName('fiducial') fiducial = fiducial[0] if fiducial.length > 0 else None calibration = xml.getElementsByTagName('calibration') calibration = calibration[0] if calibration.length > 0 else None return ReactSettings( connections=[ Connection(host=x.getAttribute('host'), protocol=x.getAttribute('type'), port=x.getAttribute('port')) for x in tuio_tags if x.hasAttribute('type') ], camera=CameraSettings(), fiducial=FiducialSettings( amoeba=fiducial.getAttribute('amoeba'), yamaarashi=True if fiducial.getAttribute('yamaarashi') == 'true' else False, mirror=True if fiducial.getAttribute('mirror') == 'true' else False) if fiducial else FiducialSettings(), calibration=CalibrationSettings( file=calibration.getAttribute('file'), ) if calibration else CalibrationSettings())
def logconnection(): request_data = request.get_json() logged_user = request_data["user"] user = User.query.filter_by(email=logged_user).first() if user is not None: c = Connection() c.email = logged_user now = datetime.utcnow() c.reported_connection = now c.log_connection() return "Application launch for " + logged_user + " is logged." else: return "The user configured is not found on Server the connection is not logged"
def accept_friend(username): """Send a friend request to another user.""" user = User.query.filter_by(username=username).first() user_a_id = current_user.id user_b_id = user.id # print(user_b_id, "sss") # print(request.form) # Check connection status between user_a and user_b sender = db.session.query(Connection).filter( Connection.user_a_id == user_b_id, Connection.user_b_id == user_a_id, Connection.status == "Requested").first() sender.status = "Accepted" db.session.commit() new_connection = Connection(user_a_id=user_a_id, user_b_id=user_b_id, status="Accepted") db.session.add(new_connection) db.session.commit() flash('You have A new friend'.format(username)) return redirect(url_for('show_friends_and_requests'))
def google_authorized(response): """Authorize the Google account :param response: The response from Google's authorization service """ access_token = response['access_token'] session['google_token'] = (access_token, '') headers = {'Authorization': 'OAuth '+ access_token} req = Request('https://www.googleapis.com/oauth2/v1/userinfo', None, headers) try: res = urlopen(req) except URLError as err: if err.code == 401: # Unauthorized - bad token session.pop('google_token', None) flash('You denied the request to sign in') return redirect(url_for('mod_auth.login')) flash('Something bad happened when signing with Google') return redirect(url_for('mod_auth.login')) user_info = json.loads(res.read()) user = User.query.filter_by(email=user_info['email']).first() if not user: user = User(email=user_info['email'], register_with_provider=True, confirmed=True) db.session.add(user) db.session.commit() connection = Connection.query.filter_by( user_id=user.id, provider_id=provider_id['GOOGLE']).first() if not connection: connection = Connection( user_id=user.id, provider_id=provider_id['GOOGLE'], provider_user_id=user_info['id'], display_name=user_info['name'], image_url=user_info['picture'], user=user) connection.oauth_token = access_token db.session.add(connection) # Create a `Saved` magazine if not Magazine.query.filter_by(name='Saved').first(): magazine = Magazine(name='Saved', public=False, user_id=user.id) db.session.add(magazine) db.session.commit() user.saved_magazine = magazine.id login_user(user) return redirect(session.get('google_auth_next_url'))
def test_get_available_connections(self): users = [] u1 = User(name='john', email='*****@*****.**') self.session.add(u1) users.append(u1) u2 = User(name='mike', email='*****@*****.**') self.session.add(u2) users.append(u2) u3 = User(name='joe', email='*****@*****.**') self.session.add(u3) users.append(u3) u4 = User(name='steve', email='*****@*****.**') self.session.add(u4) users.append(u4) self.session.flush() available_user_ids = {user.id for user in users} # first, test that with no connections, everyone is fair game self.assertEqual( get_available_connections(self.session, available_user_ids, u1), {u2.id, u3.id, u4.id}) self.assertEqual( get_available_connections(self.session, available_user_ids, u2), {u1.id, u3.id, u4.id}) self.assertEqual( get_available_connections(self.session, available_user_ids, u3), {u1.id, u2.id, u4.id}) self.assertEqual( get_available_connections(self.session, available_user_ids, u4), {u1.id, u2.id, u3.id}) # second, test that if there's a connection, that user gets removed cxn = Connection(user_1_id=u1.id, user_2_id=u2.id) self.session.add(cxn) # for the backrefs self.session.commit() self.assertEqual( get_available_connections(self.session, available_user_ids, u1), {u3.id, u4.id}) self.assertEqual( get_available_connections(self.session, available_user_ids, u2), {u3.id, u4.id}) self.assertEqual( get_available_connections(self.session, available_user_ids, u3), {u1.id, u2.id, u4.id}) self.assertEqual( get_available_connections(self.session, available_user_ids, u4), {u1.id, u2.id, u3.id}) # third, add a connection with u3 as user_1 and u1 as user_2 cxn = Connection(user_1_id=u3.id, user_2_id=u1.id) self.session.add(cxn) # for the backrefs self.session.commit() available_user_ids_after_discard = get_available_connections( self.session, available_user_ids, u1) self.assertEqual(available_user_ids_after_discard, {u4.id}) # fourth, add a connection with u2 as user_1 and u3 as user_2 cxn = Connection(user_1_id=u3.id, user_2_id=u1.id) self.session.add(cxn) # for the backrefs self.session.commit() available_user_ids_after_discard = get_available_connections( self.session, available_user_ids, u1) self.assertEqual(available_user_ids_after_discard, {u4.id})