|
|
|
@ -9,7 +9,7 @@ from sqlalchemy import func
|
|
|
|
|
from sqlalchemy.orm import aliased
|
|
|
|
|
|
|
|
|
|
from yadc import db
|
|
|
|
|
from yadc.forms import UploadForm, CommentForm
|
|
|
|
|
from yadc.forms import UploadForm, CommentForm, EditCommentForm
|
|
|
|
|
from yadc.models import FILETYPE, RATING, Post, Tag, Comment
|
|
|
|
|
from yadc.utils import query_replace
|
|
|
|
|
|
|
|
|
@ -20,46 +20,31 @@ bp = Blueprint('post', __name__)
|
|
|
|
|
@bp.route('/<int:page>')
|
|
|
|
|
def posts(page):
|
|
|
|
|
def tags_prepare(posts):
|
|
|
|
|
# tags = db.session.query(Tag, func.array_agg(posts.c.id)).join(posts, Tag.posts).group_by(Tag.id).all()
|
|
|
|
|
tags = db.session.query(Tag, func.array_agg(Post.id)).join(Tag.posts).filter(Post.id.in_([p.id for p in posts])).group_by(Tag.id).all()
|
|
|
|
|
|
|
|
|
|
# tagset = set()
|
|
|
|
|
# taglist = list()
|
|
|
|
|
# for post in posts:
|
|
|
|
|
# for tag in post.tags:
|
|
|
|
|
# tagset.add(tag)
|
|
|
|
|
# taglist.append(tag)
|
|
|
|
|
# for tag in tagset:
|
|
|
|
|
# tag.count = taglist.count(tag)
|
|
|
|
|
|
|
|
|
|
for tag,p_ids in tags:
|
|
|
|
|
tag.count = len(p_ids)
|
|
|
|
|
tag.post_ids = p_ids
|
|
|
|
|
tag.endpoint = query_replace({'tags': tag.content.replace(' ','_')}, url_for('.posts'))
|
|
|
|
|
# tags = list(tagset)
|
|
|
|
|
tag.endpoint = query_replace({'tags': tag.content}, url_for('.posts'))
|
|
|
|
|
|
|
|
|
|
tags = [t[0] for t in tags]
|
|
|
|
|
tags.sort(key=lambda x: (x.category.value, x.content) )
|
|
|
|
|
return tags
|
|
|
|
|
|
|
|
|
|
def parse_args():
|
|
|
|
|
args = request.args
|
|
|
|
|
tags = (args.get('tags') or '').split(' ')
|
|
|
|
|
tags = [t.replace('_',' ') for t in tags] if tags[0] != '' else []
|
|
|
|
|
rating = {r.name : r for r in RATING}.get(args.get('rating')) or RATING.safe
|
|
|
|
|
matched_ratings = [r for r in RATING if r.value<=rating.value]
|
|
|
|
|
# page = int(args.get('page') or 1)
|
|
|
|
|
return (tags, matched_ratings)
|
|
|
|
|
# PARSING ARGUMENTS
|
|
|
|
|
f_tags = request.args.get('tags', '').split()
|
|
|
|
|
m_ratings = RATING.matched(request.args.get('rating'))
|
|
|
|
|
|
|
|
|
|
f_tags, f_rating = parse_args()
|
|
|
|
|
posts_query = Post.query
|
|
|
|
|
if len(f_tags)>0:
|
|
|
|
|
if f_tags:
|
|
|
|
|
posts_query = posts_query.join(Post.tags).group_by(Post.id).filter(Tag.content.in_(f_tags)).having(func.count(Post.id)==len(f_tags))
|
|
|
|
|
posts_query = posts_query.filter(Post.rating.in_(f_rating)).order_by(Post.created) #.offset((page-1)*posts_on_page).limit(posts_on_page)
|
|
|
|
|
posts_query = posts_query.filter(Post.rating.in_(m_ratings)).order_by(Post.created) #.offset((page-1)*posts_on_page).limit(posts_on_page)
|
|
|
|
|
|
|
|
|
|
posts = posts_query.paginate(page, current_app.config.get('POSTS_PER_PAGE'))
|
|
|
|
|
tags = tags_prepare(posts.items)
|
|
|
|
|
|
|
|
|
|
flash(parse_args())
|
|
|
|
|
flash(f_tags)
|
|
|
|
|
flash(m_ratings)
|
|
|
|
|
# flash(posts.items)
|
|
|
|
|
# flash(tags)
|
|
|
|
|
|
|
|
|
@ -71,17 +56,50 @@ def post_show(id):
|
|
|
|
|
# flash(post)
|
|
|
|
|
|
|
|
|
|
tags_count = db.session.query(Tag, func.count(Post.id)).join(Tag.posts).filter(Post.id==id).join(aliased(Post), Tag.posts).group_by(Tag).all()
|
|
|
|
|
for tag,tag.count in tags_count:
|
|
|
|
|
tag.endpoint = query_replace({'tags': tag.content}, url_for('.posts'))
|
|
|
|
|
|
|
|
|
|
for tag_count in tags_count:
|
|
|
|
|
tag, count = tag_count
|
|
|
|
|
tag.count = count
|
|
|
|
|
tag.endpoint = query_replace({'tags': tag.content.replace(' ','_')}, url_for('.posts'))
|
|
|
|
|
form = CommentForm(post_id=post.id)
|
|
|
|
|
|
|
|
|
|
form = CommentForm()
|
|
|
|
|
form.post_id.data = post.id
|
|
|
|
|
for comment in post.comments:
|
|
|
|
|
comment.editform = EditCommentForm(comment_id=comment.id, content=comment.content)
|
|
|
|
|
|
|
|
|
|
return render_template('post/post.html', post=post, tags=post.tags, comments=post.comments, comment_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'))
|
|
|
|
|
|
|
|
|
|
@bp.route('edit_comment', methods=['POST'])
|
|
|
|
|
@login_required
|
|
|
|
|
def comment_edit():
|
|
|
|
|
form = EditCommentForm(request.form)
|
|
|
|
|
flash(str(request.form))
|
|
|
|
|
if request.method == 'POST' and form.validate():
|
|
|
|
|
comment = Comment.query.filter_by(id=form.comment_id.data).first()
|
|
|
|
|
comment.content = form.content.data
|
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
|
|
flash('Successfully edited {}'.format(str(comment)))
|
|
|
|
|
|
|
|
|
|
return redirect(url_for('.post_show', id=comment.post_id))
|
|
|
|
|
|
|
|
|
|
return redirect(url_for('.posts'))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@bp.route('/upload', methods=['GET', 'POST'])
|
|
|
|
|
@login_required
|
|
|
|
|
def upload():
|
|
|
|
@ -90,7 +108,7 @@ def upload():
|
|
|
|
|
file = request.files.get(form.post_img.name)
|
|
|
|
|
file.data = io.BytesIO(file.read())
|
|
|
|
|
|
|
|
|
|
# tagy
|
|
|
|
|
# tags = form.tags.data.split()
|
|
|
|
|
|
|
|
|
|
post = Post(file, source=form.sauce.data, rating=RATING[form.rating.data], author=current_user)
|
|
|
|
|
|
|
|
|
@ -106,6 +124,10 @@ def upload():
|
|
|
|
|
if post.jpeg_path is not None:
|
|
|
|
|
im.save(post.jpeg_path, 'JPEG', quality=80)
|
|
|
|
|
|
|
|
|
|
sim = im.copy()
|
|
|
|
|
sim.thumbnail([800,800])
|
|
|
|
|
sim.save(post.sample_path, 'JPEG', quality=80)
|
|
|
|
|
|
|
|
|
|
im.thumbnail([512,512])
|
|
|
|
|
im.save(post.thumb_path, 'JPEG', quality=80)
|
|
|
|
|
|
|
|
|
@ -119,19 +141,3 @@ def upload():
|
|
|
|
|
return redirect(url_for('.posts'))
|
|
|
|
|
|
|
|
|
|
return render_template('post/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'))
|