def _deallocate_from_sub_albums(self, img_obj): utils.debugout("DEALLOC CALLED", len(self.sub_albums)) if not self.sub_albums: return try: sub_album_with_image = [ ab for ab in self.sub_albums if img_obj.pkid in ab.image_ids ][0] except IndexError: # No sub_album has that image return counts = [ab.image_count for ab in self.sub_albums] min_cnt, max_cnt = min(counts), max(counts) album_in_max_cnt_group = sub_album_with_image.image_count == max_cnt # First remove the image sub_album_with_image.remove_image(img_obj) if not album_in_max_cnt_group: # We need to move an image from one of the albums with max_cnt. max_albums = [ ab for ab in self.sub_albums if ab.image_count == max_cnt ] max_album = random.choice(max_albums) image_to_move = random.choice(max_album.images) max_album.remove_image(image_to_move) sub_album_with_image.add_image(image_to_move)
def add_image(self, img): img_obj = Image.get(img) utils.debugout("Adding image to", self) sql = "insert into album_image (album_id, image_id) values (%s, %s);" with utils.DbCursor() as crs: crs.execute(sql, (self.pkid, img_obj.pkid)) self._allocate_to_sub_albums(img_obj)
def set_frame_album(self, frame_id): utils.debugout("SET_FRAME_ALBUM called") image_names = [] if self.image_ids: sql = "select name from image where pkid in %s;" with utils.DbCursor() as crs: crs.execute(sql, (self.image_ids, )) image_names = [rec["name"] for rec in crs.fetchall()] utils.write_key(frame_id, "images", image_names) utils.debugout("Wrote images for frame_id =", frame_id)
def update(pkid=None): rf = request.form rfc = dict(rf) if "delete" in rfc: pkid = rfc["pkid"] entities.Frame.delete(pkid) return redirect(url_for("index")) # Get rid of the 'submit' field rfc.pop("submit", None) pkid = rfc["pkid"] = rfc["pkid"] or pkid frame_dict = entities.Frame.get(pkid).to_dict() frame_dict.update(rfc) frame = entities.Frame(**frame_dict) utils.debugout("FRAMEDICT", frame_dict) frame.save() return redirect(url_for("index"))
def update_images(self, image_ids): utils.debugout("UPD IMG CALLED") if self.smart: images = self.smart_album_images(self.pkid) image_ids = [img.pkid for img in images] else: current_ids = set(self.image_ids) selected_ids = set(image_ids) to_remove = current_ids.difference(selected_ids) to_add = selected_ids.difference(current_ids) utils.debugout("TOREMOVE", len(to_remove)) utils.debugout("TOADD", len(to_add)) self.remove_images(to_remove) self.add_images(to_add) utils.debugout("CALLING UPDATE_FRAME_ALBUM") self.update_frame_album(image_ids)
def register_frame(): rf = request.form utils.debugout("FORM", rf) pkid = rf["pkid"] try: frame = entities.Frame.get(pkid) except exc.NotFound: # New frame frame = entities.Frame(pkid=pkid) frame.ip = request.remote_addr frame.freespace = rf["freespace"] frame.save(new=True) agent = request.headers.get("User-agent") debugout("Album:", frame.album_id) if agent == "photoviewer": # from the frame app; return the pkid and images if frame.album_id: album = entities.Album.get(frame.album_id) image_names = album.image_names else: image_names = [] return json.dumps([frame.pkid, image_names]) # From a web interface return render_template("registration.html")
def _allocate_to_sub_albums(self, img_obj): utils.debugout("ALLOC CALLED", len(self.sub_albums)) if not self.sub_albums: return albums_by_image_count = sorted([ab for ab in self.sub_albums], key=lambda x: x.image_count) utils.debugout("ALBBYCOUNT", albums_by_image_count) # Add the new image to the first album in the list; it will have image_count <= the others utils.debugout("ADDING IMAGE TO", albums_by_image_count[0]) albums_by_image_count[0].add_image(img_obj)
def update_frame_album(self, image_ids=None): """Updates the 'images' key for all frames that are linked to the album.""" utils.debugout("UPDATE_FRAME_ALBUM called") image_ids = image_ids or self.image_ids sql = "select pkid from frame where album_id = %s;" with utils.DbCursor() as crs: count = crs.execute(sql, (self.pkid, )) utils.debugout("FRAME COUNT", count) frame_ids = [rec["pkid"] for rec in crs.fetchall()] utils.debugout("Album.update_frame_album; frame_ids=", frame_ids, "album_id =", self.pkid) if frame_ids: sql = "select name from image where pkid in %s;" crs.execute(sql, (image_ids, )) image_names = [rec["name"] for rec in crs.fetchall()] for frame_id in frame_ids: utils.write_key(frame_id, "images", image_names) utils.debugout("Wrote images for frame_id =", frame_id)
def manage_frames_POST(frameset_id): frame_ids = request.form.getlist("selected") utils.debugout("FRAMEIDS", frame_ids) fs = entities.Frameset.get(frameset_id) fs.set_frames(frame_ids) return redirect("/framesets")