diff --git a/suchwow/app.py b/suchwow/app.py index 57b05e0..1a5c7fb 100644 --- a/suchwow/app.py +++ b/suchwow/app.py @@ -3,13 +3,12 @@ import uuid import os import json import requests -from flask import Flask, g, request, redirect, url_for, abort +from flask import Flask, request, redirect, url_for, abort from flask import jsonify, render_template, flash, session from flask import send_from_directory, make_response from flask_session import Session -from playhouse.flask_utils import PaginatedQuery from werkzeug.utils import secure_filename -from suchwow.models import Meme, db +from suchwow.models import Post, Profile, Comment, Notification, db app = Flask(__name__) @@ -17,9 +16,9 @@ app.config.from_envvar("FLASK_SECRETS") app.secret_key = app.config["SECRET_KEY"] Session(app) -OPENID_URL = app.config["OIDC_URL"][0] -OPENID_CLIENT_ID = app.config["OIDC_CLIENT_ID"][0] -OPENID_CLIENT_SECRET = app.config["OIDC_CLIENT_SECRET"][0] +OPENID_URL = app.config["OIDC_URL"] +OPENID_CLIENT_ID = app.config["OIDC_CLIENT_ID"] +OPENID_CLIENT_SECRET = app.config["OIDC_CLIENT_SECRET"] OPENID_REDIRECT_URI = "http://localhost:5000/auth" @@ -27,9 +26,22 @@ def allowed_file(filename): return "." in filename and \ filename.rsplit(".", 1)[1].lower() in app.config["ALLOWED_EXTENSIONS"] +def debug_login(): + session["auth"] = { + "state": "active", + "session_state": "debug", + "code": "abcdefg", + "preferred_username": "debuguser", + "debug": True + } + return + def login_required(f): @wraps(f) def decorated_function(*args, **kwargs): + if app.debug: + debug_login() + return f(*args, **kwargs) if "auth" not in session or not session["auth"]: return redirect(url_for("login")) return f(*args, **kwargs) @@ -38,31 +50,31 @@ def login_required(f): @app.route("/") def index(): - page = request.args.get('page') + page = request.args.get("page", "1") if page.isdigit() is False: flash("Wow, wtf hackerman. Cool it.") page = 1 - memes = Meme.select().order_by(Meme.timestamp).paginate(int(page), 10) - return render_template("index.html", memes=memes, page=page) + posts = Post.select().order_by(Post.timestamp).paginate(int(page), 10) + return render_template("index.html", posts=posts, page=page) -@app.route("/meme/") -def meme(id): - if Meme.filter(id=id): - meme = Meme.get(Meme.id == id) - return render_template("meme.html", meme=meme) +@app.route("/post/") +def read_post(id): + if Post.filter(id=id): + post = Post.get(Post.id == id) + return render_template("post.html", post=post) else: return "no meme there brah" @app.route("/uploads/") def uploaded_file(filename): - return send_from_directory(app.config['UPLOAD_FOLDER'], filename) + return send_from_directory(app.config["UPLOAD_FOLDER"], filename) -@app.route("/submit", methods=["GET", "POST"]) +@app.route("/post/create", methods=["GET", "POST"]) @login_required -def submit(): +def create_post(): if request.method == "POST": - meme_title = request.form.get('title') + post_title = request.form.get("title") # check if the post request has the file part if "file" not in request.files: flash("You didn't upload a caliente meme, bro! You're fuckin up!") @@ -73,21 +85,25 @@ def submit(): if file.filename == "": flash("You didn't upload a caliente meme, bro! You're fuckin up!") return redirect(request.url) - if meme_title is "": + if post_title is "": flash("You didn't give your meme a spicy title, bro! You're fuckin up!") return redirect(request.url) if file and allowed_file(file.filename): filename = secure_filename(file.filename) save_path = os.path.join(app.config["UPLOAD_FOLDER"], filename) file.save(save_path) - meme = Meme( - title=meme_title, + # gen wallet + post = Post( + title=post_title, + subtitle=request.form.get("subtitle", ""), submitter=session["auth"]["preferred_username"], image_name=filename, + account_index=0, + address_index=0 ) - meme.save() - return redirect(url_for("meme", id=meme.id)) - return render_template("submit.html") + post.save() + return redirect(url_for("read_post", id=post.id)) + return render_template("create_post.html") @app.route("/login") def login(): @@ -99,7 +115,11 @@ def login(): f"response_type=code&" \ f"state={state}" - return redirect(url) + if app.debug: + debug_login() + return redirect(url_for("index")) + else: + return redirect(url) @app.route("/auth/") @@ -169,9 +189,9 @@ def logout(): def not_found(error): return make_response(jsonify({"error": "Page not found"}), 404) -@app.cli.command('dbinit') +@app.cli.command("dbinit") def dbinit(): - db.create_tables([Meme]) + db.create_tables([Post, Profile, Comment, Notification]) if __name__ == "__main__": app.run() diff --git a/suchwow/config.example.py b/suchwow/config.example.py index b7bae28..fda9a78 100644 --- a/suchwow/config.example.py +++ b/suchwow/config.example.py @@ -5,5 +5,5 @@ OIDC_REDIRECT_URL = 'http://localhost:5000/auth' SECRET = 'yyyyyyyyyyyyy', SESSION_TYPE = 'filesystem' UPLOAD_FOLDER = '/path/to/the/uploads' -ALLOWED_EXTENSIONS = set(['png', 'jpg', 'jpeg', 'gif']) -MAX_CONTENT_LENGTH = 16 * 1024 * 1024 +ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg', 'gif'} +MAX_CONTENT_LENGTH = 16 * 1024 * 1024 \ No newline at end of file diff --git a/suchwow/models.py b/suchwow/models.py index 7245471..6d9d979 100644 --- a/suchwow/models.py +++ b/suchwow/models.py @@ -4,11 +4,45 @@ from datetime import datetime db = SqliteDatabase('data/sqlite.db') -class Meme(Model): +class Post(Model): id = AutoField() title = CharField() + subtitle = CharField() + # submitter = ForeignKeyField(Profile, field=Profile.username) submitter = CharField() image_name = CharField() + readonly = BooleanField(default=False) + hidden = BooleanField(default=False) + account_index = IntegerField() + address_index = IntegerField() + timestamp = DateTimeField(default=datetime.now) + + class Meta: + database = db + +class Profile(Model): + id = AutoField() + username = CharField() + address = CharField() + notifications = IntegerField() + + class Meta: + database = db + +class Comment(Model): + id = AutoField() + comment = TextField() + commenter = ForeignKeyField(Profile, field=Profile.username) + post = ForeignKeyField(Post, field=id) + timestamp = DateTimeField(default=datetime.now) + + class Meta: + database = db + +class Notification(Model): + type = CharField() + message = TextField() + username = ForeignKeyField(Profile, field=Profile.username) timestamp = DateTimeField(default=datetime.now) class Meta: diff --git a/suchwow/routes/__init__.py b/suchwow/routes/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/suchwow/templates/submit.html b/suchwow/templates/create_post.html similarity index 86% rename from suchwow/templates/submit.html rename to suchwow/templates/create_post.html index 503abd5..cf29ee5 100644 --- a/suchwow/templates/submit.html +++ b/suchwow/templates/create_post.html @@ -5,6 +5,7 @@

