From 4262ec9f04ac31bd5204b19d156cca94aabcc34e Mon Sep 17 00:00:00 2001 From: lza_menace Date: Sun, 9 Aug 2020 23:59:45 -0700 Subject: [PATCH] setup profiles --- suchwow/app.py | 1 + suchwow/models.py | 10 +++++----- suchwow/routes/post.py | 4 ++-- suchwow/routes/profile.py | 24 ++++++++++++++++++++++++ suchwow/templates/index.html | 16 ++++++++++------ suchwow/templates/navbar.html | 3 +++ suchwow/templates/post/read.html | 2 +- suchwow/templates/profile/edit.html | 20 ++++++++++++++++++++ suchwow/utils/decorators.py | 16 +++++++++++++++- 9 files changed, 81 insertions(+), 15 deletions(-) create mode 100644 suchwow/templates/profile/edit.html diff --git a/suchwow/app.py b/suchwow/app.py index 1644722..2a0d27d 100644 --- a/suchwow/app.py +++ b/suchwow/app.py @@ -16,6 +16,7 @@ Session(app) app.register_blueprint(post.bp) app.register_blueprint(auth.bp) +app.register_blueprint(profile.bp) @app.route("/") def index(): diff --git a/suchwow/models.py b/suchwow/models.py index 721e03c..640ac0e 100644 --- a/suchwow/models.py +++ b/suchwow/models.py @@ -3,7 +3,7 @@ from datetime import datetime from suchwow import config -db = SqliteDatabase(f'{config.DATA_FOLDER}/db/sqlite.db') +db = SqliteDatabase(f"{config.DATA_FOLDER}/db/sqlite.db") class Post(Model): id = AutoField() @@ -25,7 +25,7 @@ class Profile(Model): id = AutoField() username = CharField() address = CharField() - notifications = IntegerField() + notifications = IntegerField(default=0) class Meta: database = db @@ -33,8 +33,8 @@ class Profile(Model): class Comment(Model): id = AutoField() comment = TextField() - commenter = ForeignKeyField(Profile, field=Profile.username) - post = ForeignKeyField(Post, field=id) + commenter = ForeignKeyField(Profile) + post = ForeignKeyField(Post) timestamp = DateTimeField(default=datetime.now) class Meta: @@ -43,7 +43,7 @@ class Comment(Model): class Notification(Model): type = CharField() message = TextField() - username = ForeignKeyField(Profile, field=Profile.username) + username = ForeignKeyField(Profile) timestamp = DateTimeField(default=datetime.now) class Meta: diff --git a/suchwow/routes/post.py b/suchwow/routes/post.py index 150e1cb..0029ac0 100644 --- a/suchwow/routes/post.py +++ b/suchwow/routes/post.py @@ -4,7 +4,7 @@ from flask import send_from_directory, redirect, url_for, current_app from werkzeug.utils import secure_filename from suchwow import wownero from suchwow.models import Post -from suchwow.utils.decorators import login_required +from suchwow.utils.decorators import login_required, profile_required from suchwow.utils.helpers import allowed_file @@ -25,6 +25,7 @@ def read(id): @bp.route("/post/create", methods=["GET", "POST"]) @login_required +@profile_required def create(): if request.method == "POST": post_title = request.form.get("title") @@ -46,7 +47,6 @@ def create(): save_path_base = path.join(current_app.config["DATA_FOLDER"], "uploads") save_path = path.join(save_path_base, filename) file.save(save_path) - # gen wallet try: wallet = wownero.Wallet() account_index = wallet.new_account() diff --git a/suchwow/routes/profile.py b/suchwow/routes/profile.py index e69de29..c181494 100644 --- a/suchwow/routes/profile.py +++ b/suchwow/routes/profile.py @@ -0,0 +1,24 @@ +from flask import render_template, Blueprint, flash +from flask import request, redirect, url_for, session +from suchwow.models import Profile +from suchwow.utils.decorators import login_required + + +bp = Blueprint("profile", "profile") + +@bp.route("/profile/edit", methods=["GET", "POST"]) +@login_required +def edit(): + if request.method == "POST": + address = request.form.get("address") + if len(address) in [97, 108]: + profile = Profile( + username=session["auth"]["preferred_username"], + address=address + ) + profile.save() + return redirect(request.args.get("redirect", "/")) + else: + flash("WTF bro, that's not a valid Wownero address") + return redirect(request.url) + return render_template("profile/edit.html") diff --git a/suchwow/templates/index.html b/suchwow/templates/index.html index 086fcf4..603ba70 100644 --- a/suchwow/templates/index.html +++ b/suchwow/templates/index.html @@ -8,11 +8,15 @@

{% block title %}Latest Submissions{% endblock %}

- + {% if posts %} + + {% else %} +

No posts yet!

+ {% endif %} @@ -20,7 +24,7 @@ Back {% endif %} - {% if not page == total_pages %} + {% if page <= total_pages and total_pages > 0 %} Next {% endif %} diff --git a/suchwow/templates/navbar.html b/suchwow/templates/navbar.html index b80aa66..00d9761 100755 --- a/suchwow/templates/navbar.html +++ b/suchwow/templates/navbar.html @@ -18,6 +18,9 @@ Login {% else %} + diff --git a/suchwow/templates/post/read.html b/suchwow/templates/post/read.html index e489217..d7f4344 100644 --- a/suchwow/templates/post/read.html +++ b/suchwow/templates/post/read.html @@ -14,7 +14,7 @@

-

Vote for this post by sending WOW to the following address:
{{ address }}

+

Vote for this post by sending WOW to the following address:
{{ address }}


Comments

{% if comments %} diff --git a/suchwow/templates/profile/edit.html b/suchwow/templates/profile/edit.html new file mode 100644 index 0000000..e9529c8 --- /dev/null +++ b/suchwow/templates/profile/edit.html @@ -0,0 +1,20 @@ +{% extends 'base.html' %} + +{% block content %} + +
+
+

Edit Profile

+

You need to setup your profile before you can submit memes. As of now this only consists of a payout address so we know where to send Wownero if someone sends funds for your post.

+
+
+ + +
+
+ +
+
+
+
+{% endblock %} diff --git a/suchwow/utils/decorators.py b/suchwow/utils/decorators.py index 390f52e..f244370 100644 --- a/suchwow/utils/decorators.py +++ b/suchwow/utils/decorators.py @@ -1,5 +1,6 @@ from flask import session, redirect, url_for from functools import wraps +from suchwow.models import Profile def login_required(f): @@ -8,4 +9,17 @@ def login_required(f): if "auth" not in session or not session["auth"]: return redirect(url_for("auth.login")) return f(*args, **kwargs) - return decorated_function \ No newline at end of file + return decorated_function + +def profile_required(f): + @wraps(f) + def decorated_function(*args, **kwargs): + un = session["auth"]["preferred_username"] + if not Profile.filter(username=un): + url = "{}?redirect={}".format( + url_for("profile.edit"), + url_for("post.create") + ) + return redirect(url) + return f(*args, **kwargs) + return decorated_function