def new_article():
    if "loggedin" in session:
        if session['loggedin'] == True:
            user_id = session['id']
            user = User.objects.with_id(user_id)
            if user.level >= 2:
                return redirect(url_for('profile'))
            else:
                if request.method == "GET":
                    return render_template('new_article.html')
                elif request.method == "POST":
                    form = request.form
                    title = form['title']
                    sapo = form['sapo']
                    content = form['content']
                    author = form['author']
                    category = form['category']
                    thumbnail = form['thumbnail']

                    new_article = Article(title=title,
                                          sapo=sapo,
                                          thumbnail=thumbnail,
                                          content=content,
                                          author=author,
                                          time=datetime.now(),
                                          category=category)

                new_article.save()
        else:
            return redirect(url_for('login'))
    else:
        return redirect(url_for('login'))
def search(keywords):
    articles = Article.objects(title__icontains=keywords)
    articles_view = Article.objects.order_by('-view_count')
    return render_template('result.html',
                           articles=articles,
                           keywords=keywords,
                           articles_view=articles_view)
def template(article_id):
    article = Article.objects.with_id(article_id)
    print(article)
    articles_view = Article.objects.order_by('-view_count')
    articles_type_time = Article.objects(
        category=article.category).order_by('-time')
    if request.method == "GET":
        article.update(set__view_count=article['view_count'] + 1, )
        return render_template('template.html',
                               article=article,
                               articles_view=articles_view,
                               articles_type_time=articles_type_time)
    elif request.method == "POST":
        form = request.form
        if 'search' in form:
            return redirect(url_for('search', keywords=form['search']))
            # return "Search"
        else:
            if "loggedin" not in session:
                return redirect(url_for('login'))
            else:
                author = User.objects.with_id(session['id'])
                form = request.form
                comment = Comment(author=author.fullname,
                                  time=datetime.now(),
                                  content=form['content'])
                comment.save()
                article.update(push__comment=comment)
                return render_template('template.html',
                                       article=article,
                                       articles_view=articles_view)
def tech():
    articles = Article.objects(category="Tech").order_by('-time')
    articles_view = Article.objects.order_by('-view_count')
    if request.method == "GET":
        return render_template("homepage.html",
                               articles=articles,
                               articles_view=articles_view)
    elif request.method == "POST":
        return redirect(url_for('search', keywords=request.form['keywords']))
def admin_article():
    articles = Article.objects()
    return render_template('admin_article.html', articles=articles)
def article_approval():
    articles = Article.objects()
    return render_template('article_approval.html', articles=articles)