Skip to content

Instantly share code, notes, and snippets.

@AlexFrazer
Created August 5, 2014 13:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AlexFrazer/ec2ea1e8efe68bddfecd to your computer and use it in GitHub Desktop.
Save AlexFrazer/ec2ea1e8efe68bddfecd to your computer and use it in GitHub Desktop.
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
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