1 from __future__
import unicode_literals
4 from datetime
import datetime
6 from django
.contrib
.auth
.models
import User
7 from django
.contrib
.contenttypes
.models
import ContentType
8 from django
.db
import transaction
, IntegrityError
, DataError
9 from django
.utils
.text
import slugify
11 from mygpo
.users
.models
import User
as U
, UserProfile
12 from mygpo
.podcasts
.models
import (Podcast
, Episode
, URL
, Slug
, Tag
,
13 MergedUUID
, PodcastGroup
, )
14 from mygpo
.db
.couchdb
.podcast_state
import podcast_subscriber_count
17 logger
= logging
.getLogger(__name__
)
20 def to_maxlength(cls
, field
, val
):
21 """ Cut val to the maximum length of cls's field """
22 max_length
= cls
._meta
.get_field(field
).max_length
23 orig_length
= len(val
)
24 if orig_length
> max_length
:
25 val
= val
[:max_length
]
26 logger
.warn('%s.%s length reduced from %d to %d',
27 cls
.__name
__, field
, orig_length
, max_length
)
34 # no need in migrating already deleted users
38 user
, created
= User
.objects
.update_or_create(username
=u
.username
,
41 'is_active': u
.is_active
,
42 'is_staff': u
.is_staff
,
43 'is_superuser': u
.is_superuser
,
44 'last_login': u
.last_login
or datetime(1970, 01, 01),
45 'date_joined': u
.date_joined
,
46 'password': u
.password
,
50 if hasattr(user
, 'profile'):
51 profile
= user
.profile
53 profile
= UserProfile(user
=user
)
56 profile
.suggestions_up_to_date
= u
.suggestions_up_to_date
57 profile
.about
= u
.about
or ''
58 profile
.google_email
= u
.google_email
59 profile
.subscriptions_token
= u
.subscriptions_token
60 profile
.favorite_feeds_token
= u
.favorite_feeds_token
61 profile
.publisher_update_token
= u
.publisher_update_token
62 profile
.userpage_token
= u
.userpage_token
66 from couchdbkit
import Database
67 db
= Database('http://127.0.0.1:6984/mygpo_users')
68 from couchdbkit
.changes
import ChangesStream
, fold
, foreach
72 'User': (U
, migrate_user
),
73 'Suggestions': (None, None),
76 def migrate_change(c
):
77 logger
.info('Migrate seq %s', c
['seq'])
80 if not 'doc_type' in doc
:
81 logger
.warn('Document contains no doc_type: %r', doc
)
84 doctype
= doc
['doc_type']
86 cls
, migrate
= MIGRATIONS
[doctype
]
89 logger
.warn("Skipping '%s'", doctype
)
97 with
ChangesStream(db
,
103 for change
in stream
:
104 migrate_change(change
)