Subversion Repositories basico

Rev

Rev 9 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 9 Rev 74
Line 3... Line 3...
3
# File: tasks.py
3
# File: tasks.py
4
# Author: Tomás Vírseda
4
# Author: Tomás Vírseda
5
# License: GPL v3
5
# License: GPL v3
6
# Description: Tasks service
6
# Description: Tasks service
7
7
8
-
 
-
 
8
import os
9
from gi.repository import Gtk
9
from gi.repository import Gtk
10
from gi.repository.GdkPixbuf import Pixbuf
10
from gi.repository.GdkPixbuf import Pixbuf
11
from gi.repository import Pango
11
from gi.repository import Pango
12
from datetime import datetime
12
from datetime import datetime
13
from dateutil import parser as dateparser
13
from dateutil import parser as dateparser
Line 16... Line 16...
16
16
17
17
18
class Tasks(Service):
18
class Tasks(Service):
19
    def initialize(self):
19
    def initialize(self):
20
        self.get_services()
20
        self.get_services()
21
        self.setup_window()
-
 
-
 
21
        self.setup_widgets()
-
 
22
        self.__init_tasks_store()
-
 
23
        self.load_tasks()
-
 
24
-
 
25
-
 
26
    def __init_tasks_store(self):
-
 
27
        TASKS_FILE = self.get_file('TASKS')
-
 
28
        if not os.path.exists(TASKS_FILE):
-
 
29
            self.log.info("Creating store for tasks: %s" % TASKS_FILE)
-
 
30
            ftasks = open(TASKS_FILE, 'w')
-
 
31
            ftasks.close()
22
32
23
33
24
    def setup_window(self):
-
 
-
 
34
    def setup_widgets(self):
25
        # setup widgets
35
        # setup widgets
26
        self.window = self.gui.add_widget('winTasks')
-
 
27
        self.window.connect('delete-event', self.hide_window)
-
 
28
        self.parent = self.gui.get_widget('mainwindow')
-
 
29
        #~ self.window.set_transient_for(self.parent)
-
 
-
 
36
        self.window = self.gui.add_widget('boxProps')
30
        self.pname = self.gui.add_widget('etyTaskName')
37
        self.pname = self.gui.add_widget('etyTaskName')
31
        self.pname.connect('activate', self.add_task)
38
        self.pname.connect('activate', self.add_task)
32
        self.boxtask = self.gui.add_widget('boxTasks')
39
        self.boxtask = self.gui.add_widget('boxTasks')
33
        self.btnadd = self.gui.add_widget('btnAddTask')
40
        self.btnadd = self.gui.add_widget('btnAddTask')
34
        self.btnadd.connect('clicked', self.add_task)
41
        self.btnadd.connect('clicked', self.add_task)
35
        self.btndel = self.gui.add_widget('btnDelTask')
42
        self.btndel = self.gui.add_widget('btnDelTask')
36
        self.btndel.connect('clicked', self.delete_task)
43
        self.btndel.connect('clicked', self.delete_task)
37
        self.btncancel = self.gui.add_widget('btnCancelTasks')
-
 
38
        self.btncancel.connect('clicked', self.hide_window)
-
 
39
        self.btnaccept = self.gui.add_widget('btnAcceptTasks')
-
 
40
        self.btnaccept.connect('clicked', self.link_to_task)
-
 
41
        self.treeview = Gtk.TreeView()
44
        self.treeview = Gtk.TreeView()
42
        self.gui.add_widget('trvtaskwin', self.treeview)
45
        self.gui.add_widget('trvtaskwin', self.treeview)
-
 
46
        self.gui.add_widget('boxAcceptCancelTasks')
43
        self.boxtask.add(self.treeview)
47
        self.boxtask.add(self.treeview)
44
        self.hide_window()
-
 
45
48
46
        # setup model
49
        # setup model
