A Python content management system designed for kirsle.net featuring a blog, comments and photo albums. https://rophako.kirsle.net/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

photo.py 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. # -*- coding: utf-8 -*-
  2. """Endpoints for the photo albums."""
  3. from flask import Blueprint, g, request, redirect, url_for, session, flash
  4. import rophako.model.user as User
  5. import rophako.model.photo as Photo
  6. from rophako.utils import template, pretty_time, login_required
  7. from rophako.log import logger
  8. from config import *
  9. mod = Blueprint("photo", __name__, url_prefix="/photos")
  10. @mod.route("/")
  11. def index():
  12. return redirect(url_for(".albums"))
  13. @mod.route("/albums")
  14. def albums():
  15. """View the index of the photo albums."""
  16. albums = Photo.list_albums()
  17. # If there's only one album, jump directly to that one.
  18. if len(albums) == 1:
  19. return redirect(url_for(".album_index", name=albums[0]["name"]))
  20. g.info["albums"] = albums
  21. return template("photos/albums.html")
  22. @mod.route("/album/<name>")
  23. def album_index(name):
  24. """View the photos inside an album."""
  25. photos = Photo.list_photos(name)
  26. if photos is None:
  27. flash("That album doesn't exist.")
  28. return redirect(url_for(".albums"))
  29. g.info["album"] = name
  30. g.info["photos"] = photos
  31. return template("photos/album.html")
  32. @mod.route("/view/<key>")
  33. def view_photo(key):
  34. """View a specific photo."""
  35. photo = Photo.get_photo(key)
  36. if photo is None:
  37. flash("That photo wasn't found!")
  38. return redirect(url_for(".albums"))
  39. # Get the author info.
  40. author = User.get_user(uid=photo["author"])
  41. if author:
  42. g.info["author"] = author
  43. g.info["photo"] = photo
  44. g.info["photo"]["key"] = key
  45. g.info["photo"]["pretty_time"] = pretty_time(PHOTO_TIME_FORMAT, photo["uploaded"])
  46. return template("photos/view.html")
  47. @mod.route("/upload", methods=["GET", "POST"])
  48. @login_required
  49. def upload():
  50. """Upload a photo."""
  51. if request.method == "POST":
  52. # We're posting the upload. What source is the pic from?
  53. result = None
  54. location = request.form.get("location")
  55. if location == "pc":
  56. # An upload from the PC.
  57. result = Photo.upload_from_pc(request)
  58. elif location == "www":
  59. # An upload from the Internet.
  60. result = Photo.upload_from_www(request.form)
  61. else:
  62. flash("Stop messing around.")
  63. return redirect(url_for(".upload"))
  64. # How'd it go?
  65. if result["success"] is not True:
  66. flash("The upload has failed: {}".format(result["error"]))
  67. return redirect(url_for(".upload"))
  68. return redirect(url_for(".crop", photo=result["photo"]))
  69. # Get the list of available albums.
  70. g.info["album_list"] = [
  71. "My Photos", # the default
  72. ]
  73. g.info["selected"] = PHOTO_DEFAULT_ALBUM
  74. albums = Photo.list_albums()
  75. if len(albums):
  76. g.info["album_list"] = [ album["name"] for album in albums ]
  77. g.info["selected"] = albums[0]
  78. return template("photos/upload.html")
  79. @mod.route("/crop/<photo>", methods=["GET", "POST"])
  80. @login_required
  81. def crop(photo):
  82. pic = Photo.get_photo(photo)
  83. if not pic:
  84. flash("The photo you want to crop wasn't found!")
  85. return redirect(url_for(".albums"))
  86. # Saving?
  87. if request.method == "POST":
  88. try:
  89. x = int(request.form.get("x", 0))
  90. y = int(request.form.get("y", 0))
  91. length = int(request.form.get("length", 0))
  92. except:
  93. flash("Error with form inputs.")
  94. return redirect(url_for(".crop", photo=photo))
  95. # Re-crop the photo!
  96. Photo.crop_photo(photo, x, y, length)
  97. flash("The photo has been cropped!")
  98. return redirect(url_for(".albums")) # TODO go to photo
  99. # Get the photo's true size.
  100. true_width, true_height = Photo.get_image_dimensions(pic)
  101. g.info["true_width"] = true_width
  102. g.info["true_height"] = true_height
  103. g.info["photo"] = photo
  104. g.info["preview"] = pic["large"]
  105. return template("photos/crop.html")
  106. @mod.route("/set_cover/<album>/<key>")
  107. @login_required
  108. def set_cover(album, key):
  109. """Set the pic as the album cover."""
  110. pic = Photo.get_photo(key)
  111. if not pic:
  112. flash("The photo you want to crop wasn't found!")
  113. return redirect(url_for(".albums"))
  114. Photo.set_album_cover(album, key)
  115. flash("Album cover has been set.")
  116. return redirect(url_for(".albums"))
  117. @mod.route("/set_profile/<key>")
  118. @login_required
  119. def set_profile(key):
  120. """Set the pic as your profile picture."""
  121. pic = Photo.get_photo(key)
  122. if not pic:
  123. flash("The photo wasn't found!")
  124. return redirect(url_for(".albums"))
  125. uid = g.info["session"]["uid"]
  126. User.update_user(uid, dict(picture=key))
  127. flash("Your profile picture has been updated.")
  128. return redirect(url_for(".view_photo", key=key))
  129. @mod.route("/edit/<key>", methods=["GET", "POST"])
  130. @login_required
  131. def edit(key):
  132. """Edit a photo."""
  133. pic = Photo.get_photo(key)
  134. if not pic:
  135. flash("The photo wasn't found!")
  136. return redirect(url_for(".albums"))
  137. if request.method == "POST":
  138. caption = request.form.get("caption", "")
  139. Photo.edit_photo(key, dict(caption=caption))
  140. flash("The photo has been updated.")
  141. return redirect(url_for(".view_photo", key=key))
  142. g.info["key"] = key
  143. g.info["photo"] = pic
  144. return template("photos/edit.html")
  145. @mod.route("/delete/<key>", methods=["GET", "POST"])
  146. @login_required
  147. def delete(key):
  148. """Delete a photo."""
  149. pic = Photo.get_photo(key)
  150. if not pic:
  151. flash("The photo wasn't found!")
  152. return redirect(url_for(".albums"))
  153. if request.method == "POST":
  154. # Do it.
  155. Photo.delete_photo(key)
  156. flash("The photo has been deleted.")
  157. return redirect(url_for(".albums"))
  158. g.info["key"] = key
  159. g.info["photo"] = pic
  160. return template("photos/delete.html")
  161. @mod.route("/arrange_albums", methods=["GET", "POST"])
  162. @login_required
  163. def arrange_albums():
  164. """Rearrange the photo album order."""
  165. albums = Photo.list_albums()
  166. if len(albums) == 0:
  167. flash("There are no albums yet.")
  168. return redirect(url_for(".albums"))
  169. if request.method == "POST":
  170. order = request.form.get("order", "").split(";")
  171. Photo.order_albums(order)
  172. flash("The albums have been rearranged!")
  173. return redirect(url_for(".albums"))
  174. g.info["albums"] = albums
  175. return template("photos/arrange_albums.html")
  176. @mod.route("/arrange_photos/<album>", methods=["GET", "POST"])
  177. @login_required
  178. def arrange_photos(album):
  179. """Rearrange the photos in an album."""
  180. photos = Photo.list_photos(album)
  181. if photos is None:
  182. flash("That album doesn't exist.")
  183. return redirect(url_for(".albums"))
  184. if request.method == "POST":
  185. order = request.form.get("order", "").split(";")
  186. Photo.order_photos(album, order)
  187. flash("The albums have been rearranged!")
  188. return redirect(url_for(".album_index", name=album))
  189. g.info["album"] = album
  190. g.info["photos"] = photos
  191. return template("photos/arrange_photos.html")