def get_value(self, user): if self.store_as_db_field: value = getattr(user, self.name, 'None') else: prefs = BSONPostgresSerializer.to_dict(user['prefs']) value = prefs.get(self.name) return value
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): rdio, currentUser, user_id = get_rdio_and_current_user() disco_user, message = get_discoversong_user(user_id) prefs = BSONPostgresSerializer.to_dict(disco_user['prefs']) # circular import from discoversong.sources import SourceAppsManager return render.config(user=disco_user, prefs=prefs, capabilities=SourceAppsManager.all_capabilities(), editform=editform(playlists=rdio.call('getPlaylists')['result']['owned'], prefs=get_db_prefs(user_id)), env_message=get_environment_message(), message=message)
def get_db_prefs(user_id, db=None): if db is None: db = get_db() prefs = BSONPostgresSerializer.to_dict(list(db.select(USER_TABLE, what='prefs', where="rdio_user_id=%i" % user_id))[0]['prefs']) return prefs