From cf21dc291714fc98e304662056bae87ac88e313d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Ku=C5=BE=C3=ADlek?= Date: Thu, 13 Feb 2020 15:15:18 +0100 Subject: [PATCH] Minor changes preparing for editing user settings --- Pipfile | 2 ++ Pipfile.lock | 21 +++++++++++++++------ yadc/__init__.py | 6 +++++- yadc/bp/post.py | 2 +- yadc/models.py | 25 ++++++++++++++++++++++++- yadc/static/default.scss | 7 ++++++- yadc/templates/_includes.html | 20 ++++++++++++++++---- yadc/templates/post.html | 5 ++++- 8 files changed, 73 insertions(+), 15 deletions(-) diff --git a/Pipfile b/Pipfile index bfe4710..1792fe8 100644 --- a/Pipfile +++ b/Pipfile @@ -24,6 +24,8 @@ python-magic = "*" flask-mail = "*" sqlalchemy-utc = "*" flask-admin = "*" +sqlalchemy-utils = "*" +werkzeug = "==0.16.1" [requires] python_version = "3.8" diff --git a/Pipfile.lock b/Pipfile.lock index d8a9049..d1e5771 100644 --- a/Pipfile.lock +++ b/Pipfile.lock @@ -1,7 +1,7 @@ { "_meta": { "hash": { - "sha256": "4aa69f03ba5d5f31ea39beeefc97e9a9b61946c3275fb53e58897aa33c9ffc1c" + "sha256": "90470384a8160ec4176abf18d39b07271e755ae437ead34f7b70d914c514d709" }, "pipfile-spec": 6, "requires": { @@ -18,9 +18,9 @@ "default": { "alembic": { "hashes": [ - "sha256:d412982920653db6e5a44bfd13b1d0db5685cbaaccaf226195749c706e1e862a" + "sha256:2df2519a5b002f881517693b95626905a39c5faf4b5a1f94de4f1441095d1d26" ], - "version": "==1.3.3" + "version": "==1.4.0" }, "blinker": { "hashes": [ @@ -97,11 +97,11 @@ }, "flask-wtf": { "hashes": [ - "sha256:5d14d55cfd35f613d99ee7cba0fc3fbbe63ba02f544d349158c14ca15561cc36", - "sha256:d9a9e366b32dcbb98ef17228e76be15702cd2600675668bca23f63a7947fd5ac" + "sha256:57b3faf6fe5d6168bda0c36b0df1d05770f8e205e18332d0376ddb954d17aef2", + "sha256:d417e3a0008b5ba583da1763e4db0f55a1269d9dd91dcc3eb3c026d3c5dbd720" ], "index": "pypi", - "version": "==0.14.2" + "version": "==0.14.3" }, "itsdangerous": { "hashes": [ @@ -281,6 +281,13 @@ "index": "pypi", "version": "==0.10.0" }, + "sqlalchemy-utils": { + "hashes": [ + "sha256:4e637c88bf3ac5f99b7d72342092a1f636bea1287b2e3e17d441b0413771f86e" + ], + "index": "pypi", + "version": "==0.36.1" + }, "webassets": { "hashes": [ "sha256:167132337677c8cedc9705090f6d48da3fb262c8e0b2773b29f3352f050181cd", @@ -293,6 +300,7 @@ "sha256:1e0dedc2acb1f46827daa2e399c1485c8fa17c0d8e70b6b875b4e7f54bf408d2", "sha256:b353856d37dec59d6511359f97f6a4b2468442e454bd1c98298ddce53cac1f04" ], + "index": "pypi", "version": "==0.16.1" }, "wtforms": { @@ -469,6 +477,7 @@ "sha256:1e0dedc2acb1f46827daa2e399c1485c8fa17c0d8e70b6b875b4e7f54bf408d2", "sha256:b353856d37dec59d6511359f97f6a4b2468442e454bd1c98298ddce53cac1f04" ], + "index": "pypi", "version": "==0.16.1" }, "wrapt": { diff --git a/yadc/__init__.py b/yadc/__init__.py index d8f8bad..5a076fb 100644 --- a/yadc/__init__.py +++ b/yadc/__init__.py @@ -56,7 +56,11 @@ def create_app(): app.register_blueprint(post.bp, url_prefix='/post') app.register_blueprint(auth.bp, url_prefix='/user') login.login_view = 'auth.login' - + + from yadc import utils + @app.context_processor + def utility_processor(): + return dict(utils=utils) #assets.url = app.static_url_path scss = AssetsBundle('default.scss', filters='libsass', output='all.css') diff --git a/yadc/bp/post.py b/yadc/bp/post.py index d588177..9b4f8e0 100644 --- a/yadc/bp/post.py +++ b/yadc/bp/post.py @@ -63,7 +63,7 @@ def posts(page): # flash(posts.items) # flash(tags) - return render_template('index.html', posts=posts.items, tags=tags, pagination=posts, query_replace=query_replace) + return render_template('index.html', posts=posts.items, tags=tags, pagination=posts) @bp.route('/show/') def post_show(id): diff --git a/yadc/models.py b/yadc/models.py index d5b97e5..2842b84 100644 --- a/yadc/models.py +++ b/yadc/models.py @@ -4,7 +4,7 @@ import os from datetime import datetime from flask import current_app, url_for -from flask_login import UserMixin, login_user, logout_user +from flask_login import UserMixin, login_user, logout_user, current_user from PIL import Image from sqlalchemy_utc import UtcDateTime, utcnow from werkzeug.security import check_password_hash, generate_password_hash @@ -61,6 +61,10 @@ class User(UserMixin, TimestampMixin, db.Model): user_status = db.Column(db.Enum(USER_STATUS), default=USER_STATUS.active, nullable=False) last_login = db.Column(UtcDateTime) + ban_status = None + ban_until = None + ban_reason = None + #authored_posts = db.relationship('Post', back_populates='author') #approved_posts = db.relationship('Post', back_populates='approver') @@ -93,6 +97,14 @@ class User(UserMixin, TimestampMixin, db.Model): def load_user(id): return User.query.get(int(id)) +class UserPreferences(db.Model): + user_id = db.Column(db.Integer, db.ForeignKey('user.id'), primary_key=True) + user = db.relationship('User', backref=db.backref('profile', uselist=False)) + + rating = db.Column(db.Enum(RATING), default=RATING.safe, nullable=False) + tag_blacklist = None + + post_tags = db.Table('post_tags', db.metadata, db.Column('post_id', db.Integer, db.ForeignKey('post.id')), db.Column('tag_id', db.Integer, db.ForeignKey('tag.id')) @@ -184,6 +196,11 @@ class Post(TimestampMixin, db.Model): def is_approved(self): return self.status is POST_STATUS.active + @property + def can_edit(self): + author = current_user == self.author + moderator = current_user.is_moderator + return author or moderator class Tag(TimestampMixin, db.Model): id = db.Column(db.Integer, primary_key=True) @@ -208,4 +225,10 @@ class Comment(TimestampMixin, db.Model): user_id = db.Column(db.Integer, db.ForeignKey('user.id')) user = db.relationship('User', backref=db.backref('comments', lazy=True)) + @property + def can_edit(self): + author = current_user == self.user + moderator = current_user.is_moderator + return author or moderator + # THINK ABOUT LAZY LOADING diff --git a/yadc/static/default.scss b/yadc/static/default.scss index 05fdc9e..a9d0d30 100644 --- a/yadc/static/default.scss +++ b/yadc/static/default.scss @@ -403,7 +403,7 @@ header { } > .comment_container { - margin-left: 10px; + padding: 0 10px; article { overflow: hidden; } h4 { @@ -418,6 +418,11 @@ header { color: red; } } + + .head { + display: flex; + justify-content: space-between; + } } > .form > form { diff --git a/yadc/templates/_includes.html b/yadc/templates/_includes.html index dfc6a53..51fe968 100644 --- a/yadc/templates/_includes.html +++ b/yadc/templates/_includes.html @@ -18,12 +18,12 @@ {% macro render_pagination(endpoint) %}
{% if pagination.has_prev %} - + {% endif %} {% for page in pagination.iter_pages(left_edge=1, left_current=1, right_current=2, right_edge=1) %} {% if page %} {% if page != pagination.page %} - {{ page }} + {{ page }} {% else %} {{ page }} {% endif %} @@ -32,7 +32,7 @@ {% endif %} {% endfor %} {% if pagination.has_next %} - + {% endif %}
{% endmacro %} @@ -48,7 +48,12 @@
{% for comment in comments %}
-

{{ comment.user.username }}

+
+

{{ comment.user.username }}

+ {% if comment.can_edit %} + edit + {% endif %} +
{% if not comment.deleted %}

{{ comment.content }}

{% else %} @@ -60,4 +65,11 @@

No comments so far.

{% endif %}
+{% endmacro %} + +{% macro render_post_edit() %} + {% endmacro %} \ No newline at end of file diff --git a/yadc/templates/post.html b/yadc/templates/post.html index 521bf51..295db7b 100644 --- a/yadc/templates/post.html +++ b/yadc/templates/post.html @@ -1,5 +1,5 @@ {% extends 'base.html' %} -{% from '_includes.html' import render_tags, render_comments with context %} +{% from '_includes.html' import render_tags, render_comments, render_post_edit with context %} {% from "_formhelpers.html" import errors %} {% block content %} @@ -27,6 +27,9 @@ {{ render_tags() }} + {% if post.can_edit %} + {{ render_post_edit() }} + {% endif %}