def api_delete_playlist(self, playlist_id): with self.db.session_scope() as session: user = session.merge(web.get_current_user()) for i, playlist in enumerate(user.playlists): if playlist.l_id == playlist_id: user.playlists.pop(i) return dict(id=playlist_id) raise RequestApiException("Playlist not found")
def create_playlist(self, name, tracks, description): with self.db.session_scope() as session: user = session.merge(web.get_current_user()) playlist = schema.Playlist(l_name=name, l_description=description) user.playlists.append(playlist) session.commit() for track in tracks: self.add_track_to_playlist(playlist, track) return playlist
def edit_playlist(self, playlist_id, name=None, tracks=None): with self.db.session_scope() as session: playlist = session.query(schema.Playlist).filter( schema.Playlist.l_id == playlist_id, schema.Playlist.u_id == web.get_current_user().u_id).one_or_none() if playlist is None: raise RequestApiException("Playlist not found") if name is not None: playlist.l_name = name if tracks is not None: for track in tracks: self.add_track_to_playlist(playlist, track) return playlist
def user(): config = web.get_config() if 'u_email' in session: user_profile = web.get_current_user() playlist_manager = PlaylistManager(web.get_db()) playlists = playlist_manager.api_get_playlists() if user_profile.u_isadmin: return redirect('/admin/adminpage') return render_template('user.html', is_logged_in=web.is_logged_in(), user_email=user_profile.u_email, u_email=user_profile.u_email, u_firstname=user_profile.u_firstname, u_lastname=user_profile.u_lastname, u_genre=user_profile.u_genre, playlists=playlists) return redirect('/')
def api_get_playlists(self): with self.db.session_scope() as session: user = session.merge(web.get_current_user()) return [ self.playlist_to_dict(playlist) for playlist in user.playlists ]
def login(): should_redirect = False if web.get_current_user() is None: should_redirect = True manager = UserManager(web.get_db()) return dict(redirect='/user' if should_redirect else None, status=manager.api_login_user(request.json))
def api_reply(self, thread_id, params): params = v.validate(self.REPLY_SCHEMA, params) user_from = web.get_current_user() user_to = self._get_message_participant(thread_id, user_from) self.send_message(user_from, user_to, params['Content']) return True
def api_message_admin(self, params): # TODO: do we get the message from the API or do we set it ourselves? is it autogenerated? params = v.validate(self.MESSAGE_ADMIN_SCHEMA, params) self.message_admin(web.get_current_user(), params['Content']) return True
def api_get_messages(self, thread_id=None): if thread_id is None: return self.get_messages(web.get_current_user()) return self.get_message_thread(web.get_current_user(), thread_id)