Upload new File


+
diff --git a/suchwow/templates/index.html b/suchwow/templates/index.html index c10fe1b..10d6f6a 100644 --- a/suchwow/templates/index.html +++ b/suchwow/templates/index.html @@ -2,7 +2,7 @@ {% block content %} - Submit A Meme
+ Submit A Meme
Debug {% block header %} @@ -10,10 +10,11 @@ {% endblock %} {{ page }} - {% endblock %} + +{% endblock %} diff --git a/suchwow/templates/meme.html b/suchwow/templates/meme.html deleted file mode 100644 index 6b28e0a..0000000 --- a/suchwow/templates/meme.html +++ /dev/null @@ -1,14 +0,0 @@ -{% extends 'base.html' %} - -{% block header %} -

{% block title %}View Meme{% endblock %}

-{% endblock %} - -{% block content %} -

Submitter: {{ session['auth']['preferred_username'] }}

-

ID: {{ meme.id }}

-

Title: {{ meme.title }}

-

Submitted: {{ meme.timestamp }}

-

Image Name: {{ meme.image_name }}

- -{% endblock %} diff --git a/suchwow/templates/post.html b/suchwow/templates/post.html new file mode 100644 index 0000000..4f35c47 --- /dev/null +++ b/suchwow/templates/post.html @@ -0,0 +1,19 @@ +{% extends 'base.html' %} + +{% block header %} +

{% block title %}View Post{% endblock %}

+{% endblock %} + +{% block content %} + {% if post.hidden %} +You cannot see this post + {% else %} +

Submitter: {{ post.submitter }}

+

ID: {{ post.id }}

+

Title: {{ post.title }}

+

Subtitle: {{ post.subtitle }}

+

Submitted: {{ post.timestamp }}

+

Image Name: {{ post.image_name }}

+ + {% endif %} +{% endblock %}