Use urljoin to create proper feed media URLs
[larjonas-mediagoblin.git] / mediagoblin / media_types / image / migrations.py
blob83f5dc8e6899330a2b22489d9c12943002d8d76e
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/>.
16 import json
18 from sqlalchemy import MetaData, Column, ForeignKey
20 from mediagoblin.db.migration_tools import RegisterMigration, inspect_table
23 MIGRATIONS = {}
25 @RegisterMigration(1, MIGRATIONS)
26 def remove_gps_from_image(db):
27 """
28 This will remove GPS coordinates from the image model to put them
29 on the new Location model.
30 """
31 metadata = MetaData(bind=db.bind)
32 image_table = inspect_table(metadata, "image__mediadata")
33 location_table = inspect_table(metadata, "core__locations")
34 media_entries_table = inspect_table(metadata, "core__media_entries")
36 # First do the data migration
37 for row in db.execute(image_table.select()):
38 fields = {
39 "longitude": row.gps_longitude,
40 "latitude": row.gps_latitude,
41 "altitude": row.gps_altitude,
42 "direction": row.gps_direction,
45 # Remove empty values
46 for k, v in fields.items():
47 if v is None:
48 del fields[k]
50 # No point in adding empty locations
51 if not fields:
52 continue
54 # JSONEncoded is actually a string field just json.dumped
55 # without the ORM we're responsible for that.
56 fields = json.dumps(fields)
58 location = db.execute(location_table.insert().values(position=fields))
60 # now store the new location model on Image
61 db.execute(media_entries_table.update().values(
62 location=location.inserted_primary_key[0]
63 ).where(media_entries_table.c.id==row.media_entry))
65 db.commit()
67 # All that data has been migrated across lets remove the fields
68 image_table.columns["gps_longitude"].drop()
69 image_table.columns["gps_latitude"].drop()
70 image_table.columns["gps_altitude"].drop()
71 image_table.columns["gps_direction"].drop()
73 db.commit()