Created
August 5, 2014 13:39
-
-
Save AlexFrazer/ec2ea1e8efe68bddfecd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from app.extensions import db | |
class Article(db.Model): | |
""" database representation of an article """ | |
id = db.Column(db.Integer, primary_key=True) | |
title = db.Column(db.String(128)) | |
subtitle = db.Column(db.String(512)) | |
body = db.Column(db.Text()) | |
votes = db.Column(db.Integer, default=1) | |
views = db.Column(db.Integer, default=1) | |
timestamp = db.Column(db.DateTime, default=datetime.utcnow) | |
def popularity(self, gravity=1.8): | |
""" uses hacker news popularity rating """ | |
submit_delta = (self.timestamp - datetime.utcnow()).total_seconds() | |
time_decay = submit_delta / 120 | |
popularity = (self.views - 1) / (time_decay + 2) ** gravity | |
return popularity |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from flask import render_template | |
from app.articles.models import Article | |
from app.articles import bp | |
@bp.route('/') | |
def front_page(): | |
""" sort the articles by popularity and put them on page """ | |
sorted_articles = Article.query.order_by(popularity()).all() | |
return render_template('/articles/front_page.html') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment