Added translation methods to JS and Python

Modified "Create"-form
This commit is contained in:
sqozz 2015-02-15 03:48:39 +01:00
parent dc82260d08
commit 4ba2e1168f
9 changed files with 202 additions and 147 deletions

2
.gitignore vendored
View File

@ -1,3 +1,5 @@
*.swp *.swp
*.swo *.swo
backup backup
uploadTest
torrentFiles/*

View File

@ -1,23 +1,60 @@
#!/usr/bin/python3 #!/usr/bin/python3
#/* vim:set ts=2 set noexpandtab */ #/* vim:set ts=2 set noexpandtab */
import json
from flask import Flask, render_template, url_for, request from flask import Flask, render_template, url_for, request
from werkzeug import secure_filename
app = Flask(__name__) app = Flask(__name__)
strings = None
settings = None
@app.route("/") @app.route("/")
def index(): def index():
return render_template("search.html") return render_template("search.html", language="english", categories=settings["categories"], strings=strings)
@app.route("/categorys") @app.route("/categories")
def categorys(): def categorys():
return render_template("categorys.html") return render_template("categories.html", categories=settings["categories"])
@app.route("/create") @app.route("/create", methods=['GET','POST'])
def create(): def create():
return render_template("create.html") if request.method == "GET":
return render_template("create.html", language="english", categories=settings["categories"], strings=strings, errors=None)
elif request.method == "POST":
uploadfile = request.files["torrentFile"]
filename = secure_filename(uploadfile.filename)
# TODO: Create unique filename so that existing files doesn't get overwritten
uploadfile.save("torrentFiles/" + filename)
# TODO: Process inputdate from the form and save it to the (until now) non-existing DB
print(request.form["name"])
return "\o/"
@app.route("/search", methods=['GET']) @app.route("/search", methods=['GET'])
def search(): def search():
return render_template("result.html", results=request.args.get("q", "")) return render_template("result.html", results=request.args.get("q", ""))
def init():
global strings
global settings
with open("strings.json") as stringsJson:
strings = json.load(stringsJson)
with open("settings.json") as settingsJson:
settings = json.load(settingsJson)
def getLocalString(language, descriptor):
global strings
if language in strings.keys():
if descriptor in strings[language].keys():
return strings[language][descriptor]
else:
return descriptor
else:
return descriptor
if __name__ == "__main__": if __name__ == "__main__":
app.run(debug=True) init()
app.jinja_env.globals.update(getLocalString=getLocalString)
app.jinja_env.globals.update(json=json)
app.jinja_env.globals.update(sorted=sorted)
app.run(debug=True)

View File

