def delete(self): """POST /photos/delete: Create a new item""" uri = self.request.params.get("uri", None) if not uri: return HTTPBadRequest() if DBSession.query(Photo).filter_by(uri=uri).delete() == 0: raise NotFound() ip.remove_image(uri) log.debug("ip.remove_image(%s)" % uri) return True
def query(self): photos_dir = self.request.registry.settings["photos_dir"] page = int(self.request.params.get("page", 1)) maxphotos = int(self.request.params.get("max", 30)) photo_q = DBSession.query(Photo).order_by(Photo.time.asc()) photos = Page(photo_q, page=page, items_per_page=maxphotos) return { "meta": {"page": page, "page_count": photos.page_count}, "photos": [ {"image": self.request.static_url(photos_dir + "/scaled/" + str(p.uri)), "title": "mon titre"} for p in photos ], }
def get_tags(tags): global TAGS if TAGS is None: TAGS = {} for t in DBSession.query(Tag): TAGS[t.name] = t res = [] for tname in tags: if tname not in TAGS: TAGS[tname] = Tag(tname) DBSession.add(TAGS[tname]) res.append(DBSession.merge(TAGS[tname])) return res
def main(): global IP, OPTIONS args, OPTIONS = get_options(sys.argv) ini_file = args[0] app = get_app(ini_file, "PyGall") settings = app.registry.settings OPTIONS['photos_dir'] = settings['photos_dir'] IP = ImageProcessing(settings['photos_dir']) # configure engine for fspot database FS_initialize_sql(create_engine("sqlite:///%s" % OPTIONS['fspot-db'])) if OPTIONS['drop-db']: Base.metadata.drop_all() print "All tables has been dropped" Base.metadata.create_all(checkfirst=True) if OPTIONS['cleanup-files'] and os.path.exists(settings['photos_dir']): print "Photos dir %s has been cleaned up" % settings['photos_dir'] shutil.rmtree(settings['photos_dir']) fs_ids = [] msgs = [] for row in FS_DBSession.query(FS_Tag)\ .filter_by(name=OPTIONS['fspot-exporttag']).one().photos: # process the photo and appends fspot_id to the list of processed # fspot photos fs_ids.append(process(row, msgs)) sys.stdout.write('.') sys.stdout.flush() # remove photos coming from fspot that are not associated with tag pygall # anymore for photo in DBSession.query(Photo).filter(and_( Photo.fspot_id!=None, not_(Photo.fspot_id.in_(fs_ids)))).all(): IP.remove_image(photo.uri) DBSession.delete(photo) transaction.commit() msgs.append("Photo %s has been deleted from PyGall" % photo.uri) sys.stdout.write('.') sys.stdout.flush() print '' if len(msgs) > 0: for msg in msgs: print msg else: print "Nothing to do..."
def index(self): page = self.request.matchdict.get("page") photo_q = DBSession.query(Photo).order_by(Photo.time.asc()) if page == "": # default to last page page = int(ceil(float(photo_q.count()) / 20)) params = [("debug", 1)] if self.debug else [] return HTTPFound(location=self.request.route_path("photos_index", page=page, _query=params)) # Inside a view method -- ``self`` comes from the surrounding scope. def url_generator(page): return self.request.route_path("photos_index", page=page) photos = Page(photo_q, page=page, items_per_page=20, url=url_generator) return { "debug": self.debug, "logged_in": authenticated_userid(self.request), "photos": photos, "photos_dir": self.request.registry.settings["photos_dir"], }
def _import(self, abspath): # check same image has not already been imported hash = img_md5(abspath) photo = DBSession.query(Photo).filter_by(md5sum=hash) if photo.count() > 0: log.info("Same md5sum already exists in database") return photo.first() # process and import photos to public/data/photos dir info = ip.process_image(abspath, md5sum=hash) os.unlink(abspath) # import image in db photo = Photo() photo.uri = info["uri"] photo.md5sum = hash photo.time = info["date"] DBSession.add(photo) DBSession.flush() return info
def process(row, msgs): fspot_id = row.id insert = False photo = DBSession.query(Photo).filter_by(fspot_id=fspot_id).first() if photo is None: insert = True photo = Photo() src = decode_fspot_uri( row.uri if row.last_version is None else row.last_version.uri) md5sum = img_md5(src) # copy and scale image if needed info = IP.process_image(src, md5sum=md5sum) # set photo db record photo.fspot_id = fspot_id photo.uri = info['uri'] photo.md5sum = md5sum photo.time = info['date'] if insert or not OPTIONS['skip-existing']: # update row in db if row.description: photo.description = row.description photo.rating = row.rating photo.tags = get_tags([t.name for t in row.tags]) #TODO: detect if photo version has changed and update photo accordingly try: uri = photo.uri # keep track of photo.uri outside the DBSession DBSession.add(photo) transaction.commit() if insert: msgs.append("Photo %s has been imported in PyGall" % uri) except IntegrityError: #print "Photo %s already exists in db" % uri transaction.abort() #TODO: make it possible to update record return fspot_id