From 200befc65fec95df470c1220c1de11f699b8b294 Mon Sep 17 00:00:00 2001 From: Noah Petherbridge Date: Fri, 20 Jun 2014 21:25:15 -0700 Subject: [PATCH] Add ability to rotate a photo --- rophako/model/photo.py | 42 ++++++++++++++++++++++++++++++++++-- rophako/modules/photo.py | 6 ++++++ rophako/www/photos/edit.html | 14 ++++++++++++ 3 files changed, 60 insertions(+), 2 deletions(-) diff --git a/rophako/model/photo.py b/rophako/model/photo.py index cf523f6..1b1fa66 100644 --- a/rophako/model/photo.py +++ b/rophako/model/photo.py @@ -195,12 +195,47 @@ def edit_photo(key, data): album = index["map"][key] - logger.info("Completely deleting the photo {} from album {}".format(key, album)) + logger.info("Updating data for the photo {} from album {}".format(key, album)) index["albums"][album][key].update(data) write_index(index) +def rotate_photo(key, rotate): + """Rotate a photo 90 degrees to the left or right.""" + photo = get_photo(key) + if not photo: return + + # Degrees to rotate. + degrees = None + if rotate == "left": + degrees = 90 + elif rotate == "right": + degrees = -90 + else: + degrees = 180 + + new_names = dict() + for size in ["large", "thumb", "avatar"]: + fname = os.path.join(config.PHOTO_ROOT_PRIVATE, photo[size]) + logger.info("Rotating image {} by {} degrees.".format(fname, degrees)) + + # Give it a new name. + filetype = fname.split(".")[-1] + outfile = random_name(filetype) + new_names[size] = outfile + + img = Image.open(fname) + img = img.rotate(degrees) + img.save(os.path.join(config.PHOTO_ROOT_PRIVATE, outfile)) + + # Delete the old name. + os.unlink(fname) + + # Save the new image names. + edit_photo(key, new_names) + + def delete_photo(key): """Delete a photo.""" index = get_index() @@ -216,7 +251,9 @@ def delete_photo(key): # Delete all the images. for size in ["large", "thumb", "avatar"]: logger.info("Delete: {}".format(photo[size])) - os.unlink(os.path.join(config.PHOTO_ROOT_PRIVATE, photo[size])) + fname = os.path.join(config.PHOTO_ROOT_PRIVATE, photo[size]) + if os.path.isfile(fname): + os.unlink(fname) # Delete it from the sort list. index["photo-order"][album].remove(key) @@ -543,6 +580,7 @@ def write_index(index): def random_name(filetype): """Get a random available file name to save a new photo.""" + filetype = filetype.lower() outfile = random_hash() + "." + filetype while os.path.isfile(os.path.join(config.PHOTO_ROOT_PRIVATE, outfile)): outfile = random_hash() + "." + filetype diff --git a/rophako/modules/photo.py b/rophako/modules/photo.py index b571201..edfb51a 100644 --- a/rophako/modules/photo.py +++ b/rophako/modules/photo.py @@ -195,7 +195,13 @@ def edit(key): if request.method == "POST": caption = request.form.get("caption", "") + rotate = request.form.get("rotate", "") Photo.edit_photo(key, dict(caption=caption)) + + # Rotating the photo? + if rotate in ["left", "right", "180"]: + Photo.rotate_photo(key, rotate) + flash("The photo has been updated.") return redirect(url_for(".view_photo", key=key)) diff --git a/rophako/www/photos/edit.html b/rophako/www/photos/edit.html index 6dda76a..a2aebcb 100644 --- a/rophako/www/photos/edit.html +++ b/rophako/www/photos/edit.html @@ -12,6 +12,20 @@ Photo Caption:

+ Rotate: + + + +

+