|
|
|
@ -8,54 +8,90 @@ from PIL import Image
|
|
|
|
|
from sqlalchemy import func
|
|
|
|
|
from sqlalchemy.orm import aliased
|
|
|
|
|
|
|
|
|
|
from werkzeug.urls import Href
|
|
|
|
|
|
|
|
|
|
from yadc import db
|
|
|
|
|
from yadc.forms import UploadPostForm
|
|
|
|
|
from yadc.models import FILETYPE, RATING, Post, Tag, post_tags
|
|
|
|
|
|
|
|
|
|
def query_replace(dic, base=''):
|
|
|
|
|
args = request.args.to_dict()
|
|
|
|
|
return Href(base)(args.update(dic) or args)
|
|
|
|
|
|
|
|
|
|
bp = Blueprint('main', __name__)
|
|
|
|
|
|
|
|
|
|
@bp.route('/')
|
|
|
|
|
def index():
|
|
|
|
|
return posts()
|
|
|
|
|
return redirect(url_for('main.posts'))
|
|
|
|
|
|
|
|
|
|
# @bp.route('/')
|
|
|
|
|
@bp.route('/post/')
|
|
|
|
|
def posts():
|
|
|
|
|
posts = Post.query.order_by(Post.created).limit(20).all()
|
|
|
|
|
# flash(posts)
|
|
|
|
|
@bp.route('/post', defaults={'page': 1}
|
|
|
|
|
)
|
|
|
|
|
@bp.route('/post/<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()
|
|
|
|
|
|
|
|
|
|
# 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('main.posts'))
|
|
|
|
|
# tags = list(tagset)
|
|
|
|
|
tags = [t[0] for t in tags]
|
|
|
|
|
tags.sort(key=lambda x: (x.category.value, x.content) )
|
|
|
|
|
return tags
|
|
|
|
|
|
|
|
|
|
tagset = set()
|
|
|
|
|
taglist = list()
|
|
|
|
|
for post in posts:
|
|
|
|
|
for tag in post.tags:
|
|
|
|
|
tagset.add(tag)
|
|
|
|
|
taglist.append(tag)
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
|
for tag in tagset:
|
|
|
|
|
tag.count = taglist.count(tag)
|
|
|
|
|
posts_on_page = 4
|
|
|
|
|
|
|
|
|
|
f_tags, f_rating = parse_args()
|
|
|
|
|
posts_query = Post.query
|
|
|
|
|
if len(f_tags)>0:
|
|
|
|
|
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 = posts_query.all()
|
|
|
|
|
tags = tags_prepare(posts_query.subquery())
|
|
|
|
|
|
|
|
|
|
flash(parse_args())
|
|
|
|
|
# flash(posts)
|
|
|
|
|
# flash(tags)
|
|
|
|
|
|
|
|
|
|
tags = list(tagset)
|
|
|
|
|
tags.sort(key=lambda x: (x.category.value, x.content) )
|
|
|
|
|
# flash([tag.count for tag in tags])
|
|
|
|
|
return render_template('index.html', posts=posts, tags=tags)
|
|
|
|
|
|
|
|
|
|
@bp.route('/post/show/<id>/')
|
|
|
|
|
@bp.route('/post/show/<id>')
|
|
|
|
|
def post_show(id):
|
|
|
|
|
post = Post.query.filter_by(id=id).first()
|
|
|
|
|
# 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 = list()
|
|
|
|
|
for tag_count in tags_count:
|
|
|
|
|
tag = tag_count[0]
|
|
|
|
|
tag.count = tag_count[1]
|
|
|
|
|
tags.append(tag)
|
|
|
|
|
tag, count = tag_count
|
|
|
|
|
tag.count = count
|
|
|
|
|
tag.endpoint = query_replace({'tags': tag.content.replace(' ','_')}, url_for('main.posts'))
|
|
|
|
|
|
|
|
|
|
return render_template('post.html', post=post, tags=tags)
|
|
|
|
|
return render_template('post.html', post=post, tags=post.tags)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@bp.route('/post/upload/', methods=['GET', 'POST'])
|
|
|
|
|
@bp.route('/post/upload', methods=['GET', 'POST'])
|
|
|
|
|
@login_required
|
|
|
|
|
def post_upload():
|
|
|
|
|
form = UploadPostForm(request.form)
|
|
|
|
@ -113,15 +149,15 @@ def uploaded_thumb(*args, **kwargs):
|
|
|
|
|
return uploaded_img(*args, **kwargs, store='thumb', exten='jpg')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@bp.route('/threads/')
|
|
|
|
|
@bp.route('/threads')
|
|
|
|
|
def threads():
|
|
|
|
|
return render_template('index.html')
|
|
|
|
|
|
|
|
|
|
@bp.route('/user/show/<username>/')
|
|
|
|
|
@bp.route('/user/show/<username>')
|
|
|
|
|
def user_profile(username):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
@bp.route('/user/settings/')
|
|
|
|
|
@bp.route('/user/settings')
|
|
|
|
|
@login_required
|
|
|
|
|
def user_settings():
|
|
|
|
|
pass
|
|
|
|
|