Input validation work and error handling
This commit is contained in:
1 parent
4e2520ee76
commit
554b7cc82c
20 files changed
+627
-96
No files matched your search
@@ -14,17 +14,18 @@
|
||||
<div class="alert alert-info text-center" role="alert">
|
||||
Configure a stream, content that is played from a location on the local network or the Internet.
|
||||
</div>
|
||||
<form action="/configure/stream" method="POST">
|
||||
{% if error %}<div class="alert alert-danger" role="alert">{{ error }}</div>{% endif %}
|
||||
<form action="{{ url_for('configure_stream.get_page') }}" method="POST">
|
||||
<div class="form-group">
|
||||
<label for="buttonName">Button Name</label>
|
||||
<input type="text" class="form-control" name="buttonName" id="buttonName" />
|
||||
<input type="text" class="form-control" name="buttonName" id="buttonName" value="{{ name }}" maxlength="120" required />
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label for="streamURL">Stream URL</label>
|
||||
<input type="text" class="form-control" name="streamURL" id="streamURL" />
|
||||
<input type="text" class="form-control" name="streamURL" id="streamURL" value="{{ stream_url }}" required />
|
||||
</div>
|
||||
<div class="form-check form-switch form-check-input-lg">
|
||||
<input type="checkbox" class="form-check-input" name="fullScreen" id="fullScreen" checked/>
|
||||
<input type="checkbox" class="form-check-input" name="fullScreen" id="fullScreen" {% if fullscreen %}checked{% endif %}/>
|
||||
<label for="fullScreen" class="form-check-label">Play the stream in Full Screen?</label>
|
||||
</div>
|
||||
<button id="submit" class="btn btn-primary form-control" type="submit">Submit</button>
|
||||
|
||||
@@ -1,33 +1,44 @@
|
||||
from flask import Blueprint, request, render_template, redirect
|
||||
import sqlite3
|
||||
|
||||
from flask import Blueprint, request, render_template, redirect, url_for, current_app
|
||||
from utilities.tools import get_db
|
||||
from utilities.validation import (ValidationError, ensure_name_available,
|
||||
validate_media_name, valid_stream_url, write_transaction)
|
||||
|
||||
|
||||
configure_stream_bp = Blueprint('configure_stream', __name__, url_prefix='/configure', template_folder='.')
|
||||
|
||||
|
||||
@configure_stream_bp.route('/stream', methods=['GET', 'POST'])
|
||||
def get_page():
|
||||
if request.method == "GET":
|
||||
return render_get()
|
||||
elif request.method == "POST":
|
||||
return upload()
|
||||
|
||||
|
||||
def render_get():
|
||||
return render_template('configure_stream.html'), 200
|
||||
|
||||
def upload():
|
||||
# TODO Input Validation
|
||||
|
||||
new_id = get_db().insert(
|
||||
"media",
|
||||
["media_name", "media_uri"],
|
||||
[request.form["buttonName"], request.form["streamURL"]]
|
||||
)
|
||||
|
||||
if request.form.get("fullScreen", False):
|
||||
get_db().insert(
|
||||
"media_playback_option",
|
||||
["media_id", "media_playback_option", "media_playback_option_weight"],
|
||||
[new_id, "-fs", 1]
|
||||
)
|
||||
|
||||
return redirect("/")
|
||||
name = ''
|
||||
stream_url = ''
|
||||
fullscreen = True
|
||||
error = None
|
||||
status = 200
|
||||
if request.method == 'POST':
|
||||
name = request.form.get('buttonName', '')
|
||||
stream_url = request.form.get('streamURL', '').strip(' ')
|
||||
fullscreen = request.form.get('fullScreen') in ('on', '1', 'true')
|
||||
try:
|
||||
name = validate_media_name(name)
|
||||
if not valid_stream_url(stream_url):
|
||||
raise ValidationError('Enter a valid network stream URL (HTTP, HTTPS, RTSP, RTMP, RTMPS, UDP, or TCP).')
|
||||
db = get_db()
|
||||
with write_transaction(db):
|
||||
ensure_name_available(db.db, name)
|
||||
new_id = db.insert('media', ['media_name', 'media_uri'], [name, stream_url])
|
||||
if fullscreen:
|
||||
db.insert('media_playback_option',
|
||||
['media_id', 'media_playback_option', 'media_playback_option_weight'],
|
||||
[new_id, '-fs', 1])
|
||||
return redirect(url_for('home.root_route'), code=303)
|
||||
except ValidationError as exception:
|
||||
error = str(exception)
|
||||
status = 400
|
||||
except sqlite3.Error:
|
||||
current_app.logger.exception('Unable to save stream')
|
||||
error = 'Unable to save the stream due to a database error. Please try again.'
|
||||
status = 503
|
||||
return render_template('configure_stream.html', name=name, stream_url=stream_url,
|
||||
fullscreen=fullscreen, error=error), status
|
||||
Reference in new issue
Block a user