Skip to content

Commit fcaa3b7

Browse files
DirtyRacer13374c0d3r
authored andcommitted
Spli api and web routes
1 parent 47143a2 commit fcaa3b7

File tree

3 files changed

+123
-93
lines changed

3 files changed

+123
-93
lines changed

namer/web/api.py

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
"""
2+
Defines the api routes of a Flask webserver for namer.
3+
"""
4+
from pathlib import Path
5+
from queue import Queue
6+
7+
from flask import Blueprint, jsonify, render_template, request
8+
from flask.wrappers import Response
9+
from loguru import logger
10+
11+
from namer.fileutils import make_command_relative_to, move_command_files
12+
from namer.types import NamerConfig
13+
from namer.web.actions import delete_file, get_failed_files, get_queue_size, get_queued_files, get_search_results, read_failed_log_file
14+
15+
16+
def get_web_api(config: NamerConfig, command_queue: Queue) -> Blueprint:
17+
"""
18+
Builds a blueprint for flask with passed in context, the NamerConfig.
19+
"""
20+
blueprint = Blueprint('api', __name__, static_url_path='/', static_folder='public', template_folder='templates')
21+
command_queue = command_queue
22+
23+
@blueprint.route('/api/v1/render', methods=['POST'])
24+
def render() -> Response:
25+
data = request.json
26+
27+
res = False
28+
if data is not None:
29+
template: str = data.get('template')
30+
client_data = data.get('data')
31+
active_page: str = data.get('url')
32+
active_page = active_page.lstrip('/') if active_page else active_page
33+
34+
template_file = f'render/{template}.html'
35+
response = render_template(template_file, data=client_data, config=config, active_page=active_page)
36+
37+
res = {
38+
'response': response,
39+
}
40+
41+
return jsonify(res)
42+
43+
@blueprint.route('/api/v1/get_files', methods=['POST'])
44+
def get_files() -> Response:
45+
data = get_failed_files(config)
46+
return jsonify(data)
47+
48+
@blueprint.route('/api/v1/get_queued', methods=['POST'])
49+
def get_queued() -> Response:
50+
data = get_queued_files(command_queue)
51+
return jsonify(data)
52+
53+
@blueprint.route('/api/v1/get_search', methods=['POST'])
54+
def get_search() -> Response:
55+
data = request.json
56+
57+
res = False
58+
if data is not None:
59+
res = get_search_results(data['query'], data['file'], config)
60+
61+
return jsonify(res)
62+
63+
@blueprint.route('/api/v1/get_queue', methods=['POST'])
64+
def get_queue() -> Response:
65+
res = get_queue_size(command_queue)
66+
67+
return jsonify(res)
68+
69+
@blueprint.route('/api/v1/rename', methods=['POST'])
70+
def rename() -> Response:
71+
data = request.json
72+
73+
res = False
74+
if data is not None:
75+
res = False
76+
movie = config.failed_dir / Path(data['file'])
77+
logger.error(f"moving movie {movie}")
78+
command = make_command_relative_to(movie, config.failed_dir, config=config)
79+
moved_command = move_command_files(command, config.work_dir)
80+
if moved_command is not None:
81+
moved_command.tpdb_id = data['scene_id']
82+
command_queue.put(moved_command) # Todo pass selection
83+
84+
return jsonify(res)
85+
86+
@blueprint.route('/api/v1/delete', methods=['POST'])
87+
def delete() -> Response:
88+
data = request.json
89+
90+
res = False
91+
if data is not None:
92+
res = delete_file(data['file'], config)
93+
94+
return jsonify(res)
95+
96+
@blueprint.route('/api/v1/read_failed_log', methods=['POST'])
97+
def read_failed_log() -> Response:
98+
data = request.json
99+
100+
res = False
101+
if data is not None:
102+
res = read_failed_log_file(data['file'], config)
103+
104+
return jsonify(res)
105+
106+
return blueprint

namer/web/routes.py

Lines changed: 4 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,20 @@
11
"""
2-
Defines the routes of a Flask webserver for namer.
2+
Defines the web routes of a Flask webserver for namer.
33
"""
4-
from pathlib import Path
54
from queue import Queue
65

7-
from flask import Blueprint, jsonify, redirect, render_template, request
6+
from flask import Blueprint, redirect, render_template
87
from flask.wrappers import Response
9-
from loguru import logger
108

11-
from namer.fileutils import make_command_relative_to, move_command_files
129
from namer.types import NamerConfig
13-
from namer.web.actions import delete_file, get_failed_files, get_queue_size, get_queued_files, get_search_results, read_failed_log_file
10+
from namer.web.actions import get_failed_files, get_queued_files
1411

1512

