async def authorize_google(request: Request, Authorize: AuthJWT = Depends(), db: Session = Depends(get_db)): try: token = await oauth.google.authorize_access_token(request) except OAuthError as error: return HTMLResponse(f'<h1>{error.error}</h1>') user = await oauth.google.parse_id_token(request, token) account = db.query(Account).filter_by(email=user.email).first() if account is None: logging.warning('Creating a new user object for first-time login') new_account = Account( email=user.email, username=user.email.split('@')[0], first_name=user.given_name, last_name=user.family_name, oauth='google', profile_pic=user.picture, is_verified=True, ) db.add(new_account) db.commit() db.refresh(new_account) create_account_settings(new_account.uuid, db) account = new_account return create_token_for_user(Authorize, str(account.uuid))
async def authorize_github(request: Request, Authorize: AuthJWT = Depends(), db: Session = Depends(get_db)): try: token = await oauth.github.authorize_access_token(request) except OAuthError as error: return HTMLResponse(f'<h1>{error.error}</h1>') resp = await oauth.github.get('user', token=token) user = resp.json() email_to_use = user['email'] or user['login'] + '@fakegithubemail.com' account = db.query(Account).filter_by(email=email_to_use).first() if account is None: new_account = Account(email=email_to_use, username=user['login'], first_name=user['name'], last_name='no last name', oauth='github', profile_pic=user['avatar_url'], city=None if user['location'] is None else user['location'].split(', ')[0], state=None if user['location'] is None else user['location'].split(', ')[1], is_verified=True) db.add(new_account) db.commit() db.refresh(new_account) create_account_settings(new_account.uuid, db) account = new_account return create_token_for_user(Authorize, str(account.uuid))
def create_account(account: AccountCreateRequestSchema, Authorize: AuthJWT = Depends(), db: Session = Depends(get_db)): check_for_existing_username_or_email(account, db) if account.password is not None: check_valid_password(account.password) account.password = encrypt_password(account.password) account = Account(**account.dict()) db.add(account) db.commit() create_account_settings(account.uuid, db) create_access_and_refresh_tokens(str(account.uuid), Authorize) return account
def registerUser(username, password, firstName, lastName, address, email, cookie, referral): session = Session() user_query = session.query(User).filter_by(Username=username) email_query = session.query(User).filter_by(Email=email) if(user_query.count() > 0 or email_query.count() > 0): return False spinsLeft = 3 if len(referral) > 0: ref_user = session.query(User).filter_by(Username=referral) if ref_user.count > 0: ref_spins = ref_user.first().Spinsleft ref_spins += 10 ref_user.update({'Spinsleft' : ref_spins}) spinsLeft += 10 new_user = User(Username=username, Email = email, Password=password, firstName = firstName, lastName = lastName, AdIndex = 0, Spinsleft = spinsLeft, Balance = 0.0, Cookie = cookie) session.add(new_user) session.commit() return True
def send_notification(recipient: str, message: str, channel: NotificationChannel, scheduled_send_date: datetime = None, subject=None): notification = Notification(recipient=recipient, subject=subject, message=message, channel=channel, scheduled_send_date=scheduled_send_date) db = Session() db.add(notification) if notification.channel is NotificationChannel.EMAIL: send_email_notification(notification) elif notification.channel is NotificationChannel.SMS: send_sms_notification(notification) elif notification.channel is NotificationChannel.SLACK: send_slack_notification(notification) else: raise ValueError() db.commit()
from models import User from settings import Session session = Session() SingleUser = User('*****@*****.**', 'test', 'John', 'Cena') session.add(SingleUser) session.commit()