implemented sqlite database backend

This commit is contained in:
sqozz 2015-02-19 00:50:18 +01:00
parent 8da224ddf9
commit af9a8eb81b
2 changed files with 47 additions and 3 deletions

2
.gitignore vendored
View File

@ -3,3 +3,5 @@
backup backup
uploadTest uploadTest
torrentFiles/* torrentFiles/*
torrentdb.sqlite
torrentdb.sql

View File

@ -1,6 +1,6 @@
#!/usr/bin/python3 #!/usr/bin/python3
#/* vim:set ts=2 set noexpandtab */ #/* vim:set ts=2 set noexpandtab */
import json, uuid, hashlib import json, uuid, hashlib, sqlite3, base64
from flask import Flask, render_template, url_for, request from flask import Flask, render_template, url_for, request
from werkzeug import secure_filename from werkzeug import secure_filename
app = Flask(__name__) app = Flask(__name__)
@ -38,6 +38,15 @@ def init():
with open("settings.json") as settingsJson: with open("settings.json") as settingsJson:
settings = json.load(settingsJson) settings = json.load(settingsJson)
initDb()
def initDb():
connection = sqlite3.connect("torrentdb.sqlite")
c = connection.cursor()
c.execute('CREATE TABLE IF NOT EXISTS torrents (fileid TEXT PRIMARY KEY NOT NULL, name TEXT NOT NULL, category TEXT NOT NULL, subcategory TEXT NOT NULL, description TEXT NOT NULL);')
c.execute('CREATE TABLE IF NOT EXISTS language_mapping (fileid TEXT NOT NULL, language TEXT NOT NULL);')
connection.commit()
connection.close()
def getLocalString(language, descriptor): def getLocalString(language, descriptor):
global strings global strings
@ -63,8 +72,41 @@ def createNewTorrent(reuqest):
print( "Category: " + request.form["category"] ) print( "Category: " + request.form["category"] )
print( "Subcategory: " + request.form["subcategory"] ) print( "Subcategory: " + request.form["subcategory"] )
print( "Description: " + request.form["description"] ) print( "Description: " + request.form["description"] )
# TODO: Process inputdate from the form and save it to the (until now) non-existing DB
return ["Error1", "Error2"] #TODO: Validate the input serverside before writing it to the database
name = request.form["name"]
category = request.form["category"]
subcategory = request.form["subcategory"]
description = request.form["description"]
languages = []
newTFile = TorrentFile(safeFilename, name, category, subcategory, description, languages)
connection = sqlite3.connect("torrentdb.sqlite")
newTFile.writeToDb(connection.cursor())
connection.commit()
connection.close()
return ["Error1"]
class TorrentFile():
fileid = None
name = None
category = None
subcategory = None
description = None
languages = []
def __init__(self, fileid=fileid, name=name, category=category, subcategory=subcategory, description=description, languages=languages):
self.fileid = fileid
self.name = name
self.category = category
self.subcategory = subcategory
self.description = description
self.languages = languages
def writeToDb(self, cursor):
c = cursor
b64description = base64.b64encode(self.description.encode())
c.execute("INSERT INTO torrents(fileid, name, category, subcategory, description) VALUES(:fileid, :name, :category, :subcategory, :description)", { 'fileid' : self.fileid, 'name' : self.name, 'category' : self.category, 'subcategory' : self.subcategory, 'description' : b64description })
for language in self.languages:
c.execute("INSERT INTO language_mapping(fileid, language)", { "fileid" : self.fileid, "language" : language })
if __name__ == "__main__": if __name__ == "__main__":
init() init()