Great reorganization, rip flask-admin
parent
9464b2f3c0
commit
8255913c58
@ -0,0 +1,117 @@
|
|||||||
|
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
|
||||||
|
from yadc.utils import query_replace
|
||||||
|
|
||||||
|
bp = Blueprint('post', __name__)
|
||||||
|
|
||||||
|
# @bp.route('/')
|
||||||
|
@bp.route('', defaults={'page': 1})
|
||||||
|
@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)
|
||||||
|
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)
|
||||||
|
|
||||||
|
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.paginate(page, current_app.config.get('POSTS_PER_PAGE'))
|
||||||
|
tags = tags_prepare(posts.items)
|
||||||
|
|
||||||
|
flash(parse_args())
|
||||||
|
# flash(posts.items)
|
||||||
|
# flash(tags)
|
||||||
|
|
||||||
|
return render_template('index.html', posts=posts.items, tags=tags, pagination=posts, query_replace=query_replace)
|
||||||
|
|
||||||
|
@bp.route('/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()
|
||||||
|
|
||||||
|
for tag_count in tags_count:
|
||||||
|
tag, count = tag_count
|
||||||
|
tag.count = count
|
||||||
|
tag.endpoint = query_replace({'tags': tag.content.replace(' ','_')}, url_for('.posts'))
|
||||||
|
|
||||||
|
return render_template('post.html', post=post, tags=post.tags)
|
||||||
|
|
||||||
|
@bp.route('/upload', methods=['GET', 'POST'])
|
||||||
|
@login_required
|
||||||
|
def post_upload():
|
||||||
|
form = UploadPostForm(request.form)
|
||||||
|
if request.method == 'POST' and form.validate():
|
||||||
|
file = request.files.get(form.post_img.name)
|
||||||
|
file.data = io.BytesIO(file.read())
|
||||||
|
|
||||||
|
# tagy
|
||||||
|
|
||||||
|
post = Post(file, source=form.sauce.data, rating=RATING[form.rating.data], author=current_user)
|
||||||
|
|
||||||
|
|
||||||
|
with open(post.image_path, "wb") as f:
|
||||||
|
f.write(file.data.getbuffer())
|
||||||
|
# file.seek(0)
|
||||||
|
# file.save(post.image_path)
|
||||||
|
|
||||||
|
with Image.open(file.data) as im:
|
||||||
|
im = im.convert('RGB')
|
||||||
|
|
||||||
|
if post.jpeg_path is not None:
|
||||||
|
im.save(post.jpeg_path, 'JPEG', quality=80)
|
||||||
|
|
||||||
|
im.thumbnail([512,512])
|
||||||
|
im.save(post.thumb_path, 'JPEG', quality=80)
|
||||||
|
|
||||||
|
|
||||||
|
db.session.add(post)
|
||||||
|
db.session.commit()
|
||||||
|
|
||||||
|
flash('Successfully submitted {}'.format(str(post)))
|
||||||
|
|
||||||
|
return redirect(url_for('.post_upload'))
|
||||||
|
|
||||||
|
return render_template('upload.html', form=form)
|
Loading…
Reference in New Issue