def add_movie_db(self, close): session = self.db.Session() details = get_details(self) if not details['o_title'] and not details['title']: gutils.error( _("You should fill the original title\nor the movie title."), parent=self.widgets['add']['window']) return False asked = False if details['o_title']: if session.query( db.Movie).filter_by(o_title=details['o_title']).count() > 0: asked = True if not gutils.question( _('Movie with that title already exists, are you sure you want to add?' ), self.widgets['add']['window']): return False if not asked and details['title']: if session.query( db.Movie).filter_by(title=details['title']).count() > 0: if not gutils.question( _('Movie with that title already exists, are you sure you want to add?' ), self.widgets['add']['window']): return False new_poster_md5 = None if details['image']: tmp_image_path = details['image'] if not os.path.isfile(tmp_image_path): tmp_image_path = os.path.join(self.locations['temp'], "poster_%s.jpg" % details['image']) if os.path.isfile(tmp_image_path): new_poster_md5 = gutils.md5sum(file(tmp_image_path, 'rb')) if session.query( db.Poster).filter_by(md5sum=new_poster_md5).count() == 0: try: data = file(tmp_image_path, 'rb').read() except Exception, e: log.warning("cannot read poster data") else: poster = db.Poster(md5sum=new_poster_md5, data=data) del details["image"] details["poster_md5"] = new_poster_md5 session.add(poster) else: details["poster_md5"] = new_poster_md5 try: if not tmp_image_path == details['image']: os.remove(tmp_image_path) except Exception, e: log.warn("cannot remove temporary file %s", tmp_image_path)
def update_movie(self): session = self.db.Session() if self._am_movie_id is not None: movie = session.query( db.Movie).filter_by(movie_id=self._am_movie_id).one() else: movie = session.query( db.Movie).filter_by(movie_id=self._movie_id).one() if movie is None: # movie was deleted in the meantime return add_movie_db(self, True) details = get_details(self) old_poster_md5 = movie.poster_md5 new_poster_md5 = old_poster_md5 if details['image']: if old_poster_md5 != details[ 'image']: # details["image"] can contain MD5 or file path new_image_path = details['image'] if not os.path.isfile(new_image_path): new_image_path = os.path.join( self.locations['temp'], "poster_%s.jpg" % details['image']) if not os.path.isfile(new_image_path): log.warn("cannot read temporary file: %s", new_image_path) else: new_poster_md5 = gutils.md5sum(file(new_image_path, 'rb')) if session.query(db.Poster).filter_by( md5sum=new_poster_md5).count() == 0: try: data = file(new_image_path, 'rb').read() except Exception, e: log.warning("cannot read poster data") old_poster_md5 = new_poster_md5 else: poster = db.Poster(md5sum=new_poster_md5, data=data) del details["image"] details['poster_md5'] = new_poster_md5 session.add(poster) else: details['poster_md5'] = new_poster_md5
except Exception, e: log.error(str(e)) gutils.error(_("Image is not valid."), self.widgets['window']) return False poster_md5 = gutils.md5sum(data) movie = session.query(db.Movie).filter_by(number=number).one() if poster_md5 == movie.poster_md5: log.debug('same MD5 sum, no need to update poster') return False old_poster_md5 = movie.poster_md5 if session.query(db.Poster).filter_by(md5sum=poster_md5).count() == 0: poster = db.Poster(md5sum=poster_md5, data=data) session.add(poster) # update the md5 *after* all other queries (so that UPDATE will not be invoked) movie.poster_md5 = poster_md5 session.add(movie) try: session.commit() except Exception, e: session.rollback() log.error("cannot add poster to database: %s" % e) return False if old_poster_md5: delete.delete_poster(self, old_poster_md5)
config, clean_config=True) updated = {} movies_table = db.metadata.tables['movies'] for movie in self.session.query(db.Movie.image).all(): poster_file_name = os.path.join(posters_dir, "%s.jpg" % movie.image) if poster_file_name in updated: continue if os.path.isfile(poster_file_name): poster_md5 = gutils.md5sum(file(poster_file_name, 'rb')) poster = self.session.query( db.Poster).filter_by(md5sum=poster_md5).first() if not poster: poster = db.Poster(md5sum=poster_md5, data=file(poster_file_name, 'rb').read()) self.session.add(poster) update_query = movies_table.update( movies_table.c.image == movie.image, { 'poster_md5': poster_md5, 'image': None }, bind=b) # deactivating a 0.12 feature save_update_onupdated = movies_table.c['updated'].onupdate movies_table.c['updated'].onupdate = None try: # yeah, we're commiting inside the loop,