From 2b0b094cf1297501dc178ec12a9cb0642dc0bec9 Mon Sep 17 00:00:00 2001 From: aj Date: Sat, 3 Aug 2019 23:36:14 +0100 Subject: [PATCH] added cron job handler and admin run all function --- spotify/api/api.py | 76 ++++++++++++++++++++++++++++++-- src/js/Admin/Admin.js | 3 ++ src/js/Admin/Functions.js | 40 +++++++++++++++++ src/js/Admin/Lock.js | 3 +- src/js/Playlist/PlaylistsView.js | 17 ++++++- 5 files changed, 131 insertions(+), 8 deletions(-) create mode 100644 src/js/Admin/Functions.js diff --git a/spotify/api/api.py b/spotify/api/api.py index 18c5c11..bd9526b 100644 --- a/spotify/api/api.py +++ b/spotify/api/api.py @@ -267,10 +267,7 @@ def run_playlist(): if playlist_name: - data = u'{}'.format(playlist_name) - data = data.encode('utf-8') - - publisher.publish(run_playlist_topic_path, data=data, username=session['username']) + execute_playlist(session['username'], playlist_name) return jsonify({'message': 'execution requested', 'status': 'success'}), 200 @@ -279,3 +276,74 @@ def run_playlist(): else: return jsonify({'error': 'not logged in'}), 401 + + +@blueprint.route('/playlist/run/user', methods=['GET']) +def run_user(): + + if 'username' in session: + + if database.get_user_doc_ref(session['username']).get().to_dict()['type'] == 'admin': + user_name = request.args.get('username', session['username']) + else: + user_name = session['username'] + + execute_user(user_name) + + return jsonify({'message': 'executed user', 'status': 'success'}), 200 + + else: + return jsonify({'error': 'not logged in'}), 401 + + +@blueprint.route('/playlist/run/users', methods=['GET']) +def run_users(): + + if 'username' in session: + + if database.get_user_doc_ref(session['username']).get().to_dict()['type'] != 'admin': + return jsonify({'status': 'error', 'message': 'unauthorized'}), 401 + + execute_all_users() + + return jsonify({'message': 'executed all users', 'status': 'success'}), 200 + + else: + return jsonify({'error': 'not logged in'}), 401 + + +@blueprint.route('/playlist/run/users/cron', methods=['GET']) +def run_users_cron(): + + if request.headers.get('X-Appengine-Cron'): + execute_all_users() + return jsonify({'status': 'success'}), 200 + else: + return jsonify({'status': 'error', 'message': 'unauthorised'}), 401 + + +def execute_all_users(): + all_users = [i.to_dict() for i in db.collection(u'spotify_users').stream()] + + for iter_user in all_users: + if iter_user['spotify_linked'] and not iter_user['locked']: + execute_user(iter_user['username']) + + +def execute_user(username): + + playlists = [i.to_dict() for i in + database.get_user_playlists_collection(database.get_user_query_stream(username)[0].id).stream()] + + for iterate_playlist in playlists: + if len(iterate_playlist['parts']) > 0: + if iterate_playlist.get('playlist_id'): + execute_playlist(username, iterate_playlist['name']) + + +def execute_playlist(username, name): + + data = u'{}'.format(name) + data = data.encode('utf-8') + + publisher.publish(run_playlist_topic_path, data=data, username=username) diff --git a/src/js/Admin/Admin.js b/src/js/Admin/Admin.js index 2895655..97ce1bb 100644 --- a/src/js/Admin/Admin.js +++ b/src/js/Admin/Admin.js @@ -3,6 +3,7 @@ import { BrowserRouter as Router, Route, Link } from "react-router-dom"; const axios = require('axios'); import Lock from "./Lock.js"; +import Functions from "./Functions.js"; class Admin extends Component { render(){ @@ -10,9 +11,11 @@ class Admin extends Component {
+
); diff --git a/src/js/Admin/Functions.js b/src/js/Admin/Functions.js new file mode 100644 index 0000000..701a733 --- /dev/null +++ b/src/js/Admin/Functions.js @@ -0,0 +1,40 @@ +import React, { Component } from "react"; +const axios = require('axios'); + +class Functions extends Component { + + constructor(props){ + super(props); + + this.runAllUsers = this.runAllUsers.bind(this); + } + + runAllUsers(event){ + axios.get('/api/playlist/run/users') + .catch((error) => { + console.log(error); + }); + } + + render () { + return ( + + + + + + + + + + + +
+

admin functions

+
+ +
); + } +} + +export default Functions; \ No newline at end of file diff --git a/src/js/Admin/Lock.js b/src/js/Admin/Lock.js index 2a72155..794d8ef 100644 --- a/src/js/Admin/Lock.js +++ b/src/js/Admin/Lock.js @@ -22,7 +22,6 @@ class Lock extends Component { accounts: response.data.accounts, isLoading: false }) - console.log(response) }); } @@ -47,7 +46,7 @@ class Lock extends Component { -

+

account locks

diff --git a/src/js/Playlist/PlaylistsView.js b/src/js/Playlist/PlaylistsView.js index a271e62..809f15d 100644 --- a/src/js/Playlist/PlaylistsView.js +++ b/src/js/Playlist/PlaylistsView.js @@ -12,6 +12,7 @@ class PlaylistsView extends Component { this.getPlaylists(); this.handleRunPlaylist = this.handleRunPlaylist.bind(this); this.handleDeletePlaylist = this.handleDeletePlaylist.bind(this); + this.handleRunAll = this.handleRunAll.bind(this); } getPlaylists(){ @@ -27,7 +28,7 @@ class PlaylistsView extends Component { handleRunPlaylist(name, event){ axios.get('/api/playlist/run', {params: {name: name}}) - .catch((error) => { + .catch((error) => {this console.log(error); }); } @@ -41,12 +42,20 @@ class PlaylistsView extends Component { }); } + handleRunAll(event){ + axios.get('/api/playlist/run/user') + .catch((error) => { + console.log(error); + }); + } + render() { const table =
+ handleDeletePlaylist={this.handleDeletePlaylist} + handleRunAll={this.handleRunAll}/> ; const loadingMessage =

loading...

; @@ -63,6 +72,10 @@ function Table(props){ handleRunPlaylist={props.handleRunPlaylist} handleDeletePlaylist={props.handleDeletePlaylist} key={ playlist.name }/>) } + { props.playlists.length > 0 && + + + }
);