def _find_adj_photos(self, s, aid, pid): photos_q = s.query(Photo).filter(Photo.album_id == aid) cur_album = s.query(Album).filter(Album.id == aid).first() if cur_album.sort_by == utils.SORT_BY_DATE: photos_q = photos_q.order_by(Photo.created) elif cur_album.sort_by == utils.SORT_BY_DATE_DESC: photos_q = photos_q.order_by(Photo.created.desc()) photos = photos_q.all() photo_ids = map(lambda x: x.id, photos) cur_idx = photo_ids.index(long(pid)) if cur_idx == 0: prev = None first = None else: prev = photos[cur_idx - 1] first = photos[0] if cur_idx == len(photo_ids) - 1: next = None last = None else: next = photos[cur_idx + 1] last = photos[len(photo_ids) - 1] return (cur_idx + 1, len(photos), first, prev, next, last)
def album_del(self, aid): album = s.query(Album).filter(Album.id == aid).first() if not album: abort(404) s.delete(album) s.commit() redirect(url(controller = "album", action = "show_first_page", aid = album.parent_id))
def photo_del_submit(self, aid, pid): photo_q = s.query(Photo) photo_obj = photo_q.filter_by(album_id=aid, id=pid).first() if photo_obj is None: c.name = pid return render('/photo_not_found.mako') s.delete(photo_obj) s.commit() redirect(url(controller = "album", action = "show_first_page", aid = aid))
def add_archive(aid, file, arc_type): """ Add archive with photos to a given album """ print "extracting archive ..." z = zipfile.ZipFile(file) for fname in z.namelist(): if mimetypes.guess_type(fname)[0] == "image/jpeg": photo = Photo(os.path.basename(fname), aid, z.read(fname)) s.add(photo) s.commit()
def photo_edit_submit(self, aid, pid): if request.params.get("Cancel"): redirect(url(controller="album", action = "show_first_page", aid = aid)) photo = s.query(Photo).filter_by(album_id=aid, id=pid)[0] photo.display_name = request.params.get("title") photo.hidden = bool(request.params.get("hide_photo", 0)) s.commit() redirect(url(controller="album", action = "show_first_page", aid = aid))
def photo_add_single(self, aid, new_photo): name = new_photo.filename.lstrip(os.sep) tp = mimetypes.guess_type(name)[0] if tp == "image/jpeg": photo = Photo(name, aid, new_photo.file.read()) new_photo.file.close() s.add(photo) s.commit() elif tp == "application/zip": add_archive(aid, new_photo.file, arc_type = "zip") else: return "Unsupported type" return None
def setup_app(command, conf, vars): load_environment(conf.global_conf, conf.local_conf) # Create the tables if they don't already exist log.info("Creating tables...") Base.metadata.create_all(bind = Session.bind) log.info("Successfully set up.") if not os.path.exists(conf.global_conf["permanent_store"]): os.mkdir(conf.global_conf["permanent_store"]) log.info("Adding front page data...") album = model.Album(id = 0, name = "root") Session.add(album) Session.commit() log.info("Successfully set up.")
def show_photos(self, aid, page = 0): c.u = utils c.album = s.query(Album).filter(Album.id == aid).first() if not c.album: abort(404) c.photos = self._get_photos(aid, page, "show_photos", 10)[1] return render("/photo-all.mako")
def _get_counts(self, album): # get photo counts photo_counts = s.execute("SELECT albums.id AS aid, COUNT(*) FROM albums JOIN " "photos ON album_id=albums.id " "WHERE albums.parent_id=%d GROUP BY photos.album_id;" % int(album.id)).fetchall() photo_counts = dict(photo_counts) # get album counts album_counts = s.execute("SELECT albums1.id AS aid, COUNT(*) AS count " "FROM albums albums1 JOIN albums albums2 ON " "albums2.parent_id=albums1.id " "WHERE albums1.parent_id=%d and (albums2.hidden=0 or %d) " "GROUP BY albums2.parent_id;" % (int(album.id), int(c.admin))).fetchall() album_counts = dict(album_counts) counts = {} for a in c.album.albums: counts[a.id] = (album_counts.get(a.id, 0), photo_counts.get(a.id, 0)) return counts
def index(self, aid, pid): photo_q = s.query(Photo) c.photo = photo_q.filter_by(album_id=aid, id=pid).first() if c.photo is None: abort(404) (c.idx, c.all, c.first, c.prev, c.next, c.last) = self._find_adj_photos(s, aid, pid) return render('/photo.mako')
def album_edit(self, aid): c.u = utils c.aid = aid c.new_album = False c.album = s.query(Album).filter(Album.id == aid).first() if not c.album: abort(404) return render('album_edit.mako')
def show_page(self, aid, page): c.u = utils c.album = s.query(Album).filter(Album.id == aid).first() if not c.album: abort(404) c.albums = self._get_albums(aid, page) c.photos = self._get_photos(aid, page, "show_page", 16)[1] c.counts = self._get_counts(c.album) return render("/album.mako")
def _get_albums(self, aid, page): """ Return Page for albums """ albums_q = s.query(Album).filter(Album.parent_id == aid) # hide albums only for guests if not c.admin: albums_q = albums_q.filter(Album.hidden == False) def _get_page_url(page, partial=None): return url(controller = "album", action = "show_page", aid = aid, page = page) return Page(albums_q, page = page, items_per_page = 8, url = _get_page_url)
def get_photo_ajax(self, aid, pid): pid = int(pid) c.u = utils c.album = s.query(Album).filter(Album.id == aid).first() if not c.album: abort(404) all_ph, c.photos = self._get_photos(aid, 1, "show_page", 16) try: c.pnav = PhotosNav(all_ph, pid) except PhotosNavError: abort(404) return render("/album-getphoto.mako")
def show_photo(self, aid, page, pid): pid = int(pid) c.u = utils c.album = s.query(Album).filter(Album.id == aid).first() if not c.album: abort(404) c.albums = self._get_albums(aid, page) all_ph, c.photos = self._get_photos(aid, page, "show_page", 16) try: c.pnav = PhotosNav(all_ph, pid) except PhotosNavError: abort(404) c.photo = c.pnav.photo c.counts = self._get_counts(c.album) return render("/album.mako")
def _get_photos(self, aid, page, act, items_per_page): """ Return Page for photos """ photos_q = s.query(Photo).filter(Photo.album_id == aid) # hide photos only for guests if not c.admin: photos_q = photos_q.filter(Photo.hidden == False) if c.album.sort_by == utils.SORT_BY_DATE: photos_q = photos_q.order_by(Photo.created) elif c.album.sort_by == utils.SORT_BY_DATE_DESC: photos_q = photos_q.order_by(Photo.created.desc()) def _get_page_url(page, partial=None): return url(controller = "album", action = act, aid = aid, page = page) all_photos = photos_q.all() return (all_photos, Page(all_photos, page = page, items_per_page = items_per_page, url = _get_page_url))
def photo_edit(self, aid, pid): c.photo = s.query(Photo).filter_by(album_id=aid, id=pid)[0] return render('/photo_edit.mako')
def album_edit_submit(self, aid): if request.params.get("Cancel"): redirect(url(controller="album")) if request.params.get("new_album"): album = Album(parent_id = aid) s.add(album) s.commit() else: album = s.query(Album).filter(Album.id == aid).first() if not album: abort(404) album.name = request.params.get("name") album.display_name = request.params.get("title") album.descr = request.params.get("description") album.hidden = bool(request.params.get("hide_album", 0)) album.sort_by = int(request.params.get("sort_by", 1)) new_thumb = request.params.get('album_thumbnail') print new_thumb, repr(new_thumb) if type(new_thumb) is types.InstanceType: old_preview = album.preview # add preview name = new_thumb.filename.lstrip(os.sep) preview = Photo(name, album.id, new_thumb.file.read()) new_thumb.file.close() preview.hidden = True s.add(preview) s.commit() album.preview_id = preview.id if old_preview: s.delete(old_preview) s.commit() redirect(url(controller = "album", action = "show_first_page", aid = aid))
def init_model(engine, conf): """Call me before using any of the tables or classes in the model.""" global config config = conf Session.configure(bind=engine)