-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmigrateratings.py
59 lines (45 loc) · 1.93 KB
/
migrateratings.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
from Products.CMFCore.utils import getToolByName
from StringIO import StringIO
from Products.contentmigration.walker import CustomQueryWalker
from Products.contentmigration.migrator import BaseInlineMigrator
from zope.annotation.interfaces import IAnnotations
import transaction
class RatingsMigrator(BaseInlineMigrator):
"""
Migrate PSC Projects from the content ratings product to the twothumbs product
"""
src_portal_type = src_meta_type = 'PSCProject'
def migrate_ratings(self):
"""
contentratings and twothumbs both use annotations. Just want to move
one to another. Here we say anything >= 3 rating is a thumbs up
"""
from cioppino.twothumbs import rate as thumbrate
transaction.begin()
item = self.obj
annotations = IAnnotations(item)
if annotations:
if 'contentratings.userrating.psc_stars' in annotations:
ratings = annotations['contentratings.userrating.psc_stars'].all_user_ratings()
annotations = thumbrate.setupAnnotations(item)
for rating in ratings:
if rating >= 3.0:
thumbrate.loveIt(item, rating.userid)
else:
thumbrate.hateIt(item, rating.userid)
# we need to reindex th object anyways
item.reindexObject()
transaction.commit()
def migrate(self):
out = StringIO()
print >> out, "Starting ratings migration"
portal_url = getToolByName(self, 'portal_url')
portal = portal_url.getPortalObject()
# Migrate release count variable
walker = CustomQueryWalker(portal, RatingsMigrator,
query = {'portal_type': 'PSCProject'})
transaction.savepoint(optimistic=True)
print >> out, "Switching from contentratings to twothumbs.."
walker.go(out=out)
print >> out, walker.getOutput()
print >> out, "Migration finished"