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 create_user(user): new_user = User(email=user["email"], password=user["password"]) session.add(new_user) session.commit() try: created_user = (session.query(User).filter_by( email=user["email"]).exists()) print "." except SQLAlchemyError: print "Unable to create test user."
def createUser(login_session): """ Create a new user in database table 'user' :param login_session: (DBSession) login_session :return: User.id integer from newly created user row. """ session = DBSession() newUser = User(email=login_session['email']) session.add(newUser) session.commit() user = session.query(User).filter_by(email=login_session['email']).one() return user.id
def login(): pg_api = psql_api.PostgresAPI(get_db()) if flask.request.method == 'POST': username = flask.request.form['username'] # password = passwords.hash_password(flask.request.form['password']) password = flask.request.form['password'] pg_api.exec_query(q_main.get_user_id, { 'username': username, 'passkey': password }, True) sql_data = pg_api.lod() if len(sql_data) > 0: user = User(sql_data['user_id']) login_user(user) flask.flash('התחברת בהצלחה', 'success') return redirect_dest(fallback=flask.url_for('index.home')) else: flask.flash('התחברות נכשלה', 'error') if 'url_args' not in flask.session: flask.session['url_args'] = dict(flask.request.args) if 'next' in flask.session['url_args']: flask.session['url_args'].pop('next') return flask.render_template('login.html', csrf=csrf)
# start on 19-6-2019 2:PM from sqlalchemy import create_engine from sqlalchemy.orm import sessionmaker from setup import Catalog, Items, Base, User engine = create_engine('sqlite:///database.db') Base.metadata.bind = engine DBsession = sessionmaker(bind=engine) session = DBsession() # create user id User1 = User( name="Saeed Shaban", email="*****@*****.**", picture='') session.add(User1) session.commit() # catalog Front end Catalog1 = Catalog(user_id=1, name='Front End') session.add(Catalog1) session.commit() item1 = Items( user_id=1, title='html', details='HTML is the standard markup language for creating Web pages'
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) 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'] # Create user in Database if it doesn't already exist. user = session.query(User.id).filter_by(gid=gplus_id).scalar() is not None if user is False: newUser = User(gid=gplus_id, username=data['name'], picture=data['picture'], email=data['email']) session.add(newUser) session.commit() output = '' output += '<h1>Welcome, ' output += login_session['username'] output += '!</h1>' output += '<img class="login-success-image" src="' output += login_session['picture'] output += ' "> ' flash("you are now logged in as %s" % login_session['username']) print "done!" 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 users User1 = User( name="Hugo Santos", email="*****@*****.**", picture= 'https://scontent-mrs1-1.xx.fbcdn.net/hphotos-ash2/v/t1.0-9/1236029_592323727472948_1910831729_n.jpg?oh=bdedc7bb8db69cc85e6a39c1103f4606&oe=566D32AA', role='admin') session.add(User1) session.commit() # Create dummy categories and items # Items for Action category1 = Category(name="Action") session.add(category1) session.commit() item1 = Item( title="Counter-Strike: Global Ofensive", description=
def add_to_db(filepath): with open(filepath) as f: data = json.load(f) for user_id in data: print user_id user = data[user_id] user_profile = user['personal_info'] # Insert to db new_user = session.query(User).filter( User.linkedin_id == user_id).first() if not new_user: new_user = User(linkedin_id=user_id, name=user_profile['name'], school=user_profile['school'], headline=user_profile['headline'], company=user_profile['company'], summary=user_profile['summary'], location=user_profile['location']) else: #Update User info new_user.name = user_profile['name'] new_user.school = user_profile['school'] new_user.headline = user_profile['headline'] new_user.company = user_profile['company'] new_user.summary = user_profile['summary'] new_user.location = user_profile['location'] # Drop related skills skills = session.query(Skill).filter( Skill.user_id == new_user.id).all() for skill in skills: session.delete(skill) # Drop related accomplishments accomps = session.query(Accomplishment).filter( Accomplishment.user_id == new_user.id).all() for accomp in accomps: session.delete(accomp) # Drop related educations edus = session.query(Education).filter( Education.user_id == new_user.id).all() for edu in edus: session.delete(edu) # Drop related jobs jobs = session.query(Job).filter( Job.user_id == new_user.id).all() for job in jobs: session.delete(job) # Drop related volunteerings volunteerings = session.query(Volunteering).filter( Volunteering.user_id == new_user.id).all() for volunteering in volunteerings: session.delete(volunteering) session.commit() # Add Skills user_skills = user['skills'] for item in user_skills: new_skill = Skill(name=item['name']) new_user.skills.append(new_skill) # Add Accomplishments user_accomps = user['accomplishments'] for category in user_accomps: accomp_list = user_accomps[category] for accomp in accomp_list: new_accomp = Accomplishment(name=accomp, category=category) new_user.accomplishments.append(new_accomp) # Add Educations user_educations = user['experiences']['education'] for edu in user_educations: new_edu = Education(activities=edu['activities'], name=edu['name'], degree=edu['degree'], field_of_study=edu['field_of_study'], date_range=edu['date_range'], grades=edu['grades']) new_user.educations.append(new_edu) # Add Jobs user_jobs = user['experiences']['jobs'] for job in user_jobs: print job['description'] new_job = Job(company=job['company'], date_range=job['date_range'], title=job['title'], description=job['description'], location=job['location']) new_user.jobs.append(new_job) # Add Volunteerings user_volunteerings = user['experiences']['volunteering'] for volunteering in user_volunteerings: new_volunteering = Volunteering( company=volunteering['company'], date_range=volunteering['date_range'], title=volunteering['title'], description=volunteering['description'], location=volunteering['location'], cause=volunteering['cause']) new_user.volunteerings.append(new_volunteering) # Add Connection Users # user_connections = user['connections'] # for connection in user_connections: # linkedin_id = connection['href'].split('/')[-2] # name = connection['name'] # connection_user = session.query(User).filter(User.linkedin_id == linkedin_id).first() # if not connection_user: # connection_user = User(linkedin_id=linkedin_id) # session.merge(connection_user) session.merge(new_user) session.commit()
from sqlalchemy import create_engine from sqlalchemy.orm import sessionmaker from setup import Base, User, Category, Item engine = create_engine('sqlite:///catalog.db', pool_pre_ping=True) Base.metadata.bind = engine DBSession = sessionmaker(bind=engine) session = DBSession() # Add dummy user User1 = User(username="******", email="*****@*****.**", is_admin=True) session.add(User1) session.commit() # Add categories C1 = Category(name="Cats") session.add(C1) session.commit() C2 = Category(name="Coffee") session.add(C2) session.commit() C3 = Category(name="Japan") session.add(C3) session.commit() C4 = Category(name="Home") session.add(C4) session.commit() C5 = Category(name="Dogs") session.add(C5)
# 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() User1 = User(name="Hany Mikhael", email="*****@*****.**", picture='https://lh4.googleusercontent.com/-02tSrDVwrUo' '/AAAAAAAAAAI/AAAAAAAAAAA/ACHi3rdhdrCYkyfxqiiZU4RUX' '-FsRUe1AQ/s96-c/photo.jpg') session.add(User1) session.commit() # List for Meat category1 = Category(user_id=1, name="Meat", description="Meat is animal flesh that is eaten as " "food. Humans have hunted and killed " "animals for meat since prehistoric times. " "The advent of civilization allowed the " "domestication of animals such as " "chickens, sheep, rabbits, pigs and cattle.")
from sqlalchemy import create_engine from sqlalchemy.orm import sessionmaker from setup import Category, Base, Item, User engine = create_engine('sqlite:///items.db') print "Creating Dummy Data..." Base.metadata.bind = engine # Declare database session DBSession = sessionmaker(bind=engine) session = DBSession() # Dummy User user1 = User(gid="1", username="******", picture="/static/jennifer.jpg", email="*****@*****.**") session.add(user1) # Declare Categories category_office = Category(name="Office", owner_id=1) category_bedroom = Category(name="Bedroom", owner_id=1) category_kitchen = Category(name="Kitchen", owner_id=1) category_livingroom = Category(name="Living Room", owner_id=1) category_decorations = Category(name="Decorations", owner_id=1) category_bathroom = Category(name="Bathroom", owner_id=1) session.add(category_office) session.add(category_bedroom) session.add(category_kitchen)