Files
YMS/yms/view/tasks.py
T

222 lines
8.8 KiB
Python

# SPDX-License-Identifier: WTFPL
import os
from gi.repository import Gtk
from dateutil.parser import parse
from datetime import datetime
from .view import View, DatePicker
from yms import gtkutils, combobox, treeview
from yms.model import task
class TasksView(View):
def __init__(self, context):
super().__init__(context, _("Tasks"), "tasks.ui")
self.context = context
self.task = task.Task(context.db) # current record
self.newrecord = False
self.lst_tasks = Gtk.ListStore(int, str, str, str, str)
self.load_list()
treeview.add_column(self.tree['tasks'], _('ID'), 0)
treeview.add_column(self.tree['tasks'], _('Name'), 1)
treeview.add_column(self.tree['tasks'], _('Priority'), 2)
treeview.add_column(self.tree['tasks'], _('Status'), 3)
treeview.add_column(self.tree['tasks'], _('Responsible'), 4)
self.tree['tasks'].set_model(self.lst_tasks)
# Project
self.dd_project = context.lookup.project(self.context.vessel.vid)
combobox.load_from_dict(self.combo['project'], {-1: _("─── none ───")} | self.dd_project)
# Priority
self.dd_priority = self.context.lookup.enum('task.priority')
combobox.load_from_dict(self.combo['priority'], self.dd_priority, 'low')
# Status
self.dd_state = self.context.lookup.enum('task.taskstate')
combobox.load_from_dict(self.combo['status'], self.dd_state, 'pending')
# Responsible
self.dd_user = context.lookup.user()
combobox.load_from_dict(self.combo['responsible'], self.dd_user, 0)
self.entry['plandate'].set_placeholder_text(context.get_datefmt_placeholder())
self.dt_plandate = DatePicker(self.entry['plandate'], self.button['plandate'], context.get_datefmt())
self.entry['duedate'].set_placeholder_text(context.get_datefmt_placeholder())
self.dt_duedate = DatePicker(self.entry['duedate'], self.button['duedate'], context.get_datefmt())
def load_list(self):
selected_id = treeview.get_value(self.tree['tasks'])
self.lst_tasks.clear()
sql = (
"SELECT t.taskid, t.taskname, t.priority, t.taskstate, "
"u.displayname AS responsible "
"FROM task AS t JOIN user AS u ON (t.responsible=u.userid)"
"WHERE t.vid=%s "
"ORDER BY t.taskid"
)
self.db.dcur.execute(sql, (self.context.vessel.vid,))
selection = self.tree['tasks'].get_selection()
for row in self.db.dcur.fetchall():
data = (
row['taskid'],
row['taskname'],
row['priority'],
row['taskstate'],
row['responsible']
)
treeiter = self.lst_tasks.append(data)
if row['taskid'] == selected_id:
# restore previous selected row
selection.select_iter(treeiter)
def clear_record(self):
self.task.clear()
self.entry['name'].set_text('')
combobox.activate_row(self.combo['project'], -1)
combobox.activate_row(self.combo['priority'], 'low')
self.entry['plandate'].set_text('')
self.entry['duedate'].set_text('')
self.entry['started'].set_text('')
self.entry['finished'].set_text('')
combobox.activate_row(self.combo['status'], 'pending')
combobox.activate_row(self.combo['responsible'], 0)
self.text['remarks'].get_buffer().set_text('')
self.entry['name'].grab_focus()
def display_record(self):
self.label['name'].set_text(self.task.taskname)
self.label['project'].set_text(self.dd_project.get(self.task.projid, ''))
self.label['priority'].set_text(self.dd_priority[self.task.priority])
self.label['plandate'].set_text(self.format_date(self.task.plandate))
self.label['duedate'].set_text(self.format_date(self.task.duedate))
self.label['started'].set_text(self.format_datetime(self.task.started))
self.label['finished'].set_text(self.format_datetime(self.task.finished))
self.label['status'].set_text(self.task.taskstate)
self.label['responsible'].set_text('')
self.label['remarks'].set_text(self.task.remarks or '')
def edit_record(self):
self.entry['name'].set_text(self.task.taskname)
combobox.activate_row(self.combo['project'], self.task.projid)
combobox.activate_row(self.combo['priority'], self.task.priority)
self.entry['plandate'].set_text(self.format_date(self.task.plandate))
self.entry['duedate'].set_text(self.format_date(self.task.duedate))
self.entry['started'].set_text(self.format_datetime(self.task.started))
self.entry['finished'].set_text(self.format_datetime(self.task.finished))
combobox.activate_row(self.combo['status'], self.task.taskstate)
combobox.activate_row(self.combo['responsible'], self.task.responsible)
self.text['remarks'].get_buffer().set_text(self.task.remarks or '')
self.entry['name'].grab_focus()
def save_record(self):
# fields must have been validated beforehand
self.task.vid = self.context.vessel.vid
self.task.taskname = self.entry['name'].get_text()
self.task.projid = combobox.get_list_value(self.combo['project'])
if self.task.projid == -1:
self.task.projid = None
self.task.priority = combobox.get_list_value(self.combo['priority'])
try:
self.task.plandate = parse(self.entry['plandate'].get_text(), dayfirst=True)
except ValueError:
self.task.plandate = None
try:
self.task.duedate = parse(self.entry['plandate'].get_text(), dayfirst=True)
except ValueError:
self.task.duedate = None
try:
self.task.started = parse(self.entry['started'].get_text(), dayfirst=True)
except ValueError:
self.task.started = None
try:
self.task.finished = parse(self.entry['finished'].get_text(), dayfirst=True)
except ValueError:
self.task.finished = None
self.task.taskstate = combobox.get_list_value(self.combo['status'])
self.task.responsible = combobox.get_list_value(self.combo['responsible'])
self.task.remarks = gtkutils.get_text_contents(self.text['remarks'])
# buttons on list page
def on_but_new_clicked(self, widget):
self.newrecord = True
self.label['title_edit'].set_text(_("Add new Task"))
self.clear_record()
self.topwidget.set_visible_child(self.box['task_edit'])
def on_but_edit_clicked(self, widget):
self.newrecord = False
self.label['title_edit'].set_text(_("Edit Task"))
taskid = treeview.get_value(self.tree['tasks'], 0)
if not taskid:
return
self.task.load(taskid)
self.edit_record()
self.topwidget.set_visible_child(self.box['task_edit'])
def on_but_detail_clicked(self, widget):
taskid = treeview.get_value(self.tree['tasks'], 0)
if not taskid:
return
self.task.load(taskid)
self.display_record()
self.topwidget.set_visible_child(self.box['task_detail'])
self.button['close'].grab_focus()
def on_tvw_tasks_row_activated(self, widget, path, column):
taskid = treeview.get_value(widget, 0)
self.task.load(taskid)
self.display_record()
self.topwidget.set_visible_child(self.box['task_detail'])
# buttons on detail page
# edit2 uses edit callback
def on_but_close_clicked(self, widget):
self.topwidget.set_visible_child(self.box['tasks'])
def on_but_delete_clicked(self, widget):
answer = gtkutils.questionbox(
_("Delete task!\n"
"Are you sure?"), title="YMS"
)
if answer == False:
return
self.topwidget.set_visible_child(self.box['tasks'])
# buttons on edit page
def on_but_save_clicked(self, widget):
# check mandatory fields
if not self.entry['name'].get_text().strip():
# TODO error marker
return
self.save_record()
print(self.task)
self.task.save()
if self.newrecord:
self.load_list()
self.newrecord = False
else:
pass
# TODO aktuelle Listenzeile aktualisieren!
self.display_record()
self.topwidget.set_visible_child(self.box['task_detail'])
def on_but_plandate_clicked(self, widget):
pass
def on_but_duedate_clicked(self, widget):
pass
def on_but_started_clicked(self, widget):
pass
def on_but_finished_clicked(self, widget):
pass
def on_but_cancel_clicked(self, widget):
self.topwidget.set_visible_child(self.box['tasks'])