def login(redirect_after=None): jwt = request.args.get("jwt", False) if not jwt: return force_auth() r = registry_request("/auth/profile.json", jwt) try: profile = r.json() conn = Connection() conn.token = b64encode(urandom(30))[:30] conn.logged_in = datetime.datetime.now() conn.last_click = conn.logged_in conn.remote_addr = request.remote_addr conn.user_id = profile["person"]["id"] conn.name = profile["person"]["name"] conn.profile = r.text conn.jwt = jwt db.session.add(conn) db.session.commit() session["conn_id"] = conn.id session["conn_token"] = conn.token if redirect_after: return redirect(b64decode(redirect_after)) return redirect(url_for("index")) except: raise return force_auth()
def up(): for item in seed: connection = Connection(**item["init"]) if item["approve"]: connection.approve() db.session.add(connection) db.session.commit()
def post(self): """ Create a new network connection """ if "Administrator" != current_user.role: return make_response(jsonify({"msg": "Forbidden"}), 403) id = request.json.get("id") direction = request.json.get("direction") organization_A = request.json.get("organization_A") organization_B = request.json.get("organization_B") server_group_A = request.json.get('server_group_A') server_group_B = request.json.get('server_group_B') network_id = request.json.get('network_id') connection = Connection.query.filter_by(direction=direction, organization_A=organization_A, organization_B=organization_B, server_group_A=server_group_A, server_group_B=server_group_B, network_id=network_id).first() if not connection: # If that connection doesn't already exists, then we can create a new one connection = Connection(id=id, direction=direction, organization_A=organization_A, organization_B=organization_B, server_group_A=server_group_A, server_group_B=server_group_B, network_id=network_id) db.session.add(connection) db.session.commit() ret = {'msg': 'Success'} return make_response(jsonify(ret), 200) else: return make_response( jsonify({ "msg": "Unable to add connection. That connection already exists" }), 400)
from earnings_grab import get_earning_data from models import Connection from datetime import date, datetime, timedelta import pandas as pd from pandas.tseries.offsets import BDay import sqlite3 from models import Connection import pandas_datareader.data as web conn, c = Connection() def get_data(df, minpx, maxpx, maxvol): # df = pd.read_sql('''SELECT stockid,date from stockDataTable''',conn,index_col='stockid') df = df[(df['price'] > minpx) & (df['price'] < maxpx) & (df['volume'] < maxvol)] df2 = pd.read_sql('''SELECT stockid,earningsdate FROM earningsTable''', conn) df3 = pd.read_sql('''SELECT date FROM marketTable''', conn) df3['date'], df2['earningsdate'], df['date'] = pd.DatetimeIndex( df3.date), pd.DatetimeIndex( df2.earningsdate).normalize(), pd.DatetimeIndex( df.date).normalize() df = df.set_index('stockid') fail_list = [] df_final = pd.DataFrame() for i in df2['stockid'].unique().tolist(): try: df_sub, df2_sub = df.ix[i], df2[df2['stockid'] == i] df_sub['Earning'] = df_sub['date'].isin(df2_sub['earningsdate']) df_final = df_final.append(df_sub) except:
def post(): approver_id = request.json.get("approver_id") # find existing approver user and/or existing connection approver = User.query.get(approver_id) existing_connection = Connection.query.filter( or_( and_(Connection.requestor_id == approver_id, Connection.approver_id == current_user.id), and_(Connection.approver_id == approver_id, Connection.requestor_id == current_user.id))).first() # handle requests for non-existent approvers if approver is None or approver.is_deleted: raise BadRequest( response={ "notification": { "body": "The user you requested to connect with doesn't exist.", "type": "popup", "duration": 4, }, }) # In the case of an existing, active connection we'll send the client a # reminder that they are already connected to this user. if existing_connection and not existing_connection.is_deleted: raise BadRequest( response={ "notification": { "body": "You are already connected with this user.", "type": "popup", "duration": 3, }, }) # If the route compiles to this point then it means we're either # creating or restoring a connection. Either way, a notification must # be sent to the approver. elif existing_connection: connection = existing_connection connection.rejoin(current_user.id) status_code = 200 else: connection = Connection(current_user.id, approver_id) db.session.add(connection) db.session.commit() status_code = 201 notification = Notification() notification.recipient_id = approver.id notification.type = "bell_notification" notification.body = f"{current_user.first_name} {current_user.last_name} wants to connect with you." notification.action = f"/#/approvals/{connection.id}" db.session.add(notification) db.session.commit() # Emit a notification to the approver's user notifications channel. emit("receive_user_notification", { "data": connection.to_dict(approver_id), "notification": { "id": notification.id, "body": notification.body, "type": notification.type, "action": notification.action, } }, room=f"user_notifications_{approver_id}") return jsonify({ "data": connection.to_dict(current_user.id), "notification": { "body": f"A connection request has been sent to {approver.first_name}.", "type": "popup", "duration": 4, }, }), status_code
def facebook_authorized(resp): next_url = request.args.get('next') or '/' if resp is None or 'access_token' not in resp: return redirect(next_url) session['facebook_token'] = (resp['access_token'], '') data = facebook.get('/me').data profile_picture = 'https://graph.facebook.com/' + data[ 'id'] + '/picture?width=1000' # profile_picture = facebook.get('/me/picture').data if 'id' in data and 'name' in data and 'email' in data and 'link' in data: provider_user_id = data['id'] name = data['name'] username = data['username'] user_email = data['email'] profile_url = data['link'] # profile_picture = profile_picture['url'] generated_name = str(uuid.uuid1()) + '.jpg' user = User.query.filter(User.email == user_email).first() if not user: user = users.create_user(name=name, email=user_email, password=None, active=True) users.commit() current_user_id = str(user.id) folder_path = app.config[ 'UPLOADS_FOLDER'] + '/user/' + current_user_id + '/' new_folder = os.path.dirname(folder_path) if not os.path.exists(new_folder): os.makedirs(new_folder) filepath = os.path.join(folder_path, generated_name) urllib.urlretrieve(profile_picture, filepath) new_photo = 'user/' + current_user_id + '/' + generated_name User.query.filter(User.id == user.id).update( {User.photo: new_photo}) connection = Connection.query.filter( Connection.user_id == user.id, Connection.provider_id == 'facebook').first() if not connection: print "no prior connection" connection = Connection(user=user, provider_id='facebook', provider_user_id=provider_user_id, access_token=resp['access_token'], profile_url=profile_url, image_url=generated_name, full_name=name, display_name=username) db.session.add(connection) db.session.commit() else: print "updating prior connection" connection.access_token = resp['access_token'] db.session.commit() if connection and login_user(user): users.commit() return redirect(next_url) return redirect("/login")
def __init__(self): self.conn = Connection()