fuckin idk

This commit is contained in:
codl 2017-07-27 20:20:59 +02:00
parent 413341ed9d
commit f5fb843fda
No known key found for this signature in database
GPG Key ID: 6CD7C8891ED1233A
8 changed files with 74 additions and 8 deletions

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
/config.py
__pycache__
celerybeat-schedule

3
app.py
View File

@ -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)

View File

@ -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
"""

View File

@ -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 ###

View File

@ -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')

View File

@ -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"

26
tasks.py Normal file
View File

@ -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()

View File

@ -2,6 +2,12 @@
<p>Hello,
<img src="{{g.viewer.account.remote_avatar_url}}"/>
{{g.viewer.account.remote_display_name}}! <a href="/logout">Log out</a></p>
<p>your posts:</p>
{% for post in g.viewer.account.posts %}
<p>{{post.body}}</p>
{% else %}
<p>no posts :(</p>
{% endfor %}
{% else %}
<p>Hello, stranger! <a href="/login/twitter">Log in with Twitter</a></p>
{% endif %}