program: add infrastructure for the progrank job (#114)

* Add an automatic job every day at 4 AM to recompute the progrank of
  every program. Currently everyone gets progrank 0.

[MIGRATION] This commit contains a new version of the schema.

[SETUP]
* Install flask-crontab (with pip)
* Run `flask crontab add` to register the jobs
This commit is contained in:
Lephe 2022-06-15 11:23:58 +01:00
parent db0e42d285
commit 8ff21c615d
Signed by: Lephenixnoir
GPG Key ID: 1BBA026E13FC0495
8 changed files with 62 additions and 2 deletions

View File

@ -25,7 +25,12 @@ python-pyyaml
python-slugify
```
Non disponibles sur l'AUR, mais disponibles sur pip :
```
flask-crontab (0.1.2)
```
Optionnel:
```
python-flask-debugtoolbar
python-flask-debugtoolbar (out-of-date sur l'AUR : bien installer la 0.13)
```

View File

@ -4,6 +4,7 @@ from flask_migrate import Migrate
from flask_login import LoginManager
from flask_mail import Mail
from flask_wtf.csrf import CSRFProtect
from flask_crontab import Crontab
from config import FlaskApplicationSettings, V5Config
app = Flask(__name__)
@ -17,6 +18,7 @@ db = SQLAlchemy(app)
migrate = Migrate(app, db)
mail = Mail(app)
csrf = CSRFProtect(app)
crontab = Crontab(app)
login = LoginManager(app)
login.login_view = 'login'
@ -38,6 +40,9 @@ from app.utils import filters
# Register processors
from app import processors
# Register scheduled jobs
from app import jobs
# Enable flask-debug-toolbar if requested
if V5Config.ENABLE_FLASK_DEBUG_TOOLBAR:
from flask_debugtoolbar import DebugToolbarExtension

1
app/jobs/__init__.py Normal file
View File

@ -0,0 +1 @@
from app.jobs.update_progrank import update_progrank

View File

@ -0,0 +1,11 @@
from app import db, crontab
from app.models.program import Program
from datetime import datetime
@crontab.job(minute="0", hour="4")
def update_progrank():
for p in Program.query.all():
p.progrank = 0
p.progrank_date = datetime.now()
db.session.merge(p)
db.session.commit()

View File

@ -20,6 +20,10 @@ class Program(Post):
thread = db.relationship('Thread', foreign_keys=thread_id,
back_populates='owner_program')
# Progrank, and last date of progrank update
progrank = db.Column(db.Integer)
progrank_date = db.Column(db.DateTime)
# Implicit attributes:
# * tags (inherited from Post)
# * attachements (available at thread.top_comment.attachments)

View File

@ -17,12 +17,13 @@
<h2>Tous les programmes</h2>
<table class=programlist>
<tr><th>ID</th><th>Nom</th><th>Auteur</th><th>Publié le</th><th>Tags</th></tr>
<tr><th>ID</th><th>Nom</th><th>Auteur</th><th>Publié le</th><th>Progrank</th><th>Tags</th></tr>
{% for p in programs %}
<tr><td>{{ p.id }}</td>
<td><a href='{{ url_for("program_view", page=(p,1)) }}'>{{ p.name }}</a></td>
<td>{{ p.author.name }}</td>
<td>{{ p.date_created | dyndate }}</td>
<td><span title="Dernière mise à jour : {{ p.progrank_date | dyndate }}">{{ p.progrank }}</span></td>
<td>
{%- for tag in p.tags %}
<span class="tag tag-{{ tag.tag.category() }}">{{ tag.tag.pretty }}</span>

View File

@ -7,6 +7,9 @@ def filter_date(date, format="%Y-%m-%d à %H:%M"):
Print a date in a human-readable format.
"""
if date is None:
return "None"
if format == "dynamic":
d = "1er" if date.day == 1 else int(date.day)
m = ["Janvier", "Février", "Mars", "Avril", "Mai", "Juin", "Juillet",

View File

@ -0,0 +1,30 @@
"""program: add progrank score and update date fields
Revision ID: daa5d5913ef8
Revises: 189bbc0e1543
Create Date: 2022-06-15 11:14:30.745287
"""
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision = 'daa5d5913ef8'
down_revision = '189bbc0e1543'
branch_labels = None
depends_on = None
def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.add_column('program', sa.Column('progrank', sa.Integer(), nullable=True))
op.add_column('program', sa.Column('progrank_date', sa.DateTime(), nullable=True))
# ### end Alembic commands ###
def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_column('program', 'progrank_date')
op.drop_column('program', 'progrank')
# ### end Alembic commands ###