Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 27 additions & 5 deletions service.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
# ========================================================================================
Expand Down Expand Up @@ -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():
Expand All @@ -383,22 +400,27 @@ 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('<div>\n <br>\n <table class="table table-hover table-striped">\n')
out.write(' <tr> <td>Top Articles:</td> </tr>\n')
out.write('{% for article in popular_articles %}\n')
out.write('<tr><td><a href="/article/{{ article.id }}">Article Name: {{ article.title }}</a></td></tr>\n')
out.write('{% endfor %}\n </table>\n <br>\n </div>\n')
# end
for subject in subjects:
find_subjects(subject, 0)
print('\n')

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']
Expand Down
15 changes: 14 additions & 1 deletion templates/io.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
{% extends "template.html" %}
{% block content %}
<body>

<div>
<br>
<table class="table table-hover table-striped">
<tr>
<td>Top Articles:</td>
</tr>
{% for article in popular_articles %}
<tr>
<td><a href="/article/{{ article.id }}">Article Name: {{ article.title }}</a></td>
</tr>
{% endfor %}
</table>
<br>
</div>
<a href="subject/1">Physical Sciences</a><br>
&emsp;
<a href="subject/8">Physics</a><br>
Expand Down