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
*.swo
backup
uploadTest
torrentFiles/*

View File

@ -1,23 +1,60 @@
#!/usr/bin/python3
#/* vim:set ts=2 set noexpandtab */
import json
from flask import Flask, render_template, url_for, request
from werkzeug import secure_filename
app = Flask(__name__)
strings = None
settings = None
@app.route("/")
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():
return render_template("categorys.html")
return render_template("categories.html", categories=settings["categories"])
@app.route("/create")
@app.route("/create", methods=['GET','POST'])
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'])
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__":
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()
}
function fillDropdownByValue(value, dropdownToFill) {
dropdownToFill.getElementsByClassName("text")[0].innerHTML = "Subcategory"
var dropdownToFillUl = dropdownToFill.getElementsByClassName("dropdown-menu")[0]
dropdownToFillUl.innerHTML = ""
subCategory = getDescriptorByLocalString("english", value)
catList = document.getElementById("subcategory_list")
catList.innerHTML = ""
categories = global_categories[subCategory].sort()
for(var key in categories) {
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 = getLocalString("english", categories[key])
newEntryLink.onclick = function(){ setDropdownButtonText(this.innerHTML, this.parentElement.parentElement.parentElement) }
newEntry.appendChild(newEntryLink)
dropdownToFillUl.appendChild(newEntry)
}
}
//function fillDropdownByValue(value, dropdownToFill) {
// dropdownToFill.getElementsByClassName("text")[0].innerHTML = "Subcategory"
// var dropdownToFillUl = dropdownToFill.getElementsByClassName("dropdown-menu")[0]
// dropdownToFillUl.innerHTML = ""
//
// subCategory = getDescriptorByLocalString("english", value)
// catList = document.getElementById("subcategory_list")
// catList.innerHTML = ""
// categories = global_categories[subCategory].sort()
// for(var key in categories) {
// 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 = getLocalString("english", categories[key])
// newEntryLink.onclick = function(){ setDropdownButtonText(this.innerHTML, this.parentElement.parentElement.parentElement) }
// newEntry.appendChild(newEntryLink)
// dropdownToFillUl.appendChild(newEntry)
// }
//}
function setDropdownButtonText(text, dropdownButton) {
var dropdownTextSpan = dropdownButton.getElementsByClassName("text")[0]
@ -38,51 +38,38 @@ function setDropdownButtonText(text, dropdownButton) {
function sortCategories(categories) {
var sortedCategories = []
for(var key in categories) {
sortedCategories.push(key)
sortedCategories.push(categories[key])
}
sortedCategories.sort()
return sortedCategories
}
// Fill a defined dropdown with values.
// These values will be generated out of the categories.json
function fillDropdownByValue(value, dropdownToFill) {
dropdownToFill.getElementsByTagName("button")[0].disabled = false
dropdownToFill.getElementsByClassName("text")[0].innerHTML = "Subcategorie"
var dropdownToFillUl = dropdownToFill.getElementsByClassName("dropdown-menu")[0]
dropdownToFillUl.innerHTML = ""
switch(value) {
case "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 = "foo" + i
newEntryLink.onclick = function(){ setDropdownButtonText(this.innerHTML, this.parentElement.parentElement.parentElement) }
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;
valueDescriptor = getDescriptorByLocalString("english", value)
subcategories = global_categories[valueDescriptor]
subcategories = sortCategories(subcategories)
for(subcategoryIndex in subcategories) {
subcategoryLocalString = getLocalString("english", subcategories[subcategoryIndex])
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 = subcategoryLocalString
newEntryLink.onclick = function(){ setDropdownButtonText(this.innerHTML, this.parentElement.parentElement.parentElement) }
newEntry.appendChild(newEntryLink)
dropdownToFillUl.appendChild(newEntry)
}
}
// Hides the default browser-upload-form and replaces it by an button
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").hide();
@ -91,19 +78,31 @@ function customizeUploadButton() {
});
}
// This sets the Uploadbutton to the filename of the uploaded file
function setButtonToFilename(event) {
$("input[name='Datei']").each(function() {
$("input[name='torrentFile']").each(function() {
var fileName = $(this).val().split('/').pop().split('\\').pop();
console.log(event["target"]);
targetInput = event["target"]
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) {
var dropdownTextSpan = dropdownButton.getElementsByClassName("text")[0]
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",
"torrent_file" : "Torrent file",
"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
-->
{% extends "index.html" %}
{% block title %}{{ super() }} - Create{% endblock%}
{% block title %}{{ super() }} - {{ getLocalString(language, "create") }}{% endblock%}
{% set active_page = "create" %}
{% block content %}
<link href="{{ url_for("static", filename="css/create.css") }}" rel="stylesheet">
<script src="{{ url_for("static", filename="js/create.js") }}"></script>
<link href="{{ url_for("static", filename="css/create.css") }}" rel="stylesheet">
<script src="{{ url_for("static", filename="js/create.js") }}"></script>
<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">
<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.
</div>
-->
<div class="row">
<div class="col-md-4 labelColumn">
<strong>Torrent file</strong>
<form action="/create" method="post" enctype="multipart/form-data">
<div class="row">
<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 class="col-md-8">
<input name="Datei" class="tfile" type="file" size="50" maxlength="100000" accept="text/*" onchange="setButtonToFilename(event)">
</div>
</div>
<div class="row">
<div class="col-md-4 labelColumn">
<strong>Name</strong>
</div>
<div class="col-md-8">
<input type="text" class="form-control" placeholder="e.g. Attack of the Killer Tomatoes" aria-describedby="basic-addon1">
</div>
</div>
<div class="row">
<div class="col-md-4 labelColumn">
<strong>Categorie</strong>
</div>
<div class="col-md-8">
<div class="row">
<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 class="row">
<div class="col-md-4 labelColumn">
<strong>{{ getLocalString(language, "category") }}</strong>
</div>
<div class="col-md-8">
<div class="row">
<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">{{ getLocalString(language, "category") }}</span>
<span class="caret"></span>
</button>
<ul class="dropdown-menu" role="menu" aria-labelledby="dropdownMenu1">
{% for category in sorted(categories.keys()) %}
<li role="presentation"><a role="menuitem" tabindex="-1" href="#">{{ getLocalString(language, category) }}</a></li>
{% endfor %}
</ul>
</div>
</div>
</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" disabled>
<span class="text">Subcategorie</span>
<span class="caret"></span>
</button>
<ul class="dropdown-menu" role="menu" aria-labelledby="dropdownMenu1">
</ul>
<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" disabled>
<span class="text">{{ getLocalString(language, "subcategory") }}</span>
<span class="caret"></span>
</button>
<ul class="dropdown-menu" role="menu" aria-labelledby="dropdownMenu1">
</ul>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-4 labelColumn">
<strong>Audio-Quality</strong>
<div class="row">
<div class="col-md-4 labelColumn">
<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 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 class="row">
<div class="col-md-4 labelColumn">
<strong>{{ getLocalString(language, "name") }}</strong>
</div>
<div class="col-md-8">
<input type="text" name="name" class="form-control" placeholder="e.g. Attack of the Killer Tomatoes" aria-describedby="basic-addon1">
</div>
</div>
</div>
<button type="button" class="btn btn-default"><span class="glyphicon glyphicon-plus" aria-hidden="true"></span> Create!</button>
<div class="row">
<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>
{% endblock content%}

View File

@ -3,7 +3,7 @@ vim: ts=2 noexpandtab
-->
{% set navigation_bar = [
("/", "search", "Search", "glyphicon-search"),
("/categorys", "categorys", "Categorys", "glyphicon-th"),
("/categories", "categories", "Categories", "glyphicon-th"),
("/create", "create", "Create", "glyphicon-plus")
] -%}
@ -18,9 +18,14 @@ vim: ts=2 noexpandtab
<title>{% block title %}TorrentIndexer{% endblock %}</title>
<link href="{{ url_for("static", filename="css/bootstrap.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>
<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-inner">
<div class="cover-container">

View File

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