~sirn/fanboi2

ref: c1ecc7d29fbc07c844c237fd70ef38b7a617b7b0 fanboi2/fanboi2/scripts/topic_sync.py -rw-r--r-- 900 bytes
c1ecc7d2Kridsada Thanabulpong Third time's the charm. 4 years ago
                                                                                
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
import os
import sys
import transaction
import sqlalchemy as sa
from zope.sqlalchemy import mark_changed
from fanboi2.models import DBSession, TopicMeta, Post
from pyramid.paster import bootstrap


def main(argv=sys.argv):
    if not len(argv) >= 2:
        sys.stderr.write("Usage: %s config\n" % os.path.basename(argv[0]))
        sys.stderr.write("Configuration file not present.\n")
        sys.exit(1)

    bootstrap(argv[1])

    query = TopicMeta.__table__.update().\
            values(bumped_at=sa.select([Post.created_at]).\
                   where(Post.topic_id == TopicMeta.topic_id).\
                   where(Post.bumped).\
                   order_by(sa.desc(Post.created_at)).\
                   limit(1))

    with transaction.manager:
        session = DBSession()
        results = session.execute(query)
        mark_changed(session)
        print("Successfully synced topic.")