def get_discoversong_user(user_id): assert user_id is not None db = get_db() disco_user = list(db.select(USER_TABLE, where="rdio_user_id=%i" % user_id)) if len(disco_user) == 0: access_token = web.cookies().get('at') access_token_secret = web.cookies().get('ats') db.insert(USER_TABLE, rdio_user_id=user_id, address=make_unique_email(), token=access_token, secret=access_token_secret, first_use=datetime.date.today(), last_use=datetime.date.today(), emails=0, searches=0, songs=0, prefs=BSONPostgresSerializer.from_dict({})) disco_user = list(db.select(USER_TABLE, where="rdio_user_id=%i" % user_id))[0] count = int(list(db.query("SELECT count(*) from %s" % USER_TABLE))[0]['count']) announce_new_user(count) else: disco_user = disco_user[0] def none_or_empty(strg): return strg is None or strg == '' def fields_need_update(field_names): for field in field_names: if not disco_user.has_key(field): return True if none_or_empty(disco_user[field]): return True return False if fields_need_update(['token', 'secret', 'address', 'prefs']): if fields_need_update(['token', 'secret']): access_token = web.cookies().get('at') access_token_secret = web.cookies().get('ats') db.update(USER_TABLE, where="rdio_user_id=%i" % user_id, secret=access_token_secret, token=access_token) if fields_need_update(['address']): db.update(USER_TABLE, where="rdio_user_id=%i" % user_id, address=make_unique_email()) if fields_need_update(['prefs']): db.update(USER_TABLE, where="rdio_user_id=%i" % user_id, prefs=BSONPostgresSerializer.from_dict({})) disco_user = list(db.select(USER_TABLE, where="rdio_user_id=%i" % user_id))[0] if not disco_user.has_key('prefs') or not disco_user['prefs']: logging.info('resetting preferences') db.update(USER_TABLE, where="rdio_user_id=%i" % user_id, prefs=BSONPostgresSerializer.from_dict({})) disco_user = list(db.select(USER_TABLE, where="rdio_user_id=%i" % user_id))[0] return disco_user, BSONPostgresSerializer.to_dict(disco_user['prefs'])
def GET(self): form_input = get_input() action = form_input['button'] svc, current_user, user_id = get_rdio_and_current_user() db = get_db() where_to_next = '/' if action == 'new_email': new_email = make_unique_email() db.update(USER_TABLE, where="rdio_user_id=%i" % user_id, address=new_email) where_to_next += 'config?saved=True' elif action == 'save': where_to_next += 'config?saved=True' preferences = get_db_prefs(user_id, db=db) new_preferences = get_preferences_from_input(form_input) preferences.update(new_preferences) db.update(USER_TABLE, where="rdio_user_id=%i" % user_id, prefs=BSONPostgresSerializer.from_dict(preferences)) # circular import from discoversong.sources import SourceAppsManager for source_app in SourceAppsManager.ALL: for capability in source_app.capabilities: for configurable_thing in capability.config_options(): if isinstance(configurable_thing, ConfigStoredValue): new_value = configurable_thing.read_from_input(form_input) if not configurable_thing.store_as_db_field: preferences = get_db_prefs(user_id, db=db) preferences[configurable_thing.name] = new_value db.update(USER_TABLE, where="rdio_user_id=%i" % user_id, prefs=BSONPostgresSerializer.from_dict(preferences)) else: db.update(USER_TABLE, where="rdio_user_id=%i" % user_id, **{configurable_thing.name: new_value}) raise web.seeother(where_to_next)
def GET(self): rdio, currentUser = get_rdio_and_current_user() if rdio and currentUser: user_id = int(currentUser['key'][1:]) myPlaylists = rdio.call('getPlaylists')['result']['owned'] db = get_db() result = list(db.select('discoversong_user', what='address, playlist', where="rdio_user_id=%i" % user_id)) if len(result) == 0: access_token = web.cookies().get('at') access_token_secret = web.cookies().get('ats') db.insert('discoversong_user', rdio_user_id=user_id, address=make_unique_email(currentUser), token=access_token, secret=access_token_secret, playlist='new') result = list(db.select('discoversong_user', what='address, playlist', where="rdio_user_id=%i" % user_id))[0] else: result = result[0] message = '' if 'saved' in get_input(): message = ' Saved your selections.' return render.loggedin(name=currentUser['firstName'], message=message, to_address=result['address'], editform=editform(myPlaylists, result['playlist']) ) else: return render.loggedout()