47
        model = Gtk.ListStore(
50
        model = Gtk.ListStore(
48
            bool,           # CheckBox
51
            bool,           # CheckBox
49
            str             # Task name
52
            str             # Task name
Line 52... Line 55...
52
55
53
        # setup columns
56
        # setup columns
54
        # Checkbox
57
        # Checkbox
55
        renderer = Gtk.CellRendererToggle()
58
        renderer = Gtk.CellRendererToggle()
56
        renderer.connect("toggled", self.on_cell_toggled)
59
        renderer.connect("toggled", self.on_cell_toggled)
57
        column = Gtk.TreeViewColumn('X', renderer, active=0)
-
 
-
 
60
        column = Gtk.TreeViewColumn('', renderer, active=0)
58
        column.set_visible(True)
61
        column.set_visible(True)
59
        column.set_expand(False)
62
        column.set_expand(False)
60
        column.set_clickable(False)
63
        column.set_clickable(False)
61
        column.set_sort_indicator(False)
64
        column.set_sort_indicator(False)
62
        self.treeview.append_column(column)
65
        self.treeview.append_column(column)
63
66
64
        # Task name
67
        # Task name
65
        renderer = Gtk.CellRendererText()
68
        renderer = Gtk.CellRendererText()
66
        column = Gtk.TreeViewColumn('Task name', renderer, text=1)
69
        column = Gtk.TreeViewColumn('Task name', renderer, text=1)
67
        column.set_visible(True)
70
        column.set_visible(True)
-
 
71
        column.set_sort_column_id(1)
68
        column.set_expand(True)
72
        column.set_expand(True)
69
        column.set_clickable(True)
73
        column.set_clickable(True)
70
        column.set_sort_indicator(True)
74
        column.set_sort_indicator(True)
71
        self.treeview.append_column(column)
75
        self.treeview.append_column(column)
72
76
Line 78... Line 82...
78
        #~ self.task_filter.set_visible_column(1)
82
        #~ self.task_filter.set_visible_column(1)
79
83
80
        # Treeview features
84
        # Treeview features
81
        self.treeview.set_headers_visible(True)
85
        self.treeview.set_headers_visible(True)
82
        self.treeview.set_enable_search(True)
86
        self.treeview.set_enable_search(True)
83
        self.treeview.set_grid_lines(Gtk.TreeViewGridLines.HORIZONTAL)
-
 
-
 
87
        self.treeview.set_grid_lines(Gtk.TreeViewGridLines.NONE)
84
        self.treeview.set_search_column(1)
88
        self.treeview.set_search_column(1)
85
        self.treeview.connect('row-activated', self.double_click)
89
        self.treeview.connect('row-activated', self.double_click)
86
        selection = self.treeview.get_selection()
90
        selection = self.treeview.get_selection()
87
        selection.set_mode(Gtk.SelectionMode.SINGLE)
91
        selection.set_mode(Gtk.SelectionMode.SINGLE)
88
        self.treeview.set_search_entry(self.pname)
92
        self.treeview.set_search_entry(self.pname)
-
 
93
-
 
94
-
 
95
    def clear(self):
-
 
96
        model = self.treeview.get_model()
-
 
97
        model.clear()
-
 
98
-
 
99
-
 
100
    def set_model(self, model):
-
 
101
        self.treeview.set_model(model)
89
102
90
103
91
    # row task visibility
104
    # row task visibility
92
    def task_row_visibility(self, model, iter, data):
105
    def task_row_visibility(self, model, iter, data):
93
        entry = self.gui.get_widget('etyTaskName')
106
        entry = self.gui.get_widget('etyTaskName')
Line 96... Line 109...
96
        return text.upper() in model.get_value(iter, 1)
109
        return text.upper() in model.get_value(iter, 1)
97
110
98
111
99
    def task_filter_func(self, model, iter, data):
112
    def task_filter_func(self, model, iter, data):
100
        """Tests if the task in the row is the one in the filter"""
113
        """Tests if the task in the row is the one in the filter"""
101
        #~ self.log.debug(data)
-
 
102
        entry = self.gui.get_widget('etyTaskName')
114
        entry = self.gui.get_widget('etyTaskName')
103
        task = entry.get_text()
115
        task = entry.get_text()
104
-
 
105
        #~ if len(task) == 0:
-
 
106
            #~ return False
-
 
107
-
 
108
        task_row = model[iter][1]
116
        task_row = model[iter][1]
109
117
110
        if task.upper() in task_row.upper():
118
        if task.upper() in task_row.upper():
111
            return True
119
            return True
112
        else:
120
        else:
113
            return False
121
            return False
114
122
115
123
116
    def delete_task(self, *args):
124
    def delete_task(self, *args):
-
 
125
        db = self.app.get_service('DB')
117
        found = 0
126
        found = 0
118
        selection = self.treeview.get_selection()
127
        selection = self.treeview.get_selection()
119
        result = selection.get_selected()
128
        result = selection.get_selected()
120
        if result: #result could be None
129
        if result: #result could be None
121
            model, iter = result
130
            model, iter = result
122
            task = model.get_value(iter, 1)
131
            task = model.get_value(iter, 1)
123
            #~ self.log.debug(task)
-
 
124
            sapnotes = self.sap.get_notes()
-
 
-
 
132
            sapnotes = db.get_notes()
125
            for sapnote in sapnotes:
133
            for sapnote in sapnotes:
126
                try:
134
                try:
127
                    tasks = sapnotes[sapnote]['tasks']
135
                    tasks = sapnotes[sapnote]['tasks']
128
                    if task in tasks:
136
                    if task in tasks:
129
                        found += 1
137
                        found += 1
Line 134... Line 142...
134
                head = "Task could not be deleted"
142
                head = "Task could not be deleted"
135
                body = "Task %s is still assigned to other SAP Notes" % task
143
                body = "Task %s is still assigned to other SAP Notes" % task
136
                self.uif.message_dialog(head , body)
144
                self.uif.message_dialog(head , body)
137
            else:
145
            else:
138
                model.remove(iter)
146
                model.remove(iter)
139
-
 
-
 
147
                self.log.info("Task deleted: %s" % task)
-
 
148
                self.save_tasks()
140
149
141
150
142
151
143
    def add_task(self, *args):
152
    def add_task(self, *args):
144
        task = self.pname.get_text()
153
        task = self.pname.get_text()
145
        model = self.treeview.get_model()
154
        model = self.treeview.get_model()
146
155
147
        found = False
156
        found = False
148
        for row in model:
157
        for row in model:
149
            if row[1].upper() == task.upper():
158
            if row[1].upper() == task.upper():
-
 
159
                self.log.info("Task already exists: %s" % task)
150
                found = True
160
                found = True
151
161
152
        if not found and len(task) > 0:
162
        if not found and len(task) > 0:
153
            model.append([False, task])
163
            model.append([False, task])
-
 
164
            self.log.info("Task added: %s" % task)
154
165
155
        self.pname.set_text('')
166
        self.pname.set_text('')
-
 
167
        self.save_tasks()
-
 
168
        self.load_tasks()
156
169
157
170
158
171
159
    def show_window(self, sapnotes=[]):
-
 
160
        rootwin = self.gui.get_widget('mainwindow')
-
 
161
        #~ self.window.set_transient_for(rootwin)
-
 
-
 
172
    def show_window(self, sapnotes=[], active=None):
-
 
173
        ntboperations = self.gui.get_widget('ntbOperations')
-
 
174
        ntboperations.set_current_page(1)
-
 
175
        winoper = self.gui.get_widget('tgbShowManage')
-
 
176
        switch = winoper.get_active()
-
 
177
        if active is None:
-
 
178
            if switch is True:
-
 
179
                active = True
-
 
180
            else:
-
 
181
                active = False
-
 
182
        winoper.set_active(active)
162
        self.load_tasks(sapnotes)
183
        self.load_tasks(sapnotes)
163
        self.window.set_no_show_all(False)
-
 
164
        self.window.show_all()
-
 
165
        self.window.set_keep_above(True)
-
 
166
-
 
167
-
 
168
-
 
169
    def hide_window(self, *args):
-
 
170
        self.window.set_no_show_all(True)
-
 
171
        self.window.hide()
-
 
172
184
173
185
174
    def get_services(self):
186
    def get_services(self):
175
        self.gui = self.app.get_service("GUI")
187
        self.gui = self.app.get_service("GUI")
176
        self.sap = self.app.get_service('SAP')
188
        self.sap = self.app.get_service('SAP')
177
        self.uif = self.app.get_service('UIF')
189
        self.uif = self.app.get_service('UIF')
178
        self.cb = self.app.get_service('Callbacks')
190
        self.cb = self.app.get_service('Callbacks')
-
 
191
        self.db = self.app.get_service('DB')
179
192
180
193
181
    def double_click(self, treeview, row, col):
194
    def double_click(self, treeview, row, col):
182
        model = treeview.get_model()
195
        model = treeview.get_model()
183
        self.pname.set_text(model[row][1])
196
        self.pname.set_text(model[row][1])
Line 199... Line 212...
199
                        selected.add(model[treeiter][0])
212
                        selected.add(model[treeiter][0])
200
            print (selected)
213
            print (selected)
201
214
202
        except Exception as error:
215
        except Exception as error:
203
            self.log.error (self.get_traceback())
216
            self.log.error (self.get_traceback())
-
 
217
-
 
218
-
 
219
    def select(self, tasks):
-
 
220
        model = self.treeview.get_model()
-
 
221
-
 
222
        def select_task(model, path, iter, user_data=None):
-
 
223
            task = model.get_value(iter, 1)
-
 
224
            if task in tasks:
-
 
225
                model[path][0] = True
-
 
226
                self.log.debug("Task '%s' switch to true" % task)
-
 
227
            else:
-
 
228
                model[path][0] = False
-
 
229
            return False
-
 
230
-
 
231
        model.foreach(select_task)
204
232
205
233
206
    def on_cell_toggled(self, widget, path):
234
    def on_cell_toggled(self, widget, path):
-
 
235
        cb = self.get_service('Callbacks')
207
        model = self.treeview.get_model()
236
        model = self.treeview.get_model()
208
        model[path][0] = not model[path][0]
237
        model[path][0] = not model[path][0]
-
 
238
        cb.check_task_link_button_status()
-
 
239
        #~ button = self.gui.get_widget('btnLinkTasksApply')
-
 
240
        #~ sapnoteview = self.gui.get_widget('sapnoteview')
209
241
-
 
242
        #~ tasks_selected = len(self.get_selected()) > 0
-
 
243
        #~ notes_selected = len(sapnoteview.get_selected_notes()) > 0
-
 
244
        #~ if  tasks_selected and notes_selected:
-
 
245
            #~ button.set_no_show_all(False)
-
 
246
            #~ button.show_all()
-
 
247
        #~ else:
-
 
248
            #~ button.hide()
-
 
249
            #~ button.set_no_show_all(True)
210
250
211
    def get_selected_notes(self):
-
 
212
        sapnoteview = self.gui.get_widget('sapnoteview')
-
 
213
        return sapnoteview.get_selected_notes()
-
 
-
 
251
-
 
252
-
 
253
    def get_selected(self):
-
 
254
        model = self.treeview.get_model()
-
 
255
        tasks = set()
-
 
256
-
 
257
        def traverse_treeview(model, path, iter, user_data=None):
-
 
258
            toggled = model.get_value(iter, 0)
-
 
259
            if toggled:
-
 
260
                task = model.get_value(iter, 1)
-
 
261
                tasks.add(task)
-
 
262
            return False
-
 
263
-
 
264
        model.foreach(traverse_treeview)
-
 
265
        ltasks = list(tasks)
-
 
266
        ltasks.sort()
-
 
267
-
 
268
        #~ self.log.debug("Tasks selected: %s" % ', '.join(tasks))
-
 
269
        return ltasks
-
 
270
-
 
271
-
 
272
    #~ def get_selected_notes(self):
-
 
273
        #~ sapnoteview = self.gui.get_widget('sapnoteview')
-
 
274
        #~ return sapnoteview.get_selected_notes()
214
275
215
276
216
    def link_to_task(self, *args):
277
    def link_to_task(self, *args):
-
 
278
        sapnoteview = self.gui.get_widget('sapnoteview')
-
 
279
217
        self.save_tasks()
280
        self.save_tasks()
218
        self.hide_window()
-
 
219
        sapnotes = list(self.get_selected_notes())
-
 
-
 
281
        sapnotes = list(sapnoteview.get_selected_notes())
220
        model = self.treeview.get_model()
282
        model = self.treeview.get_model()
221
        tasks = []
283
        tasks = []
222
        for row in model:
284
        for row in model:
223
           link = row[0]
285
           link = row[0]
224
           if link == True:
286
           if link == True:
225
                tasks.append(row[1])
287
                tasks.append(row[1])
226
288
227
        sapnotes.sort()
289
        sapnotes.sort()
228
        tasks.sort()
290
        tasks.sort()
229
        #~ self.log.debug('SAP Notes: %s' % sapnotes)
-
 
230
        #~ self.log.debug(' Tasks: %s' % tasks)
-
 
231
        self.sap.link_to_task(sapnotes, tasks)
291
        self.sap.link_to_task(sapnotes, tasks)
232
        self.cb.refresh_view(view='tasks')
292
        self.cb.refresh_view(view='tasks')
233
293
234
294
235
    def get_all_tasks(self):
295
    def get_all_tasks(self):
Line 242... Line 302...
242
        return tasks
302
        return tasks
243
303
244
304
245
    def load_tasks(self, sapnotes=[]):
305
    def load_tasks(self, sapnotes=[]):
246
        model = self.treeview.get_model()
306
        model = self.treeview.get_model()
-
 
307
        TASKS_FILE = self.get_file('TASKS')
-
 
308
        alltasks = open(TASKS_FILE, 'r').readlines()
247
        model.clear()
309
        model.clear()
248
        try:
-
 
249
            alltasks = set(self.get_config_value('Tasks').split(','))
-
 
250
        except:
-
 
251
            alltasks = set()
-
 
252
-
 
253
        self.log.debug("All Tasks: %s" % alltasks)
-
 
254
-
 
255
        linkedtasks = set()
-
 
256
        for sapnote in sapnotes:
-
 
257
            tasks = self.sap.get_linked_tasks(sapnote)
-
 
258
            for task in tasks:
-
 
259
                linkedtasks.add(task)
-
 
260
        self.log.debug("Linked Tasks: %s" % alltasks)
-
 
261
-
 
-
 
310
        alltasks.sort()
262
        for task in alltasks:
311
        for task in alltasks:
263
            if task in linkedtasks:
-
 
264
                model.append([True, task])
-
 
265
            else:
-
 
266
                if len(task) > 0:
-
 
267
                    model.append([False, task])
-
 
268
-
 
-
 
312
            model.append([False, task[0:-1]])
269
313
270
314
271
    def save_tasks(self):
315
    def save_tasks(self):
272
        settings = {}
-
 
-
 
316
        TASKS_FILE = self.get_file('TASKS')
273
        tasks = self.get_all_tasks()
317
        tasks = self.get_all_tasks()
274
318
275
        settings['Tasks'] = ','.join(tasks)
-
 
276
        self.config[self.section] = settings
-
 
277
        self.save_config()
-
 
-
 
319
        ftasks = open(TASKS_FILE, 'w')
-
 
320
        for task in tasks:
-
 
321
            ftasks.write("%s\n" % task)
-
 
322
            self.log.info("Task saved: %s" % task)
-
 
323
        ftasks.close()
-
 
324
278
325
279
326
280
    def save_tasks_from_stats(self, alltasks):
327
    def save_tasks_from_stats(self, alltasks):
281
        settings = {}
328
        settings = {}
282
        settings['Tasks'] = ','.join(alltasks)
-
 
-
 
329
        tasks = ','.join(alltasks)
-
 
330
        settings['Tasks'] = tasks
283
        self.config[self.section] = settings
331
        self.config[self.section] = settings
284
        self.save_config()
332
        self.save_config()