# -*- coding: utf-8 -*-
"""User views."""
from flask import Blueprint, render_template
from flask_login import login_required, current_user

blueprint = Blueprint('user', __name__, url_prefix='/users', static_folder='../static')

from ..user.models import Keep
from ..post.models import Post

@blueprint.route('/')
@login_required
def members():
    """List members."""
    return render_template('users/members.html')

@blueprint.route('/favorites')
@login_required
def favorites():
    """收藏"""
    keeps = Keep.query.filter_by(user_id=current_user.id).all()
    post_ids = [keep.post_id for keep in keeps]

    posts = [Post.get_by_id(post_id) for post_id in post_ids]       # 查询所有收藏的文章

    blogs = []
    newses = []
    for id in post_ids:
        post = Post.get_by_id(id)
        if post.type.name == 'blog':
            blogs.append(post)
        elif post.type.name == 'news':
            newses.append(post)

    # return render_template('users/favorites.html', posts=posts, blogs=blogs, newses=newses)
    return render_template('users/favorites.html', posts=posts)