From 5da59fa909f1f57d0804a75a34c58bc34ae96022 Mon Sep 17 00:00:00 2001 From: Jackoldwolf <58628366+Jackoldwolf@users.noreply.github.com> Date: Sun, 6 Dec 2020 19:46:33 +0800 Subject: [PATCH 1/2] Update service.py 1.Add an static method : get_top_artilces() it return a list of top articles according to their metric 2.Add some html tabs in create_index() it show the top articles in io.html --- service.py | 32 +++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/service.py b/service.py index af5395e..50e0eb2 100644 --- a/service.py +++ b/service.py @@ -1,5 +1,5 @@ from flask_sqlalchemy import SQLAlchemy -from sqlalchemy import or_ +from sqlalchemy import or_, func import os, uuid ,math, random from flask import Flask, flash, request, redirect, url_for, session, jsonify, render_template, send_from_directory from werkzeug.utils import secure_filename @@ -200,6 +200,23 @@ def email_display_filter(email): return display + suf + # add @return a list of top articles + @staticmethod + def get_top_articles(): + times = 50 + articles = [] + lens = db.session.query(func.count(Subject.id)).scalar() + while (times > 0): + subjectId = random.randint(1, lens) + article = Article.query.filter(Article.metric > 0, Article.subject_id == subjectId).limit(1) + articles.extend(article) + if len(articles) >= 3: + break + times -= 1 + articles = list(set(articles)) + return articles + # end + # ========================================================================================= # like and dislike # ======================================================================================== @@ -357,7 +374,7 @@ def before_request(): # ============================================================================================# @app.route('/') def index(): - return render_template('io.html') + return render_template('io.html', popular_articles=Tool.get_top_articles()) @app.route('/test') def test_one(): @@ -383,6 +400,13 @@ def find_subjects(subject, count): subjects = Subject.query.filter_by(pid='None').all() out.write('{% extends "template.html" %}' + '\n') out.write('{% block content %}' + '\n') + # add + out.write('
\n
\n \n') + out.write(' \n') + out.write('{% for article in popular_articles %}\n') + out.write('\n') + out.write('{% endfor %}\n
Top Articles:
Article Name: {{ article.title }}
\n
\n
\n') + # end for subject in subjects: find_subjects(subject, 0) print('\n') @@ -390,15 +414,13 @@ def find_subjects(subject, count): out.write('{% endblock %}' + '\n') out.flush() out.close() - return render_template('io.html') + return render_template('io.html', popular_articles=Tool.get_top_articles()) # ================================================================================ # Edit and add subject # ================================================================================ @app.route('/edit_subcategory', methods=['GET', 'POST']) def add_sub_category(): - if not session.get('logged_in'): - return render_template('login.html') if request.method == 'POST': subject_id = request.form['subject_id'] subject_name = request.form['subject_name'] From 17f7ef6e2b5ea9cd3267d935f93483248eba6c5e Mon Sep 17 00:00:00 2001 From: Jackoldwolf <58628366+Jackoldwolf@users.noreply.github.com> Date: Sun, 6 Dec 2020 19:50:54 +0800 Subject: [PATCH 2/2] Update io.html 1.Add some tags in io.html to show the top articles returned by the get_top_articles() in service.py --- templates/io.html | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/templates/io.html b/templates/io.html index 6af74c5..e8803fe 100644 --- a/templates/io.html +++ b/templates/io.html @@ -1,7 +1,20 @@ {% extends "template.html" %} {% block content %} - +
+
+ + + + + {% for article in popular_articles %} + + + + {% endfor %} +
Top Articles:
Article Name: {{ article.title }}
+
+
Physical Sciences
Physics