def post(self): songs = cache.get_user(self.user, "admin_associate_groups_songs") if not songs: songs = [] songs.append(self.get_argument("song_id")) cache.set_user(self.user, "admin_associate_groups_songs", songs) self.append_standard("song_added")
def get_requests(self, refresh = False): if self.id <= 1: return [] requests = cache.get_user(self, "requests") if refresh or (not requests or len(requests) == 0): line_sid = self.get_request_line_sid() requests = db.c.fetch_all( "SELECT r4_request_store.song_id AS id, r4_request_store.reqstor_order AS order, r4_request_store.reqstor_id AS request_id, " "song_rating AS rating, song_title AS title, song_length AS length, r4_song_sid.song_cool AS cool, r4_song_sid.song_cool_end AS cool_end, " "r4_song_sid.song_elec_blocked AS elec_blocked, r4_song_sid.song_elec_blocked_by AS elec_blocked_by, " "r4_song_sid.song_elec_blocked_num AS elec_blocked_num, r4_song_sid.song_exists AS valid, " "r4_song_sid.album_id AS album_id, r4_albums.album_name " "FROM r4_request_store " "JOIN r4_songs USING (song_id) " "LEFT JOIN r4_song_sid ON (r4_song_sid.sid = %s AND r4_songs.song_id = r4_song_sid.song_id) " "LEFT JOIN r4_song_ratings ON (r4_request_store.song_id = r4_song_ratings.song_id) " "JOIN r4_albums ON (r4_song_sid.album_id = r4_albums.album_id) " "WHERE r4_request_store.user_id = %s " "ORDER BY reqstor_order, reqstor_id", (line_sid, self.id)) if not requests: requests = [] for song in requests: if not song['album_name']: song['albums'] = [ { "id": song['album_id'], "name": db.c.fetch_var("SELECT album_name FROM r4_albums WHERE album_id = %s", (song['album_id'],)) } ] else: song['albums'] = [ { "name": song['album_name'], "id": song['album_id'] } ] song.pop('album_name', None) song.pop('album_id', None) cache.set_user(self, "requests", requests) return requests
def post(self): albums = cache.get_user(self.user, "admin_associate_groups_albums") if not albums: albums = [] albums.append((self.get_argument("album_id"), self.get_argument("album_sid"))) cache.set_user(self.user, "admin_associate_groups_albums", albums) self.append_standard("album_added")
def get_requests(self, sid): if self.id <= 1: return [] requests = [] if db.c.is_postgres: requests = db.c.fetch_all( "SELECT r4_request_store.song_id AS id, COALESCE(r4_song_sid.sid, r4_request_store.sid) AS sid, r4_songs.song_origin_sid AS origin_sid, " "r4_request_store.reqstor_order AS order, r4_request_store.reqstor_id AS request_id, " "song_rating AS rating, song_title AS title, song_length AS length, " "r4_song_sid.song_cool AS cool, r4_song_sid.song_cool_end AS cool_end, " "r4_song_sid.song_elec_blocked AS elec_blocked, r4_song_sid.song_elec_blocked_by AS elec_blocked_by, " "r4_song_sid.song_elec_blocked_num AS elec_blocked_num, r4_song_sid.song_exists AS valid, " "COALESCE(song_rating_user, 0) AS rating_user, COALESCE(album_rating_user, 0) AS album_rating_user, " "song_fave AS fave, album_fave AS album_fave, " "r4_songs.album_id AS album_id, r4_albums.album_name, r4_album_sid.album_rating AS album_rating " "FROM r4_request_store " "JOIN r4_songs USING (song_id) " "JOIN r4_albums USING (album_id) " "JOIN r4_album_sid ON (r4_albums.album_id = r4_album_sid.album_id AND r4_request_store.sid = r4_album_sid.sid) " "LEFT JOIN r4_song_sid ON (r4_request_store.song_id = r4_song_sid.song_id AND r4_song_sid.sid = %s) " "LEFT JOIN r4_song_ratings ON (r4_request_store.song_id = r4_song_ratings.song_id AND r4_song_ratings.user_id = %s) " "LEFT JOIN r4_album_ratings ON (r4_songs.album_id = r4_album_ratings.album_id AND r4_album_ratings.user_id = %s AND r4_album_ratings.sid = r4_request_store.sid) " "WHERE r4_request_store.user_id = %s " "ORDER BY reqstor_order, reqstor_id", (sid, self.id, self.id, self.id)) # Lovely but too heavy considering this SQL query sits in the way of a page refresh # It also needs to be updated to make use of the sid argument # requests = db.c.fetch_all( # "SELECT r4_request_store.song_id AS id, " # "r4_request_store.reqstor_order AS order, r4_request_store.reqstor_id AS request_id, " # "song_rating AS rating, song_title AS title, song_length AS length, " # "r4_song_sid.song_cool AS cool, r4_song_sid.song_cool_end AS cool_end, " # "r4_song_sid.song_elec_blocked AS elec_blocked, r4_song_sid.song_elec_blocked_by AS elec_blocked_by, " # "r4_song_sid.song_elec_blocked_num AS elec_blocked_num, r4_song_sid.song_exists AS valid, " # "COALESCE(song_rating_user, 0) AS rating_user, COALESCE(album_rating_user, 0) AS album_rating_user, " # "r4_songs.album_id AS album_id, r4_albums.album_name, r4_album_sid.album_rating " # "FROM r4_request_store " # "JOIN r4_songs USING (song_id) " # "JOIN r4_albums USING (album_id) " # "JOIN r4_album_sid ON (r4_albums.album_id = r4_album_sid.album_id AND r4_request_store.sid = r4_album_sid.sid) " # "JOIN r4_song_sid ON (r4_song_sid.sid = r4_request_store.sid AND r4_song_sid.song_id = r4_request_store.song_id) " # "LEFT JOIN r4_song_ratings ON (r4_request_store.song_id = r4_song_ratings.song_id AND r4_song_ratings.user_id = %s) " # "LEFT JOIN r4_album_ratings ON (r4_songs.album_id = r4_album_ratings.album_id AND r4_album_ratings.user_id = %s AND r4_album_ratings.sid = r4_request_store.sid) " # "WHERE r4_request_store.user_id = %s " # "ORDER BY reqstor_order, reqstor_id", # (self.id, self.id, self.id)) if not requests: requests = [] for song in requests: song['albums'] = [ { "name": song.pop('album_name'), "id": song['album_id'], "fave": song.pop('album_fave'), "rating": song.pop('album_rating'), "rating_user": song.pop('album_rating_user'), "art": playlist.Album.get_art_url(song.pop('album_id'), song['sid']) } ] cache.set_user(self, "requests", requests) return requests
def post(self): songs = cache.get_user(self.user.id, "dj_election") if not songs: raise APIException("no_dj_election", "No songs found queued for a DJ election.") elec = events.election.Election.create(self.sid) for song in songs: elec.add_song(song) cache.set_user(self.user.id, "dj_election", None) self.append(self.return_name, { "success": True })
def post(self): songs = cache.get_user(self.user.id, self.return_name) if not songs: raise APIException("no_dj_election", "No songs found queued for a DJ election.") for song in songs: if song.id == self.get_argument("song_id"): songs.remove(song) cache.set_user(self.user.id, self.return_name, songs) self.append(self.return_name, { "success": True })
def post(self): songs = cache.get_user(self.user.id, self.return_name) if not songs: songs = [] songs.append(playlist.Song.load_from_id(self.get_argument("song_id"), self.get_argument("song_sid"))) cache.set_user(self.user.id, self.return_name, songs) to_output = [] for song in songs: to_output.append(song.to_dict()) self.append(self.return_name, to_output)
def post(self): songs = cache.get_user(self.user.id, "dj_election") if not songs: raise APIException("no_dj_election", "No songs found queued for a DJ election.") if self.get_argument("sched_id", None) and not self.user.is_admin: if not self.user.id == db.c.fetch_var("SELECT sched_dj_user_id FROM r4_schedule WHERE sched_id = %s", (self.get_argument("sched_id"),)): raise APIException("auth_required", http_code=403) elec = events.election.Election.create(self.sid, self.get_argument("sched_id")) for song in songs: elec.add_song(song) cache.set_user(self.user.id, "dj_election", None) self.append(self.return_name, { "success": True })
def _auth_registered_user(self, ip_address, api_key, bypass = False): if not bypass: # TODO: Users can have multiple keys, should we cache them all? key = cache.get_user(self, "api_key") if not key: key = db.c.fetch_row("SELECT api_key, api_is_rainwave FROM r4_api_keys WHERE user_id = %s AND api_key = %s", (self.id, api_key)) if not key: log.debug("auth", "Invalid user ID %s and/or API key %s." % (self.id, api_key)) return cache.set_user(self, "api_key", key) if key['api_key'] != api_key: log.debug("auth", "Invalid user ID %s and/or API key %s from cache." % (self.id, api_key)) return if key['api_is_rainwave']: self._official_ui = True # Set as authorized and begin populating information # Pay attention to the "AS _variable" names in the SQL fields, they won't get exported to private JSONable dict self.authorized = True user_data = None # user_data = cache.get_user(self, "db_data") if not user_data: user_data = db.c_old.fetch_row("SELECT user_id, username, user_new_privmsg, user_avatar, user_avatar_type AS _user_avatar_type, radio_listenkey AS radio_listen_key, group_id AS _group_id " "FROM phpbb_users WHERE user_id = %s", (self.id,)) cache.set_user(self, "db_data", user_data) self.data.update(user_data) if self.data['_user_avatar_type'] == 1: self.data['user_avatar'] = _AVATAR_PATH % self.data['user_avatar'] elif self.data['_user_avatar_type'] > 0: pass else: self.data['user_avatar'] = "images/blank.png" # Privileged folk - donors, admins, etc - get perks. # The numbers are the phpBB group IDs. if self.data['_group_id'] in [5, 4, 8, 12, 15, 14, 17]: self.data['radio_perks'] = True # Admin and station manager groups if self.data['_group_id'] in [5, 12, 15, 14, 17]: self.data['radio_admin'] = True # jfinalfunk is a special case since he floats around outside the main admin groups elif self.id == 9575: self.data['radio_admin'] = True if not self.data['radio_listen_key']: self.generate_listen_key()
def get(self, group_id): #pylint: disable=W0221 group = SongGroup.load_from_id(group_id) songs = cache.get_user(self.user, "admin_associate_groups_songs") or [] cache.set_user(self.user, "admin_associate_groups_songs", []) for song_id in songs: group.associate_song_id(song_id) albums = cache.get_user(self.user, "admin_associate_groups_albums") or [] cache.set_user(self.user, "admin_associate_groups_albums", []) for album_set in albums: album = Album.load_from_id_with_songs(album_set[0], album_set[1]) for song in album.data['songs']: group.associate_song_id(song['id']) self.write(self.render_string("bare_header.html", title="Added Groups")) self.write("<p>Now associated.</p><p><a href='/admin/tools/associate_groups'>Start over.</a></p>") self.write(self.render_string("basic_footer.html"))
def post(self, sid): listener = db.c.fetch_row("SELECT user_id, listener_ip FROM r4_listeners WHERE listener_relay = %s AND listener_icecast_id = %s", (self.relay, self.get_argument("client"))) if not listener: return self.append("No user record to delete for client %s on relay %s." % (self.get_argument("client"), self.relay)) db.c.update("UPDATE r4_listeners SET listener_purge = TRUE WHERE listener_relay = %s AND listener_icecast_id = %s", (self.relay, self.get_argument("client"))) if listener['user_id'] > 1: self.append("Registered user ID %s flagged for removal." % (listener['user_id'],)) db.c.update("UPDATE r4_request_line SET line_expiry_tune_in = %s WHERE user_id = %s", (time.time() + 600, listener['user_id'])) cache.set_user(listener['user_id'], "listener_record", None) sync_to_front.sync_frontend_user_id(listener['user_id']) else: self.append("Anonymous user, client ID %s relay %s flagged for removal." % (self.get_argument("client"), self.relay)) sync_to_front.sync_frontend_ip(listener['listener_ip']) self.failed = False
def post(self, sid): #pylint: disable=W0221 listener = db.c.fetch_row("SELECT user_id, listener_ip FROM r4_listeners WHERE listener_relay = %s AND listener_icecast_id = %s", (self.relay, self.get_argument("client"))) if not listener: self.append(" RMFAIL: %s %s %s." % (sid, '{:<15}'.format(self.relay), '{:<10}'.format(self.get_argument("client")))) return db.c.update("UPDATE r4_listeners SET listener_purge = TRUE WHERE listener_relay = %s AND listener_icecast_id = %s", (self.relay, self.get_argument("client"))) if listener['user_id'] > 1: db.c.update("UPDATE r4_request_line SET line_expiry_tune_in = %s WHERE user_id = %s", (time.time() + 600, listener['user_id'])) cache.set_user(listener['user_id'], "listener_record", None) sync_to_front.sync_frontend_user_id(listener['user_id']) else: sync_to_front.sync_frontend_ip(listener['listener_ip']) self.append("%s remove: %s %s %s." % ('{:<5}'.format(listener['user_id']), sid, '{:<15}'.format(self.relay), '{:<10}'.format(self.get_argument("client")))) self.failed = False
def get_listener_record(self, use_cache=True): listener = None if self.id > 1: listener = cache.get_user(self.id, "listener_record") if not listener or not use_cache: listener = db.c.fetch_row("SELECT " "listener_id, sid, listener_lock AS lock, listener_lock_sid AS lock_sid, listener_lock_counter AS lock_counter, listener_voted_entry AS voted_entry " "FROM r4_listeners " "WHERE user_id = %s AND listener_purge = FALSE", (self.id,)) else: listener = db.c.fetch_row("SELECT " "listener_id, sid, listener_lock AS lock, listener_lock_sid AS lock_sid, listener_lock_counter AS lock_counter, listener_voted_entry AS voted_entry " "FROM r4_listeners " "WHERE listener_ip = %s AND listener_purge = FALSE", (self.ip_address,)) if listener: self.data.update(listener) if self.id > 1: cache.set_user(self.id, "listener_record", listener) return listener
def get_requests(self, sid): if self.id <= 1: return [] requests = [] if db.c.is_postgres: requests = db.c.fetch_all( "SELECT r4_request_store.song_id AS id, COALESCE(r4_song_sid.sid, r4_request_store.sid) AS sid, r4_songs.song_origin_sid AS origin_sid, " "r4_request_store.reqstor_order AS order, r4_request_store.reqstor_id AS request_id, " "song_rating AS rating, song_title AS title, song_length AS length, song_exists AS good, " "r4_song_sid.song_cool AS cool, r4_song_sid.song_cool_end AS cool_end, " "r4_song_sid.song_elec_blocked AS elec_blocked, r4_song_sid.song_elec_blocked_by AS elec_blocked_by, " "r4_song_sid.song_elec_blocked_num AS elec_blocked_num, r4_song_sid.song_exists AS valid, " "COALESCE(song_rating_user, 0) AS rating_user, COALESCE(album_rating_user, 0) AS album_rating_user, " "song_fave AS fave, album_fave AS album_fave, " "r4_songs.album_id AS album_id, r4_albums.album_name, r4_album_sid.album_rating AS album_rating " "FROM r4_request_store " "JOIN r4_songs USING (song_id) " "JOIN r4_albums USING (album_id) " "JOIN r4_album_sid ON (r4_albums.album_id = r4_album_sid.album_id AND r4_request_store.sid = r4_album_sid.sid) " "LEFT JOIN r4_song_sid ON (r4_request_store.song_id = r4_song_sid.song_id AND r4_song_sid.sid = %s) " "LEFT JOIN r4_song_ratings ON (r4_request_store.song_id = r4_song_ratings.song_id AND r4_song_ratings.user_id = %s) " "LEFT JOIN r4_album_ratings ON (r4_songs.album_id = r4_album_ratings.album_id AND r4_album_ratings.user_id = %s AND r4_album_ratings.sid = r4_request_store.sid) " "WHERE r4_request_store.user_id = %s " "ORDER BY reqstor_order, reqstor_id", (sid, self.id, self.id, self.id)) if not requests: requests = [] for song in requests: if not song['good'] or song['cool'] or song['elec_blocked'] or song['sid'] != sid: song['good'] = False else: song['good'] = True song['albums'] = [ { "name": song.pop('album_name'), "id": song['album_id'], "fave": song.pop('album_fave'), "rating": song.pop('album_rating'), "rating_user": song.pop('album_rating_user'), "art": playlist.Album.get_art_url(song.pop('album_id'), song['sid']) } ] cache.set_user(self, "requests", requests) return requests
def get(self): cache.set_user(self.user, "admin_associate_groups_songs", []) cache.set_user(self.user, "admin_associate_groups_albums", []) self.write(self.render_string("bare_header.html", title="Added Groups")) self.write("<p>Reset.</p><p><a href='/admin/tools/associate_groups'>Start over.</a></p>") self.write(self.render_string("basic_footer.html"))
def vote(self, entry_id, event, lock_count): # Subtract a previous vote from the song's total if there was one already_voted = False if self.user.is_anonymous(): # log.debug("vote", "Anon already voted: %s" % (self.user.id, self.user.data['voted_entry'])) if self.user.data['voted_entry'] and self.user.data[ 'voted_entry'] == entry_id: # immediately return and a success will be registered return True if self.user.data['voted_entry']: already_voted = self.user.data['voted_entry'] else: previous_vote = db.c.fetch_row( "SELECT entry_id, vote_id, song_id FROM r4_vote_history WHERE user_id = %s AND elec_id = %s", (self.user.id, event.id)) # log.debug("vote", "Already voted: %s" % repr(already_voted)) if previous_vote and previous_vote['entry_id'] == entry_id: # immediately return and a success will be registered return True elif previous_vote: already_voted = previous_vote['entry_id'] db.c.start_transaction() try: if already_voted: if not event.add_vote_to_entry(already_voted, -1): log.warn( "vote", "Could not subtract vote from entry: listener ID %s voting for entry ID %s." % (self.user.data['listener_id'], already_voted)) raise APIException("internal_error") # If this is a new vote, we need to check to make sure the listener is not locked. if not already_voted and self.user.data[ 'lock'] and self.user.data['lock_sid'] != self.sid: raise APIException( "user_locked", "User locked to %s for %s more songs." % (config.station_id_friendly[self.user.data['lock_sid']], self.user.data['lock_counter'])) # Issue the listener lock (will extend a lock if necessary) if not self.user.lock_to_sid(self.sid, lock_count): log.warn( "vote", "Could not lock user: listener ID %s voting for entry ID %s, tried to lock for %s events." % (self.user.data['listener_id'], entry_id, lock_count)) raise APIException( "internal_error", "Internal server error. User is now locked to station ID %s." % self.sid) if self.user.is_anonymous(): if not db.c.update( "UPDATE r4_listeners SET listener_voted_entry = %s WHERE listener_id = %s", (entry_id, self.user.data['listener_id'])): log.warn( "vote", "Could not set voted_entry: listener ID %s voting for entry ID %s." % (self.user.data['listener_id'], entry_id)) raise APIException("internal_error") self.user.update({"listener_voted_entry": entry_id}) else: if already_voted: db.c.update( "UPDATE r4_vote_history SET song_id = %s, entry_id = %s WHERE user_id = %s and entry_id = %s", (event.get_entry(entry_id).id, entry_id, self.user.id, already_voted)) else: time_window = int(time.time()) - 1209600 vote_count = db.c.fetch_var( "SELECT COUNT(vote_id) FROM r4_vote_history WHERE vote_time > %s AND user_id = %s", (time_window, self.user.id)) db.c.execute( "SELECT user_id, COUNT(song_id) AS c FROM r4_vote_history WHERE vote_time > %s GROUP BY user_id HAVING COUNT(song_id) > %s", (time_window, vote_count)) rank = db.c.rowcount + 1 db.c.update( "INSERT INTO r4_vote_history (elec_id, entry_id, user_id, song_id, vote_at_rank, vote_at_count, sid) " "VALUES (%s, %s, %s, %s, %s, %s, %s)", (event.id, entry_id, self.user.id, event.get_entry(entry_id).id, rank, vote_count, event.sid)) db.c.update( "UPDATE phpbb_users SET radio_inactive = FALSE, radio_last_active = %s, radio_totalvotes = %s WHERE user_id = %s", (time.time(), vote_count, self.user.id)) user_vote_cache = cache.get_user(self.user, "vote_history") if not user_vote_cache: user_vote_cache = [] while len(user_vote_cache) > 5: user_vote_cache.pop(0) user_vote_cache.append((event.id, entry_id)) cache.set_user(self.user, "vote_history", user_vote_cache) # Register vote if not event.add_vote_to_entry(entry_id): log.warn( "vote", "Could not add vote to entry: listener ID %s voting for entry ID %s." % (self.user.data['listener_id'], entry_id)) raise APIException("internal_error") db.c.commit() except: db.c.rollback() raise return True
def vote(self, entry_id, event, lock_count): # Subtract a previous vote from the song's total if there was one already_voted = False if self.user.is_anonymous(): log.debug("vote", "Anon already voted: %s" % (self.user.id, self.user.data['listener_voted_entry'])) if self.user.data['listener_voted_entry'] and self.user.data['listener_voted_entry'] == entry_id: raise APIException("already_voted_for_song") if self.user.data['listener_voted_entry']: already_voted = True if not event.add_vote_to_entry(entry_id, -1): log.warn("vote", "Could not subtract vote from entry: listener ID %s voting for entry ID %s." % (self.user.data['listener_id'], entry_id)) raise APIException("internal_error") else: already_voted = db.c.fetch_row("SELECT entry_id, vote_id, song_id FROM r4_vote_history WHERE user_id = %s AND elec_id = %s", (self.user.id, event.id)) log.debug("vote", "Already voted: %s" % repr(already_voted)) if already_voted and already_voted['entry_id'] == entry_id: raise APIException("already_voted_for_song") elif already_voted: log.debug("vote", "Subtracting vote from %s" % already_voted['entry_id']) if not event.add_vote_to_entry(already_voted['entry_id'], -1): log.warn("vote", "Could not subtract vote from entry: listener ID %s voting for entry ID %s." % (self.user.data['listener_id'], entry_id)) raise APIException("internal_error") # If this is a new vote, we need to check to make sure the listener is not locked. if not already_voted and self.user.data['listener_lock'] and self.user.data['listener_lock_sid'] != self.sid: raise APIException("user_locked", "User locked to %s for %s more songs." % (config.station_id_friendly[self.user.data['listener_lock_sid']], self.user.data['listener_lock_counter'])) # Issue the listener lock (will extend a lock if necessary) if not self.user.lock_to_sid(self.sid, lock_count): log.warn("vote", "Could not lock user: listener ID %s voting for entry ID %s, tried to lock for %s events." % (self.user.data['listener_id'], entry_id, lock_count)) raise APIException("internal_error", "Internal server error. User is now locked to station ID %s." % self.sid) # Make sure the vote is tracked track_success = False if self.user.is_anonymous(): if not db.c.update("UPDATE r4_listeners SET listener_voted_entry = %s WHERE listener_id = %s", (entry_id, self.user.data['listener_id'])): log.warn("vote", "Could not set voted_entry: listener ID %s voting for entry ID %s." % (self.user.data['listener_id'], entry_id)) raise APIException("internal_error") self.user.update({ "listener_voted_entry": entry_id }) track_success = True else: if already_voted: db.c.update("UPDATE r4_vote_history SET song_id = %s, entry_id = %s WHERE vote_id = %s", (event.get_entry(entry_id).id, entry_id, already_voted['vote_id'])) else: time_window = int(time.time()) - 1209600 vote_count = db.c.fetch_var("SELECT COUNT(vote_id) FROM r4_vote_history WHERE vote_time > %s AND user_id = %s", (time_window, self.user.id)) db.c.execute("SELECT user_id, COUNT(song_id) AS c FROM r4_vote_history WHERE vote_time > %s GROUP BY user_id HAVING COUNT(song_id) > %s", (time_window, vote_count)) rank = db.c.rowcount + 1 db.c.update( "INSERT INTO r4_vote_history (elec_id, entry_id, user_id, song_id, vote_at_rank, vote_at_count) " "VALUES (%s, %s, %s, %s, %s, %s)", (event.id, entry_id, self.user.id, event.get_entry(entry_id).id, rank, vote_count)) track_success = True user_vote_cache = cache.get_user(self.user, "vote_history") if not user_vote_cache: user_vote_cache = [] while len(user_vote_cache) > 5: user_vote_cache.pop(0) user_vote_cache.append((event.id, entry_id)) cache.set_user(self.user, "vote_history", user_vote_cache) # Register vote if not event.add_vote_to_entry(entry_id): log.warn("vote", "Could not add vote to entry: listener ID %s voting for entry ID %s." % (self.user.data['listener_id'], entry_id)) raise APIException("internal_error")
def vote(self, entry_id, event, lock_count): # Subtract a previous vote from the song's total if there was one already_voted = False if self.user.is_anonymous(): # log.debug("vote", "Anon already voted: %s" % (self.user.id, self.user.data['voted_entry'])) if self.user.data['voted_entry'] and self.user.data['voted_entry'] == entry_id: # immediately return and a success will be registered return True if self.user.data['voted_entry']: already_voted = self.user.data['voted_entry'] else: previous_vote = db.c.fetch_row("SELECT entry_id, vote_id, song_id FROM r4_vote_history WHERE user_id = %s AND elec_id = %s", (self.user.id, event.id)) # log.debug("vote", "Already voted: %s" % repr(already_voted)) if previous_vote and previous_vote['entry_id'] == entry_id: # immediately return and a success will be registered return True elif previous_vote: already_voted = previous_vote['entry_id'] db.c.start_transaction() try: if already_voted: if not event.add_vote_to_entry(already_voted, -1): log.warn("vote", "Could not subtract vote from entry: listener ID %s voting for entry ID %s." % (self.user.data['listener_id'], already_voted)) raise APIException("internal_error") # If this is a new vote, we need to check to make sure the listener is not locked. if not already_voted and self.user.data['lock'] and self.user.data['lock_sid'] != self.sid: raise APIException("user_locked", "User locked to %s for %s more songs." % (config.station_id_friendly[self.user.data['lock_sid']], self.user.data['lock_counter'])) # Issue the listener lock (will extend a lock if necessary) if not self.user.lock_to_sid(self.sid, lock_count): log.warn("vote", "Could not lock user: listener ID %s voting for entry ID %s, tried to lock for %s events." % (self.user.data['listener_id'], entry_id, lock_count)) raise APIException("internal_error", "Internal server error. User is now locked to station ID %s." % self.sid) if self.user.is_anonymous(): if not db.c.update("UPDATE r4_listeners SET listener_voted_entry = %s WHERE listener_id = %s", (entry_id, self.user.data['listener_id'])): log.warn("vote", "Could not set voted_entry: listener ID %s voting for entry ID %s." % (self.user.data['listener_id'], entry_id)) raise APIException("internal_error") self.user.update({ "listener_voted_entry": entry_id }) else: if already_voted: db.c.update("UPDATE r4_vote_history SET song_id = %s, entry_id = %s WHERE user_id = %s and entry_id = %s", (event.get_entry(entry_id).id, entry_id, self.user.id, already_voted)) else: time_window = int(timestamp()) - 1209600 vote_count = db.c.fetch_var("SELECT COUNT(vote_id) FROM r4_vote_history WHERE vote_time > %s AND user_id = %s", (time_window, self.user.id)) db.c.execute("SELECT user_id, COUNT(song_id) AS c FROM r4_vote_history WHERE vote_time > %s GROUP BY user_id HAVING COUNT(song_id) > %s", (time_window, vote_count)) rank = db.c.rowcount + 1 db.c.update( "INSERT INTO r4_vote_history (elec_id, entry_id, user_id, song_id, vote_at_rank, vote_at_count, sid) " "VALUES (%s, %s, %s, %s, %s, %s, %s)", (event.id, entry_id, self.user.id, event.get_entry(entry_id).id, rank, vote_count, event.sid)) db.c.update("UPDATE phpbb_users SET radio_inactive = FALSE, radio_last_active = %s, radio_totalvotes = %s WHERE user_id = %s", (timestamp(), vote_count, self.user.id)) user_vote_cache = cache.get_user(self.user, "vote_history") if not user_vote_cache: user_vote_cache = [] found = False for voted in user_vote_cache: if voted[0] == event.id: found = True voted[1] = entry_id while len(user_vote_cache) > 5: user_vote_cache.pop(0) if not found: user_vote_cache.append([event.id, entry_id]) print "setting user cache: %s" % user_vote_cache cache.set_user(self.user, "vote_history", user_vote_cache) # Register vote if not event.add_vote_to_entry(entry_id): log.warn("vote", "Could not add vote to entry: listener ID %s voting for entry ID %s." % (self.user.data['listener_id'], entry_id)) raise APIException("internal_error") db.c.commit() except: db.c.rollback() raise return True
def _get_all_api_keys(self): keys = db.c.fetch_list( "SELECT api_key FROM r4_api_keys WHERE user_id = %s ", (self.id, )) cache.set_user(self, "api_keys", keys) return keys
def get_all_api_keys(self): if self.id > 1: keys = db.c.fetch_list("SELECT api_key FROM r4_api_keys WHERE user_id = %s ", (self.id,)) cache.set_user(self, "api_keys", keys) return keys