diff --git a/app.py b/app.py index ca3f27f..29832f7 100644 --- a/app.py +++ b/app.py @@ -32,9 +32,10 @@ def get_db(): db.execute(""" CREATE TABLE IF NOT EXISTS Posts ( id INTEGER PRIMARY KEY, - username TEXT, - comment TEXT, - pattern TEXT NOT NULL + username TEXT NOT NULL UNIQUE, + comment TEXT NOT NULL, + pattern TEXT NOT NULL, + creation_time INTEGER NOT NULL ) """) @@ -64,7 +65,15 @@ def unathorized_handler(): @app.route("/") @login_required def main(): - return render_template("index.jinja2") + username = flask_login.current_user.id + + cur = get_db().cursor() + + cur.execute("SELECT * FROM Posts LIMIT 20") + + cur.close() + + return render_template("index.jinja2", username=username) @app.route("/login", methods=["GET", "POST"]) def login(): @@ -122,12 +131,6 @@ def register(): return redirect(url_for("login")) -@app.route("/posts") -@login_required -def posts(): - username = flask_login.current_user.id - return render_template("posts.jinja2", username=username) - @app.route("/profile") @login_required def profile(): diff --git a/static/pumpkin.js b/static/pumpkin.js new file mode 100644 index 0000000..4422583 --- /dev/null +++ b/static/pumpkin.js @@ -0,0 +1,80 @@ +function setup_pumpkin(canvas_id, clearbtn_id, form_id, pattern_field_id, grid_size) { + const canvas = document.getElementById(canvas_id); + const ctx = canvas.getContext('2d'); + const img = new Image(); + img.src = '/static/pumpkin.png'; + + const GRID_SIZE = grid_size; + const CELL_SIZE = canvas.width / GRID_SIZE; + + img.onload = () => { + ctx.drawImage(img, 0, 0, canvas.width, canvas.height); + drawGrid(); + }; + + let drawing = false; + let currentPattern = []; + + canvas.addEventListener('mousedown', () => { drawing = true; }); + canvas.addEventListener('mouseup', () => { drawing = false; }); + canvas.addEventListener('mousemove', draw); + canvas.addEventListener('click', (e) => {draw(e, true)}); + + function draw(e, force=false) { + if (!drawing && !force) return; + var rect = canvas.getBoundingClientRect(); + var x = e.clientX - rect.left; + var y = e.clientY - rect.top; + + var gridX = Math.floor(x / CELL_SIZE); + var gridY = Math.floor(y / CELL_SIZE); + + var cellX = gridX * CELL_SIZE + CELL_SIZE / 3; + var cellY = gridY * CELL_SIZE + CELL_SIZE / 3; + + var pixel = ctx.getImageData(cellX, cellY, 1, 1).data; + + if (pixel[0] >= 254 && (pixel[1] >= 124 && pixel[1] <= 126)) { + var key = `${gridX},${gridY}`; + + if (!currentPattern.includes(key)) { + currentPattern.push(key); + ctx.fillStyle = 'black'; + ctx.fillRect(cellX - CELL_SIZE / 3, cellY - CELL_SIZE / 3, CELL_SIZE, CELL_SIZE); + } + } + } + + function drawGrid() { + ctx.strokeStyle = 'rgba(0, 0, 0, 0.6)'; + ctx.lineWidth = 1; + + for (let i = 0; i <= GRID_SIZE; i++) { + const pos = i * CELL_SIZE; + + ctx.beginPath(); + ctx.moveTo(pos, 0); + ctx.lineTo(pos, canvas.height); + ctx.stroke(); + + ctx.beginPath() + ctx.moveTo(0, pos); + ctx.lineTo(canvas.width, pos); + ctx.stroke() + } + } + + document.getElementById(clearbtn_id).addEventListener('click', clearCanvas); + + function clearCanvas() { + drawing = false; // Fix hold staying after clear + ctx.clearRect(0, 0, canvas.width, canvas.height); + ctx.drawImage(img, 0, 0, canvas.width, canvas.height); + drawGrid() + currentPattern = []; + } + + document.getElementById(form_id).addEventListener('submit', function(event) { + document.getElementById(pattern_field_id).value = JSON.stringify(currentPattern); + }); +} \ No newline at end of file diff --git a/templates/base.jinja2 b/templates/base.jinja2 index e164702..3a503b1 100644 --- a/templates/base.jinja2 +++ b/templates/base.jinja2 @@ -5,7 +5,8 @@ - + + {% block title %} {% endblock %} {% block head %} {% endblock %} @@ -28,7 +29,5 @@ {% block body %} {% endblock %} - - \ No newline at end of file diff --git a/templates/index.jinja2 b/templates/index.jinja2 index 451041a..59fa90e 100644 --- a/templates/index.jinja2 +++ b/templates/index.jinja2 @@ -6,9 +6,6 @@ - @@ -18,5 +15,7 @@ {% endblock %} {% block body %} -

LoginWeen is a an app where you login/register with a halloween pumpkin carving as a password.

+
+

LoginWeen is a an app where you login/register with a halloween pumpkin carving as a password.

+
{% endblock body %} \ No newline at end of file diff --git a/templates/login.jinja2 b/templates/login.jinja2 index 0c9daa0..02eb494 100644 --- a/templates/login.jinja2 +++ b/templates/login.jinja2 @@ -31,84 +31,6 @@ {% endblock body %} \ No newline at end of file diff --git a/templates/posts.jinja2 b/templates/posts.jinja2 deleted file mode 100644 index 4013036..0000000 --- a/templates/posts.jinja2 +++ /dev/null @@ -1,22 +0,0 @@ -{% extends "base.jinja2" %} - -{% block title %} LoginWeen Posts {% endblock title %} - -{% block nav %} - - - - -{% endblock %} - -{% block body %} - -{% endblock body %} \ No newline at end of file diff --git a/templates/profile.jinja2 b/templates/profile.jinja2 index 05207cc..ee0a689 100644 --- a/templates/profile.jinja2 +++ b/templates/profile.jinja2 @@ -6,9 +6,6 @@ - @@ -84,165 +81,8 @@ {% endif %} - - - {% endblock body %} \ No newline at end of file diff --git a/templates/register.jinja2 b/templates/register.jinja2 index 0677cfc..fd08330 100644 --- a/templates/register.jinja2 +++ b/templates/register.jinja2 @@ -31,83 +31,6 @@ {% endblock body %} \ No newline at end of file