replace last_delete with next_delete

This commit is contained in:
codl 2017-08-14 20:58:22 +02:00
parent cebfab2542
commit 1cd5b8fa47
No known key found for this signature in database
GPG Key ID: 6CD7C8891ED1233A
4 changed files with 44 additions and 8 deletions

View File

@ -0,0 +1,32 @@
"""replace last_delete with next_delete
Revision ID: e769c033e5c9
Revises: 6d298e6406f2
Create Date: 2017-08-14 20:51:02.248343
"""
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql
# revision identifiers, used by Alembic.
revision = 'e769c033e5c9'
down_revision = '6d298e6406f2'
branch_labels = None
depends_on = None
def upgrade():
op.add_column('accounts', sa.Column('next_delete', sa.DateTime(), server_default='epoch', nullable=True))
op.execute('UPDATE accounts SET next_delete = last_delete + policy_delete_every;')
op.create_index(op.f('ix_accounts_next_delete'), 'accounts', ['next_delete'], unique=False)
op.drop_index('ix_accounts_last_delete', table_name='accounts')
op.drop_column('accounts', 'last_delete')
def downgrade():
op.add_column('accounts', sa.Column('last_delete', postgresql.TIMESTAMP(), server_default=sa.text("'1970-01-01 00:00:00'::timestamp without time zone"), autoincrement=False, nullable=True))
op.execute('UPDATE accounts SET last__delete = next_delete - policy_delete_every;')
op.create_index('ix_accounts_last_delete', 'accounts', ['last_delete'], unique=False)
op.drop_index(op.f('ix_accounts_next_delete'), table_name='accounts')
op.drop_column('accounts', 'next_delete')

View File

@ -5,7 +5,7 @@ from app import db
from twitter import Twitter, OAuth
import secrets
from lib import decompose_interval
from datetime import timedelta
from datetime import timedelta, datetime
class TimestampMixin(object):
created_at = db.Column(db.DateTime, server_default=db.func.now(), nullable=False)
@ -53,14 +53,17 @@ class Account(TimestampMixin, RemoteIDMixin):
reported_post_count = db.Column(db.Integer)
last_fetch = db.Column(db.DateTime, server_default='epoch', index=True)
last_delete = db.Column(db.DateTime, server_default='epoch', index=True)
last_refresh = db.Column(db.DateTime, server_default='epoch', index=True)
next_delete = db.Column(db.DateTime, server_default='epoch', index=True)
def touch_fetch(self):
self.last_fetch = db.func.now()
def touch_delete(self):
self.last_delete = db.func.now()
if(datetime.now() - self.next_delete > 3 * self.delete_every):
self.next_delete = db.func.now() + self.delete_every
else:
self.next_delete += self.delete_every
def touch_refresh(self):
self.last_refresh = db.func.now()

View File

@ -151,12 +151,12 @@ def enable():
return render_template('warn.html', message=f"""You've set the time between deleting posts to 0. Every post that matches your expiration rules will be deleted within minutes.
{ ("That's about " + str(approx) + " posts.") if approx > 0 else "" }
Go ahead?""")
if g.viewer.account.last_delete < datetime.now() - timedelta(days=365):
if g.viewer.account.next_delete < datetime.now() - timedelta(days=365):
return render_template('warn.html', message="""Once you enable Forget, posts that match your expiration rules will be deleted <b>permanently</b>. We can't bring them back. Make sure that you won't miss them.""")
if not g.viewer.account.policy_enabled:
g.viewer.account.last_delete = db.func.now()
g.viewer.account.next_delete = datetime.now() + g.viewer.account.policy_delete_every
g.viewer.account.policy_enabled = True
db.session.commit()
@ -202,6 +202,6 @@ def api_viewer_post_counts():
last_refresh_rel=lib.interval.relnow(viewer.last_refresh),
last_fetch=viewer.last_fetch,
last_fetch_rel=lib.interval.relnow(viewer.last_fetch),
last_delete=viewer.last_delete,
last_delete_rel=lib.interval.relnow(viewer.last_delete),
next_delete=viewer.next_delete,
next_delete_rel=lib.interval.relnow(viewer.next_delete),
)

View File

@ -127,7 +127,7 @@ def periodic_cleanup():
@app.task
def queue_deletes():
eligible_accounts = Account.query.filter(Account.policy_enabled == True).\
filter(Account.last_delete + Account.policy_delete_every < db.func.now())
filter(Account.next_delete < db.func.now())
for account in eligible_accounts:
delete_from_account.s(account.id).apply_async()
@ -151,6 +151,7 @@ def delete_from_account(account_id):
print("deleting all {} eligible posts for {}".format(len(eligible), account))
for post in eligible:
lib.twitter.delete(post)
account.touch_delete()
else:
post = random.choice(eligible)
print("deleting {}".format(post))