def __init__(self): '''Creates entities to populate the mock database. ''' self.mock_users = (User('A', '*****@*****.**'), User('B', '*****@*****.**'), User('C', '*****@*****.**')) self.pantries = [ Pantry('Pantry_A', 1), Pantry('Pantry_B', 2), Pantry('Pantry_C', 3), Pantry('Pantry_D', 1) ] self.categories = [ Category('vegetables', 1), Category('starches', 1), Category('desserts', 1), Category('veggies', 2), Category('snacks', 2), Category('meat', 2), Category('fruit', 3), # 7 Category('meat', 3), Category('drinks', 3) ] self.items = [ Item('apple', 'shiny and red', 5, 1, 1), # 0 Item('broccoli', 'small tree', 10, 5, 1), # 1 Item('chips', 'crispy', 4, 5, 5), # 2 Item('steak', 'high in protein', 1, 20, 8), Item('seltzer', 'fizzy', 15, 1, 3), Item('cake', 'moist', 1, 15, 3), Item('potato', 'high in carbs', 50, 20, 2), Item('apple', 'shiny and red', 5, 1, 7) ]
def createUser(login_session): newUser = User(name=login_session['username'], email=login_session[ 'email'], picture=login_session['picture']) session.add(newUser) session.commit() user = session.query(User).filter_by(email=login_session['email']).one() return user.id
def createUser(login_session): """This method creates a user and stores it in the database. """ newUser = User(name=login_session['username'], email=login_session[ 'email']) session.add(newUser) session.commit() user = session.query(User).filter_by(email=login_session['email']).one() return user.id
def createUser(login_session): """ Creates a new user in the database. Args: login_Session (dict): session object with user data. Returns: user_id (int): disticnt integer value identifying the newly created user""" newUser = User(name=login_session['username'], email=login_session['email'], picture=login_session['picture']) session.add(newUser) session.commit() user = session.query(User).filter_by(email=login_session['email']).one() return user.id
Base.metadata.bind = engine DBSession = sessionmaker(bind=engine) # A DBSession() instance establishes all conversations with the database # and represents a "staging zone" for all the objects loaded into the # database session object. Any change made against the objects in the # session won't be persisted into the database until you call # session.commit(). If you're not happy about the changes, you can # revert all of them back to the last commit by calling # session.rollback() session = DBSession() # Create dummy user User1 = User( name="Robo Barista", email="*****@*****.**", picture= 'https://pbs.twimg.com/profile_images/2671170543/18debd694829ed78203a5a36dd364160_400x400.png' ) session.add(User1) session.commit() #Items for Soccer catalog1 = Catalog(user_id=1, name="Soccer") session.add(catalog1) session.commit() catalogItem1 = CatalogItem( user_id=1, name="Jersey", description=
def gconnect(): # Validate state token if request.args.get('state') != login_session['state']: response = make_response(json.dumps('Invalid state parameter.'), 401) response.headers['Content-Type'] = 'application/json' return response # Obtain authorization code code = request.data try: # Upgrade the authorization code into a credentials object oauth_flow = flow_from_clientsecrets('catalog_client_secret.json', scope='') oauth_flow.redirect_uri = 'postmessage' credentials = oauth_flow.step2_exchange(code) except FlowExchangeError: response = make_response( json.dumps('Failed to upgrade the authorization code.'), 401) response.headers['Content-Type'] = 'application/json' return response # Check that the access token is valid. access_token = credentials.access_token url = ('https://www.googleapis.com/oauth2/v1/tokeninfo?access_token=%s' % access_token) h = httplib2.Http() result = json.loads(h.request(url, 'GET')[1]) # If there was an error in the access token info, abort. if result.get('error') is not None: response = make_response(json.dumps(result.get('error')), 500) response.headers['Content-Type'] = 'application/json' return response # Verify that the access token is used for the intended user. gplus_id = credentials.id_token['sub'] if result['user_id'] != gplus_id: response = make_response( json.dumps("Token's user ID doesn't match given user ID."), 401) response.headers['Content-Type'] = 'application/json' return response # Verify that the access token is valid for this app. if result['issued_to'] != CLIENT_ID: response = make_response( json.dumps("Token's client ID does not match app's."), 401) print "Token's client ID does not match app's." response.headers['Content-Type'] = 'application/json' return response stored_access_token = login_session.get('access_token') stored_gplus_id = login_session.get('gplus_id') if stored_access_token is not None and gplus_id == stored_gplus_id: response = make_response( json.dumps('Current user is already connected.'), 200) response.headers['Content-Type'] = 'application/json' return response # Store the access token in the session for later use. login_session['access_token'] = credentials.access_token login_session['gplus_id'] = gplus_id # Get user info userinfo_url = "https://www.googleapis.com/oauth2/v1/userinfo" params = {'access_token': credentials.access_token, 'alt': 'json'} answer = requests.get(userinfo_url, params=params) data = answer.json() login_session['username'] = data['name'] login_session['picture'] = data['picture'] login_session['email'] = data['email'] print data['email'] if session.query(User).filter_by(email=data['email']).count() != 0: current_user = session.query(User).filter_by(email=data['email']).one() else: newUser = User(name=data['name'], email=data['email']) session.add(newUser) session.commit() current_user = newUser login_session['user_id'] = current_user.id print current_user.id output = '' output += '<h1>Welcome, ' output += login_session['username'] output += '!</h1>' output += '<img src="' output += login_session['picture'] output += ' " style = "width: 300px; height: 300px;border-radius: 150px;">' flash("you are now logged in as %s" % login_session['username']) return output
Base.metadata.bind = engine DBSession = sessionmaker(bind=engine) # A DBSession() instance establishes all conversations with the database # and represents a "staging zone" for all the objects loaded into the # database session object. Any change made against the objects in the # session won't be persisted into the database until you call # session.commit(). If you're not happy about the changes, you can # revert all of them back to the last commit by calling # session.rollback() session = DBSession() # Create dummy user user1 = User( name="Amhar Ford", email="*****@*****.**", picture= 'https://lh5.googleusercontent.com/-fEdEA2m4s20/AAAAAAAAAAI/AAAAAAAAEXY/wJ66AgeAHLQ/photo.jpg' ) session.add(user1) session.commit() #Category for Dairy category1 = Category(name="Dairy", user=user1) session.add(category1) session.commit() groceryItem1 = GroceryItem(name="cheese", description="Cheese is a food \ derived from milk that is produced in a wide range of flavors, \ textures, and forms by coagulation of the milk protein casein. It\
from sqlalchemy import create_engine from sqlalchemy.orm import sessionmaker from catalog_database_setup import Base, Category, Item, User engine = create_engine('sqlite:///catalogitems.db') DBsession = sessionmaker(bind=engine) session = DBsession() # Users user1 = User(name="Phoenix Daphne", email="*****@*****.**") session.add(user1) session.commit() user2 = User(name="Sonny Tilander", email="*****@*****.**") session.add(user1) session.commit() user3 = User(name="Hugh Altretch", email="*****@*****.**") session.add(user1) session.commit() # Category 'Soccer' category1 = Category(name="Soccer") session.add(category1) session.commit() item1 = Item(name="Pair of Shin Guards", description=("Description: Black pair of shin guards. " "Made of foam rubber. Light and sturdy."), category=category1, user=user1)
Base.metadata.bind = engine DBSession = sessionmaker(bind=engine) # A DBSession() instance establishes all conversations with the database # and represents a "staging zone" for all the objects loaded into the # database session object. Any change made against the objects in the # session won't be persisted into the database until you call # session.commit(). If you're not happy about the changes, you can # revert all of them back to the last commit by calling # session.rollback() session = DBSession() # Create dummy user User1 = User(name="Rafaela Cavalcante", email="*****@*****.**", picture='https://pbs.twimg.com/profile_images/2671170543/' '18debd694829ed78203a5a36dd364160_400x400.png') session.add(User1) session.commit() # Items for Kitchen category1 = Category(user_id=1, name="Kitchen") session.add(category1) session.commit() houseItem1 = HouseItem(user_id=1, name="Freezer", description=" Brastemp Freezer inverted position 2015.", price="$2,500", status="for sale",
#!/usr/bin/env python # -*- coding: utf-8 -*- from sqlalchemy import create_engine from sqlalchemy.orm import sessionmaker from catalog_database_setup import Base, Category, Item, User from random import randint engine = create_engine('sqlite:///catalog.db') Base.metadata.bind = engine DBSession = sessionmaker(bind=engine) session = DBSession() user = User(email="*****@*****.**") session.add(user) category = Category(name="Apps & Games") session.add(category) item = Item(name="Tetris", description="Description of Tetris", category=category, owner=user) session.add(item) item = Item(name="Minecraft", description="Description of Minecraft", category=category, owner=user) session.add(item) item = Item(name="Hungry Shark Evolution", description="Description of Hungry Shark Evolution", category=category, owner=user) session.add(item)
def gconnect(): # Validate state token if request.args.get('state') != login_session['state']: response = make_response(json.dumps('Invalid state parameter.'), 401) response.headers['Content-Type'] = 'application/json' return response # Obtain authorization code code = request.data try: # Upgrade the authorization code into a credentials object oauth_flow = flow_from_clientsecrets('client_secrets.json', scope='') oauth_flow.redirect_uri = 'postmessage' credentials = oauth_flow.step2_exchange(code) except FlowExchangeError: response = make_response( json.dumps('Failed to upgrade the authorization code.'), 401) response.headers['Content-Type'] = 'application/json' return response # Check that the access token is valid. access_token = credentials.access_token url = ('https://www.googleapis.com/oauth2/v1/tokeninfo?access_token=%s' % access_token) h = httplib2.Http() result = json.loads(h.request(url, 'GET')[1]) # If there was an error in the access token info, abort. if result.get('error') is not None: response = make_response(json.dumps(result.get('error')), 500) response.headers['Content-Type'] = 'application/json' return response # Verify that the access token is used for the intended user. gplus_id = credentials.id_token['sub'] if result['user_id'] != gplus_id: response = make_response( json.dumps("Token's user ID doesn't match given user ID."), 401) response.headers['Content-Type'] = 'application/json' return response # Verify that the access token is valid for this app. if result['issued_to'] != CLIENT_ID: response = make_response( json.dumps("Token's client ID does not match app's."), 401) response.headers['Content-Type'] = 'application/json' return response stored_access_token = login_session.get('access_token') stored_gplus_id = login_session.get('gplus_id') if stored_access_token is not None and gplus_id == stored_gplus_id: response = make_response( json.dumps('Current user is already connected.'), 200) response.headers['Content-Type'] = 'application/json' return response # Store the access token in the session for later use. login_session['access_token'] = credentials.access_token login_session['gplus_id'] = gplus_id # Get user info userinfo_url = "https://www.googleapis.com/oauth2/v1/userinfo" params = {'access_token': credentials.access_token, 'alt': 'json'} answer = requests.get(userinfo_url, params=params) data = answer.json() login_session['username'] = data['name'] login_session['picture'] = data['picture'] login_session['email'] = data['email'] email = login_session['email'] # Check if user already exists in database, if not add it try: user = session.query(User).filter_by(email=email).one() except exc.SQLAlchemyError: # no user with the given email exists => create it user = User(email=email) session.add(user) session.commit() output = '' output += '<h3>You are now logged in as' output += login_session['username'] output += '! Redirecting to home page...</h3>' return output