Browse Source

Almost Done

master
Eddie 2 years ago
parent
commit
cbcbe39dd5
  1. 7
      app/__init__.py
  2. 77
      app/index.py
  3. 6
      app/qrCode.py
  4. 3
      app/schema.sql
  5. 10979
      app/static/bootstrap.css
  6. BIN
      app/static/uploads/a0980e34-cbf7-41a2-8641-c6fb8c7e79d9.png
  7. 0
      app/static/uploads/init.txt
  8. 13
      app/templates/base.html
  9. 62
      app/templates/index.html
  10. 8
      app/templates/mdView.html
  11. 8
      app/templates/qrView.html
  12. 17
      app/templates/upload.html
  13. 66
      app/upload.py

7
app/__init__.py

@ -1,7 +1,7 @@
import os import os
from flask import Flask from flask import Flask
from flaskext.markdown import Markdown
def create_app(test_config=None): def create_app(test_config=None):
# create and configure the app # create and configure the app
@ -11,6 +11,7 @@ def create_app(test_config=None):
DATABASE=os.path.join(app.instance_path, 'app.sqlite'), DATABASE=os.path.join(app.instance_path, 'app.sqlite'),
) )
Markdown(app)
if test_config is None: if test_config is None:
# load the instance config, if it exists, when not testing # load the instance config, if it exists, when not testing
@ -37,8 +38,12 @@ def create_app(test_config=None):
from . import auth from . import auth
app.register_blueprint(auth.bp) app.register_blueprint(auth.bp)
from . import upload
app.register_blueprint(upload.bp)
from . import index from . import index
app.register_blueprint(index.bp) app.register_blueprint(index.bp)
app.add_url_rule('/', endpoint='index') app.add_url_rule('/', endpoint='index')
return app return app

77
app/index.py

@ -2,14 +2,9 @@ import os, uuid
from flask import ( from flask import (
Blueprint, flash, g, redirect, render_template, request, url_for, session Blueprint, flash, g, redirect, render_template, request, url_for, session
) )
from werkzeug.exceptions import abort
from app.auth import login_required from app.auth import login_required
from app.db import get_db from app.db import get_db
from werkzeug.utils import secure_filename
ALLOWED_EXTENSIONS = {'md'}
UPLOAD_FOLDER = os.getcwd() + '/app/uploads'
bp = Blueprint('index', __name__) bp = Blueprint('index', __name__)
@ -17,58 +12,40 @@ bp = Blueprint('index', __name__)
@login_required @login_required
def index(): def index():
db = get_db() db = get_db()
return render_template('index.html') mdFiles = db.execute(
'SELECT * FROM markdownFile WHERE creator = ?', (session['user_id'],)
).fetchall()
return render_template('index.html', mdFiles=mdFiles)
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
@bp.route('/upload', methods=['GET', 'POST']) @bp.route('/qr/<uuid>')
@login_required @login_required
def upload_file(): def qrView(uuid):
if request.method == 'POST': qrCode = '/static/uploads/' + str(uuid) + '.png'
# check if the post request has the file part return render_template('qrView.html', qr = qrCode)
if 'file' not in request.files:
flash('No file part')
return redirect(request.url)
file = request.files['file']
# If the user does not select a file, the browser submits an
# empty file without a filename.
if file.filename == '':
flash('No selected file')
return redirect(request.url)
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
path = os.path.join(UPLOAD_FOLDER, filename)
file.save(path)
createMarkdownEntry(path)
os.remove(path)
return redirect(url_for('index'))
return '''
<!doctype html>
<title>Upload new File</title>
<h1>Upload new File</h1>
<form method=post enctype=multipart/form-data>
<input type=file name=file>
<input type=submit value=Upload>
</form>
'''
def createMarkdownEntry(path):
@bp.route('/viewfile/<uuid>', methods=('GET', 'POST'))
@login_required
def viewfile(uuid):
db = get_db()
mdFile = db.execute(
'SELECT * FROM markdownFile WHERE uuid = ?', (uuid,)
).fetchone()
return render_template('mdView.html', mdFile=mdFile['body'])
@bp.route('/delete/<uuid>', methods=('GET', 'POST'))
@login_required
def deletefile(uuid):
db = get_db() 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( db.execute(
'INSERT INTO markdownFile (title, body, qrCode, uuid, creator) VALUES (?, ?, ?, ?, ?)', 'DELETE FROM markdownFile WHERE uuid = ?', (uuid,)
(title[0], data, qrCode, uniqueID, session['user_id'])
) )
db.commit() db.commit()
return os.remove(os.getcwd() + '/app/static/uploads/' + uuid + '.png')
return redirect(url_for('index'))

6
app/qrCode.py

@ -0,0 +1,6 @@
import qrcode, os
def createQrCode(data, name):
img = qrcode.make(data)
type(img)
img.save(os.getcwd() + '/app/static/uploads/' + str(name) + '.png')

3
app/schema.sql

@ -13,5 +13,6 @@ CREATE TABLE markdownFile (
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 creator INTEGER NOT NULL,
created TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
); );

10979
app/static/bootstrap.css vendored

File diff suppressed because it is too large Load Diff

BIN
app/static/uploads/a0980e34-cbf7-41a2-8641-c6fb8c7e79d9.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 955 B

0
app/uploads/init.txt → app/static/uploads/init.txt

13
app/templates/base.html

