-
Notifications
You must be signed in to change notification settings - Fork 0
/
manage.py
62 lines (51 loc) · 1.73 KB
/
manage.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# -*- coding: utf-8 -*-
import datetime
import pytz
from flask.ext.script import Manager
from wootpaste.app import create_app
from wootpaste.config import config
from wootpaste.database import db_session
from wootpaste.models import *
from wootpaste.utils import utcnow
if config['paste.spam_ml']:
import wootpaste.utils.spam_ml as spam_ml
app = create_app()
manager = Manager(app)
import logging
logger = logging.getLogger('wootpaste')
@manager.command
def cleanup():
"""Removes expired pastes and old visit logs from the database.
Should be called periodically to cleanup the database.
"""
delete_expired_pastes()
delete_old_paste_visit()
def delete_expired_pastes():
deleted= []
for paste in Paste.query.filter(Paste.expire_in > 0).all():
if paste.is_expired():
db_session.delete(paste)
deleted.append(paste.key)
db_session.commit()
if len(deleted) > 0: logger.info('delete {} expired pastes: {}'.format(len(deleted), ', '.join(deleted)))
def delete_old_paste_visit():
num = 0
for visit in PasteVisit.query.filter(PasteVisit.created_at < utcnow() - timedelta(hours=6)).all():
num+=1
db_session.delete(visit)
db_session.commit()
if num > 0: logger.info('delete {} paste visits'.format(num))
if config['paste.spam_ml']:
@manager.command
def spam_evaluate():
"""Evaluates the model used for paste spam detection."""
print 'Running evaluation... '
avg = spam_ml.evaluate()
print 'Average Score: ' + str(avg)
@manager.command
def spam_train():
"""Trains the model used for paste spam detection."""
print 'Training running...'
spam_ml.train()
if __name__ == "__main__":
manager.run()