|
|
@ -9,7 +9,7 @@ from sqlalchemy import func
|
|
|
|
from sqlalchemy.orm import aliased
|
|
|
|
from sqlalchemy.orm import aliased
|
|
|
|
|
|
|
|
|
|
|
|
from yadc import db
|
|
|
|
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.models import FILETYPE, RATING, Post, Tag, Comment
|
|
|
|
from yadc.utils import query_replace
|
|
|
|
from yadc.utils import query_replace
|
|
|
|
|
|
|
|
|
|
|
@ -20,46 +20,31 @@ bp = Blueprint('post', __name__)
|
|
|
|
@bp.route('/<int:page>')
|
|
|
|
@bp.route('/<int:page>')
|
|
|
|
def posts(page):
|
|
|
|
def posts(page):
|
|
|
|
def tags_prepare(posts):
|
|
|
|
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()
|
|
|
|
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:
|
|
|
|
for tag,p_ids in tags:
|
|
|
|
tag.count = len(p_ids)
|
|
|
|
tag.count = len(p_ids)
|
|
|
|
tag.post_ids = p_ids
|
|
|
|
tag.post_ids = p_ids
|
|
|
|
tag.endpoint = query_replace({'tags': tag.content.replace(' ','_')}, url_for('.posts'))
|
|
|
|
tag.endpoint = query_replace({'tags': tag.content}, url_for('.posts'))
|
|
|
|
# tags = list(tagset)
|
|
|
|
|
|
|
|
tags = [t[0] for t in tags]
|
|
|
|
tags = [t[0] for t in tags]
|
|
|
|
tags.sort(key=lambda x: (x.category.value, x.content) )
|
|
|
|
tags.sort(key=lambda x: (x.category.value, x.content) )
|
|
|
|
return tags
|
|
|
|
return tags
|
|
|
|
|
|
|
|
|
|
|
|
def parse_args():
|
|
|
|
# PARSING ARGUMENTS
|
|
|
|
args = request.args
|
|
|
|
f_tags = request.args.get('tags', '').split()
|
|
|
|
tags = (args.get('tags') or '').split(' ')
|
|
|
|
m_ratings = RATING.matched(request.args.get('rating'))
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
f_tags, f_rating = parse_args()
|
|
|
|
|
|
|
|
posts_query = Post.query
|
|
|
|
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.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'))
|
|
|
|
posts = posts_query.paginate(page, current_app.config.get('POSTS_PER_PAGE'))
|
|
|
|
tags = tags_prepare(posts.items)
|
|
|
|
tags = tags_prepare(posts.items)
|
|
|
|
|
|
|
|
|
|
|
|
flash(parse_args())
|
|
|
|
flash(f_tags)
|
|
|
|
|
|
|
|
flash(m_ratings)
|
|
|
|
# flash(posts.items)
|
|
|
|
# flash(posts.items)
|
|
|
|
# flash(tags)
|
|
|
|
# flash(tags)
|
|
|
|
|
|
|
|
|
|
|
@ -71,17 +56,50 @@ def post_show(id):
|
|
|
|
# flash(post)
|
|
|
|
# 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()
|
|
|
|
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:
|
|
|
|
form = CommentForm(post_id=post.id)
|
|
|
|
tag, count = tag_count
|
|
|
|
|
|
|
|
tag.count = count
|
|
|
|
|
|
|
|
tag.endpoint = query_replace({'tags': tag.content.replace(' ','_')}, url_for('.posts'))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
form = CommentForm()
|
|
|
|
for comment in post.comments:
|
|
|
|
form.post_id.data = post.id
|
|
|
|
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)
|
|
|
|
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'])
|
|
|
|
@bp.route('/upload', methods=['GET', 'POST'])
|
|
|
|
@login_required
|
|
|
|
@login_required
|
|
|
|
def upload():
|
|
|
|
def upload():
|
|
|
@ -90,7 +108,7 @@ def upload():
|
|
|
|
file = request.files.get(form.post_img.name)
|
|
|
|
file = request.files.get(form.post_img.name)
|
|
|
|
file.data = io.BytesIO(file.read())
|
|
|
|
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)
|
|
|
|
post = Post(file, source=form.sauce.data, rating=RATING[form.rating.data], author=current_user)
|
|
|
|
|
|
|
|
|
|
|
@ -105,6 +123,10 @@ def upload():
|
|
|
|
|
|
|
|
|
|
|
|
if post.jpeg_path is not None:
|
|
|
|
if post.jpeg_path is not None:
|
|
|
|
im.save(post.jpeg_path, 'JPEG', quality=80)
|
|
|
|
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.thumbnail([512,512])
|
|
|
|
im.save(post.thumb_path, 'JPEG', quality=80)
|
|
|
|
im.save(post.thumb_path, 'JPEG', quality=80)
|
|
|
@ -118,20 +140,4 @@ def upload():
|
|
|
|
#return redirect(url_for('.upload'))
|
|
|
|
#return redirect(url_for('.upload'))
|
|
|
|
return redirect(url_for('.posts'))
|
|
|
|
return redirect(url_for('.posts'))
|
|
|
|
|
|
|
|
|
|
|
|
return render_template('post/upload.html', form=form)
|
|
|
|
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'))
|
|
|
|
|