update song edit page

This commit is contained in:
Bui 2020-03-15 16:16:18 +00:00
parent 38f1384f27
commit 61e521dda3
4 changed files with 104 additions and 21 deletions

47
app.py
View File

@ -7,7 +7,7 @@ import schema
import os import os
from functools import wraps from functools import wraps
from flask import Flask, g, jsonify, render_template, request, abort, redirect, session from flask import Flask, g, jsonify, render_template, request, abort, redirect, session, flash
from flask_caching import Cache from flask_caching import Cache
from flask_session import Session from flask_session import Session
from ffmpy import FFmpeg from ffmpy import FFmpeg
@ -135,9 +135,52 @@ def route_admin_songs_id(id):
categories = list(db.categories.find({})) categories = list(db.categories.find({}))
song_skins = list(db.song_skins.find({})) song_skins = list(db.song_skins.find({}))
makers = list(db.makers.find({}))
user = db.users.find_one({'username': session['username']})
return render_template('admin_song_detail.html', return render_template('admin_song_detail.html',
song=song, categories=categories, song_skins=song_skins) song=song, categories=categories, song_skins=song_skins, makers=makers, admin=user)
@app.route('/admin/songs/<int:id>', methods=['POST'])
@admin_required
def route_admin_songs_id_post(id):
song = db.songs.find_one({'id': id})
if not song:
return abort(404)
user = db.users.find_one({'username': session['username']})
user_level = user['user_level']
output = {'title_lang': {}, 'subtitle_lang': {}, 'courses': {}}
if user_level >= 100:
output['enabled'] = True if request.form.get('enabled') else False
output['title'] = request.form.get('title') or None
output['subtitle'] = request.form.get('subtitle') or None
for lang in ['ja', 'en', 'cn', 'tw', 'ko']:
output['title_lang'][lang] = request.form.get('title_%s' % lang) or None
output['subtitle_lang'][lang] = request.form.get('subtitle_%s' % lang) or None
for course in ['easy', 'normal', 'hard', 'oni', 'ura']:
if request.form.get('course_%s' % course):
output['courses'][course] = {'stars': int(request.form.get('course_%s' % course)),
'branch': True if request.form.get('branch_%s' % course) else False}
else:
output['courses'][course] = None
output['category_id'] = int(request.form.get('category_id'))
output['type'] = request.form.get('type')
output['offset'] = float(request.form.get('offset')) or None
output['skin_id'] = int(request.form.get('skin_id')) or None
output['preview'] = float(request.form.get('preview')) or None
output['volume'] = float(request.form.get('volume')) or None
output['maker_id'] = int(request.form.get('maker_id')) or None
db.songs.update_one({'id': id}, {'$set': output})
flash('Changes saved.')
return redirect('/admin/songs/%s' % id)
@app.route('/api/preview') @app.route('/api/preview')

View File

@ -123,3 +123,10 @@ h1 small {
margin-right: 3px; margin-right: 3px;
margin-left: 5px; margin-left: 5px;
} }
.message {
background: #2c862f;
padding: 15px;
margin-bottom: 10px;
color: white;
}

View File

