[Models] set episode slug only if one can be generated
[mygpo.git] / mygpo / maintenance / migrate.py
blob2e3b5e9a6d5dd9a6fa5fda81f92609722e41603a
1 from __future__ import unicode_literals
3 import json
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
16 import logging
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)
29 return val
32 def migrate_user(u):
34 # no need in migrating already deleted users
35 if u.deleted:
36 return
38 user, created = User.objects.update_or_create(username=u.username,
39 defaults = {
40 'email': u.email,
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
52 else:
53 profile = UserProfile(user=user)
55 profile.uuid = u._id
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
63 profile.save()
66 from couchdbkit import Database
67 db = Database('http://127.0.0.1:6984/mygpo_users')
68 from couchdbkit.changes import ChangesStream, fold, foreach
71 MIGRATIONS = {
72 'User': (U, migrate_user),
73 'Suggestions': (None, None),
76 def migrate_change(c):
77 logger.info('Migrate seq %s', c['seq'])
78 doc = c['doc']
80 if not 'doc_type' in doc:
81 logger.warn('Document contains no doc_type: %r', doc)
82 return
84 doctype = doc['doc_type']
86 cls, migrate = MIGRATIONS[doctype]
88 if cls is None:
89 logger.warn("Skipping '%s'", doctype)
90 return
92 obj = cls.wrap(doc)
93 migrate(obj)
96 def migrate(since=0):
97 with ChangesStream(db,
98 feed="continuous",
99 heartbeat=True,
100 include_docs=True,
101 since=since,
102 ) as stream:
103 for change in stream:
104 migrate_change(change)