1613
def get_web_routes(config: NamerConfig, command_queue: Queue) -> Blueprint:
1714
"""
1815
Builds a blueprint for flask with passed in context, the NamerConfig.
1916
"""
20-
blueprint = Blueprint('/', __name__, static_url_path='/', static_folder='public', template_folder='templates')
17+
blueprint = Blueprint('web', __name__, static_url_path='/', static_folder='public', template_folder='templates')
2118
command_queue = command_queue
2219

2320
"""
@@ -52,87 +49,4 @@ def queue() -> str:
5249
data = get_queued_files(command_queue)
5350
return render_template('pages/queue.html', data=data, config=config)
5451

55-
@blueprint.route('/api/v1/render', methods=['POST'])
56-
def render() -> Response:
57-
data = request.json
58-
59-
res = False
60-
if data is not None:
61-
template: str = data.get('template')
62-
client_data = data.get('data')
63-
active_page: str = data.get('url')
64-
active_page = active_page.lstrip('/') if active_page else active_page
65-
66-
template_file = f'render/{template}.html'
67-
response = render_template(template_file, data=client_data, config=config, active_page=active_page)
68-
69-
res = {
70-
'response': response,
71-
}
72-
73-
return jsonify(res)
74-
75-
@blueprint.route('/api/v1/get_files', methods=['POST'])
76-
def get_files() -> Response:
77-
data = get_failed_files(config)
78-
return jsonify(data)
79-
80-
@blueprint.route('/api/v1/get_queued', methods=['POST'])
81-
def get_queued() -> Response:
82-
data = get_queued_files(command_queue)
83-
return jsonify(data)
84-
85-
@blueprint.route('/api/v1/get_search', methods=['POST'])
86-
def get_search() -> Response:
87-
data = request.json
88-
89-
res = False
90-
if data is not None:
91-
res = get_search_results(data['query'], data['file'], config)
92-
93-
return jsonify(res)
94-
95-
@blueprint.route('/api/v1/get_queue', methods=['POST'])
96-
def get_queue() -> Response:
97-
res = get_queue_size(command_queue)
98-
99-
return jsonify(res)
100-
101-
@blueprint.route('/api/v1/rename', methods=['POST'])
102-
def rename() -> Response:
103-
data = request.json
104-
105-
res = False
106-
if data is not None:
107-
res = False
108-
movie = config.failed_dir / Path(data['file'])
109-
logger.error(f"moving movie {movie}")
110-
command = make_command_relative_to(movie, config.failed_dir, config=config)
111-
moved_command = move_command_files(command, config.work_dir)
112-
if moved_command is not None:
113-
moved_command.tpdb_id = data['scene_id']
114-
command_queue.put(moved_command) # Todo pass selection
115-
116-
return jsonify(res)
117-
118-
@blueprint.route('/api/v1/delete', methods=['POST'])
119-
def delete() -> Response:
120-
data = request.json
121-
122-
res = False
123-
if data is not None:
124-
res = delete_file(data['file'], config)
125-
126-
return jsonify(res)
127-
128-
@blueprint.route('/api/v1/read_failed_log', methods=['POST'])
129-
def read_failed_log() -> Response:
130-
data = request.json
131-
132-
res = False
133-
if data is not None:
134-
res = read_failed_log_file(data['file'], config)
135-
136-
return jsonify(res)
137-
13852
return blueprint

namer/web/server.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from waitress.server import BaseWSGIServer, MultiSocketServer
1313

1414
from namer.types import NamerConfig
15+
from namer.web.api import get_web_api
1516
from namer.web.routes import get_web_routes
1617

1718
app = Flask(__name__)
@@ -36,16 +37,25 @@ def __init__(self, config: NamerConfig, command_queue: Queue):
3637
self.__config = config
3738
self.__command_queue = command_queue
3839
self.__add_mime_types()
40+
self.__register_blueprints()
3941
self.__make_server()
4042

4143
def __make_server(self):
42-
path = '/' if self.__config.web_root is None else self.__config.web_root
43-
blueprint = get_web_routes(self.__config, self.__command_queue)
44-
app.register_blueprint(blueprint, url_prefix=path, root_path=path)
4544
compress.init_app(app)
4645
self.__server = create_server(app, host=self.__config.host, port=self.__config.port)
4746
self.__thread = Thread(target=self.__run, daemon=True)
4847

48+
def __register_blueprints(self):
49+
path = '/' if self.__config.web_root is None else self.__config.web_root
50+
51+
blueprints = [
52+
get_web_routes(self.__config, self.__command_queue),
53+
get_web_api(self.__config, self.__command_queue),
54+
]
55+
56+
for blueprint in blueprints:
57+
app.register_blueprint(blueprint, url_prefix=path, root_path=path)
58+
4959
def __add_mime_types(self):
5060
app.config['JSONIFY_MIMETYPE'] = 'application/json; charset=utf-8'
5161

0 commit comments

Comments
 (0)