Plugin and Restart changes

nightly
Andrew 4 years ago
parent 9ab2b08adf
commit b012174e65
  1. 89
      stashr/api.py
  2. 15
      stashr/server.py
  3. 56
      stashr/templates/settings_page_plugins.html
  4. 19
      stashr/templates/settings_page_tasks.html
  5. 7
      stashr/utils.py

@ -56,7 +56,7 @@ from operator import itemgetter, attrgetter
from validate import Validator
""" --- STASHR CORE IMPORTS --- """
from stashr import log, database, utils, paths, folders, naming, forms, tasks
from stashr import log, database, utils, paths, folders, naming, forms, tasks, server
from stashr.config import stashrconfig
from stashr.comicvine import cv
@ -2261,6 +2261,93 @@ def api_put_directories_edit(scrape_id):
return jsonify(create_json_return('200'))
""" --- PLUGIN UPLOAD/INSTALL --- """
@api.route('/plugins/upload', methods=['POST'])
def api_post_upload_plugin():
"""To Update Later
This is using docstrings for specifications.
---
tags:
- plugins
"""
user = current_user
if not user.is_authenticated:
if not request.json:
return jsonify(create_json_return('400'))
if "api_key" not in request.json:
return jsonify(create_json_return('400'))
if request.json['api_key'] == "":
return jsonify(create_json_return('100'))
user = database.session \
.query(database.Users) \
.filter(database.Users.api_key == request.json['api_key']) \
.first()
if user is None:
return jsonify(create_json_return('100'))
if user.role != 'admin':
return jsonify(create_json_return('401'))
if 'file' not in request.files:
return jsonify(create_json_return('400'))
# DO THINGS HERE
file = request.files['file']
file_path =os.path.join(
folders.StashrFolders().temp_folder(),
file.filename
)
file.save(file_path)
utils.install_plugin(file_path)
return jsonify(create_json_return('200'))
""" --- RESTART SERVER --- """
@api.route('/restart', methods=['POST'])
def restart_server():
"""To Update Later
This is using docstrings for specifications.
---
tags:
- plugins
"""
user = current_user
if not user.is_authenticated:
if not request.json:
return jsonify(create_json_return('400'))
if "api_key" not in request.json:
return jsonify(create_json_return('400'))
if request.json['api_key'] == "":
return jsonify(create_json_return('100'))
user = database.session \
.query(database.Users) \
.filter(database.Users.api_key == request.json['api_key']) \
.first()
if user is None:
return jsonify(create_json_return('100'))
if user.role != 'admin':
return jsonify(create_json_return('401'))
server.server.restart_server()
return create_json_return('200')
""" --- API WRAPPER --- """
"""---------------------

@ -86,26 +86,23 @@ class Server:
app.logger.debug('Unknown Error while starting gevent')
def start_server(self):
app.logger.debug('STARTING SERVER')
app.logger.info('STARTING SERVER')
self.define_wsgi()
self.wsgiserver.serve_forever()
if self.restart:
app.logger.info('Restarting Server')
app.logger.info('RESTARTING SERVER)
try:
subprocess.check_call([sys.executable, 'start.py'])
subprocess.call([sys.executable, 'start.py'], close_fds=True)
except subprocess.CalledProcessError as e:
print(e)
def stop_server(self):
app.logger.debug('STOPPING SERVER')
app.logger.info('STOPPING SERVER')
self.wsgiserver.stop()
def restart_server(self, ignored_signum, ignored_frame):
def restart_server(self, ignored_signum=None, ignored_frame=None):
self.restart = True
app.logger.debug('Restarting Server')
if self.wsgiserver is not None :
self.wsgiserver.stop()
self.wsgiserver.close()
server = Server()

@ -8,12 +8,64 @@
<div id="app">
<plugins ref="plugins" v-bind:plugins="plugins"></plugins>
<modals></modals>
</div>
{% endblock %}
{% block script %}
Vue.component('modals', {
props: [],
template: `
<div>
<div class="modal" id="modalUpload" tabindex="-1" role="dialog" aria-labelledby="notesModal" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="notesModalTitle">
Upload Plugin
</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<form>
<div class="input-group mb-3">
<input id="pluginupload" type="file" accept=".zip" name="plugin" @change='uploadFile' required="required" class="form-control">
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
`,
methods: {
uploadFile() {
let formData = new FormData();
var pluginfile = document.querySelector('#pluginupload');
formData.append("file", pluginfile.files[0]);
bootstrap.Modal.getInstance(document.getElementById('modalUpload')).hide();
stashrToast('Uploading Plugin', 'info');
axios.post('{{ url_for('api.api_post_upload_plugin') }}', formData, {
headers: {
'Content-Type': 'multipart/form-data'
}
})
.then(res => {
if(res.data.status_code == 200) {
stashrToast('Plugin Uploaded', 'success')
} else {
stashrToast(res.data.message, 'error')
}
})
},
},
delimiters: ["[[","]]"]
})
Vue.component('plugin',{
props:['plugin'],
template: `
@ -57,7 +109,7 @@ Vue.component('plugins',{
<h2>Plugins</h2>
</div>
<div class="col-sm-12 col-md-6 text-center text-md-end">
<button type="button" class="btn btn-info" data-bs-toggle="modal" data-bs-target="#settingsModal">Install Plugin</button>
<button type="button" class="btn btn-info" data-bs-toggle="modal" data-bs-target="#modalUpload">Install Plugin</button>
</div>
</div>
<hr />
@ -104,7 +156,7 @@ var app = new Vue({
.then(res => {
this.plugins = res.data.results
})
}
},
},
delimiters: ["[[","]]"]
})

@ -1,5 +1,9 @@
{% extends "settings_page.html" %}
{% block header_script_files %}
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
{% endblock %}
{% block settings_pane %}
<div class="row r-10 m-2">
<div class="col col-12">
@ -28,7 +32,7 @@
<th scope="row">Clean Database</th><td><i class="fas fa-play"></i></td>
</tr>
<tr>
<th scope="row">Restart Server</th><td><i class="fas fa-play"></i></td>
<th scope="row">Restart Server</th><td><a onclick="restartServer()"><i class="fas fa-play"></i></a></td>
</tr>
{{ emit_tep('settings_page_tasks_task') }}
</tbody>
@ -36,4 +40,17 @@
</div>
</div>
</div>
{% endblock %}
{% block script %}
function restartServer() {
axios.post('{{ url_for('api.restart_server') }}')
.then(res=>{
console.log(res)
stashrToast('Restarting Server', 'info')
})
.catch()
}
{% endblock %}

@ -1082,6 +1082,7 @@ def install_plugin(filepath):
if install['code'] == 0:
logger.info(f'Plugin {filepath} installed')
else:
print(install)
logger.warning(f'Error occurred installing {filepath}')
@ -1095,5 +1096,11 @@ def uninstall_plugin(package_name):
logger.warning(f'Error uninstalling {package_name}')
def enable_plugin(plugin):
stashr.pm.enable_plugin(plugin)
def disable_plugin(plugin):
stashr.pm.disable_plugin(plugin)
def install_package(package):
subprocess.check_call([sys.executable, '-m', 'pip', 'install', package])
Loading…
Cancel
Save