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
, MediaAttachmentFile
)
23 from mediagoblin
.media_types
.video
.models
import VideoData
24 from mediagoblin
.db
.sql
.open import setup_connection_and_db_from_config
as \
26 from mediagoblin
.db
.mongo
.open import setup_connection_and_db_from_config
as \
28 from mediagoblin
.db
.sql
.base
import Session
33 def add_obj_ids(entry
, new_entry
):
35 print "%r -> %r" % (entry
._id
, new_entry
.id)
36 obj_id_table
[entry
._id
] = new_entry
.id
39 def copy_attrs(entry
, new_entry
, attr_list
):
42 setattr(new_entry
, a
, val
)
44 def copy_reference_attr(entry
, new_entry
, ref_attr
):
46 val
= obj_id_table
[val
]
47 setattr(new_entry
, ref_attr
, val
)
50 def convert_users(mk_db
):
53 for entry
in mk_db
.User
.find().sort('created'):
57 copy_attrs(entry
, new_entry
,
58 ('username', 'email', 'created', 'pw_hash', 'email_verified',
59 'status', 'verification_key', 'is_admin', 'url',
61 'fp_verification_key', 'fp_token_expire',))
62 # new_entry.fp_verification_expire = entry.fp_token_expire
64 session
.add(new_entry
)
66 add_obj_ids(entry
, new_entry
)
72 def convert_media_entries(mk_db
):
75 for entry
in mk_db
.MediaEntry
.find().sort('created'):
76 print repr(entry
.title
)
78 new_entry
= MediaEntry()
79 copy_attrs(entry
, new_entry
,
80 ('title', 'slug', 'created',
82 'media_type', 'state', 'license',
83 'fail_error', 'fail_metadata',
85 copy_reference_attr(entry
, new_entry
, "uploader")
87 session
.add(new_entry
)
89 add_obj_ids(entry
, new_entry
)
91 for key
, value
in entry
.media_files
.iteritems():
92 new_file
= MediaFile(name
=key
, file_path
=value
)
93 new_file
.media_entry
= new_entry
.id
96 for attachment
in entry
.attachment_files
:
97 new_attach
= MediaAttachmentFile(
98 name
=attachment
["name"],
99 filepath
=attachment
["filepath"],
100 created
=attachment
["created"]
102 new_attach
.media_entry
= new_entry
.id
103 Session
.add(new_attach
)
109 def convert_video(mk_db
):
112 for media
in mk_db
.MediaEntry
.find(
113 {'media_type': 'mediagoblin.media_types.video'}).sort('created'):
114 media_data_row
= VideoData(**media
.media_data
)
115 media_data_row
.media_entry
= obj_id_table
[media
._id
]
116 session
.add(media_data_row
)
122 def convert_media_tags(mk_db
):
124 session
.autoflush
= False
126 for media
in mk_db
.MediaEntry
.find().sort('created'):
127 print repr(media
.title
)
129 for otag
in media
.tags
:
130 print " ", repr((otag
["slug"], otag
["name"]))
132 nslug
= session
.query(Tag
).filter_by(slug
=otag
["slug"]).first()
133 print " ", repr(nslug
)
135 nslug
= Tag(slug
=otag
["slug"])
138 print " ", repr(nslug
), nslug
.id
142 ntag
.name
= otag
["name"]
143 ntag
.media_entry
= obj_id_table
[media
._id
]
150 def convert_media_comments(mk_db
):
153 for entry
in mk_db
.MediaComment
.find().sort('created'):
154 print repr(entry
.content
)
156 new_entry
= MediaComment()
157 copy_attrs(entry
, new_entry
,
160 copy_reference_attr(entry
, new_entry
, "media_entry")
161 copy_reference_attr(entry
, new_entry
, "author")
163 session
.add(new_entry
)
165 add_obj_ids(entry
, new_entry
)
171 def run_conversion(config_name
):
172 global_config
, app_config
= setup_global_and_app_config(config_name
)
174 sql_conn
, sql_db
= sql_connect(app_config
)
175 mk_conn
, mk_db
= mongo_connect(app_config
)
177 Base
.metadata
.create_all(sql_db
.engine
)
181 convert_media_entries(mk_db
)
185 convert_media_tags(mk_db
)
187 convert_media_comments(mk_db
)
191 if __name__
== '__main__':
192 run_conversion("mediagoblin.ini")