def add_spotcomment(self, id): comment = request.params.get('comment') if not comment or not id: abort(400) comment = h.util.html_escape(comment) spotcomment = SpotlightComment(session['userid'], id, comment) Session.save(spotcomment) Session.commit() # send facebook notification to the person who owns this spotlight # (unless its yours) spot = Session.query(Spotlight).get(spotcomment.spotlightid) if not spot.uid == session['userid']: owner = Session.query(User).get(spot.uid) fbml = " commented on <a href='http://harmonize.fm/player#/people/profile/" + str(owner.id) + "/spcomments/" + str(spot.id) + "' target='_blank'>" + spot.title + "</a>" response = facebook.notifications.send(owner.fbid, fbml) commenter = Session.query(User).get(session['userid']) subject = '%s has commented on your Spotlight' % commenter.name user_href = '<a href="http://harmonize.fm/player#people/profile/%s" target="_blank">%s</a>' % (commenter.id, commenter.name) body = '%s commented on <a href="http://harmonize.fm/player#people/profile/%s/spcomments/%s" target="_blank">%s.</a>' % \ (user_href, owner.id, spot.id, spot.title) facebook.notifications.sendEmail(owner.fbid, subject, text=body) return str(spotcomment.id)
def album(self, user, **kwargs): if not kwargs.get("entity") and kwargs.get("friend"): abort(400) album = Session.query(Album).get(kwargs["entity"]) if not album: abort(404) c.entity = album.title c.recommender = user c.recommendee = kwargs["friend"] c.href = "http://%s/player#/browse/friend=%s/album=%s/song" % ( request.host, session["userid"], kwargs["entity"], ) fbml = render("facebook/recommend.fbml.mako") facebook.notifications.send(c.recommendee, fbml) recommendee_obj = Session.query(User).filter(User.fbid == c.recommendee).first() if recommendee_obj: href = self._get_profile_href(recommendee_obj) subject = "%s has recommended you an album" % user.name body = '%s recommended you <a href="%s" target="_blank">%s</a> by %s.' % ( user.firstname, href, album.title, album.artist, ) facebook.notifications.sendEmail(c.recommendee, subject, text=body) rec = Recommendation(c.recommender.id, int(c.recommendee), albumid=album.id) Session.save(rec) Session.commit() self.whitelist_user(c.recommendee) return "1"
def playlist(self, user, **kwargs): if not kwargs.get("entity") and kwargs.get("friend"): abort(400) playlist = Session.query(Playlist).get(kwargs["entity"]) if not playlist: abort(404) c.entity = playlist.name c.recommender = user c.recommendee = kwargs["friend"] c.href = "http://%s/player#/browse/friend=%s/playlist=%s/playlistsong" % ( request.host, session["userid"], kwargs["entity"], ) fbml = render("facebook/recommend.fbml.mako") facebook.notifications.send(c.recommendee, fbml) recommendee_obj = Session.query(User).filter(User.fbid == c.recommendee).first() if recommendee_obj: href = self._get_profile_href(recommendee_obj) subject = "%s has recommended you a playlist" % user.name body = '%s recommended you a playlist <a href="%s">"%s"</a>.' % (user.name, href, playlist.name) facebook.notifications.sendEmail(c.recommendee, subject, text=body) rec = Recommendation(c.recommender.id, int(c.recommendee), playlistid=playlist.id) Session.save(rec) Session.commit() self.whitelist_user(c.recommendee) return "1"
def get_user(): friendid = request.params.get('friend') if not friendid: user = Session.query(User).get(session['userid']) else: # TODO: Make sure they're friends user = Session.query(User).get(friendid) if not user: abort(400) return user
def delete(self,id): if not id: abort(400) user = Session.query(User).get(session['userid']) spot = Session.query(Spotlight).get(id) if not spot or not spot in user.spotlights: abort(404) Session.delete(spot) Session.commit() user.update_profile() return "1"
def find_by_playlist(self,user, **kwargs): id = kwargs['id'] if not id: abort(400) playlist = Session.query(Playlist).filter(Playlist.id == id) if playlist.first(): qry = Session.query(Spotlight).filter(and_( Spotlight.playlistid == playlist[0].id, Spotlight.uid == user.id, Spotlight.active == 1)) if qry.first(): return "1" return "0"
def playlist(self, id): if not request.params.has_key('comment') or not id: abort(400) comment = request.params['comment'] user = Session.query(User).get(session['userid']) playlist = Session.query(Playlist).get(id) if playlist: spotlight = Spotlight(user=user, playlist=playlist, comment=comment) Session.add(spotlight) Session.commit() return str(spotlight.id) else: abort(404)
def album(self, id): if not request.params.has_key('comment') or not id: abort(400) comment = request.params['comment'] user = Session.query(User).get(session['userid']) album = Session.query(Album).get(id) if album: spotlight = Spotlight(user=user, album = album, comment=comment) Session.add(spotlight) Session.commit() return str(spotlight.id) else: abort(404)
def whitelist_user(self, user): # first check to see if the user is already in our database: qry = Session.query(User).filter(User.fbid == user) if qry.count() != 0: # user is already a member return "1" # now we check to see if they're already on the whitelist qry = Session.query(Whitelist).filter(Whitelist.fbid == user) if qry.count() != 0: # the user is already on the whitelist return "1" w = Whitelist(fbid=user, registered=False) Session.save(w) Session.commit() return "1"
def find_by_album(self, user, **kwargs): id = kwargs['id'] if not id: abort(400) album = Session.query(Album).filter(Album.id == id) if album.first(): qry = Session.query(Spotlight).filter(and_( Spotlight.albumid == album[0].id, Spotlight.uid == user.id, Spotlight.active == 1)) if qry.first(): return "1" else: return "0" else: return "0"
def remove_from_whitelist(self): for id in request.params.iterkeys(): entry = Session.query(Whitelist).get(id) if entry: Session.delete(entry) Session.commit() redirect_to(action='manage_whitelist')
def delete(self, id): playlist = Session.query(Playlist).get(int(id)) if playlist.ownerid != session["userid"]: abort(404, "Cannot delete another man's playlist!") Session.delete(playlist) Session.commit() return "1"
def __before__(self): if not ensure_fb_session(): redirect_to("/") user = Session.query(User).get(session['userid']) if not user.fbid in self.admin: redirect_to('/')
def _rmentity(self, rmclass): for id in request.params.iterkeys(): entity = Session.query(rmclass).get(id) if entity: Session.delete(entity) Session.commit() redirect_to(action='rmentities')
def songurl(self, friend, **kwargs): def set_now_playing(): if not request.params.has_key('pid'): return 'false' song = Session.query(Song).get(request.params.get('pid')) user = Session.query(User).get(session['userid']) # we need to now add the database entries for this song being played. # this includes setting the now playing and updating the song statistic song and source if request.params.has_key('source'): src = int(request.params.get('source')) if src in SongStat.sources: session['src'] = src user.nowplaying = song Session.add(user) Session.commit() user.update_profile() return 'true' song = Session.query(Song).\ join(Song.owners).filter(Song.id==int(kwargs['id'])) song = song.first() if not song: abort(404) qsgen = S3.QueryStringAuthGenerator( config['S3.accesskey'], config['S3.secret'], is_secure = False ) qsgen.set_expires_in(DEFAULT_EXPIRATION*60) if request.params.has_key('pid'): worked = set_now_playing(); return qsgen.get(config['S3.music_bucket'], song.sha)
def next_radio_song(self,user, **kwargs): #TODO: replace this with recommendations #songlist is a list where each element is a song id. #this will be used to generate a random number between 0 and the number #of songs (the length of the list) songlist = [] data = Session.query(*dbfields['song']).\ join(Song.album).reset_joinpoint().\ join(Song.artist).reset_joinpoint().\ join(SongOwner) for friend in user.friends: # grab each users songs and append their song_ids to songlist temp = data.filter(SongOwner.uid == friend.id) for record in temp: songlist.append(record.Song_id) num_songs = len(songlist) if num_songs == 0: abort(404) song_index = random.randint(0,num_songs-1) song_id = songlist[song_index] #now grab the actual song data based on the song_id song = data.filter(Song.id == song_id) return song
def edit(self, id): if not request.params.has_key('comment') or not id: abort(400) #id = request.params.get('spot_id') comment = request.params.get('comment') spotlight = Session.query(Spotlight).filter(Spotlight.id == id)[0] user = Session.query(User).get(session['userid']) if not spotlight or not spotlight in user.spotlights: abort(404) spotlight.comment = comment Session.add(spotlight) Session.commit() return "1"
def find_album(self, user, **kwargs): id = kwargs['id'] if not id: abort(400) qry = Session.query(*dbfields['spotlight']).join(Spotlight.album).\ join(Album.artist).filter(Spotlight.id == id).\ filter(User.id == user.id) return qry.all()
def find_playlist(self, user, **kwargs): id = kwargs['id'] if not id: abort(400) qry = Session.query(*dbfields['spotlight']).\ join(Spotlight.playlist).filter(Spotlight.id == id).\ filter(User.id == user.id).limit(1) return qry.all()
def set_now_playing(): if not request.params.has_key('pid'): return 'false' song = Session.query(Song).get(request.params.get('pid')) user = Session.query(User).get(session['userid']) # we need to now add the database entries for this song being played. # this includes setting the now playing and updating the song statistic song and source if request.params.has_key('source'): src = int(request.params.get('source')) if src in SongStat.sources: session['src'] = src user.nowplaying = song Session.add(user) Session.commit() user.update_profile() return 'true'
def create(self): name = request.params.get("name") uid = session.get("userid") if name and uid: playlist = Playlist(name[:255], uid) Session.save(playlist) Session.commit() qry = Session.query(*dbfields["playlist"]).filter(Playlist.id == playlist.id) json = build_json([qry.one()]) json["data"][0]["type"] = "playlist" return json else: abort(400)
def s3cleanup(self): a = S3.AWSAuthConnection(config['S3.accesskey'], config['S3.secret']) files = a.list_bucket(config['S3.music_bucket']).entries for file in files: db=Session.query(File).filter(File.sha==file.key).first() if db: files.remove(file) removed =[] for file in files: removed.append(file.key) if request.params.get('doit'): a.delete(config['S3.music_bucket'], file.key) return removed
def get_asin(id,type): album = None if type == 'album': album = Session.query(Album).get(id) else: qry = Session.query(SongOwner).filter(SongOwner.uid == session['userid']).filter(SongOwner.songid == id) if qry.count() != 0: # user already has this song return "0" try: album = Session.query(Song).get(id).album except: return "0" if album: if album.mp3_asin: return album.mp3_asin elif album.asin: return album.asin else: return "0" else: return "0"
def home(self): user = Session.query(User).get(session['userid']) c.entries = user.feed_entries c.main = True c.user = user c.num_songs = c.user.song_count c.fbapp_href = "http://www.facebook.com/apps/application.php?id=%s" % \ config['pyfacebook.appid'] c.appid = config['pyfacebook.appid'] if 'Windows' in request.headers['User-Agent']: c.platform = 'windows' elif 'Macintosh' in request.headers['User-Agent']: c.platform = 'mac' return render('/home.mako')
def save(self): if not (request.params.has_key("playlist") or request.params.has_key("songs")): abort(400, "playlist or songs parameter not provided") user = Session.query(User).get(session["userid"]) playlist = int(request.params["playlist"]) playlistobj = ( Session.query(Playlist).filter(and_(Playlist.id == playlist, Playlist.ownerid == session["userid"])).first() ) if not playlistobj: abort(400) songs = request.params["songs"] old_pl_songs = playlistobj.songs for old_pl_song in old_pl_songs: Session.delete(old_pl_song) Session.commit() if songs != "": i = 0 for song in songs.split(","): try: songid = int(song) if not user.get_song_by_id(songid): raise ValueError except ValueError: abort(404) pl_song = PlaylistSong(playlist, i, songid) pl_song.playlist = playlistobj Session.save(pl_song) i += 1 Session.commit() return "1"
def filter_friends(qry): """ This function ensures that songs belong to you by default. If you are browsing a friend's music store, ensure that the songs belong to them. """ friend = request.params.get('friend') friend_fbid = None if friend: friend_fbid = Session.query(User).get(friend).fbid friend = int(friend) if friend_fbid in session['fbfriends']: qry = qry.filter(User.id == friend) else: qry = qry.filter(User.id == session['userid']) return qry
def feedback(self): if not request.params.has_key('email') or\ not request.params.get('feedback'): return '0'; user_email = request.params['email'] user_feedback = request.params['feedback'] user_browser = request.params.get('browser') browser = screen = user = '' user = Session.query(User).get(session['userid']) if user_browser: bdata = cjson.decode(urllib.unquote(user_browser)) for key, value in bdata['browser'].items(): browser = browser + "%s = %s\n" % (key, value) for key, value in bdata['screen'].items(): screen = screen + "%s = %s\n" % (key, value) message = feedback_template % \ (user_feedback, browser, screen) if (self.email_regex.match(user_email) != None): if user.email != user_email: user.email = user_email Session.add(user) Session.commit() else: user_email = None subject = 'harmonize.fm feedback from %s' % user.name def sendmail(): cc = user_email if config['use_gmail'] == 'yes' or not user_email: frm = config['feedback_email'] pword = config['feedback_password'] else: frm = user_email pword = None mail(config['smtp_server'], config['smtp_port'], frm, pword, '*****@*****.**', subject, message, cc=cc) if not 'paste.testing_variables' in request.environ: thread.start_new_thread(sendmail, ()) return '1'
def artist(self, user, **kwargs): if not kwargs.get("entity") and kwargs.get("friend"): abort(400) artist = Session.query(Artist).get(kwargs["entity"]) if not artist: abort(404) c.entity = artist.name c.recommender = user c.recommendee = kwargs["friend"] c.href = "http://%s/player#/browse/friend=%s/artist=%s/album" % ( request.host, session["userid"], kwargs["entity"], ) facebook.notifications.send(c.recommendee, render("facebook/recommend.fbml.mako")) self.whitelist_user(c.recommendee) return "1"
def index(self): assert request.environ.get('HTTP_USER_AGENT'),\ "Cannot serve to unidentified clients" c.profile = Profile() c.user = Session.query(User).get(session['userid']) c.fields = schema.fields c.fblogin_url = facebook.get_login_url(canvas=False) if snippets.is_ie6(request.environ['HTTP_USER_AGENT']): redirect_to('/ie6') if config.get('compressed') == 'true': c.include_files = compressed_player_files else: c.include_files = player_files return render('/player.mako')
def support(self): if request.params.has_key('email'): # we've submitted the form email = request.params.get('email') if (self.email_regex.match(email) == None): return render('/pages/support.mako.html') data = cjson.decode(urllib.unquote(request.params.get('browser'))) qry = Session.query(Notification).\ filter(Notification.email == email).\ filter(Notification.type == 'harmonizer') if qry.count() == 0: # this user hasn't already asked to be notified n = Notification(email, 'harmonizer', data = data) Session.save(n) Session.commit() return render('/pages/thankyou.mako.html') else: # we haven't submitted yet return render('/pages/support.mako.html')