def authenticate(): """get oauth token""" auth = tweepy.OAuthHandler(TWITTER_CONSUMER_API_KEY, TWITTER_CONSUMER_SECRET_KEY) # Exchange request token token = request.session['request_token'] request.session.pop('request_token', None) request.session.pop('twitter_id', None) auth.request_token = token # Get verifier from request oauth_verifier = request.query.oauth_verifier # Get access token try: access_token = auth.get_access_token(oauth_verifier) except tweepy.TweepError as e: print('Error') print(e) # Create API object api = tweepy.API(auth) # User object from api twitter_user = api.me() twitter_id = str(twitter_user._json['id']) # Get Today's date today = str(datetime.utcnow()) # Attempt to grab user from database user = users.find_one(twitter_id=twitter_id) # Generate new address from authorized Twitter user dogecoin_address = doge.get_newaddress(str(twitter_id)) # If the user is in the database we pretty much already have the # access token necesary to do Twitter API requests if user: user.update(dict(updated_at=today)) request.session['twitter_id'] = twitter_id request.session.save() return redirect('/app') # If user is not in the database we create one try: users.insert(dict( twitter_id=twitter_id, created_at=today, updated_at=today, access_token=access_token[0], access_token_secret=access_token[1], address=dogecoin_address, )) request.session['twitter_id'] = twitter_id request.session.save() # TODO: Use a more specific exception except Exception as e: print(e) return redirect('/') # If everything went well, redirect user to the profile dashboard return redirect('/app')
async def sample_data(): """Takes data from the link, decodes them and writes to the database""" url_users = 'http://admin.geliospro.com/sdk/?login=demo&pass=demo&svc=get_users' url_units = 'http://admin.geliospro.com/sdk/?login=demo&pass=demo&svc=get_units' conn = engine.connect() async with aiohttp.ClientSession() as session: async with session.get(url_users) as resp: print(resp.status) data = await resp.text() received_data = json.loads(data) for i in received_data: insert = users.insert().values(id=i['id'], creator=i['creator'], login=i['login']) conn.execute(insert) async with session.get(url_units) as r: print(r.status) data = await r.text() received_data = json.loads(data) for i in received_data: insert = units.insert().values(id=i['id'], creator=i['creator'], name=i['name']) conn.execute(insert)
async def create_user(user: User): hash = get_password_hash(user.password) print('hashed password:'******'id': last_record_id}
async def post_create_user(user: UserCreate) -> Dict: password_hash = get_password_hash(user.password) query = users.insert().values( email=user.email, username=user.username, hashed_password=password_hash, is_active=True, ) await database.execute(query=query) created_user = await get_user_by_username(user.username) return dict(created_user)
async def create_user(self, user: dict): async with self.database_service.instance.acquire() as conn: formatted_user = { 'client_name': user['name'], 'client_email': user['email'], 'password': user['password'] } await conn.execute(users.insert().values(formatted_user)) result = await conn.execute( sql.select( [sql.func.max(users.c.client_id).label('client_id')])) user = await result.fetchone() return dict(user)
async def create_user( username: str, email: str, password: str, is_admin: bool = False, is_active: bool = False ) -> Optional[Mapping]: hashed_password = get_password_hash(password) query = users.insert().returning(users) values = { 'username': username, 'email': email, 'password': hashed_password, 'is_admin': is_admin, 'activation_code': generate_secret_code() } if is_admin or is_active: values['activation_code'] = '' values['is_active'] = True return await database.fetch_one(query=query, values=values)
def create_sample_data(target_config=None): engine = get_engine(target_config) with engine.connect() as conn: conn.execute(users.insert(), [ {'id': 2000000001, 'username': '******', 'password_hash': generate_password_hash('admin')}, {'id': 2000000002, 'username': '******', 'password_hash': generate_password_hash('root')}, ]) conn.execute(flags.insert(), [ {'id': 2000000001, 'seller_id': 2000000001, 'name': '317c6e65-7e38-481a-9227-edbdae51836c', 'description': '0N3KYAYIAR43482F6EQN60ZH8AIHWVJM=', 'cost': 1, 'in_stock': 2}, {'id': 2000000002, 'seller_id': 2000000001, 'name': '88ec2d5a-2330-4181-819a-db2c4d6dd628', 'description': '423A4JFGKG5MCUCZZUGYE6IQ220IY0UG=', 'cost': 1, 'in_stock': 1}, {'id': 2000000003, 'seller_id': 2000000002, 'name': '22aba0f9-ccaf-404d-b6c2-5b5dda1e5a63', 'description': '8WJTQ6UQX5ZQV7DOINDM3Y9BX6D8IEJR=', 'cost': 2, 'in_stock': 6}, ]) conn.execute(orders.insert(), [ {'id': 2000000001, 'user_id': 2000000001, 'flags_id': 2000000001, 'name': '317c6e65-7e38-481a-9227-edbdae51836c', 'description': '0N3KYAYIAR43482F6EQN60ZH8AIHWVJM=', 'created_date': datetime.now()}, {'id': 2000000002, 'user_id': 2000000001, 'flags_id': 2000000002, 'name': '88ec2d5a-2330-4181-819a-db2c4d6dd628', 'description': '423A4JFGKG5MCUCZZUGYE6IQ220IY0UG=', 'created_date': datetime.now()} ]) conn.execute(transactions.insert(), [ {'id': 2000000001, 'from_user_id': 2000000001, 'to_user_id': 2000000002, 'msg': 'ENRVLN3SYOJDRY1JM025RM7P96OSZ08O=', 'value': 2, 'created_date': datetime.now()}, {'id': 2000000002, 'from_user_id': 2000000002, 'to_user_id': 2000000001, 'msg': '333ZG3JX4BPAWC6JLQHILMLAVY2ABQU4=', 'value': 4, 'created_date': datetime.now()}, ])
async def post(payload: UserSchema) -> database: query = users.insert().values(payload) return await database.execute(query=query)