@ -1,18 +1,7 @@
<!doctype html> <!doctype html>
<title>{% block title %}{% endblock %} - Markdown Presenter</title> <title>{% block title %}{% endblock %} - Markdown Presenter</title>
<link rel="stylesheet" href="{{ url_for('static', filename='bootstrap.css') }}"> <link rel="stylesheet" href="{{ url_for('static', filename='bootstrap.css') }}">
<nav>
<h1>Eddsons Markdown Presenter</h1>
<ul>
{% if g.user %}
<li><span>{{ g.user['username'] }}</span>
<li><a href="{{ url_for('auth.logout') }}">Log Out</a>
{% else %}
<li><a href="{{ url_for('auth.register') }}">Register</a>
<li><a href="{{ url_for('auth.login') }}">Log In</a>
{% endif %}
</ul>
</nav>
<section class="content"> <section class="content">
<header> <header>
{% block header %}{% endblock %} {% block header %}{% endblock %}

62
app/templates/index.html

@ -1,11 +1,69 @@
{% extends 'base.html' %} {% extends 'base.html' %}
{% block header %} {% block header %}
<h1>{% block title %}Login Events{% endblock %}</h1>
<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
<div class="container-fluid">
<a href="{{ url_for('index') }}" class="navbar-brand" href="#">Eddsons Markdown Presenter</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarColor02" aria-controls="navbarColor02" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarColor02">
<ul class="navbar-nav me-auto">
<li class="nav-item">
<a type="button" href="{{ url_for('upload.upload_file') }}" class="btn btn-outline-light">Upload</a>
</li>
</ul>
<form class="d-flex">
{% if g.user %}
<a type="button" class="btn btn-outline-light">{{ g.user['username'] }}</a>
<a class="btn btn-secondary my-2 my-sm-0" href="{{ url_for('auth.logout') }}">Log Out</a>
{% else %}
<a class="btn btn-secondary my-2 my-sm-0" href="{{ url_for('auth.register') }}">Register</a>
<a class="btn btn-secondary my-2 my-sm-0" href="{{ url_for('auth.login') }}">Log In</a>
{% endif %}
</form>
</div>
</div>
</nav>
<h1>{% block title %}Markdown Files{% endblock %}</h1>
{% endblock %} {% endblock %}
{% block content %} {% block content %}
<a>Hello World</a>
<table class="table table-secondary">
<thead>
<tr>
<th scope="col">ID</th>
<th scope="col">Title</th>
<th scope="col">UUID</th>
<th scope="col">Created</th>
<th scope="col"></th>
<th scope="col"></th>
<th scope="col"></th>
</tr>
</thead>
<tbody>
{% for file in mdFiles %}
<tr class="table-dark">
<td>{{ file['id'] }}</td>
<td>{{ file['title'] }}</td>
<td>{{ file['uuid'] }}</td>
<td>{{ file['created'] }}</td>
<td><a type="button" href="{{ url_for('index.qrView', uuid=file['uuid']) }}" class="btn btn-primary">QR Code</a></td>
<td><a type="button" href="{{ url_for('index.viewfile', uuid=file['uuid']) }}" class="btn btn-primary">View</a></td>
<td><a type="button" href="{{ url_for('index.deletefile', uuid=file['uuid']) }}" class="btn btn-danger">Delete</a></td>
</tr>
{% endfor %}
</tbody>
</table>
{% endblock %} {% endblock %}

8
app/templates/mdView.html

@ -0,0 +1,8 @@
{% extends 'base.html' %}
{% block content %}
<div class="row row-cols-1 row-cols-md-3 g-4">
<div class="col"></div>
<div class="col">{{ mdFile|markdown }}</div>
<div class="col"></div>
{% endblock %}

8
app/templates/qrView.html

@ -0,0 +1,8 @@
{% extends 'base.html' %}
{% block content %}
<div class="row row-cols-1 row-cols-md-3 g-4">
<div class="col"></div>
<div class="col"><img src="{{ qr }}" alt="QR Code"></div>
<div class="col"></div>
{% endblock %}

17
app/templates/upload.html

@ -0,0 +1,17 @@
{% extends 'base.html' %}
{% block header %}
<h1>{% block title %}Markdown Upload{% endblock %}</h1>
{% endblock %}
{% block content %}
<title>Upload</title>
<h1>Upload new File</h1>
<form method=post enctype=multipart/form-data>
<input type=file name=file>
<input type=submit value=Upload>
</form>
{% endblock %}

66
app/upload.py

@ -0,0 +1,66 @@
import os, uuid
from flask import (
Blueprint, flash, g, redirect, render_template, request, url_for, session
)
from app.auth import login_required
from app.db import get_db
from werkzeug.utils import secure_filename
from app.qrCode import createQrCode
bp = Blueprint('upload', __name__, url_prefix='/upload')
ALLOWED_EXTENSIONS = {'md'}
UPLOAD_FOLDER = os.getcwd() + '/app/static/uploads'
@bp.route('/upload', methods=['GET', 'POST'])
@login_required
def upload_file():
if request.method == 'POST':
# check if the post request has the file part
if 'file' not in request.files:
flash('No file part')
return redirect(request.url)
file = request.files['file']
# If the user does not select a file, the browser submits an
# empty file without a filename.
if file.filename == '':
flash('No selected file')
return redirect(request.url)
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
path = os.path.join(UPLOAD_FOLDER, filename)
file.save(path)
createMarkdownEntry(path)
os.remove(path)
return redirect(url_for('index'))
return render_template('upload.html')
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
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())
base_uri = request.url_root
qrData = base_uri + 'viewfile/' + uniqueID
createQrCode(qrData, uniqueID)
qrCode = qrData
db.execute(
'INSERT INTO markdownFile (title, body, qrCode, uuid, creator) VALUES (?, ?, ?, ?, ?)',
(title[0], data, qrCode, uniqueID, session['user_id'])
)
db.commit()
return
Loading…
Cancel
Save