diff --git a/data/config/Localization.json b/data/config/Localization.json index 3528ab5..1f02d7c 100644 --- a/data/config/Localization.json +++ b/data/config/Localization.json @@ -48,6 +48,12 @@ "SECRET": "Для Кристины", "ACCEPT": "Применить", "CANCEL": "Отмена", + "ENTER_NAME": "Введите сюда ваше имя...", + "ENTER_EMAIL": "Введите сюда ваш E-mail...", + "SETTINGS_NAME_TEXT": "Имя", + "SETTINGS_EMAIL_TEXT": "Электронная почта", + "WARNING_HEADER": "Предупреждение", + "EMAIL_WARNING_TEXT": "Кажется, вы указали некорректный адрес электронной почты. Убедитесь, что ваш Email указан корректно.", "RELOAD_TITLE": "Требуется перезагрузка", "RELOAD_REQUIRED_TEXT": "Для применения изменений требуется перезагрузить программу. Перезагрузить сейчас?", @@ -84,6 +90,9 @@ "EMAIL_ASK_HEADER": "Отправка результата", "EMAIL_ASK_TEXT": "Введите ваш Email:", "EMAIL_NAME_ASK_TEXT": "Введите ваше имя:", + "EMAIL_TIP_NO_NAME_AND_EMAIL": "Совет: вы можете установить имя и Email\nв настройках, чтобы не вводить их вручную.", + "EMAIL_TIP_NO_NAME": "Совет: вы можете установить имя\nв настройках, чтобы не вводить его вручную.", + "EMAIL_TIP_NO_EMAIL": "Совет: вы можете установить Email\nв настройках, чтобы не вводить его вручную.", "EMAIL_ERROR_HEADER": "Ошибка", "EMAIL_ERROR_0": "Ошибка отправки Email: некорректный адрес.", "EMAIL_ERROR_1": "Вы не ввели Email.", diff --git a/data/config/config.jet b/data/config/config.jet index 2ea6aa3..6994815 100644 --- a/data/config/config.jet +++ b/data/config/config.jet @@ -33,7 +33,7 @@ }, "TIMER_TIME_IN_SECONDS": 14101, "TIMER_FORMAT": "%s:%s:%s", - "build": "27", + "build": "31", "SIZE_PARAMETERS": { "tiny": { "multiplier": 0.7, @@ -74,4 +74,4 @@ "wrong": "background-color: #ab3b2c; " } } -} \ No newline at end of file +} diff --git a/data_manager.py b/data_manager.py index dc332b1..47d4b31 100644 --- a/data_manager.py +++ b/data_manager.py @@ -8,6 +8,7 @@ import sys import pathlib import Levenshtein +import re from github import Github from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText @@ -140,6 +141,13 @@ def checkIfNameNeedsToBeTriggered(name: str) -> bool: def getButtonStyles() -> dict: return Config.button_styles + + def checkIfSizeWasChanged(old_settings: dict, new_settings: dict) -> bool: + return old_settings["size"] != new_settings["size"] + + def emailIsValid(email: str) -> bool: + pattern = r"[^@]+@[^@]+\.[^@]+" + return not not re.match(pattern, email) or email is None class Email(object): diff --git a/main.py b/main.py index 17035cd..f0a3451 100644 --- a/main.py +++ b/main.py @@ -136,6 +136,9 @@ def main(): save_manager.addSettingsParameter() if not "easteregg_unlocked" in save_manager.read_save(): save_manager.addEasterEggParameter() + if not "name" in save_manager.read_save()["settings"]: + save_manager.addNameEmailParameters() + currentExitCode = 1337 while currentExitCode == 1337: diff --git a/save_manager.py b/save_manager.py index 0382453..a1bd5bb 100644 --- a/save_manager.py +++ b/save_manager.py @@ -1,7 +1,5 @@ import json import datetime -import os -import platform from collections import Counter from data_manager import Config @@ -27,7 +25,7 @@ def generate_empty_save() -> None: for task in range(1, 28): save_data[task] = [] save_ex = {"save_data": save_data, "easteregg_unlocked": False, "exam_history": [], - "settings": {"size": "default"}, + "settings": {"size": "default", "name": None, "email": None}, 'update_time': str(datetime.datetime.now())} update_save(save_ex) @@ -109,7 +107,7 @@ def getExamHistory() -> list: def addSettingsParameter() -> None: save_data = read_save() - save_data["settings"] = {"size": "default"} + save_data["settings"] = {"size": "default", "name": None, "email": None} update_save(save_data) def changeSize(new_size: str) -> None: @@ -138,4 +136,10 @@ def setEasterEggUnlocked() -> None: def checkIfEasterEggIsUnlocked() -> bool: save_data = read_save() - return save_data["easteregg_unlocked"] \ No newline at end of file + return save_data["easteregg_unlocked"] + +def addNameEmailParameters() -> None: + save_data = read_save() + save_data["settings"]["name"] = None + save_data["settings"]["email"] = None + update_save(save_data) \ No newline at end of file diff --git a/task_manager.py b/task_manager.py index efb2ace..5c53118 100644 --- a/task_manager.py +++ b/task_manager.py @@ -20,4 +20,11 @@ def choose_task(num: Union[int, str]) -> dict: def get_task_list(num: Union[int, str]) -> list: list_name = 'task_%s_list_of_dicts' % str(num) data = data_manager.__dict__[list_name] - return data \ No newline at end of file + return data + + def get_task_by_id(task_num: Union[int, str], task_id: Union[int, str]) -> dict: + list_name = 'task_%s_list_of_dicts' % str(task_num) + data = data_manager.__dict__[list_name] + for dct in data: + if dct["id"] == str(task_id): + return dct \ No newline at end of file diff --git a/ui_exam.py b/ui_exam.py index ca50131..ae69238 100644 --- a/ui_exam.py +++ b/ui_exam.py @@ -3,7 +3,7 @@ import webbrowser from PyQt5.QtWidgets import * from PyQt5.QtCore import * -from PyQt5.QtGui import QIcon, QPixmap +from PyQt5.QtGui import QIcon, QPixmap, QFont from task_manager import Task_Chooser from data_manager import Localization, Config, Email, ID_Vars, Logger @@ -1392,8 +1392,6 @@ def emailAction(self): Logger.add_line_to_log("Succesfully sent Email to %s." % self.email) QMessageBox.information(self, Localization.EMAIL_SUCCESS_HEADER, Localization.EMAIL_SUCCESS_TEXT, QMessageBox.Ok) self.deleteResultFile() - else: - QMessageBox.critical(self, Localization.EMAIL_ERROR_HEADER, Localization.EMAIL_ERROR_1, QMessageBox.Ok) else: Logger.add_line_to_log("Error initializing Email module. Code: 2. Cause: no Internet access.") QMessageBox.critical(self, Localization.EMAIL_ERROR_HEADER, Localization.EMAIL_ERROR_2, QMessageBox.Ok) @@ -1428,11 +1426,30 @@ def __init__(self, parent=None): self.first = QLineEdit(self) self.second = QLineEdit(self) - buttonBox = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel, self); + self.current_settings = save_manager.getCurrentSettings() + self.current_name = self.current_settings["name"] + self.current_email = self.current_settings["email"] + if self.current_name: + self.second.setText(self.current_name) + if self.current_email: + self.first.setText(self.current_email) + buttonBox = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel, self) layout = QFormLayout(self) layout.addRow(Localization.EMAIL_ASK_TEXT, self.first) layout.addRow(Localization.EMAIL_NAME_ASK_TEXT, self.second) + if not self.current_name and not self.current_email: + self.tip_label = QLabel(Localization.EMAIL_TIP_NO_NAME_AND_EMAIL) + elif not self.current_name: + self.tip_label = QLabel(Localization.EMAIL_TIP_NO_NAME) + elif not self.current_email: + self.tip_label = QLabel(Localization.EMAIL_TIP_NO_EMAIL) + else: + self.tip_label = None + if self.tip_label: + self.tip_label.setFont(QFont("SF Pro Display", Config.multiplyNumberAccordingToSize(16, save_manager.getCurrentSettings()["size"]))) + layout.addRow(self.tip_label) + layout.addWidget(buttonBox) buttonBox.accepted.connect(self.acceptAction) diff --git a/ui_main.py b/ui_main.py index 1e9b24a..a8131b0 100644 --- a/ui_main.py +++ b/ui_main.py @@ -217,15 +217,16 @@ def clear_confirmed(self): class UI_Settings(object): def setupUi(self, SettingsWindow): - self.setMinimumSize(QSize(Config.multiplyNumberAccordingToSize(480, save_manager.getCurrentSettings()["size"]), - Config.multiplyNumberAccordingToSize(240, save_manager.getCurrentSettings()["size"]))) - central_widget = QWidget(self) - self.setCentralWidget(central_widget) + current_settings = save_manager.getCurrentSettings() + self.setMinimumSize(QSize(Config.multiplyNumberAccordingToSize(480, current_settings["size"]), + Config.multiplyNumberAccordingToSize(240, current_settings["size"]))) + self.central_widget = QWidget(self) + self.setCentralWidget(self.central_widget) self.setWindowIcon(QIcon('icons/icon.png')) - grid_layout = QGridLayout() - central_widget.setLayout(grid_layout) - grid_layout.addWidget(QLabel(Localization.SETTING_SIZE_TEXT, self), 0, 0) + self.grid_layout = QGridLayout() + self.central_widget.setLayout(self.grid_layout) + self.grid_layout.addWidget(QLabel(Localization.SETTING_SIZE_TEXT, self), 0, 0) self.available_sizes = ["tiny", "default", "big", "large"] if save_manager.checkIfEasterEggIsUnlocked(): @@ -233,33 +234,64 @@ def setupUi(self, SettingsWindow): self.combo = QComboBox() self.list_of_items = [Localization.getPrintfText(key) for key in self.available_sizes] self.combo.addItems(self.list_of_items) - self.combo.setCurrentText(Localization.getPrintfText(save_manager.getCurrentSettings()["size"])) + self.combo.setCurrentText(Localization.getPrintfText(current_settings["size"])) + + self.current_name, self.current_email = current_settings["name"], current_settings["email"] + self.name_blank = QLineEdit() + self.name_blank.setPlaceholderText(Localization.ENTER_NAME) + self.email_blank = QLineEdit() + self.email_blank.setPlaceholderText(Localization.ENTER_EMAIL) + if self.current_name: + self.name_blank.setText(self.current_name) + if self.current_email: + self.email_blank.setText(self.current_email) self.ok_btn = QPushButton(Localization.ACCEPT) self.cancel_btn = QPushButton(Localization.CANCEL) self.cancel_btn.clicked.connect(lambda: self.close()) self.ok_btn.clicked.connect(self.ok_btn_clicked) - grid_layout.addWidget(self.combo, 1, 0, 1, 2) - grid_layout.addItem(QSpacerItem(0, 0, QSizePolicy.Expanding, QSizePolicy.Expanding), 2, 0, 1, 2) - grid_layout.addWidget(self.ok_btn, 3, 0) - grid_layout.addWidget(self.cancel_btn, 3, 1) + self.grid_layout.addWidget(self.combo, 1, 0, 1, 2) + self.grid_layout.addItem(QSpacerItem(0, 0, QSizePolicy.Expanding, QSizePolicy.Expanding), 2, 0, 1, 2) + self.grid_layout.addWidget(QLabel(Localization.SETTINGS_NAME_TEXT, self), 3, 0) + self.grid_layout.addWidget(self.name_blank, 4, 0, 1, 2) + self.grid_layout.addWidget(QLabel(Localization.SETTINGS_EMAIL_TEXT, self), 5, 0) + self.grid_layout.addWidget(self.email_blank, 6, 0, 1, 2) + self.grid_layout.addWidget(self.ok_btn, 7, 0) + self.grid_layout.addWidget(self.cancel_btn, 7, 1) def ok_btn_clicked(self): + current_settings = save_manager.getCurrentSettings() + new_settings = deepcopy(current_settings) + sizes = { 0: "tiny", 1: "default", 2: "big", 3: "large", 4: "secret" } size_index = self.combo.currentIndex() - current_settings = save_manager.getCurrentSettings() - new_settings = deepcopy(current_settings) - new_size = sizes[size_index] new_settings["size"] = new_size - if not new_settings == current_settings: + + name = self.name_blank.text() + email = self.email_blank.text() + new_settings["name"] = name if name else None + new_settings["email"] = email if email else None + + settings_was_changed = new_settings != current_settings + reload_required = Config.checkIfSizeWasChanged(current_settings, new_settings) + email_was_changed = new_settings["email"] != current_settings["email"] + email_is_valid = True + if email_was_changed: + email_is_valid = Config.emailIsValid(new_settings["email"]) + if settings_was_changed and reload_required: + if not email_is_valid: + QMessageBox.information(self, Localization.WARNING_HEADER, Localization.EMAIL_WARNING_TEXT, QMessageBox.Ok) save_manager.updateSettings(new_settings) self.ask_for_reload() - else: - self.close() + elif settings_was_changed: + save_manager.updateSettings(new_settings) + if not email_is_valid: + QMessageBox.information(self, Localization.WARNING_HEADER, Localization.EMAIL_WARNING_TEXT, QMessageBox.Ok) + self.close() def ask_for_reload(self): box = QMessageBox()