def _save(self):
     try:
         img_file = os.path.basename(self._img_file)
         _, extension = os.path.splitext(img_file)
         new_file_name = f"{self.front}{extension}"
         tmp_link = f"{IMG_DIR}/{self.deck}/{new_file_name}"
         print()
         if self._img_file != "":
             if not os.path.exists(tmp_link):
                 shutil.copyfile(self._img_file, tmp_link)
             else:
                 os.remove(tmp_link)
                 shutil.copyfile(self._img_file, tmp_link)
         else:
             pass
         print(tmp_link)
         output = io_.SQLiteExporter(self.deck)
         output.updateTable("DECK", "IMG", tmp_link, condition=self.front)
     except FileNotFoundError:
         pass
     finally:
         output = io_.SQLiteExporter(self.deck)
         output.updateTable("DECK", "BACK", self._back_box.toPlainText(),
                            self.front)
         self.close()
Ejemplo n.º 2
0
    def save(self):
        def isValidFileName(file_name):
            restricted_char = ["<", ">", ":", "\"", "/", "\\", "|", "?", "*"]
            if len(file_name) == 0 or len(file_name) > 256:
                return False
            for i in file_name:
                if i in restricted_char:
                    return False
            return True

        name = self.deck_name_box.text()
        file_name = f"{DECKS_DIR}/{name}.db"
        file_list = os.listdir(DECKS_DIR)
        restricted_chars = ["<", ">", ":", "\"", "/", "\\", "|", "?", "*"]
        if name == "":
            self._send_message("The name must not be blank")
        else:
            if os.path.basename(file_name) not in file_list:
                if isValidFileName(name):
                    open(file_name, "a+").close()
                    out = io_.SQLiteExporter(name)
                    out.createTable("DECK")
                    out.createTable("DATE_")
                    os.chdir(f"{IMG_DIR}")
                    os.mkdir(f"{IMG_DIR}/{name}")
                    del out
                    self.close()
                else:
                    self._send_message(
                        f"""Can't add new deck. Please try a different name.
    Note: Don't use these characters: {", ".join(restricted_chars)}""")
            else:
                self._send_message(f"""The deck {name} has been existed""")
        if self.importing_radiobutton.isChecked(
        ) is True and self.file_obj[0] != "":
            try:
                open(file_name, "a+").close()
                output = io_.SQLiteExporter(name)
                for card in self.parsed_data:
                    output.writeToDB(card, "DECK")
                    output.writeToDB((card[0], card[1], None, date.today()),
                                     "DATE_")
                self.close()
            except sql.OperationalError:
                self._send_message("Error in importing file(s)")
        elif self.importing_radiobutton.isChecked(
        ) is True and self.file_obj[0] == "":
            self._send_message(
                "The name of the imported file must not be blank")
        else:
            pass
 def _delete_card(self, card: str):
     print(card)
     db = io_.SQLiteImporter(self.deck)
     out = io_.SQLiteExporter(self.deck)
     out.cursor.execute("DELETE FROM DECK WHERE FRONT = ?", (card, ))
     out.conn.commit()
     out.cursor.execute("DELETE FROM DATE_ WHERE CARD = ?", (card, ))
     out.conn.commit()
     db.cursor.execute("SELECT FRONT, BACK, IMG FROM DECK")
     tmp_deck = db.cursor.fetchall()
     out.cursor.execute("DROP TABLE DECK")
     out.conn.commit()
     out.createTable("DECK")
     self.cards_data = []
     for key, val in enumerate(tmp_deck):
         data = (key + 1, ) + val
         out.writeToDB(data, "DECK")
         self.cards_data.append(data)
     db.cursor.execute("SELECT CARD, LAST_REVIEW, NEXT_REVIEW FROM DATE_")
     tmp_date = db.cursor.fetchall()
     out.cursor.execute("DROP TABLE DATE_")
     out.conn.commit()
     out.createTable("DATE_")
     for key, val in enumerate(tmp_date):
         data = (key + 1, ) + val
         out.writeToDB(data, "DATE_")
     self._createCardsList()
Ejemplo n.º 4
0
 def _save_single_card(self):
     inp = io_.SQLiteImporter(self.deck)
     index = len(inp.fetch_from_db_Deck()) + 1
     out = io_.SQLiteExporter(self.deck)
     front: str = self.front_box.toPlainText()
     back: str = self.back_box.toPlainText()
     if not (front.isspace() or back.isspace()):
         # try:
         img_file = os.path.basename(self.img_file)
         # except:
         #     pass
         data_card = (index, front, back,
                      f"{IMG_DIR}/{self.deck}/{img_file}")
         data_date = (index, front, None, date.today())
         try:
             out.writeToDB(data_card, "DECK")
             out.writeToDB(data_date, "DATE_")
         except sql.IntegrityError:
             self._send_message(f"Card '{front}' is already exist",
                                "Card exist")
         if self.img_file != "":
             shutil.copyfile(self.img_file,
                             f"{IMG_DIR}/{self.deck}/{img_file}")
         else:
             pass
         self.close()
     else:
         self._send_message(
             "The front and back of the card must not be empty")
Ejemplo n.º 5
0
 def record_learning_process(self):
     exporter = io_.SQLiteExporter(self.deck)
     importer = io_.SQLiteImporter(self.deck)
     # date_ = importer.fetch_from_db_Date_()
     for item in self.cards_list.keys():
         card = item
         status = self.cards_status[card]
         last_review = date.today()
         next_review = date.today()
         if status["writing"] and status["multiple choice"]:
             print(f"{card}: 2/2")
             next_review += timedelta(days=5)
         elif status["writing"] or status["multiple choice"]:
             print(f"{card}: 1/2")
             next_review += timedelta(days=1)
         else:
             print(f"{card}: 0/2")
             next_review += timedelta(days=0)
         exporter.updateTable(
             table="DATE_", column_to_change="LAST_REVIEW",
             data=last_review, condition=card
         )
         exporter.updateTable(
             table="DATE_", column_to_change="NEXT_REVIEW",
             data=next_review, condition=card
         )
 def reset_card(self, card: str):
     output = io_.SQLiteExporter(self.deck)
     output.updateTable("DATE_", "NEXT_REVIEW", str(date.today()), card)