Skip to content

Commit

Permalink
Fix saving credentials from relogin menu window
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikolay Golub committed Jul 4, 2014
1 parent 52128c1 commit 19f67ef
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 28 deletions.
31 changes: 7 additions & 24 deletions pyjtt/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

from os import path, mkdir
import sys
import configparser

import logging
import logging.handlers
logger = logging.getLogger(__name__)
Expand All @@ -36,24 +36,6 @@
import utils

LOGGING_FORMAT = '%(asctime)s %(levelname)s - %(name)s@%(thread)d - %(message)s'
CONFIG_FILENAME = 'pyjtt.cfg'


def init_config(workdir):
"""
Prepares config class for usage
"""
defaults = {'log_level': 'INFO'}
for item in ('jirahost', 'login', 'password', 'save_password'):
defaults[item] = ''

config_filename = path.join(workdir, CONFIG_FILENAME)
config = configparser.ConfigParser(defaults=defaults)
config.read(config_filename)

if not config.has_section('main'):
config.add_section('main')
return config


def main():
Expand All @@ -64,7 +46,9 @@ def main():
if not path.isdir(workdir):
mkdir(workdir)

config = init_config(workdir)
config = utils.init_config()

# Initialize logging
log_filename = path.join(workdir, 'application.log')
log_rotater = logging.handlers.RotatingFileHandler(log_filename,
mode='a',
Expand All @@ -80,8 +64,6 @@ def app_quit():
"""
Standard procedures before close application
"""
with open(path.join(workdir, CONFIG_FILENAME), 'w') as configfile:
config.write(configfile)
app.quit()
sys.exit(app.exec_())
jira_host = config.get('main', 'jirahost')
Expand All @@ -104,11 +86,12 @@ def app_quit():

if login_window.ui.checkBoxSaveCredentials.isChecked():
config.set('main', 'password', password)
utils.write_config(config)
else:
app_quit()
main_window = gui.MainWindow(jira_host, login, password)
main_window.show()
#TODO: add check that login and password from config are correct

main_window = gui.MainWindow(jira_host, login, password)
app_quit()


Expand Down
31 changes: 27 additions & 4 deletions pyjtt/gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ def __init__(self, icon, parent=None):
super(SystemTrayIcon, self).__init__(icon, parent)
menu = QtWidgets.QMenu(parent)
exitAction = menu.addAction("Exit")
exitAction.triggered.connect(parent.close)
self.setContextMenu(menu)


Expand Down Expand Up @@ -196,6 +197,7 @@ def __init__(self, jirahost, login, password, parent=None):
# Initialize app
self.app = core.TimeTrackerApp(jirahost, login, password)


# Initialize workers
self.worker_threads = []
for i in range(self.number_of_workers):
Expand Down Expand Up @@ -224,6 +226,9 @@ def __init__(self, jirahost, login, password, parent=None):
self.ui.actionLogout.triggered.connect(self.logout)
self.ui.tray_icon.activated.connect(self.tray_click)

# Appear
self.show()

# Request assigned issues
get_assigned_issues_job = partial(self.app.get_user_assigned_issues)
self.tasks_queue.put(get_assigned_issues_job)
Expand All @@ -233,10 +238,19 @@ def __init__(self, jirahost, login, password, parent=None):
# Core stuff

def closeEvent(self, event):
for thread in self.worker_threads:
thread.quit()
# TODO: Add checking of online tracking
event.accept()
buttons = QtWidgets.QMessageBox.Yes | QtWidgets.QMessageBox.No
message = 'Are you sure you want to close this application?'
confirmation = QtWidgets.QMessageBox.question(self,
'Exit',
message,
buttons=buttons)
if confirmation == QtWidgets.QMessageBox.Yes:
for thread in self.worker_threads:
thread.quit()
# TODO: Add checking of online tracking
super(MainWindow, self).closeEvent(event)
else:
event.ignore()

def logout(self):
relogin_window = LoginWindow(self.app.jira_accessor.jirahost,
Expand All @@ -248,7 +262,16 @@ def logout(self):
jira_host = relogin_window.ui.lineEditHostAddress.text()
login = relogin_window.ui.lineEditLogin.text()
password = relogin_window.ui.lineEditPassword.text()
config = utils.init_config()
config.set('main', 'jirahost', jira_host)
config.set('main', 'login', login)
if relogin_window.ui.checkBoxSaveCredentials.isChecked():
config.set('main', 'password', password)
else:
config.remove_option('main', 'password')
utils.write_config(config)
self.app = core.TimeTrackerApp(jira_host, login, password)

else:
self.close()

Expand Down
26 changes: 26 additions & 0 deletions pyjtt/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@
__license__ = "GPL"

import os
import configparser
import platform
from datetime import datetime, timedelta

import logging
logger = logging.getLogger(__name__)

CONFIG_FILENAME = 'pyjtt.cfg'

def get_app_working_dir():
"""Returns path to application operational folder.
Expand All @@ -45,6 +47,30 @@ def get_app_working_dir():
return os.path.abspath('.' + app_name)


def init_config(workdir=None):
"""
Prepares config class for usage
"""
if workdir is None:
workdir = get_app_working_dir()
defaults = {'log_level': 'INFO'}
for item in ('jirahost', 'login', 'password', 'save_password'):
defaults[item] = ''

config_filename = os.path.join(workdir, CONFIG_FILENAME)
config = configparser.ConfigParser(defaults=defaults)
config.read(config_filename)

if not config.has_section('main'):
config.add_section('main')
return config


def write_config(config):
workdir = get_app_working_dir()
with open(os.path.join(workdir, CONFIG_FILENAME), 'w') as configfile:
config.write(configfile)

def get_local_utc_offset(now, utcnow):
"""Calculates local UTC offset. Returns string"""
logger.debug('Getting local UTC Offset')
Expand Down

0 comments on commit 19f67ef

Please sign in to comment.