From 49eb1470f59e3253cf9fa10746b6d0591e5ea21a Mon Sep 17 00:00:00 2001 From: lza_menace Date: Mon, 20 Dec 2021 01:34:37 -0800 Subject: [PATCH] setup db models and redoing pages --- Makefile | 9 ++--- suchwowx/cli.py | 11 ++++++ suchwowx/factory.py | 5 ++- suchwowx/models.py | 17 +++++++-- suchwowx/routes.py | 47 +++++++++++++++++------ suchwowx/templates/index.html | 68 ++++++++------------------------- suchwowx/templates/new.html | 72 +++++++++++++++++++++++++++++++++++ 7 files changed, 155 insertions(+), 74 deletions(-) create mode 100644 suchwowx/cli.py create mode 100644 suchwowx/templates/new.html diff --git a/Makefile b/Makefile index ebbc19b..3ab93d6 100644 --- a/Makefile +++ b/Makefile @@ -1,10 +1,14 @@ setup: python3 -m venv .venv .venv/bin/pip install -r requirements.txt + mkdir -p data/uploads shell: bash manage.sh shell +init: + bash manage.sh init + dev: bash manage.sh run @@ -16,10 +20,5 @@ install-ipfs: tar -xvzf go-ipfs_v0.10.0_linux-amd64.tar.gz cd go-ipfs && bash install.sh -huey: - mkdir -p data/ - .venv/bin/huey_consumer suchwowx.tasks.huey -w 1 -v | tee -a data/huey.log - kill: - pkill -e -f huey pkill -e -f suchwowx diff --git a/suchwowx/cli.py b/suchwowx/cli.py new file mode 100644 index 0000000..4b779b7 --- /dev/null +++ b/suchwowx/cli.py @@ -0,0 +1,11 @@ +from flask import Blueprint + +from suchwowx.factory import db + + +bp = Blueprint('cli', 'cli', cli_group=None) + +@bp.cli.command('init') +def init(): + import suchwowx.models + db.create_all() diff --git a/suchwowx/factory.py b/suchwowx/factory.py index b06270b..19a399f 100644 --- a/suchwowx/factory.py +++ b/suchwowx/factory.py @@ -11,7 +11,7 @@ db = SQLAlchemy() def setup_db(app: Flask, db:SQLAlchemy=db): - app.config['SQLALCHEMY_DATABASE_URI'] = f'sqlite://{config.DATA_FOLDER}/sqlite.db' # noqa + app.config['SQLALCHEMY_DATABASE_URI'] = f'sqlite:///{config.DATA_FOLDER}/sqlite.db' # noqa app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False db.init_app(app) return db @@ -30,7 +30,8 @@ def create_app(): Mobility(app) with app.app_context(): - from suchwowx import filters, routes + from suchwowx import filters, routes, cli app.register_blueprint(filters.bp) app.register_blueprint(routes.bp) + app.register_blueprint(cli.bp) return app diff --git a/suchwowx/models.py b/suchwowx/models.py index bab68a8..67909f0 100644 --- a/suchwowx/models.py +++ b/suchwowx/models.py @@ -1,4 +1,5 @@ from uuid import uuid4 +from datetime import datetime from suchwowx.factory import db @@ -7,7 +8,17 @@ def rand_id(): return uuid4().hex -class User(db.Model): - __tablename__ = 'users' +class Meme(db.Model): + __tablename__ = 'memes' - id = db.Column(db.Integer, primary_key=True) + id = db.Column(db.String(80), default=rand_id, primary_key=True) + create_date = db.Column(db.DateTime, default=datetime.utcnow()) + upload_path = db.Column(db.String(200), unique=True) + meta_ipfs_hash = db.Column(db.String(100), unique=True) + meme_ipfs_hash = db.Column(db.String(100), unique=True) + title = db.Column(db.String(50)) + description = db.Column(db.String(400)) + creator_handle = db.Column(db.String(50)) + + def __repr__(self): + return str(f'meme-{self.id}') diff --git a/suchwowx/routes.py b/suchwowx/routes.py index 1b4fdf8..157749f 100644 --- a/suchwowx/routes.py +++ b/suchwowx/routes.py @@ -3,13 +3,18 @@ from os import path from secrets import token_urlsafe from json import loads, dumps -from flask import Blueprint, render_template, request, current_app +from flask import Blueprint, render_template, request, current_app, redirect + +from suchwowx.models import Meme +from suchwowx.factory import db +from suchwowx import config bp = Blueprint('meta', 'meta') -@bp.route('/', methods=['GET', 'POST']) -def index(): +@bp.route('/new', methods=['GET', 'POST']) +def new(): + meme = None if "file" in request.files: title = request.form.get('title') description = request.form.get('description') @@ -19,11 +24,15 @@ def index(): token_urlsafe(24), path.splitext(file.filename)[1] ) - file.save(filename) + full_path = f'{config.DATA_FOLDER}/uploads/{filename}' + file.save(full_path) try: client = ipfsApi.Client('127.0.0.1', 5001) - artwork_hash = client.add(filename)['Hash'] - client.pin_add(artwork_hash) + artwork_hashes = client.add(full_path) + print(artwork_hashes) + artwork_hash = artwork_hashes[0]['Hash'] + print(artwork_hash) + # client.pin_add(artwork_hash) print(f'[+] Uploaded artwork to IPFS: {artwork_hash}') # Create meta json meta = { @@ -35,16 +44,30 @@ def index(): 'creator': creator } } - print(meta) meta_hash = client.add_json(meta) - client.pin_add(meta_hash) + # client.pin_add(meta_hash) print(f'[+] Uploaded metadata to IPFS: {meta_hash}') + meme = Meme( + upload_path=filename, + meta_ipfs_hash=meta_hash, + meme_ipfs_hash=artwork_hash, + title=title, + description=description, + creator_handle=creator + ) + db.session.add(meme) + db.session.commit() + return redirect('/') except ConnectionError: print('[!] Unable to connect to local ipfs') except Exception as e: print(e) - return render_template('index.html') + return render_template( + 'new.html', + meme=meme + ) -@bp.route('/next') -def next(): - return render_template('next.html') +@bp.route('/') +def index(): + memes = Meme.query.filter().order_by(Meme.create_date.desc()) + return render_template('index.html', memes=memes) diff --git a/suchwowx/templates/index.html b/suchwowx/templates/index.html index ab38c95..87f9cdb 100644 --- a/suchwowx/templates/index.html +++ b/suchwowx/templates/index.html @@ -13,65 +13,29 @@

- SuchwowX.
+ SuchWowX.

Memes. Interplanetary!

- - - -
-
- -
- -
-
- -
- -
- -
-
- -
- -
- -
-
- -
- -
- -
-
- -
-
- -
-
- -
-
- + New Meme + + {% if memes %} +
+ {% for meme in memes %} +
+

Meme: {{ meme }}

+

Upload path: {{ meme.upload_path }}

+

Meta IPFS: {{ meme.meta_ipfs_hash }}

+

Meme IPFS: {{ meme.meme_ipfs_hash }}

+

Title: {{ meme.title }}

+

Description: {{ meme.description }}

+

Creator handle: {{ meme.creator_handle }}

+ {% endfor %}
- + {% endif %}
diff --git a/suchwowx/templates/new.html b/suchwowx/templates/new.html new file mode 100644 index 0000000..e3a2e00 --- /dev/null +++ b/suchwowx/templates/new.html @@ -0,0 +1,72 @@ + + + + + + SuchWowX! + + + + + + +
+
+

+ SuchWowX.
+ +

+

+ Memes. Interplanetary! +

+ Go Back + +
+
+ +
+ +
+
+ +
+ +
+ +
+
+ +
+ +
+ +
+
+ +
+ +
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+
+
+ +
+
+ +