added cron job handler and admin run all function

This commit is contained in:
aj 2019-08-03 23:36:14 +01:00
parent 79392fd34d
commit 2b0b094cf1
5 changed files with 131 additions and 8 deletions

View File

@ -267,10 +267,7 @@ def run_playlist():
if playlist_name: if playlist_name:
data = u'{}'.format(playlist_name) execute_playlist(session['username'], playlist_name)
data = data.encode('utf-8')
publisher.publish(run_playlist_topic_path, data=data, username=session['username'])
return jsonify({'message': 'execution requested', 'status': 'success'}), 200 return jsonify({'message': 'execution requested', 'status': 'success'}), 200
@ -279,3 +276,74 @@ def run_playlist():
else: else:
return jsonify({'error': 'not logged in'}), 401 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)

View File

@ -3,6 +3,7 @@ import { BrowserRouter as Router, Route, Link } from "react-router-dom";
const axios = require('axios'); const axios = require('axios');
import Lock from "./Lock.js"; import Lock from "./Lock.js";
import Functions from "./Functions.js";
class Admin extends Component { class Admin extends Component {
render(){ render(){
@ -10,9 +11,11 @@ class Admin extends Component {
<div> <div>
<ul className="navbar" style={{width: "100%"}}> <ul className="navbar" style={{width: "100%"}}>
<li><Link to={`${this.props.match.url}/lock`}>lock accounts</Link></li> <li><Link to={`${this.props.match.url}/lock`}>lock accounts</Link></li>
<li><Link to={`${this.props.match.url}/functions`}>functions</Link></li>
</ul> </ul>
<Route path={`${this.props.match.url}/lock`} component={Lock} /> <Route path={`${this.props.match.url}/lock`} component={Lock} />
<Route path={`${this.props.match.url}/functions`} component={Functions} />
</div> </div>
); );

40
src/js/Admin/Functions.js Normal file
View File

@ -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 (
<table className="app-table max-width">
<thead>
<tr>
<th>
<h1 className="text-no-select full-width center-text ui-text">admin functions</h1>
</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<button className="full-width button" onClick={this.runAllUsers}>run all users</button>
</td>
</tr>
</tbody>
</table>);
}
}
export default Functions;

View File

@ -22,7 +22,6 @@ class Lock extends Component {
accounts: response.data.accounts, accounts: response.data.accounts,
isLoading: false isLoading: false
}) })
console.log(response)
}); });
} }
@ -47,7 +46,7 @@ class Lock extends Component {
<thead> <thead>
<tr> <tr>
<th colSpan='3'> <th colSpan='3'>
<h1 className="text no-select"> <h1 className="text-no-select">
account locks account locks
</h1> </h1>
</th> </th>

View File

@ -12,6 +12,7 @@ class PlaylistsView extends Component {
this.getPlaylists(); this.getPlaylists();
this.handleRunPlaylist = this.handleRunPlaylist.bind(this); this.handleRunPlaylist = this.handleRunPlaylist.bind(this);
this.handleDeletePlaylist = this.handleDeletePlaylist.bind(this); this.handleDeletePlaylist = this.handleDeletePlaylist.bind(this);
this.handleRunAll = this.handleRunAll.bind(this);
} }
getPlaylists(){ getPlaylists(){
@ -27,7 +28,7 @@ class PlaylistsView extends Component {
handleRunPlaylist(name, event){ handleRunPlaylist(name, event){
axios.get('/api/playlist/run', {params: {name: name}}) axios.get('/api/playlist/run', {params: {name: name}})
.catch((error) => { .catch((error) => {this
console.log(error); 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() { render() {
const table = <div> const table = <div>
<Table playlists={this.state.playlists} <Table playlists={this.state.playlists}
handleRunPlaylist={this.handleRunPlaylist} handleRunPlaylist={this.handleRunPlaylist}
handleDeletePlaylist={this.handleDeletePlaylist}/> handleDeletePlaylist={this.handleDeletePlaylist}
handleRunAll={this.handleRunAll}/>
</div>; </div>;
const loadingMessage = <p className="center-text">loading...</p>; const loadingMessage = <p className="center-text">loading...</p>;
@ -63,6 +72,10 @@ function Table(props){
handleRunPlaylist={props.handleRunPlaylist} handleRunPlaylist={props.handleRunPlaylist}
handleDeletePlaylist={props.handleDeletePlaylist} handleDeletePlaylist={props.handleDeletePlaylist}
key={ playlist.name }/>) } key={ playlist.name }/>) }
{ props.playlists.length > 0 &&
<tr>
<td colSpan="3"><button className="full-width button" onClick={props.handleRunAll}>run all</button></td>
</tr> }
</tbody> </tbody>
</table> </table>
); );