Merge branch 'newlayout' into newlayout-stage
[mediagoblin.git] / mediagoblin / db / sql / convert.py
CommitLineData
fbad3a9f 1# GNU MediaGoblin -- federated, autonomous media hosting
7f4ebeed 2# Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
fbad3a9f
E
3#
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.
8#
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.
13#
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/>.
16
17
dbcf5289 18from mediagoblin.init import setup_global_and_app_config, setup_database
010fe2d7 19from mediagoblin.db.mongo.util import ObjectId
dbcf5289
E
20
21from mediagoblin.db.sql.models import (Base, User, MediaEntry, MediaComment,
35029581 22 Tag, MediaTag, MediaFile, MediaAttachmentFile)
94df840b 23from mediagoblin.media_types.video.models import VideoData
010fe2d7
E
24from mediagoblin.db.sql.open import setup_connection_and_db_from_config as \
25 sql_connect
26from mediagoblin.db.mongo.open import setup_connection_and_db_from_config as \
27 mongo_connect
7b194a79 28from mediagoblin.db.sql.base import Session
dbcf5289
E
29
30
31obj_id_table = dict()
32
33def add_obj_ids(entry, new_entry):
34 global obj_id_table
35 print "%r -> %r" % (entry._id, new_entry.id)
36 obj_id_table[entry._id] = new_entry.id
37
38
39def copy_attrs(entry, new_entry, attr_list):
40 for a in attr_list:
41 val = entry[a]
42 setattr(new_entry, a, val)
43
44def copy_reference_attr(entry, new_entry, ref_attr):
45 val = entry[ref_attr]
46 val = obj_id_table[val]
47 setattr(new_entry, ref_attr, val)
48
49
50def convert_users(mk_db):
51 session = Session()
52
15821817 53 for entry in mk_db.User.find().sort('created'):
dbcf5289
E
54 print entry.username
55
56 new_entry = User()
57 copy_attrs(entry, new_entry,
58 ('username', 'email', 'created', 'pw_hash', 'email_verified',
59 'status', 'verification_key', 'is_admin', 'url',
e61ab099 60 'bio',
dbcf5289
E
61 'fp_verification_key', 'fp_token_expire',))
62 # new_entry.fp_verification_expire = entry.fp_token_expire
63
64 session.add(new_entry)
65 session.flush()
66 add_obj_ids(entry, new_entry)
67
68 session.commit()
69 session.close()
70
71
72def convert_media_entries(mk_db):
73 session = Session()
74
15821817 75 for entry in mk_db.MediaEntry.find().sort('created'):
dbcf5289
E
76 print repr(entry.title)
77
78 new_entry = MediaEntry()
79 copy_attrs(entry, new_entry,
80 ('title', 'slug', 'created',
1e72e075 81 'description',
ac014f04 82 'media_type', 'state', 'license',
cf27accc 83 'fail_error', 'fail_metadata',
dbcf5289
E
84 'queued_task_id',))
85 copy_reference_attr(entry, new_entry, "uploader")
86
87 session.add(new_entry)
88 session.flush()
89 add_obj_ids(entry, new_entry)
90
02db7e0a
E
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
94 Session.add(new_file)
95
35029581
E
96 for attachment in entry.attachment_files:
97 new_attach = MediaAttachmentFile(
98 name=attachment["name"],
99 filepath=attachment["filepath"],
100 created=attachment["created"]
101 )
102 new_attach.media_entry = new_entry.id
103 Session.add(new_attach)
104
dbcf5289
E
105 session.commit()
106 session.close()
107
108
109def convert_media_tags(mk_db):
110 session = Session()
111 session.autoflush = False
112
15821817 113 for media in mk_db.MediaEntry.find().sort('created'):
dbcf5289
E
114 print repr(media.title)
115
116 for otag in media.tags:
117 print " ", repr((otag["slug"], otag["name"]))
118
119 nslug = session.query(Tag).filter_by(slug=otag["slug"]).first()
120 print " ", repr(nslug)
121 if nslug is None:
122 nslug = Tag(slug=otag["slug"])
123 session.add(nslug)
124 session.flush()
125 print " ", repr(nslug), nslug.id
126
127 ntag = MediaTag()
128 ntag.tag = nslug.id
129 ntag.name = otag["name"]
130 ntag.media_entry = obj_id_table[media._id]
131 session.add(ntag)
132
133 session.commit()
134 session.close()
135
136
137def convert_media_comments(mk_db):
138 session = Session()
139
15821817 140 for entry in mk_db.MediaComment.find().sort('created'):
dbcf5289
E
141 print repr(entry.content)
142
143 new_entry = MediaComment()
144 copy_attrs(entry, new_entry,
145 ('created',
feba5c52 146 'content',))
dbcf5289
E
147 copy_reference_attr(entry, new_entry, "media_entry")
148 copy_reference_attr(entry, new_entry, "author")
149
150 session.add(new_entry)
151 session.flush()
152 add_obj_ids(entry, new_entry)
153
154 session.commit()
155 session.close()
156
157
98913512
E
158def run_conversion(config_name):
159 global_config, app_config = setup_global_and_app_config(config_name)
dbcf5289 160
a45631e3 161 sql_conn, sql_db = sql_connect(app_config)
010fe2d7 162 mk_conn, mk_db = mongo_connect(app_config)
dbcf5289 163
010fe2d7 164 Base.metadata.create_all(sql_db.engine)
dbcf5289
E
165
166 convert_users(mk_db)
7b194a79 167 Session.remove()
dbcf5289 168 convert_media_entries(mk_db)
7b194a79 169 Session.remove()
dbcf5289 170 convert_media_tags(mk_db)
7b194a79 171 Session.remove()
dbcf5289 172 convert_media_comments(mk_db)
7b194a79 173 Session.remove()
dbcf5289
E
174
175
176if __name__ == '__main__':
98913512 177 run_conversion("mediagoblin.ini")