@ -7,28 +7,28 @@ window.onload = function () {
customizeUploadButton() customizeUploadButton()
} }
function fillDropdownByValue(value, dropdownToFill) { //function fillDropdownByValue(value, dropdownToFill) {
dropdownToFill.getElementsByClassName("text")[0].innerHTML = "Subcategory" // dropdownToFill.getElementsByClassName("text")[0].innerHTML = "Subcategory"
var dropdownToFillUl = dropdownToFill.getElementsByClassName("dropdown-menu")[0] // var dropdownToFillUl = dropdownToFill.getElementsByClassName("dropdown-menu")[0]
dropdownToFillUl.innerHTML = "" // dropdownToFillUl.innerHTML = ""
//
subCategory = getDescriptorByLocalString("english", value) // subCategory = getDescriptorByLocalString("english", value)
catList = document.getElementById("subcategory_list") // catList = document.getElementById("subcategory_list")
catList.innerHTML = "" // catList.innerHTML = ""
categories = global_categories[subCategory].sort() // categories = global_categories[subCategory].sort()
for(var key in categories) { // for(var key in categories) {
var newEntry = document.createElement("li") // var newEntry = document.createElement("li")
var newEntryLink = document.createElement("a") // var newEntryLink = document.createElement("a")
newEntry.setAttribute("role", "presentation") // newEntry.setAttribute("role", "presentation")
newEntryLink.setAttribute("role", "menuitem") // newEntryLink.setAttribute("role", "menuitem")
newEntryLink.tabIndex = -1 // newEntryLink.tabIndex = -1
newEntryLink.href = "#" // newEntryLink.href = "#"
newEntryLink.innerHTML = getLocalString("english", categories[key]) // newEntryLink.innerHTML = getLocalString("english", categories[key])
newEntryLink.onclick = function(){ setDropdownButtonText(this.innerHTML, this.parentElement.parentElement.parentElement) } // newEntryLink.onclick = function(){ setDropdownButtonText(this.innerHTML, this.parentElement.parentElement.parentElement) }
newEntry.appendChild(newEntryLink) // newEntry.appendChild(newEntryLink)
dropdownToFillUl.appendChild(newEntry) // dropdownToFillUl.appendChild(newEntry)
} // }
} //}
function setDropdownButtonText(text, dropdownButton) { function setDropdownButtonText(text, dropdownButton) {
var dropdownTextSpan = dropdownButton.getElementsByClassName("text")[0] var dropdownTextSpan = dropdownButton.getElementsByClassName("text")[0]
@ -38,51 +38,38 @@ function setDropdownButtonText(text, dropdownButton) {
function sortCategories(categories) { function sortCategories(categories) {
var sortedCategories = [] var sortedCategories = []
for(var key in categories) { for(var key in categories) {
sortedCategories.push(key) sortedCategories.push(categories[key])
} }
sortedCategories.sort() sortedCategories.sort()
return sortedCategories return sortedCategories
} }
// Fill a defined dropdown with values.
// These values will be generated out of the categories.json
function fillDropdownByValue(value, dropdownToFill) { function fillDropdownByValue(value, dropdownToFill) {
dropdownToFill.getElementsByTagName("button")[0].disabled = false dropdownToFill.getElementsByTagName("button")[0].disabled = false
dropdownToFill.getElementsByClassName("text")[0].innerHTML = "Subcategorie" dropdownToFill.getElementsByClassName("text")[0].innerHTML = "Subcategorie"
var dropdownToFillUl = dropdownToFill.getElementsByClassName("dropdown-menu")[0] var dropdownToFillUl = dropdownToFill.getElementsByClassName("dropdown-menu")[0]
dropdownToFillUl.innerHTML = "" dropdownToFillUl.innerHTML = ""
switch(value) { valueDescriptor = getDescriptorByLocalString("english", value)
case "Action": subcategories = global_categories[valueDescriptor]
for(i = 0; i < 10; i++) { subcategories = sortCategories(subcategories)
// <li role="presentation"><a role="menuitem" tabindex="-1" href="#">Action</a></li> for(subcategoryIndex in subcategories) {
var newEntry = document.createElement("li") subcategoryLocalString = getLocalString("english", subcategories[subcategoryIndex])
var newEntryLink = document.createElement("a") var newEntry = document.createElement("li")
newEntry.setAttribute("role", "presentation") var newEntryLink = document.createElement("a")
newEntryLink.setAttribute("role", "menuitem") newEntry.setAttribute("role", "presentation")
newEntryLink.tabIndex = -1 newEntryLink.setAttribute("role", "menuitem")
newEntryLink.href = "#" newEntryLink.tabIndex = -1
newEntryLink.innerHTML = "foo" + i newEntryLink.href = "#"
newEntryLink.onclick = function(){ setDropdownButtonText(this.innerHTML, this.parentElement.parentElement.parentElement) } newEntryLink.innerHTML = subcategoryLocalString
newEntry.appendChild(newEntryLink) newEntryLink.onclick = function(){ setDropdownButtonText(this.innerHTML, this.parentElement.parentElement.parentElement) }
dropdownToFillUl.appendChild(newEntry) newEntry.appendChild(newEntryLink)
} dropdownToFillUl.appendChild(newEntry)
break;
case "Another action":
for(i = 0; i < 10; i++) {
// <li role="presentation"><a role="menuitem" tabindex="-1" href="#">Action</a></li>
var newEntry = document.createElement("li")
var newEntryLink = document.createElement("a")
newEntry.setAttribute("role", "presentation")
newEntryLink.setAttribute("role", "menuitem")
newEntryLink.tabIndex = -1
newEntryLink.href = "#"
newEntryLink.innerHTML = "bar" + i
newEntryLink.onclick = function(){ setDropdownButtonText(this.innerHTML, this.parentElement.parentElement.parentElement) }
newEntry.appendChild(newEntryLink)
dropdownToFillUl.appendChild(newEntry)
}
break;
} }
} }
// Hides the default browser-upload-form and replaces it by an button
function customizeUploadButton() { function customizeUploadButton() {
$(".tfile").before('<button id="button-file" type="button" class="btn btn-default"><span class="text">Upload...</span><span class="glyphicon glyphicon-open-file" aria-hidden="true"></span></button>'); $(".tfile").before('<button id="button-file" type="button" class="btn btn-default"><span class="text">Upload...</span><span class="glyphicon glyphicon-open-file" aria-hidden="true"></span></button>');
$(".tfile").hide(); $(".tfile").hide();
@ -91,19 +78,31 @@ function customizeUploadButton() {
}); });
} }
// This sets the Uploadbutton to the filename of the uploaded file
function setButtonToFilename(event) { function setButtonToFilename(event) {
$("input[name='Datei']").each(function() { $("input[name='torrentFile']").each(function() {
var fileName = $(this).val().split('/').pop().split('\\').pop(); var fileName = $(this).val().split('/').pop().split('\\').pop();
console.log(event["target"]); console.log(event["target"]);
targetInput = event["target"] targetInput = event["target"]
button = targetInput.previousSibling.getElementsByClassName("text")[0] button = targetInput.previousSibling.getElementsByClassName("text")[0]
button.innerHTML = fileName button.innerHTML = chunk(fileName, 40)
}); });
} }
// Sets the text of a given dropdown-button to a given value
function setDropdownButtonText(text, dropdownButton) { function setDropdownButtonText(text, dropdownButton) {
var dropdownTextSpan = dropdownButton.getElementsByClassName("text")[0] var dropdownTextSpan = dropdownButton.getElementsByClassName("text")[0]
dropdownTextSpan.innerHTML = text dropdownTextSpan.innerHTML = text
} }
customizeUploadButton() function chunk(string, n) {
var ret = "";
for(var i=0, len=string.length; i < len; i += n) {
if(i==0) {
ret = string.substr(i, n)
} else {
ret += "<br/>" + string.substr(i, n)
}
}
return ret
};

