From 4ba2e1168ffe41d2f2f2e17b03f00b6a9cbf133c Mon Sep 17 00:00:00 2001 From: sqozz Date: Sun, 15 Feb 2015 03:48:39 +0100 Subject: [PATCH] Added translation methods to JS and Python Modified "Create"-form --- .gitignore | 2 + indexer.py | 51 +++++++++++-- static/js/create.js | 113 ++++++++++++++-------------- strings.json | 3 +- templates/categories.html | 9 +++ templates/categorys.html | 9 --- templates/create.html | 153 ++++++++++++++++++++------------------ templates/index.html | 7 +- templates/search.html | 2 +- 9 files changed, 202 insertions(+), 147 deletions(-) create mode 100644 templates/categories.html delete mode 100644 templates/categorys.html diff --git a/.gitignore b/.gitignore index da39247..63ebf93 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ *.swp *.swo backup +uploadTest +torrentFiles/* diff --git a/indexer.py b/indexer.py index c302593..2d5ff5e 100644 --- a/indexer.py +++ b/indexer.py @@ -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) diff --git a/static/js/create.js b/static/js/create.js index 49b1131..e899972 100644 --- a/static/js/create.js +++ b/static/js/create.js @@ -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++) { - //
  • Action
  • - 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++) { - //
  • Action
  • - 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(''); $(".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 += "
    " + string.substr(i, n) + } + } + return ret +}; diff --git a/strings.json b/strings.json index e77d8c7..d142a42 100644 --- a/strings.json +++ b/strings.json @@ -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" } } diff --git a/templates/categories.html b/templates/categories.html new file mode 100644 index 0000000..c502b76 --- /dev/null +++ b/templates/categories.html @@ -0,0 +1,9 @@ + +{% extends "index.html" %} +{% block title %}{{ super() }} - Categories{% endblock%} +{% set active_page = "categories" %} +{% block content %} +

    CATEGORIES

    +{% endblock content%} diff --git a/templates/categorys.html b/templates/categorys.html deleted file mode 100644 index 3200c46..0000000 --- a/templates/categorys.html +++ /dev/null @@ -1,9 +0,0 @@ - -{% extends "index.html" %} -{% block title %}{{ super() }} - Categorys{% endblock%} -{% set active_page = "categorys" %} -{% block content %} -

    CATEGORYS

    -{% endblock content%} diff --git a/templates/create.html b/templates/create.html index dbe3806..22d29af 100644 --- a/templates/create.html +++ b/templates/create.html @@ -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 %} - - + +
    -

    Create new

    +

    {{ getLocalString(language, "create_new_torrent") }}

    + + +
    +
    +
    + {{ getLocalString(language, "torrent_file") }} +
    +
    + +
    -
    - -
    -
    -
    -
    - Name -
    -
    - -
    -
    -
    -
    - Categorie -
    -
    -
    -
    -