def search_news():
    search = request.args.get('search')
    news = News.query.order_by(News.posted.desc()).filter(
        func.lower(News.name).contains(search.lower())).all()
    news_dic = {'news': []}
    for n in news:
        try:
            avatar = n.user.profile.image_link
            about = n.user.profile.about
        except Exception:
            avatar = 'None'
            about = 'None'
        news_dic['news'].append({
            'id': n.id,
            'liked': current_user.is_like(n),
            'likes': n.count_likes(),
            'avatar': avatar,
            'username': n.user.username,
            'about': about,
            'followers': n.user.followers.count(),
            'title': n.name,
            'description': n.description,
            'image_link': n.image_link,
            'posted': n.posted.strftime("%B %d, %Y %H:%M")
        })
    return jsonify(news_dic)
def news_by_id(news_id):
    n = News.query.filter_by(id=int(news_id)).first()
    if n is None:
        return 404

    try:
        avatar = n.user.profile.image_link
        about = n.user.profile.about
    except Exception:
        avatar = 'None'
        about = 'None'
    news_dic = {
        'id': n.id,
        'liked': current_user.is_like(n),
        'likes': n.count_likes(),
        'avatar': avatar,
        'username': n.user.username,
        'about': about,
        'followers': n.user.followers.count(),
        'title': n.name,
        'description': n.description,
        'image_link': n.image_link,
        'posted': n.posted.strftime("%B %d, %Y %H:%M")
    }
    return jsonify(news_dic)
def unlike(news_id):
    news = News.query.filter_by(id=int(news_id)).first()
    if news is None:
        return jsonify({'error': 'Invalid news id'})
    if not current_user.is_like(news):
        return jsonify({'error': 'You do not like this'})
    current_user.unlike(news)
    return jsonify({'Message': 'Success'})
def news_all():
    news = News.query.order_by(News.posted.desc()).all()
    news_dic = {'news': []}
    for n in news:
        news_dic['news'].append({
            'id': n.id,
            'liked': current_user.is_like(n),
            'likes': n.count_likes(),
            'username': n.user.username,
            'title': n.name,
            'description': n.description,
            'image_link': n.image_link,
        })

    return jsonify(news_dic)
def new_for_user(username):
    user = User.query.filter_by(username=username).first()
    news_dic = {'news': []}
    for n in user.news:
        try:
            avatar = n.user.profile.image_link
            about = n.user.profile.about
        except Exception:
            avatar = 'None'
            about = 'None'
        news_dic['news'].append({
            'id': n.id,
            'liked': current_user.is_like(n),
            'likes': n.count_likes(),
            'avatar': avatar,
            'username': n.user.username,
            'about': about,
            'followers': n.user.followers.count(),
            'title': n.name,
            'description': n.description,
            'image_link': n.image_link,
            'posted': n.posted.strftime("%B %d, %Y %H:%M")
        })
    return jsonify(news_dic)