View File

@ -35,6 +35,7 @@
"create" : "Create", "create" : "Create",
"torrent_file" : "Torrent file", "torrent_file" : "Torrent file",
"name" : "Name", "name" : "Name",
"create_new_torrent" : "Create new torrent" "create_new_torrent" : "Create new torrent",
"description" : "Description"
} }
} }

View File

@ -0,0 +1,9 @@
<!--
vim: ts=2 noexpandtab
-->
{% extends "index.html" %}
{% block title %}{{ super() }} - Categories{% endblock%}
{% set active_page = "categories" %}
{% block content %}
<p>CATEGORIES</p>
{% endblock content%}

View File

@ -1,9 +0,0 @@
<!--
vim: ts=2 noexpandtab
-->
{% extends "index.html" %}
{% block title %}{{ super() }} - Categorys{% endblock%}
{% set active_page = "categorys" %}
{% block content %}
<p>CATEGORYS</p>
{% endblock content%}

View File

@ -2,91 +2,102 @@
vim: ts=2 noexpandtab vim: ts=2 noexpandtab
--> -->
{% extends "index.html" %} {% extends "index.html" %}
{% block title %}{{ super() }} - Create{% endblock%} {% block title %}{{ super() }} - {{ getLocalString(language, "create") }}{% endblock%}
{% set active_page = "create" %} {% set active_page = "create" %}
{% block content %} {% block content %}
<link href="{{ url_for("static", filename="css/create.css") }}" rel="stylesheet"> <link href="{{ url_for("static", filename="css/create.css") }}" rel="stylesheet">
<script src="{{ url_for("static", filename="js/create.js") }}"></script> <script src="{{ url_for("static", filename="js/create.js") }}"></script>
<div> <div>
<h2 class="headline">Create new</h2> <h2 class="headline">{{ getLocalString(language, "create_new_torrent") }}</h2>
<!--
<div class="alert alert-danger alert-dismissible" role="alert"> <div class="alert alert-danger alert-dismissible" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button> <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
<strong>Warning!</strong> Better check yourself, you're not looking too good. <strong>Warning!</strong> Better check yourself, you're not looking too good.
</div> </div>
-->
<div class="row"> <form action="/create" method="post" enctype="multipart/form-data">
<div class="col-md-4 labelColumn"> <div class="row">
<strong>Torrent file</strong> <div class="col-md-4 labelColumn">
<strong>{{ getLocalString(language, "torrent_file") }}</strong>
</div>
<div class="col-md-8">
<input name="torrentFile" class="tfile" type="file" size="50" maxlength="100000" accept="text/*" onchange="setButtonToFilename(event)">
</div>
</div> </div>
<div class="col-md-8"> <div class="row">
<input name="Datei" class="tfile" type="file" size="50" maxlength="100000" accept="text/*" onchange="setButtonToFilename(event)"> <div class="col-md-4 labelColumn">
</div> <strong>{{ getLocalString(language, "category") }}</strong>
</div> </div>
<div class="row"> <div class="col-md-8">
<div class="col-md-4 labelColumn"> <div class="row">
<strong>Name</strong> <div class="col-md-6">
</div> <div class="dropdown">
<div class="col-md-8"> <button class="btn btn-default dropdown-toggle" type="button" id="dropdownMenu1" data-toggle="dropdown" aria-expanded="true">
<input type="text" class="form-control" placeholder="e.g. Attack of the Killer Tomatoes" aria-describedby="basic-addon1"> <span class="text">{{ getLocalString(language, "category") }}</span>
</div> <span class="caret"></span>
</div> </button>
<div class="row"> <ul class="dropdown-menu" role="menu" aria-labelledby="dropdownMenu1">
<div class="col-md-4 labelColumn"> {% for category in sorted(categories.keys()) %}
<strong>Categorie</strong> <li role="presentation"><a role="menuitem" tabindex="-1" href="#">{{ getLocalString(language, category) }}</a></li>
</div> {% endfor %}
<div class="col-md-8"> </ul>
<div class="row"> </div>
<div class="col-md-6">
<div class="dropdown">
<button class="btn btn-default dropdown-toggle" type="button" id="dropdownMenu1" data-toggle="dropdown" aria-expanded="true">
<span class="text">Categorie</span>
<span class="caret"></span>
</button>
<ul class="dropdown-menu" role="menu" aria-labelledby="dropdownMenu1">
<li role="presentation"><a role="menuitem" tabindex="-1" href="#">Action</a></li>
<li role="presentation"><a role="menuitem" tabindex="-1" href="#">Another action</a></li>
<li role="presentation"><a role="menuitem" tabindex="-1" href="#">Something else here</a></li>
<li role="presentation"><a role="menuitem" tabindex="-1" href="#">Separated link</a></li>
</ul>
</div> </div>
</div> <div class="col-md-6">
<div class="col-md-6"> <div class="dropdown">
<div class="dropdown"> <button class="btn btn-default dropdown-toggle" type="button" id="dropdownMenu1" data-toggle="dropdown" aria-expanded="true" disabled>
<button class="btn btn-default dropdown-toggle" type="button" id="dropdownMenu1" data-toggle="dropdown" aria-expanded="true" disabled> <span class="text">{{ getLocalString(language, "subcategory") }}</span>
<span class="text">Subcategorie</span> <span class="caret"></span>
<span class="caret"></span> </button>
</button> <ul class="dropdown-menu" role="menu" aria-labelledby="dropdownMenu1">
<ul class="dropdown-menu" role="menu" aria-labelledby="dropdownMenu1"> </ul>
</ul> </div>
</div> </div>
</div> </div>
</div> </div>
</div> </div>
</div> <div class="row">
<div class="row"> <div class="col-md-4 labelColumn">
<div class="col-md-4 labelColumn"> <strong>Audio-Quality</strong>
<strong>Audio-Quality</strong> </div>
<div class="col-md-8">
<div class="btn-group" data-toggle="buttons">
<label class="btn btn-default">
<input type="checkbox" autocomplete="off"><span class="glyphicon glyphicon-subtitles" aria-hidden="true"></span> Subs</input>
</label>
<label class="btn btn-default">
<input type="checkbox" autocomplete="off"><span class="glyphicon glyphicon-sound-stereo" aria-hidden="true"></span> Stereo</input>
</label>
<label class="btn btn-default">
<input type="checkbox" autocomplete="off"><span class="glyphicon glyphicon-sound-5-1" aria-hidden="true"></span> 5.1</input>
</label>
<label class="btn btn-default">
<input type="checkbox" autocomplete="off"><span class="glyphicon glyphicon-sound-5-1" aria-hidden="true"></span> 6.1</input>
</label>
<label class="btn btn-default">
<input type="checkbox" autocomplete="off"><span class="glyphicon glyphicon-sound-7-1" aria-hidden="true"></span> 7.1</input>
</label>
</div>
</div>
</div> </div>
<div class="col-md-8"> <div class="row">
<div class="btn-group" data-toggle="buttons"> <div class="col-md-4 labelColumn">
<label class="btn btn-default"> <strong>{{ getLocalString(language, "name") }}</strong>
<input type="checkbox" autocomplete="off"><span class="glyphicon glyphicon-subtitles" aria-hidden="true"></span> Subs</input> </div>
</label> <div class="col-md-8">
<label class="btn btn-default"> <input type="text" name="name" class="form-control" placeholder="e.g. Attack of the Killer Tomatoes" aria-describedby="basic-addon1">
<input type="checkbox" autocomplete="off"><span class="glyphicon glyphicon-sound-stereo" aria-hidden="true"></span> Stereo</input> </div>
</label>
<label class="btn btn-default">
<input type="checkbox" autocomplete="off"><span class="glyphicon glyphicon-sound-5-1" aria-hidden="true"></span> 5.1</input>
</label>
<label class="btn btn-default">
<input type="checkbox" autocomplete="off"><span class="glyphicon glyphicon-sound-5-1" aria-hidden="true"></span> 6.1</input>
</label>
<label class="btn btn-default">
<input type="checkbox" autocomplete="off"><span class="glyphicon glyphicon-sound-7-1" aria-hidden="true"></span> 7.1</input>
</label>
</div>
</div> </div>
</div> <div class="row">
<button type="button" class="btn btn-default"><span class="glyphicon glyphicon-plus" aria-hidden="true"></span> Create!</button> <div class="col-md-4 labelColumn">
<strong>{{ getLocalString(language, "description") }}</strong>
</div>
<div class="col-md-8">
<textarea name="description" class="form-control" rows="10"></textarea>
</div>
</div>
<button type="submit" class="btn btn-default"><span class="glyphicon glyphicon-plus" aria-hidden="true"></span> {{ getLocalString(language, "create") }}!</button>
</form>
</div> </div>
{% endblock content%} {% endblock content%}

