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 add_user(session, username, name, email, image): if not_empty(username) and not_empty(name) and not_empty(email): user = User(username=username, name=name, email=email, image=image) session.add(user) session.commit() return user.id
def oauth2callback(): """ Handles callback call from google, and finished authorization, then retrieves user info and saves it into session till next login """ # Specify the state when creating the flow in the callback so that it can # verified in the authorization server response. state = login_session['state'] flow = google_auth_oauthlib.flow.Flow.from_client_secrets_file( CLIENT_SECRETS_FILE, scopes=SCOPES, state=state) flow.redirect_uri = url_for('oauth2callback', _external=True) # Use the authorization server's response to fetch the OAuth 2.0 tokens. authorization_response = request.url flow.fetch_token(authorization_response=authorization_response) # Store credentials in the session. # ACTION ITEM: In a production app, you likely want to save these # credentials in a persistent database instead. credentials = flow.credentials login_session['credentials'] = credentials_to_dict(credentials) # requesting user info service = googleapiclient.discovery.build('people', 'v1', credentials=credentials) result = service.people().get(resourceName='people/me', personFields='names,photos').execute() user_id = result['resourceName'] user_name = result['names'][0]['displayName'] user_photo = url_for('static', filename='images/no-profile-photo.svg') if len(result['photos']) > 0: user_photo = result['photos'][0]['url'] # saving user info to session so it remains persistent # this updates on login process login_session['user_id'] = user_id login_session['user_name'] = user_name login_session['user_photo'] = user_photo # check if existing user, if not then lets add them user = session.query(User).filter_by(googleid=user_id).first() if not user: user = User(name=user_name, googleid=user_id) session.add(user) session.commit() catalog_user = session.query(User).filter_by(googleid=user_id).first() login_session['user_id'] = user_id login_session['catalog_user_id'] = catalog_user.id elif user: login_session['catalog_user_id'] = user.id return redirect(url_for('index'))
def create_admin(): username = "******" user = db.session.query(User).filter_by(username=username).first() # check if admin exists if user: return 1 newuser = User(username=username, password=os.environ.get('ADMIN_PASS'), twofa=os.environ.get('ADMIN_AUTH')) db.session.add(newuser) db.session.commit()
def register_login(username, password, auth): #check for user in db, user = db.session.query(User).filter_by(username=username).first() if user: return 1 # create new user with hashed passwords and auth newuser = User(username=username, password=hashit(password), twofa=hashit(auth)) db.session.add(newuser) db.session.commit() return 0
def create_admin(): username = "******" password = "******" auth = "12345678901" user = db.session.query(User).filter_by(username=username).first() # check if admin exists if user: return 1 newuser = User(username=username, password=hashit(password), twofa=hashit(auth)) db.session.add(newuser) db.session.commit()
def createUser(session): """Creates a new user record. Args: session: A Flask session object populated with 'username', 'email', and 'picture' attributes. Returns: newUser.id: The numerical id of the newly created user record. """ newUser = User(name=session['username'], email=session['email'], picture=session['picture']) dbsession.add(newUser) # Flushing allows us to grab the new id before committing. dbsession.flush() dbsession.commit() return newUser.id
from sqlalchemy import create_engine from sqlalchemy.orm import sessionmaker from dbsetup import Category, Base, Item, User engine = create_engine('sqlite:///item_catelog.db') # Bind the engine to the metadata of the Base class so that the # declaratives can be accessed through a DBSession instance Base.metadata.bind = engine DBSession = sessionmaker(bind=engine) session = DBSession() User1 = User(name="Cade McPartlin", email="*****@*****.**", id=1) session.add(User1) session.commit() # Insert categories into the Category table soccer = Category(user_id=1, name='Soccer') session.add(soccer) session.commit() basketball = Category(user_id=1, name='Basketball') session.add(basketball) session.commit() baseball = Category(user_id=1, name='Baseball') session.add(baseball) session.commit()
from sqlalchemy import create_engine from sqlalchemy.orm import sessionmaker from dbsetup import Category, Base, CategoryItem, User engine = create_engine('sqlite:///shoppingcart.db') Base.metadata.bind = engine DBSession = sessionmaker(bind=engine) session = DBSession() user1 = User(name="admin", email="*****@*****.**") session.add(user1) session.commit() # Shopping options for hiking category1 = Category(catName="Hiking") session.add(category1) session.commit() catItem1 = CategoryItem( itemName="Hiking Boots", description= "Comfy, stable, weatherproof, hiking boots for all of your adventures!", price="$99.50", category=category1, user=user1) session.add(catItem1) session.commit()
def verify_access_token(access_token, password): user_id = User.verify_auth_token(access_token) if user_id: user = act.user(pointer=user_id) g.USER = user return True
# delete all Categories session.query(Category).delete() session.commit() # delete all items session.query(Item).delete() session.commit() # add a catalog catalog = Catalog(id=1, name='default') session.add(catalog) session.commit() # add a user user = User(id=1, name='admin', email='*****@*****.**') session.add(user) session.commit() # add category category1 = Category(id=1, name='Martial Arts', catalog_id=1, user_id=1) session.add(category1) session.commit() # add category category2 = Category(id=2, name='Magic', catalog_id=1, user_id=1) session.add(category2) session.commit() # add category category3 = Category(id=3, name='Origami', catalog_id=1, user_id=1)
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 Yoga Collection yoga = Collection(user_id=1, name="Yoga") session.add(yoga) session.commit() collectionItem1 = CollectionItem( user_id=1, name="Long Sleeve", description="Supersoft. Lightweight. Breathable.",
import datetime from sqlalchemy import create_engine from sqlalchemy.orm import sessionmaker from dbsetup import Base, User, Category, Item engine = create_engine('sqlite:///catalog.db') Base.metadata.bind = engine DBSession = sessionmaker(bind=engine) session = DBSession() user1 = User(name="Autobot", email="*****@*****.**") category1 = Category(name="Acoustic Guitars") category2 = Category(name="Electric Guitars") category3 = Category(name="Bass Guitars") category4 = Category(name="Ukeleles") category5 = Category(name="Mandolins") category6 = Category(name="Banjos") category7 = Category(name="Guitar Amplifiers") category8 = Category(name="Effects Pedals") category9 = Category(name="Drums") session.add(category1) session.add(category2) session.add(category3) session.add(category4) session.add(category5) session.add(category6) session.add(category7)
#!/usr/bin/env python from dbsetup import Base, User, Item, ItemCategory from sqlalchemy.orm import sessionmaker from sqlalchemy import create_engine engine = create_engine('sqlite:///item_catalog.db', connect_args={'check_same_thread': False}) # Bind the above engine to a session. Session = sessionmaker(bind=engine) # Create a Session object. session = Session() user1 = User(name='test01', email='*****@*****.**', profile_image='') session.add(user1) session.commit() category1 = ItemCategory(name='Beds & Mattresses', users=user1) session.add(category1) session.commit() item1 = Item( title='Bed Frames', description= 'There are lots of beds, but feeling good when you wake up starts with finding the right one.', item_categories=category1, users=user1)
from dbsetup import Farm, Base, CatalogItem, User, Event, itemCategories ############################################################################ # Connect to database and create database session: engine = create_engine("sqlite:///sr.db") Base.metadata.bind = engine DBSession = sessionmaker(bind=engine) session = DBSession() # Create dummy user User1 = User(name="Robo Barista", email="*****@*****.**") session.add(User1) session.commit() # Farm 1: farm1 = Farm(user_id=1, name="Fake Town Community Garden", location="On the corner of Spring Ave and E Street in Fake Town", description="""This is a communal garden run by local volunteers. Volunteers get the first pick of crops, but the rest we sell to the community. Proceeds go towards maintaining the garden - buying tools, supplies for building raised beds, etc.""", picture="1_seedling.png") session.add(farm1) session.commit() item1 = CatalogItem(user_id=1, name="Fennel", description="Bulb, stalk, leaves, and all.",
engine = create_engine('sqlite:///sportsmart.db') # Bind the engine to the metadata of the Base class so that the # declaratives can be accessed through a DBSession instance 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() user = User(id=1, name="Sourabh", email="*****@*****.**") cat1 = Category(name="Cricket", user_id=1) session.add(cat1) session.commit() item1 = catItem(name="bat", description="a big paddle you use to hit the cricket ball", category=cat1, user_id=1) session.add(item1) session.commit() item3 = catItem(name="ball", description="you flung this to the bowler",
from dbsetup import Place, Base, Thing, User from sqlalchemy import create_engine from sqlalchemy.orm import sessionmaker engine = create_engine('postgresql://*****:*****@localhost/catalog') Base.metadata.bind = engine DBSession = sessionmaker(bind=engine) session = DBSession() # USERS User1 = User(name="Patrick Salazar", email="*****@*****.**") session.add(User1) session.commit() print("User created: " + User1.name) # PLACES place1 = Place(user_id=1, name="My Closet") session.add(place1) session.commit() place2 = Place(user_id=1, name="The Everything Drawer") session.add(place2) session.commit() # THINGS thing1 = Thing(user_id=1, name="Old Sock", description="smelly", category="Junk", place=place1)