From f5fb843fdacd97b8174261666a52111d7c0f0d3a Mon Sep 17 00:00:00 2001 From: codl Date: Thu, 27 Jul 2017 20:20:59 +0200 Subject: [PATCH] fuckin idk --- .gitignore | 1 + app.py | 3 ++- config.example.py | 2 ++ .../{c3faafd828ad_.py => a5718ca3ead1_.py} | 21 +++++++++++---- model.py | 15 ++++++++++- routes.py | 8 +++++- tasks.py | 26 +++++++++++++++++++ templates/index.html | 6 +++++ 8 files changed, 74 insertions(+), 8 deletions(-) rename migrations/versions/{c3faafd828ad_.py => a5718ca3ead1_.py} (66%) create mode 100644 tasks.py diff --git a/.gitignore b/.gitignore index 173a626..16f2746 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ /config.py __pycache__ +celerybeat-schedule diff --git a/app.py b/app.py index e620572..6203610 100644 --- a/app.py +++ b/app.py @@ -9,7 +9,8 @@ default_config = { "SQLALCHEMY_ECHO": True, "SQLALCHEMY_TRACK_MODIFICATIONS": False, "SQLALCHEMY_DATABASE_URI": "postgresql+psycopg2:///forget", - "SECRET_KEY": "hunter2" + "SECRET_KEY": "hunter2", + "CELERY_BROKER": "amqp://", } app.config.update(default_config) diff --git a/config.example.py b/config.example.py index 6d4333c..4b779c3 100644 --- a/config.example.py +++ b/config.example.py @@ -27,6 +27,8 @@ this will be necessary so we can tell twitter where to redirect """ SERVER_NAME="localhost:5000" +CELERY_BROKER='amqp://' + """ you can also use any config variable that flask expects here, such as """ diff --git a/migrations/versions/c3faafd828ad_.py b/migrations/versions/a5718ca3ead1_.py similarity index 66% rename from migrations/versions/c3faafd828ad_.py rename to migrations/versions/a5718ca3ead1_.py index d97b7e5..bbd341b 100644 --- a/migrations/versions/c3faafd828ad_.py +++ b/migrations/versions/a5718ca3ead1_.py @@ -1,8 +1,8 @@ """empty message -Revision ID: c3faafd828ad +Revision ID: a5718ca3ead1 Revises: -Create Date: 2017-07-27 01:16:34.114238 +Create Date: 2017-07-27 20:12:42.873090 """ from alembic import op @@ -10,7 +10,7 @@ import sqlalchemy as sa # revision identifiers, used by Alembic. -revision = 'c3faafd828ad' +revision = 'a5718ca3ead1' down_revision = None branch_labels = None depends_on = None @@ -24,6 +24,7 @@ def upgrade(): sa.Column('remote_id', sa.String(), nullable=False), sa.Column('remote_display_name', sa.String(), nullable=True), sa.Column('remote_avatar_url', sa.String(), nullable=True), + sa.Column('last_post_fetch', sa.DateTime(), server_default='epoch', nullable=True), sa.PrimaryKeyConstraint('remote_id', name=op.f('pk_accounts')) ) op.create_table('oauth_tokens', @@ -35,12 +36,21 @@ def upgrade(): sa.ForeignKeyConstraint(['remote_id'], ['accounts.remote_id'], name=op.f('fk_oauth_tokens_remote_id_accounts')), sa.PrimaryKeyConstraint('token', name=op.f('pk_oauth_tokens')) ) + op.create_table('posts', + sa.Column('created_at', sa.DateTime(), server_default=sa.text('now()'), nullable=True), + sa.Column('updated_at', sa.DateTime(), server_default=sa.text('now()'), nullable=True), + sa.Column('remote_id', sa.String(), nullable=False), + sa.Column('body', sa.String(), nullable=True), + sa.Column('author_remote_id', sa.String(), nullable=True), + sa.ForeignKeyConstraint(['author_remote_id'], ['accounts.remote_id'], name=op.f('fk_posts_author_remote_id_accounts')), + sa.PrimaryKeyConstraint('remote_id', name=op.f('pk_posts')) + ) op.create_table('sessions', sa.Column('created_at', sa.DateTime(), server_default=sa.text('now()'), nullable=True), sa.Column('updated_at', sa.DateTime(), server_default=sa.text('now()'), nullable=True), sa.Column('id', sa.String(), nullable=False), - sa.Column('remote_id', sa.String(), nullable=True), - sa.ForeignKeyConstraint(['remote_id'], ['accounts.remote_id'], name=op.f('fk_sessions_remote_id_accounts')), + sa.Column('account_id', sa.String(), nullable=True), + sa.ForeignKeyConstraint(['account_id'], ['accounts.remote_id'], name=op.f('fk_sessions_account_id_accounts')), sa.PrimaryKeyConstraint('id', name=op.f('pk_sessions')) ) # ### end Alembic commands ### @@ -49,6 +59,7 @@ def upgrade(): def downgrade(): # ### commands auto generated by Alembic - please adjust! ### op.drop_table('sessions') + op.drop_table('posts') op.drop_table('oauth_tokens') op.drop_table('accounts') # ### end Alembic commands ### diff --git a/model.py b/model.py index 672e211..bac5742 100644 --- a/model.py +++ b/model.py @@ -26,6 +26,10 @@ class Account(db.Model, TimestampMixin): remote_display_name = db.Column(db.String) remote_avatar_url = db.Column(db.String) + last_post_fetch = db.Column(db.DateTime, server_default='epoch') + + # backref: posts + class OAuthToken(db.Model, TimestampMixin): __tablename__ = 'oauth_tokens' @@ -40,5 +44,14 @@ class Session(db.Model, TimestampMixin): id = db.Column(db.String, primary_key=True, default=lambda: secrets.token_urlsafe()) - remote_id = db.Column(db.String, db.ForeignKey('accounts.remote_id')) + account_id = db.Column(db.String, db.ForeignKey('accounts.remote_id')) account = db.relationship(Account, lazy='joined') + +class Post(db.Model, TimestampMixin): + __tablename__ = 'posts' + + remote_id = db.Column(db.String, primary_key=True) + body = db.Column(db.String) + + author_remote_id = db.Column(db.String, db.ForeignKey('accounts.remote_id')) + author = db.relationship(Account, lazy='joined', backref='posts') diff --git a/routes.py b/routes.py index b05a8e4..10e723e 100644 --- a/routes.py +++ b/routes.py @@ -35,7 +35,7 @@ def twitter_login_step2(): oauth_token = request.args['oauth_token'] oauth_verifier = request.args['oauth_verifier'] token = lib.twitter.receive_verifier(oauth_token, oauth_verifier, **app.config.get_namespace("TWITTER_")) - session = Session(remote_id = token.remote_id) + session = Session(account_remote_id = token.remote_id) db.session.add(session) db.session.commit() resp = Response(status=301, headers={"location": url_for('index')}) @@ -49,3 +49,9 @@ def logout(): db.session.commit() g.viewer = None return redirect(url_for('index')) + +@app.route('/debug') +def debug(): + import tasks + tasks.remove_old_sessions.delay() + return "hi" diff --git a/tasks.py b/tasks.py new file mode 100644 index 0000000..2429c7e --- /dev/null +++ b/tasks.py @@ -0,0 +1,26 @@ +from celery import Celery + +from app import app as flaskapp +from app import db +from model import Session +from datetime import timedelta + +app = Celery('tasks', broker=flaskapp.config['CELERY_BROKER'], task_serializer='pickle') + +@app.task +def remove_old_sessions(): + Session.query.filter(Session.updated_at < (db.func.now() - timedelta(minutes=30))).\ + delete(synchronize_session=False) + db.session.commit() + +@app.task +def fetch_posts(remote_id): + pass + +app.add_periodic_task(60*60, remove_old_sessions) + + + + +if __name__ == '__main__': + app.worker_main() diff --git a/templates/index.html b/templates/index.html index 838f99b..727959d 100644 --- a/templates/index.html +++ b/templates/index.html @@ -2,6 +2,12 @@

Hello, {{g.viewer.account.remote_display_name}}! Log out

+

your posts:

+{% for post in g.viewer.account.posts %} +

{{post.body}}

+{% else %} +

no posts :(

+{% endfor %} {% else %}

Hello, stranger! Log in with Twitter

{% endif %}