def create_trakt_oauth_credential(cls, trakt_account): if not Environment.dict['trakt.pin.code'] or not Environment.dict['trakt.pin.authorization']: return False try: TraktOAuthCredential.create( account=trakt_account, code=Environment.dict['trakt.pin.code'], **Environment.dict['trakt.pin.authorization'] ) except (apsw.ConstraintError, peewee.IntegrityError), ex: log.debug('Ignoring oauth credential update for %r, already exists (%s)', trakt_account, ex) return False
def create_trakt_oauth_credential(cls, trakt_account): if not Environment.dict['trakt.pin.code'] or not Environment.dict[ 'trakt.pin.authorization']: return False try: TraktOAuthCredential.create( account=trakt_account, code=Environment.dict['trakt.pin.code'], **Environment.dict['trakt.pin.authorization']) except (apsw.ConstraintError, peewee.IntegrityError), ex: log.debug( 'Ignoring oauth credential update for %r, already exists (%s)', trakt_account, ex) return False
def from_pin(self, t_account, pin): if not pin: log.debug('"pin" parameter is empty, ignoring account authorization update') return t_account # Retrieve current account `OAuthCredential` oauth = t_account.oauth_credentials.first() if oauth and oauth.code == pin: log.debug("PIN hasn't changed, ignoring account authorization update") return t_account if not oauth: # Create new `OAuthCredential` for the account oauth = TraktOAuthCredential( account=t_account ) # Update `OAuthCredential` if not TraktOAuthCredentialManager.update.from_pin(oauth, pin, save=False): log.warn("Unable to update OAuthCredential (token exchange failed, hasn't changed, etc..)") # Save code into database (to avoid future re-authentication with the same pin) oauth.save() return None # Validate the account authorization with t_account.oauth_authorization(oauth).http(retry=True): settings = Trakt['users/settings'].get() if not settings: log.warn('Unable to retrieve account details for authorization') return None # Update `TraktAccount` username try: self.update_username(t_account, settings) except (apsw.ConstraintError, peewee.IntegrityError) as ex: log.debug('Trakt account already exists - %s', ex, exc_info=True) raise TraktAccountExistsException('Trakt account already exists') # Save oauth credential changes oauth.save() # Refresh `TraktAccount` t_account.refresh( force=True, settings=settings ) # Refresh `Account` t_account.account.refresh( force=True ) log.info('Updated account authorization for %r', t_account) return t_account