View File

@ -3,7 +3,7 @@ vim: ts=2 noexpandtab
--> -->
{% set navigation_bar = [ {% set navigation_bar = [
("/", "search", "Search", "glyphicon-search"), ("/", "search", "Search", "glyphicon-search"),
("/categorys", "categorys", "Categorys", "glyphicon-th"), ("/categories", "categories", "Categories", "glyphicon-th"),
("/create", "create", "Create", "glyphicon-plus") ("/create", "create", "Create", "glyphicon-plus")
] -%} ] -%}
@ -18,9 +18,14 @@ vim: ts=2 noexpandtab
<title>{% block title %}TorrentIndexer{% endblock %}</title> <title>{% block title %}TorrentIndexer{% endblock %}</title>
<link href="{{ url_for("static", filename="css/bootstrap.css") }}" rel="stylesheet"> <link href="{{ url_for("static", filename="css/bootstrap.css") }}" rel="stylesheet">
<link href="{{ url_for("static", filename="css/style.css") }}" rel="stylesheet"> <link href="{{ url_for("static", filename="css/style.css") }}" rel="stylesheet">
<script src="{{ url_for("static", filename="js/main.js") }}"></script>
</head> </head>
<body> <body>
<script type="text/javascript">
var global_strings = {{json.dumps(strings)|safe}};
var global_categories = {{json.dumps(categories)|safe}};
</script>
<div class="site-wrapper"> <div class="site-wrapper">
<div class="site-wrapper-inner"> <div class="site-wrapper-inner">
<div class="cover-container"> <div class="cover-container">

View File

@ -16,7 +16,7 @@ vim: ts=2 noexpandtab
<span class="input-group-btn"> <span class="input-group-btn">
<button class="btn btn-default" type="submit"> <button class="btn btn-default" type="submit">
<span class="glyphicon glyphicon-search" aria-hidden="true"></span> <span class="glyphicon glyphicon-search" aria-hidden="true"></span>
Search! {{ getLocalString(language, "search") }}!
</button> </button>
</span> </span>
</div> </div>