def get_connection(self): if not self.connection: try: engine = self.get_engine() self.connection = engine.connect() except OperationalError as error: show_msg(texts.db_error, texts.msg_db_conn, QMessageBox.Critical, QMessageBox.Close, str(error)) return self.connection
def edit_series(self): """ Save the edit to the database. """ if not self.le_title.text() or not self.le_year: show_msg(texts.insert_error, texts.no_title, QMessageBox.Warning, QMessageBox.Close) else: self.series.name = self.le_title.text() self.series.original_name = self.le_original_title.text() self.series.year = self.le_year.text() self.series.seasons = self.le_seasons.text() if self.le_poster.text(): self.series.poster = self.le_poster.text() else: self.series.poster = '../images/poster_placeholder.png' self.series.web_url = self.le_web_url.text() self.series.summary = self.le_summary.toPlainText() id, name = get_combobox_info(self.cb_media) if id: self.series.media_id = id self.series.keyword_id = db_get_id(self.session, self.cb_keyword, Keyword()) self.series.category_1_id = db_get_id(self.session, self.cb_category_1, Category()) self.series.category_2_id = db_get_id(self.session, self.cb_category_2, Category()) try: self.session.commit() text = texts.msg_insert_ok(self.series.name) show_msg(texts.insert_ok, text, QMessageBox.Information, QMessageBox.Close) self.clear() except (DBAPIError, SQLAlchemyError) as error: self.session.rollback() self.session.commit() text = texts.msg_edit_erro(self.series.name) show_msg(texts.insert_error, text, QMessageBox.Critical, QMessageBox.Close, str(error)) else: try: self.series.view = write_series_html( self.session, self.series) self.session.add(self.series) self.session.commit() except SQLAlchemyError as error: self.session.rollback() self.session.commit() show_msg(texts.insert_error, texts.html_write, QMessageBox.Critical, QMessageBox.Close, str(error))
def edit_object(self): """ Save object in database. """ self.obj.name = self.le.text() result = db_insert_obj(self.session, self.obj) if result: text = texts.msg_edit_ok(self.obj.name) show_msg(texts.edit_ok, text, QMessageBox.Information, QMessageBox.Close) else: text = texts.msg_edit_erro(self.obj.name) show_msg(texts.edit_error, text, QMessageBox.Critical, QMessageBox.Close) self.le.setText('') objs = db_select_all(self.session, self.cls) populate_combobox(self.cb_select, objs) self.lb.setEnabled(False) self.lb.setHidden(True) self.le.setEnabled(False) self.le.setHidden(True) self.pb_save.setEnabled(False) self.pb_save.setHidden(True) self.pb_help_2.setEnabled(False) self.pb_help_2.setHidden(True) self.pb_leave.setEnabled(False) self.pb_leave.setHidden(True) self.vbox_main.setContentsMargins(20, 0, 20, 40) self.setGeometry(0, 0, 511, 205) self.lb_select.setEnabled(True) self.lb_select.setHidden(False) self.cb_select.setEnabled(True) self.cb_select.setHidden(False) self.pb_select.setEnabled(True) self.pb_select.setHidden(False) self.pb_help_1.setEnabled(True) self.pb_help_1.setHidden(False) self.hbox_select.setEnabled(True) self.le.setText('') self.le.returnPressed.disconnect() self.cb_select.currentIndexChanged.connect(self.obj_selected)
def save_season_episodes(self): """ Saved season and episodes in database. :return: Errors when inserting into the database or ok if not errors. """ self.season.season_num = self.le_season_num.text() self.season.year = self.le_year.text() id_m, name_m = get_combobox_info(self.cb_media) self.season.media_id = id_m _, name_s = get_combobox_info(self.cb_series) try: self.session.add(self.season) self.session.commit() text = texts.msg_edit_season_ok(name_s, self.le_season_num.text()) show_msg( texts.edit_ok, text, QMessageBox.Information, QMessageBox.Ok) except (IntegrityError, DBAPIError) as error: self.session.rollback() self.session.commit() text = texts.msg_insert_season_error( name_s, self.le_season_num.text()) show_msg(texts.insert_error, text, QMessageBox.Critical, QMessageBox.Close, str(error)) episodes = [] i = 0 for name, summary, code in self.result_episode: self.episodes_all[i].name = name self.episodes_all[i].summary = summary self.episodes_all[i].code = code episodes.append(self.episodes_all[i]) i += 1 try: self.session.add_all(episodes) self.session.commit() text = texts.msg_edit_epidsode_ok(name_s, self.le_season_num.text()) show_msg( texts.edit_ok, text, QMessageBox.Information, QMessageBox.Ok) self.clear() except (IntegrityError, DBAPIError) as error: self.session.rollback() self.session.commit() text = texts.msg_edit_episode_error( name_s, self.le_season_num.text()) show_msg(texts.insert_error, text, QMessageBox.Critical, QMessageBox.Close, str(error))
def delete_season(self): result = 0 name = texts.season_s + ' ' + str(self.season.season_num) text = texts.msg_before_delete(name) answer = QMessageBox.information( self, texts.warning, text, QMessageBox.Yes | QMessageBox.No, QMessageBox.No ) if answer == QMessageBox.Yes: self.series = self.session.query(Season).get(self.season.id) result = self.session.query(Season).\ filter(Season.id == self.season.id).delete() if result == 1: self.session.commit() text = texts.msg_delete_ok(name) show_msg(texts.delete_ok, text, QMessageBox.Information, QMessageBox.Close)
def delete_series(self): """ Delete the series in the database. """ result = 0 id, name = get_combobox_info(self.cb_select) text = texts.msg_before_delete(name) answer = QMessageBox.information(self, texts.warning, text, QMessageBox.Yes | QMessageBox.No, QMessageBox.No) if answer == QMessageBox.Yes: self.series = self.session.query(Series).get(id) result = self.session.query(Series).filter( Series.id == id).delete() if result == 1: self.session.commit() text = texts.msg_delete_ok(name) show_msg(texts.delete_ok, text, QMessageBox.Information, QMessageBox.Close) self.clear()
def insert_movie(self): """ Insert movie in database. :return: The errors in dictionary containing the errors. """ self.movie = Movie() if not self.le_title.text() or not self.le_year: show_msg(texts.insert_error, texts.no_title, QMessageBox.Warning, QMessageBox.Close) else: self.movie.name = self.le_title.text() self.movie.original_name = self.le_original_title.text() self.movie.year = self.le_year.text() self.movie.time = self.le_time.text() if self.le_imdb_url.isEnabled(): self.movie.search_url = self.le_imdb_url.text() elif self.le_ad_url.isEnabled(): self.movie.search_url = self.le_ad_url.text() if self.le_poster.text(): self.movie.poster = self.le_poster.text() else: self.movie.poster = '../images/poster_placeholder.png' self.movie.web_url = self.le_web_url.text() self.movie.summary = self.le_summary.toPlainText() id, name = get_combobox_info(self.cb_media) if id: self.movie.media_id = id self.movie.box_id = db_get_id(self.session, self.cb_box, Box()) self.movie.keyword_id = db_get_id(self.session, self.cb_keyword, Keyword()) self.movie.category_1_id = db_get_id(self.session, self.cb_category_1, Category()) self.movie.category_2_id = db_get_id(self.session, self.cb_category_2, Category()) id, name = get_combobox_info(self.cb_director) director = db_get_obj(self.session, id, name, Director) if director: movie_director = MovieDirector(order=1, director=director) self.movie.directors.append(movie_director) i = 0 old_order = 0 for cb in self.cb_actor: actor_id = db_get_id(self.session, cb, Actor()) character_id = db_get_id(self.session, self.cb_character[i], Character()) if actor_id and character_id: cast = Cast(actor_id=actor_id, character_id=character_id) try: self.session.add(cast) self.session.commit() # If except most probably is because cast exist so we try to get it except IntegrityError: self.session.rollback() self.session.commit() cast = self.session.query(Cast).filter( Cast.actor_id == actor_id, Cast.character_id == character_id).first() except DBAPIError as error: self.session.rollback() self.session.commit() _, actor = get_combobox_info(cb) _, character = get_combobox_info(self.cb_character[i]) text = texts.cast_error + actor + ' ' + character + '.' show_msg(texts.db_error, text, QMessageBox.Critical, QMessageBox.Close, str(error)) continue if cast: order = int(self.le_order[i].text()) if order == old_order: order += 1 old_order = order else: old_order = order movie_cast = MovieCast( order=order, star=self.chbox_star[i].isChecked(), cast=cast) self.movie.movie_cast.append(movie_cast) i += 1 hour_now = datetime.datetime.utcnow() self.movie.date_create = hour_now self.movie.date_edit = hour_now try: self.session.add(self.movie) self.session.commit() text = texts.msg_insert_ok(self.movie.name) show_msg(texts.insert_ok, text, QMessageBox.Information, QMessageBox.Close) except IntegrityError as error: self.session.rollback() self.session.commit() show_msg(texts.insert_error, texts.movie_exist, QMessageBox.Critical, QMessageBox.Close, str(error)) except (DBAPIError, SQLAlchemyError) as error: self.session.rollback() self.session.commit() text = texts.msg_insert_erro(self.movie.name) show_msg(texts.insert_error, text, QMessageBox.Critical, QMessageBox.Close, str(error)) else: try: self.movie.view = write_movie_html(self.session, self.movie) self.session.add(self.movie) self.session.commit() except SQLAlchemyError as error: self.session.rollback() self.session.commit() show_msg(texts.insert_error, texts.html_write, QMessageBox.Critical, QMessageBox.Close, str(error))
def save_cast(self): """ Save values from cast table in database. """ for i in range(self.ch_del_count): if self.chbox_del[i].isChecked(): result = self.session.query(SeasonCast).\ filter(SeasonCast.id == self.season_cast[i].id).delete() if result == 1: self.session.commit() else: self.session.rollback() continue self.season_cast[i].cast.actor_id = db_get_id( self.session, self.cb_actor[i], Actor()) self.season_cast[i].cast.character_id = db_get_id( self.session, self.cb_character[i], Character()) self.season_cast[i].order = self.le_order[i].text() self.season_cast[i].star = self.chbox_star[i].isChecked() db_insert_obj(self.session, self.season_cast[i]) for i in range(self.ch_del_count, self.rows): actor_id = db_get_id(self.session, self.cb_actor[i], Actor()) character_id = db_get_id( self.session, self.cb_character[i], Character()) if actor_id and character_id: cast = Cast(actor_id=actor_id, character_id=character_id) try: self.session.add(cast) self.session.commit() # If except most probably is because cast exist so we try to # get it except sqlalchemy.exc.IntegrityError: self.session.rollback() self.session.commit() cast = self.session.query(Cast).filter( Cast.actor_id == actor_id, Cast.character_id == character_id).first() if cast: series_cast = SeasonCast( season_id=self.id, cast_id=cast.id, order=self.le_order[i].text(), star=self.chbox_star[i].isChecked() ) db_insert_obj(self.session, series_cast) series = self.cb_series.currentText() name = series + ' ' + texts.cast_s text = texts.msg_edit_ok(name) show_msg( texts.edit_ok, text, QMessageBox.Information, QMessageBox.Close) self.clear() sc = None sc = self.session.query(Season).get(self.id) self.session.add(sc) self.session.commit()