Browse Source

Markdown Upload Done

master
Eddie 2 years ago
parent
commit
10713bda91
  1. 1
      app/db.py
  2. 35
      app/index.py
  3. 5
      app/schema.sql

1
app/db.py

@ -29,6 +29,7 @@ def init_db():
db.executescript(f.read().decode('utf8')) db.executescript(f.read().decode('utf8'))
@click.command('init-db') @click.command('init-db')
@with_appcontext @with_appcontext
def init_db_command(): def init_db_command():

35
app/index.py

@ -1,6 +1,6 @@
import os import os, uuid
from flask import ( from flask import (
Blueprint, flash, g, redirect, render_template, request, url_for Blueprint, flash, g, redirect, render_template, request, url_for, session
) )
from werkzeug.exceptions import abort from werkzeug.exceptions import abort
@ -17,12 +17,7 @@ bp = Blueprint('index', __name__)
@login_required @login_required
def index(): def index():
db = get_db() db = get_db()
loginEvents = db.execute( return render_template('index.html')
'SELECT *'
' FROM loginEvent'
' ORDER BY created DESC'
).fetchall()
return render_template('index.html', loginEvents = loginEvents)
def allowed_file(filename): def allowed_file(filename):
@ -30,6 +25,7 @@ def allowed_file(filename):
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
@bp.route('/upload', methods=['GET', 'POST']) @bp.route('/upload', methods=['GET', 'POST'])
@login_required
def upload_file(): def upload_file():
if request.method == 'POST': if request.method == 'POST':
# check if the post request has the file part # check if the post request has the file part
@ -44,7 +40,11 @@ def upload_file():
return redirect(request.url) return redirect(request.url)
if file and allowed_file(file.filename): if file and allowed_file(file.filename):
filename = secure_filename(file.filename) filename = secure_filename(file.filename)
file.save(os.path.join(UPLOAD_FOLDER, filename)) path = os.path.join(UPLOAD_FOLDER, filename)
file.save(path)
createMarkdownEntry(path)
os.remove(path)
return redirect(url_for('index')) return redirect(url_for('index'))
return ''' return '''
<!doctype html> <!doctype html>
@ -55,3 +55,20 @@ def upload_file():
<input type=submit value=Upload> <input type=submit value=Upload>
</form> </form>
''' '''
def createMarkdownEntry(path):
db = get_db()
title = os.path.basename(path)
title = title.split('.')
file = open(path)
data = file.read()
file.close()
uniqueID = str(uuid.uuid4())
qrCode = 'QR'
db.execute(
'INSERT INTO markdownFile (title, body, qrCode, uuid, creator) VALUES (?, ?, ?, ?, ?)',
(title[0], data, qrCode, uniqueID, session['user_id'])
)
db.commit()
return

5
app/schema.sql

@ -7,10 +7,11 @@ CREATE TABLE user (
password TEXT NOT NULL password TEXT NOT NULL
); );
CREATE TABLE MarkdownFile ( CREATE TABLE markdownFile (
id INTEGER PRIMARY KEY AUTOINCREMENT, id INTEGER PRIMARY KEY AUTOINCREMENT,
title TEXT NOT NULL, title TEXT NOT NULL,
body TEXT NOT NULL, body TEXT NOT NULL,
qrCode TEXT NOT NULL, qrCode TEXT NOT NULL,
uuid TEXT NOT NULL uuid TEXT NOT NULL,
creator INTEGER NOT NULL
); );
Loading…
Cancel
Save