1 from base64
import b64decode
2 from optparse
import make_option
5 from couchdb
.multipart
import write_multipart
7 from django
.core
.management
.base
import BaseCommand
9 from mygpo
.core
.models
import Podcast
10 from mygpo
.users
.models
import PodcastUserState
, EpisodeUserState
, \
12 from mygpo
.directory
.models
import Category
13 from mygpo
.utils
import progress
14 from mygpo
.json
import json
17 class Command(BaseCommand
):
19 Dumps a Sample of the whole Database that can be used for
20 testing/development. All objects that are (indirectly) referenced
21 be the users specified by --user args are dumped.
23 The dump is similar to a dump of couchdb-python's couchdb-dump and
24 can be imported by its couchdb-load
28 option_list
= BaseCommand
.option_list
+ (
29 make_option('--user', action
='append', type="string", dest
='users',
30 help="User for which related data should be dumped"),
34 def handle(self
, *args
, **options
):
38 for username
in options
.get('users', []):
39 user
= User
.get_user(username
)
45 suggestions
= Suggestions
.for_user(user
)
46 docs
.add(suggestions
._id
)
49 for p_state
in PodcastUserState
.for_user(user
):
53 for tag
in p_state
.tags
:
54 c
= Category
.for_tag(tag
)
58 podcast
= Podcast
.get(p_state
.podcast
)
62 for s
in podcast
.tags
:
63 for tag
in podcast
.tags
[s
]:
64 c
= Category
.for_tag(tag
)
68 for episode
in podcast
.get_episodes():
72 e_state
= episode
.get_user_state(user
)
82 def dump(self
, docs
, db
):
86 envelope
= write_multipart(output
, boundary
=boundary
)
89 for n
, docid
in enumerate(docs
):
91 doc
= db
.get(docid
, attachments
=True)
92 attachments
= doc
.pop('_attachments', {})
93 jsondoc
= json
.encode(doc
)
96 parts
= envelope
.open({
97 'Content-ID': doc
['_id'],
98 'ETag': '"%s"' % doc
['_rev']
100 parts
.add('application/json', jsondoc
)
102 for name
, info
in attachments
.items():
103 content_type
= info
.get('content_type')
104 if content_type
is None: # CouchDB < 0.8
105 content_type
= info
.get('content-type')
106 parts
.add(content_type
, b64decode(info
['data']), {
112 envelope
.add('application/json', jsondoc
, {
113 'Content-ID': doc
['_id'],
114 'ETag': '"%s"' % doc
['_rev']
117 progress(n
+1, total
, docid
, stream
=sys
.stderr
)