diff --git a/indexer.py b/indexer.py index 12fb3ee..5a02339 100644 --- a/indexer.py +++ b/indexer.py @@ -4,7 +4,7 @@ import json, uuid, hashlib, sqlite3, base64 from hashlib import sha1 import bencoder import requests -from flask import Flask, render_template, url_for, request, send_file +from flask import Flask, render_template, url_for, request, send_file, redirect from werkzeug import secure_filename app = Flask(__name__) strings = None @@ -24,11 +24,12 @@ def create(): if request.method == "GET": return render_template("create.html", language="english", categories=settings["categories"], strings=strings, errors=None) elif request.method == "POST": - errors = createNewTorrent(request) - if errors == None: - return "It's allright" + newTorrent = createNewTorrent(request) + if len(newTorrent.errors) == 0: + message = "Successfully created torrent {}".format(newTorrent.fileid, newTorrent.fileid[:-20]) + return render_template("create.html", language="english", categories=settings["categories"], strings=strings, messages=[message]) else: - return render_template("create.html", language="english", categories=settings["categories"], strings=strings, errors=errors) + return render_template("create.html", language="english", categories=settings["categories"], strings=strings, errors=newTorrent.errors) @app.route("/download/") def download(filename): @@ -144,13 +145,13 @@ def createNewTorrent(reuqest): newTFile.metadata.writeToDb(connection.cursor()) connection.commit() connection.close() - return ["Success"] except sqlite3.IntegrityError as e: print(e) - return ["Torrent {} does already exist".format(info_hash, info_hash[:-20])] + newTFile.errors = ["Torrent {} already exists".format(info_hash, info_hash[:-20])] except Exception as e: - print(e) - return ["Unknown error in creation"] + newTFile.errors = ["Unknown error in creation"] + + return newTFile class Metadata(): def __init__(self, fileid): @@ -175,6 +176,7 @@ class Metadata(): c.execute("INSERT INTO metadata(fileid, created_by, creation_date, announce_url, source, torrentsize, name, private) VALUES(:fileid, :created_by, :creation_date, :announce_url, :source, :torrentsize, :name, :private)", { 'fileid' : self.fileid, 'created_by' : b64created_by, 'creation_date' : self.creation_date, 'announce_url' : b64announce_url, 'source' : b64source , 'torrentsize' : self.torrentsize, 'name' : b64name, 'private' : self.private}) class TorrentFile(): + errors = [] fileid = None name = None category = None diff --git a/templates/create.html b/templates/create.html index 28f708e..c9affb8 100644 --- a/templates/create.html +++ b/templates/create.html @@ -17,6 +17,14 @@ vim: ts=2 noexpandtab {% endfor %} {% endif %} + {% if messages %} + {% for message in messages %} + + {% endfor %} + {% endif %}