diff options
author | Jessica Tallon <jessica@megworld.co.uk> | 2014-09-15 19:34:42 +0100 |
---|---|---|
committer | Jessica Tallon <jessica@megworld.co.uk> | 2014-10-09 19:16:54 +0100 |
commit | c0434db46910e891313495b5ae94cbbe1dd08058 (patch) | |
tree | 9f7cfe31ca3bf4c5f49adf2daa964eafe91061dd /mediagoblin/media_types | |
parent | d60d686a14d10af3f58867569622735ff9ecd068 (diff) | |
download | mediagoblin-c0434db46910e891313495b5ae94cbbe1dd08058.tar.lz mediagoblin-c0434db46910e891313495b5ae94cbbe1dd08058.tar.xz mediagoblin-c0434db46910e891313495b5ae94cbbe1dd08058.zip |
Add location model and migrations
Diffstat (limited to 'mediagoblin/media_types')
-rw-r--r-- | mediagoblin/media_types/image/migrations.py | 56 | ||||
-rw-r--r-- | mediagoblin/media_types/image/models.py | 4 | ||||
-rw-r--r-- | mediagoblin/media_types/image/processing.py | 4 |
3 files changed, 58 insertions, 6 deletions
diff --git a/mediagoblin/media_types/image/migrations.py b/mediagoblin/media_types/image/migrations.py index f54c23ea..4af8f298 100644 --- a/mediagoblin/media_types/image/migrations.py +++ b/mediagoblin/media_types/image/migrations.py @@ -13,5 +13,61 @@ # # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. +import json + +from sqlalchemy import MetaData, Column, ForeignKey + +from mediagoblin.db.migration_tools import RegisterMigration, inspect_table + MIGRATIONS = {} + +@RegisterMigration(1, MIGRATIONS) +def remove_gps_from_image(db): + """ + This will remove GPS coordinates from the image model to put them + on the new Location model. + """ + metadata = MetaData(bind=db.bind) + image_table = inspect_table(metadata, "image__mediadata") + location_table = inspect_table(metadata, "core__locations") + media_entires_table = inspect_table(metadata, "core__media_entries") + + # First do the data migration + for row in db.execute(image_table.select()): + fields = { + "longitude": row.gps_longitude, + "latitude": row.gps_latitude, + "altitude": row.gps_altitude, + "direction": row.gps_direction, + } + + # Remove empty values + for k, v in fields.items(): + if v is None: + del fields[k] + + # No point in adding empty locations + if not fields: + continue + + # JSONEncoded is actually a string field just json.dumped + # without the ORM we're responsible for that. + fields = json.dumps(fields) + + location = db.execute(location_table.insert().values(position=fields)) + + # now store the new location model on Image + db.execute(media_entires_table.update().values( + location=location.inserted_primary_key[0] + ).where(media_entires_table.c.id==row.media_entry)) + + db.commit() + + # All that data has been migrated across lets remove the fields + image_table.columns["gps_longitude"].drop() + image_table.columns["gps_latitude"].drop() + image_table.columns["gps_altitude"].drop() + image_table.columns["gps_direction"].drop() + + db.commit() diff --git a/mediagoblin/media_types/image/models.py b/mediagoblin/media_types/image/models.py index b2ea3960..02040ec2 100644 --- a/mediagoblin/media_types/image/models.py +++ b/mediagoblin/media_types/image/models.py @@ -39,10 +39,6 @@ class ImageData(Base): width = Column(Integer) height = Column(Integer) exif_all = Column(JSONEncoded) - gps_longitude = Column(Float) - gps_latitude = Column(Float) - gps_altitude = Column(Float) - gps_direction = Column(Float) DATA_MODEL = ImageData diff --git a/mediagoblin/media_types/image/processing.py b/mediagoblin/media_types/image/processing.py index 1db82ee7..6dd540e5 100644 --- a/mediagoblin/media_types/image/processing.py +++ b/mediagoblin/media_types/image/processing.py @@ -23,6 +23,7 @@ import logging import argparse from mediagoblin import mg_globals as mgg +from mediagoblin.db.models import Location from mediagoblin.processing import ( BadMediaFail, FilenameBuilder, MediaProcessor, ProcessingManager, @@ -231,8 +232,7 @@ class CommonImageProcessor(MediaProcessor): self.entry.media_data_init(exif_all=exif_all) if len(gps_data): - for key in list(gps_data.keys()): - gps_data['gps_' + key] = gps_data.pop(key) + Location.create({"position": gps_data}, self.entry) self.entry.media_data_init(**gps_data) |