diff --git a/yadc/__init__.py b/yadc/__init__.py index 1ad61c0..d8f8bad 100644 --- a/yadc/__init__.py +++ b/yadc/__init__.py @@ -6,8 +6,8 @@ from flask_migrate import Migrate from flask_login import LoginManager from flask_assets import Environment as AssetsEnvironment, Bundle as AssetsBundle -# from flask_admin import Admin -# from flask_admin.contrib.sqla import ModelView +from flask_admin import Admin +from flask_admin.contrib.sqla import ModelView db = SQLAlchemy() migrate = Migrate() @@ -15,7 +15,7 @@ login = LoginManager() assets = AssetsEnvironment() # THIS FUCKER DARES TO MESS WITH MY BLUEPRINTS (that blueprint collision shitxception) -# admin = Admin() +admin = Admin() def create_app(): @@ -27,7 +27,7 @@ def create_app(): MAX_CONTENT_LENGTH=10*1024*1024, POST_LIST_THUMB_HEIGHT=200, POST_UPLOADS=os.path.join(app.instance_path, 'post'), - INSTANCE_NAME='YADC', + INSTANCE_NAME='YaDc', POSTS_PER_PAGE=4, SQLALCHEMY_ECHO=True, @@ -45,11 +45,11 @@ def create_app(): from yadc import models - # admin.init_app(app) - # admin.add_view(ModelView(models.User, db.session)) - # admin.add_view(ModelView(models.Post, db.session)) - # admin.add_view(ModelView(models.Tag, db.session)) - # admin.add_view(ModelView(models.Comment, db.session)) + admin.init_app(app) + admin.add_view(ModelView(models.User, db.session, endpoint='admin_user')) + admin.add_view(ModelView(models.Post, db.session, endpoint='admin_post')) + admin.add_view(ModelView(models.Tag, db.session, endpoint='admin_tag')) + admin.add_view(ModelView(models.Comment, db.session, endpoint='admin_comment')) from yadc.bp import main, post, auth app.register_blueprint(main.bp) diff --git a/yadc/bp/main.py b/yadc/bp/main.py index 61496a5..d1aa061 100644 --- a/yadc/bp/main.py +++ b/yadc/bp/main.py @@ -1,16 +1,8 @@ -import io import os from flask import (Blueprint, abort, current_app, flash, redirect, render_template, request, send_from_directory, url_for) -from flask_login import current_user, login_required -from PIL import Image -from sqlalchemy import func -from sqlalchemy.orm import aliased - -from yadc import db -from yadc.forms import UploadPostForm -from yadc.models import FILETYPE, RATING, Post, Tag + bp = Blueprint('main', __name__) diff --git a/yadc/bp/post.py b/yadc/bp/post.py index 55b1647..d588177 100644 --- a/yadc/bp/post.py +++ b/yadc/bp/post.py @@ -9,8 +9,8 @@ from sqlalchemy import func from sqlalchemy.orm import aliased from yadc import db -from yadc.forms import UploadPostForm -from yadc.models import FILETYPE, RATING, Post, Tag +from yadc.forms import UploadForm, CommentForm +from yadc.models import FILETYPE, RATING, Post, Tag, Comment from yadc.utils import query_replace bp = Blueprint('post', __name__) @@ -77,12 +77,15 @@ def post_show(id): tag.count = count tag.endpoint = query_replace({'tags': tag.content.replace(' ','_')}, url_for('.posts')) - return render_template('post.html', post=post, tags=post.tags) + form = CommentForm() + form.post_id.data = post.id + + return render_template('post.html', post=post, tags=post.tags, comments=post.comments, comment_form=form) @bp.route('/upload', methods=['GET', 'POST']) @login_required -def post_upload(): - form = UploadPostForm(request.form) +def upload(): + form = UploadForm(request.form) if request.method == 'POST' and form.validate(): file = request.files.get(form.post_img.name) file.data = io.BytesIO(file.read()) @@ -112,6 +115,22 @@ def post_upload(): flash('Successfully submitted {}'.format(str(post))) - return redirect(url_for('.post_upload')) + return redirect(url_for('.upload')) return render_template('upload.html', form=form) + +@bp.route('comment', methods=['POST']) +@login_required +def comment(): + form = CommentForm(request.form) + if request.method == 'POST' and form.validate(): + comment = Comment(content=form.content.data, post_id=form.post_id.data, user=current_user) + + db.session.add(comment) + db.session.commit() + + flash('Successfully submitted {}'.format(str(comment))) + + return redirect(url_for('.post_show', id=form.post_id.data)) + + return redirect(url_for('.posts')) \ No newline at end of file diff --git a/yadc/forms.py b/yadc/forms.py index d271314..f608547 100644 --- a/yadc/forms.py +++ b/yadc/forms.py @@ -1,5 +1,5 @@ from wtforms import Form -from wtforms import StringField, PasswordField, BooleanField, SubmitField, FileField, MultipleFileField, ValidationError, RadioField +from wtforms import StringField, PasswordField, BooleanField, SubmitField, FileField, MultipleFileField, ValidationError, RadioField, TextAreaField, HiddenField from wtforms.validators import DataRequired, InputRequired, Email, EqualTo, AnyOf from werkzeug.utils import cached_property @@ -61,7 +61,7 @@ def validate_file(form, field): if not file or file.filename == '': raise ValidationError('Please select a file') -class UploadPostForm(CSRFForm): +class UploadForm(CSRFForm): post_img = FileField('Image', validators=[validate_file], render_kw={'required':''}) sauce = StringField('Sauce', validators=[DataRequired()]) tags = StringField('Tags') @@ -80,5 +80,7 @@ class UploadPostForm(CSRFForm): if client_mimetype not in ['image/png','image/jpeg']: raise ValidationError('Please select an image file of PNG or JPEG format') - -# class CommentForm(CSRFForm): +class CommentForm(CSRFForm): + post_id = HiddenField(validators=[DataRequired()]) + content = TextAreaField('Comment', validators=[DataRequired()]) + submit = SubmitField('Send') \ No newline at end of file diff --git a/yadc/models.py b/yadc/models.py index d4e5933..d5b97e5 100644 --- a/yadc/models.py +++ b/yadc/models.py @@ -44,6 +44,10 @@ class TAG_CATEGORY(enum.Enum): character = 4 copyright = 5 +# class COMMENT_STATUS(enum.Enum): +# visible = 0 +# deleted = 2 + class TimestampMixin(object): created = db.Column(UtcDateTime, nullable=False, default=utcnow()) updated = db.Column(UtcDateTime, nullable=False, onupdate=utcnow(), default=utcnow()) @@ -78,7 +82,8 @@ class User(UserMixin, TimestampMixin, db.Model): @property def is_moderator(self): - return self.op_level in [OP_LEVEL.moderator, OP_LEVEL.admin] + # return self.op_level in [OP_LEVEL.moderator, OP_LEVEL.admin] + return self.op_level.value >= OP_LEVEL.moderator.value @property def is_admin(self): @@ -192,7 +197,11 @@ class Tag(TimestampMixin, db.Model): class Comment(TimestampMixin, db.Model): id = db.Column(db.Integer, primary_key=True) - content = db.Column(db.String(512)) + content = db.Column(db.String(512), nullable=False) + + # status = db.Column(db.Enum(COMMENT_STATUS), default=COMMENT_STATUS.visible) + deleted = db.Column(db.Boolean, default=False) + delete_reason = db.Column(db.String(512)) post_id = db.Column(db.Integer, db.ForeignKey('post.id')) post = db.relationship('Post', backref=db.backref('comments', lazy=True)) diff --git a/yadc/static/default.scss b/yadc/static/default.scss index 06f0c00..05fdc9e 100644 --- a/yadc/static/default.scss +++ b/yadc/static/default.scss @@ -397,17 +397,35 @@ header { section.comments { padding: 10px; + @include media($bp-desktop) { margin-left: $side-panel-width; } - article { overflow: hidden; } - h4 { - margin-top: .5em; - margin-bottom: .5em; + > .comment_container { + margin-left: 10px; + + article { overflow: hidden; } + h4 { + // margin-top: .5em; + margin: 0; + margin-bottom: .5em; + } + p { + margin-top: .5em; + + &.deleted { + color: red; + } + } } - p { - margin-top: .5em; + + > .form > form { + margin-left: 10px; + + input:required { + box-shadow: none; + } } } } diff --git a/yadc/templates/_includes.html b/yadc/templates/_includes.html index 1e5cab3..dfc6a53 100644 --- a/yadc/templates/_includes.html +++ b/yadc/templates/_includes.html @@ -42,4 +42,22 @@
{{ comment.user.username }}
+ {% if not comment.deleted %} +{{ comment.content }}
+ {% else %} +{{ comment.delete_reason }}
+ {% endif %} +No comments so far.
+ {% endif %} +