1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU Affero General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU Affero General Public License for more details.
14 # You should have received a copy of the GNU Affero General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18 from mediagoblin
.init
import setup_global_and_app_config
, setup_database
19 from mediagoblin
.db
.mongo
.util
import ObjectId
21 from mediagoblin
.db
.sql
.models
import (Base
, User
, MediaEntry
, MediaComment
,
22 Tag
, MediaTag
, MediaFile
)
23 from mediagoblin
.db
.sql
.open import setup_connection_and_db_from_config
as \
25 from mediagoblin
.db
.mongo
.open import setup_connection_and_db_from_config
as \
27 from mediagoblin
.db
.sql
.base
import Session
32 def add_obj_ids(entry
, new_entry
):
34 print "%r -> %r" % (entry
._id
, new_entry
.id)
35 obj_id_table
[entry
._id
] = new_entry
.id
38 def copy_attrs(entry
, new_entry
, attr_list
):
41 setattr(new_entry
, a
, val
)
43 def copy_reference_attr(entry
, new_entry
, ref_attr
):
45 val
= obj_id_table
[val
]
46 setattr(new_entry
, ref_attr
, val
)
49 def convert_users(mk_db
):
52 for entry
in mk_db
.User
.find():
56 copy_attrs(entry
, new_entry
,
57 ('username', 'email', 'created', 'pw_hash', 'email_verified',
58 'status', 'verification_key', 'is_admin', 'url',
60 'fp_verification_key', 'fp_token_expire',))
61 # new_entry.fp_verification_expire = entry.fp_token_expire
63 session
.add(new_entry
)
65 add_obj_ids(entry
, new_entry
)
71 def convert_media_entries(mk_db
):
74 for entry
in mk_db
.MediaEntry
.find():
75 print repr(entry
.title
)
77 new_entry
= MediaEntry()
78 copy_attrs(entry
, new_entry
,
79 ('title', 'slug', 'created',
81 'media_type', 'state', 'license',
82 'fail_error', 'fail_metadata',
84 copy_reference_attr(entry
, new_entry
, "uploader")
86 session
.add(new_entry
)
88 add_obj_ids(entry
, new_entry
)
90 for key
, value
in entry
.media_files
.iteritems():
91 new_file
= MediaFile(name
=key
, file_path
=value
)
92 new_file
.media_entry
= new_entry
.id
99 def convert_media_tags(mk_db
):
101 session
.autoflush
= False
103 for media
in mk_db
.MediaEntry
.find():
104 print repr(media
.title
)
106 for otag
in media
.tags
:
107 print " ", repr((otag
["slug"], otag
["name"]))
109 nslug
= session
.query(Tag
).filter_by(slug
=otag
["slug"]).first()
110 print " ", repr(nslug
)
112 nslug
= Tag(slug
=otag
["slug"])
115 print " ", repr(nslug
), nslug
.id
119 ntag
.name
= otag
["name"]
120 ntag
.media_entry
= obj_id_table
[media
._id
]
127 def convert_media_comments(mk_db
):
130 for entry
in mk_db
.MediaComment
.find():
131 print repr(entry
.content
)
133 new_entry
= MediaComment()
134 copy_attrs(entry
, new_entry
,
137 copy_reference_attr(entry
, new_entry
, "media_entry")
138 copy_reference_attr(entry
, new_entry
, "author")
140 session
.add(new_entry
)
142 add_obj_ids(entry
, new_entry
)
149 global_config
, app_config
= setup_global_and_app_config("mediagoblin.ini")
151 sql_conn
, sql_db
= sql_connect(app_config
)
152 mk_conn
, mk_db
= mongo_connect(app_config
)
154 Base
.metadata
.create_all(sql_db
.engine
)
158 convert_media_entries(mk_db
)
160 convert_media_tags(mk_db
)
162 convert_media_comments(mk_db
)
166 if __name__
== '__main__':