@ -1,43 +1,46 @@
{% extends 'admin.html' %} {% extends 'admin.html' %}
{% block content %} {% block content %}
<h1>{{ song.title }} <small>(ID: {{ song.id }})</small></h1> <h1>{{ song.title }} <small>(ID: {{ song.id }})</small></h1>
{% for message in get_flashed_messages() %}
<div class="message">{{ message }}</div>
{% endfor %}
<div class="song-form"> <div class="song-form">
<form method="post"> <form method="post">
<div class="form-field"> <div class="form-field">
<span class="checkbox"><input type="checkbox" name="enabled" id="enabled"{% if song.enabled %} checked{% endif %}><label for="enabled"> Enabled</label></span> <span class="checkbox"><input type="checkbox" name="enabled" id="enabled"{% if song.enabled %} checked{% endif %}{% if admin.user_level < 100 %} disabled {% endif %}><label for="enabled"> Enabled</label></span>
</div> </div>
<div class="form-field"> <div class="form-field">
<p>Title</p> <p>Title</p>
<label for="title">Original</label> <label for="title">Original</label>
<input type="text" id="title" value="{{song.title}}" name="title"> <input type="text" id="title" value="{{song.title or ''}}" name="title" required>
<label for="title_ja">Japanese</label> <label for="title_ja">Japanese</label>
<input type="text" id="title_ja" value="{{song.title_lang.ja}}" name="title_ja"> <input type="text" id="title_ja" value="{{song.title_lang.ja or ''}}" name="title_ja">
<label for="title_en">English</label> <label for="title_en">English</label>
<input type="text" id="title_en" value="{{song.title_lang.en}}" name="title_en"> <input type="text" id="title_en" value="{{song.title_lang.en or ''}}" name="title_en">
<label for="title_cn">Chinese (Simplified)</label> <label for="title_cn">Chinese (Simplified)</label>
<input type="text" id="title_cn" value="{{song.title_lang.cn}}" name="title_cn"> <input type="text" id="title_cn" value="{{song.title_lang.cn or ''}}" name="title_cn">
<label for="title_tw">Chinese (Traditional)</label> <label for="title_tw">Chinese (Traditional)</label>
<input type="text" id="title_tw" value="{{song.title_lang.tw}}" name="title_tw"> <input type="text" id="title_tw" value="{{song.title_lang.tw or ''}}" name="title_tw">
<label for="title_ko">Korean</label> <label for="title_ko">Korean</label>
<input type="text" id="title_ko" value="{{song.title_lang.ko}}" name="title_ko"> <input type="text" id="title_ko" value="{{song.title_lang.ko or ''}}" name="title_ko">
</div> </div>
<div class="form-field"> <div class="form-field">
<p>Subtitle</p> <p>Subtitle</p>
<label for="subtitle">Original</label> <label for="subtitle">Original</label>
<input type="text" id="subtitle" value="{{song.subtitle}}" name="subtitle"> <input type="text" id="subtitle" value="{{song.subtitle or ''}}" name="subtitle">
<label for="subtitle_ja">Japanese</label> <label for="subtitle_ja">Japanese</label>
<input type="text" id="subtitle_ja" value="{{song.subtitle_lang.ja}}" name="subtitle_ja"> <input type="text" id="subtitle_ja" value="{{song.subtitle_lang.ja or ''}}" name="subtitle_ja">
<label for="subtitle_en">English</label> <label for="subtitle_en">English</label>
<input type="text" id="subtitle_en" value="{{song.subtitle_lang.en}}" name="subtitle_en"> <input type="text" id="subtitle_en" value="{{song.subtitle_lang.en or ''}}" name="subtitle_en">
<label for="subtitle_cn">Chinese (Simplified)</label> <label for="subtitle_cn">Chinese (Simplified)</label>
<input type="text" id="subtitle_cn" value="{{song.subtitle_lang.cn}}" name="subtitle_cn"> <input type="text" id="subtitle_cn" value="{{song.subtitle_lang.cn or ''}}" name="subtitle_cn">
<label for="subtitle_tw">Chinese (Traditional)</label> <label for="subtitle_tw">Chinese (Traditional)</label>
<input type="text" id="subtitle_tw" value="{{song.subtitle_lang.tw}}" name="subtitle_tw"> <input type="text" id="subtitle_tw" value="{{song.subtitle_lang.tw or ''}}" name="subtitle_tw">
<label for="subtitle_ko">Korean</label> <label for="subtitle_ko">Korean</label>
<input type="text" id="subtitle_ko" value="{{song.subtitle_lang.ko}}" name="subtitle_ko"> <input type="text" id="subtitle_ko" value="{{song.subtitle_lang.ko or ''}}" name="subtitle_ko">
</div> </div>
<div class="form-field"> <div class="form-field">
@ -78,7 +81,37 @@
<div class="form-field"> <div class="form-field">
<p><label for="offset">Offset</label></p> <p><label for="offset">Offset</label></p>
<input type="text" id="offset" value="{{song.offset}}" name="offset"> <input type="text" id="offset" value="{{song.offset or '0'}}" name="offset" required>
</div>
<div class="form-field">
<p><label for="skin_id">Skin</label></p>
<select name="skin_id" id="skin_id">
<option value="0">(none)</option>
{% for skin in song_skins %}
<option value="{{ skin.id }}"{% if song.skin_id == skin.id %} selected{% endif %}>{{ skin.name }}</option>
{% endfor %}
</select>
</div>
<div class="form-field">
<p><label for="preview">Preview</label></p>
<input type="text" id="preview" value="{{song.preview or '0'}}" name="preview" required>
</div>
<div class="form-field">
<p><label for="volume">Volume</label></p>
<input type="text" id="volume" value="{{song.volume or '1.0'}}" name="volume" required>
</div>
<div class="form-field">
<p><label for="maker_id">Maker</label></p>
<select name="maker_id" id="maker_id">
<option value="0">(none)</option>
{% for maker in makers %}
<option value="{{ maker.id }}"{% if song.maker_id == maker.id %} selected{% endif %}>{{ maker.name }}</option>
{% endfor %}
</select>
</div> </div>
<button type="submit">Save</button> <button type="submit">Save</button>

View File

@ -5,7 +5,7 @@ import sqlite3
from pymongo import MongoClient from pymongo import MongoClient
client = MongoClient() client = MongoClient()
#client.drop_database('taiko') client.drop_database('taiko')
db = client.taiko db = client.taiko
sqdb = sqlite3.connect('taiko.db') sqdb = sqlite3.connect('taiko.db')
sqdb.row_factory = sqlite3.Row sqdb.row_factory = sqlite3.Row
@ -26,10 +26,10 @@ def migrate_songs():
'enabled': True if row['enabled'] else False, 'enabled': True if row['enabled'] else False,
'category_id': row['category'], 'category_id': row['category'],
'type': row['type'], 'type': row['type'],
'offset': row['offset'], 'offset': row['offset'] or 0,
'skin_id': row['skin_id'], 'skin_id': row['skin_id'],
'preview': row['preview'], 'preview': row['preview'] or 0,
'volume': row['volume'], 'volume': row['volume'] or 1.0,
'maker_id': row['maker_id'], 'maker_id': row['maker_id'],
'hash': row['hash'], 'hash': row['hash'],
'order': row['id'] 'order': row['id']