forked from snarfed/freedom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
migrations.py
141 lines (111 loc) · 3.94 KB
/
migrations.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
"""Serves the migration page.
"""
__author__ = 'Ryan Barrett <[email protected]>'
import itertools
import logging
import urllib
from webob import exc
import appengine_config
import models
from webutil import handlers
# Import all sources and destinations because their Model classes are
# instantiated here.
import blogger
import dropbox
import facebook
import googleplus
import instagram
import tumblr
import twitter
import wordpress_xmlrpc
from google.appengine.api import taskqueue
from google.appengine.ext import db
import webapp2
class MigrateHandler(webapp2.RequestHandler):
"""Starts a migration."""
# TODO
# @db.transactional
def post(self):
source = db.Key(self.request.get('source'))
dest = db.Key(self.request.get('dest'))
key_name = models.Migration.make_key_name(source.kind(), source.name(),
dest.kind(), dest.name())
id = db.allocate_ids(db.Key.from_path('Migration', 1), 1)[0]
migration = models.Migration.get_or_insert(key_name, id=id)
taskqueue.add(queue_name='scan', params={'migration': key_name})
self.redirect('/migration/%d' % migration.id)
class StopHandler(webapp2.RequestHandler):
"""Stops a migration if it's currently running."""
message = 'Stopped migration.'
def post(self, id):
@db.transactional
def stop(key):
migration = models.Migration.get(key)
if not migration.stopped:
migration.stopped = True
migration.save()
else:
self.message = 'Migration is already stopped.'
id = int(id)
stop(models.Migration.all().filter('id =', id).get().key())
self.redirect('/migration/%d' % id)
class ResumeHandler(webapp2.RequestHandler):
"""Resumes a migration if it's currently stopped."""
message = 'Resumed migration.'
def post(self, id):
@db.transactional
def resume(key):
migration = models.Migration.get(key)
if migration.stopped:
migration.stopped = False
migration.save()
else:
self.message = 'Migration is already running.'
id = int(id)
resume(models.Migration.all().filter('id =', id).get().key())
self.redirect('/migration/%d' % id)
class MigrationHandler(handlers.TemplateHandler):
"""Shows the status page for a migration."""
# map source kind to model classes for that source
MIGRATABLES = {
'Facebook': (facebook.FacebookPost, facebook.FacebookComment),
'GooglePlus': (googleplus.GooglePlusPost, googleplus.GooglePlusComment),
'Instagram': (instagram.InstagramMedia, instagram.InstagramComment),
'Twitter': (twitter.Tweet, twitter.Reply),
}
"""Renders and serves the migration page.
Attributes:
id: integer, migration id
"""
def get(self, id):
self.id = int(id)
super(MigrationHandler, self).get()
def template_file(self):
return 'templates/migration.html'
def template_vars(self):
logging.info('Fetching migration id %d.', self.id)
# TODO: port to ndb so these queries can run in parallel
migration = models.Migration.all().filter('id =', self.id).get()
if not migration:
raise exc.HTTPBadRequest('Migration id %d not found.' % self.id)
logging.info('Got migration %s', migration.key().name())
logging.info('Fetching posts and comments')
source_kind = migration.source_key().kind()
migratables = {
status: itertools.chain(*(
cls.all().filter('migration =', migration.key())
.filter('status =', status)
.order('-last_updated')
.fetch(20)
for cls in self.MIGRATABLES[source_kind]))
for status in models.Migratable.STATUSES}
return {'migration': migration,
'migratables': migratables,
'message': self.request.get('message')}
application = webapp2.WSGIApplication(
[('/migrate', MigrateHandler),
('/migration/([^/]+)', MigrationHandler),
('/migration/([^/]+)/stop', StopHandler),
('/migration/([^/]+)/resume', ResumeHandler),
],
debug=appengine_config.DEBUG)