Subversion Repositories basico

Rev

Rev 5 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
4 t00mlabs 1
#!/usr/bin/python
2
# -*- coding: utf-8 -*-
3
# File: tasks.py
4
# Author: Tomás Vírseda
5
# License: GPL v3
6
# Description: Tasks service
7
 
8
 
9
from gi.repository import Gtk
10
from gi.repository.GdkPixbuf import Pixbuf
11
from gi.repository import Pango
12
from datetime import datetime
13
from dateutil import parser as dateparser
14
 
15
from .service import Service
16
 
17
 
18
class Tasks(Service):
19
    def initialize(self):
20
        self.get_services()
21
        self.setup_window()
22
 
23
 
24
    def setup_window(self):
25
        # 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)
30
        self.pname = self.gui.add_widget('etyTaskName')
31
        self.pname.connect('activate', self.add_task)
32
        self.boxtask = self.gui.add_widget('boxTasks')
33
        self.btnadd = self.gui.add_widget('btnAddTask')
34
        self.btnadd.connect('clicked', self.add_task)
35
        self.btndel = self.gui.add_widget('btnDelTask')
36
        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()
42
        self.gui.add_widget('trvtaskwin', self.treeview)
43
        self.boxtask.add(self.treeview)
44
        self.hide_window()
45
 
46
        # setup model
47
        model = Gtk.ListStore(
48
            bool,           # CheckBox
49
            str             # Task name
50
        )
51
        self.treeview.set_model(model)
52
 
53
        # setup columns
54
        # Checkbox
55
        renderer = Gtk.CellRendererToggle()
56
        renderer.connect("toggled", self.on_cell_toggled)
57
        column = Gtk.TreeViewColumn('X', renderer, active=0)
58
        column.set_visible(True)
59
        column.set_expand(False)
60
        column.set_clickable(False)
61
        column.set_sort_indicator(False)
62
        self.treeview.append_column(column)
63
 
64
        # Task name
65
        renderer = Gtk.CellRendererText()
66
        column = Gtk.TreeViewColumn('Task name', renderer, text=1)
67
        column.set_visible(True)
68
        column.set_expand(True)
69
        column.set_clickable(True)
70
        column.set_sort_indicator(True)
71
        self.treeview.append_column(column)
72
 
73
        # Filters
74
        #Creating the filter, feeding it with the liststore model
75
        #~ self.task_filter = model.filter_new()
76
        #setting the filter function, note that we're not using the
77
        #~ self.task_filter.set_visible_func(self.task_filter_func)
78
        #~ self.task_filter.set_visible_column(1)
79
 
80
        # Treeview features
81
        self.treeview.set_headers_visible(True)
82
        self.treeview.set_enable_search(True)
83
        self.treeview.set_grid_lines(Gtk.TreeViewGridLines.HORIZONTAL)
84
        self.treeview.set_search_column(1)
85
        self.treeview.connect('row-activated', self.double_click)
86
        selection = self.treeview.get_selection()
87
        selection.set_mode(Gtk.SelectionMode.SINGLE)
88
        self.treeview.set_search_entry(self.pname)
89
 
90
 
91
    # row task visibility
92
    def task_row_visibility(self, model, iter, data):
93
        entry = self.gui.get_widget('etyTaskName')
94
        text = entry.get_text()
95
 
96
        return text.upper() in model.get_value(iter, 1)
97
 
98
 
99
    def task_filter_func(self, model, iter, data):
100
        """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')
103
        task = entry.get_text()
104
 
105
        #~ if len(task) == 0:
106
            #~ return False
107
 
108
        task_row = model[iter][1]
109
 
110
        if task.upper() in task_row.upper():
111
            return True
112
        else:
113
            return False
114
 
115
 
116
    def delete_task(self, *args):
117
        found = 0
118
        selection = self.treeview.get_selection()
119
        result = selection.get_selected()
120
        if result: #result could be None
121
            model, iter = result
122
            task = model.get_value(iter, 1)
123
            #~ self.log.debug(task)
124
            sapnotes = self.sap.get_notes()
125
            for sapnote in sapnotes:
126
                try:
127
                    tasks = sapnotes[sapnote]['tasks']
128
                    if task in tasks:
129
                        found += 1
130
                except:
131
                    pass
132
            if found > 1:
133
                self.log.warning("Task %s is still assigned to other SAP Notes" % task)
134
                head = "Task could not be deleted"
135
                body = "Task %s is still assigned to other SAP Notes" % task
136
                self.uif.message_dialog(head , body)
137
            else:
138
                model.remove(iter)
139
 
140
 
141
 
142
 
143
    def add_task(self, *args):
144
        task = self.pname.get_text()
145
        model = self.treeview.get_model()
146
 
147
        found = False
148
        for row in model:
149
            if row[1].upper() == task.upper():
150
                found = True
151
 
152
        if not found and len(task) > 0:
153
            model.append([False, task])
154
 
155
        self.pname.set_text('')
156
 
157
 
158
 
159
    def show_window(self, sapnotes=[]):
160
        rootwin = self.gui.get_widget('mainwindow')
161
        #~ self.window.set_transient_for(rootwin)
162
        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
 
173
 
174
    def get_services(self):
175
        self.gui = self.app.get_service("GUI")
176
        self.sap = self.app.get_service('SAP')
177
        self.uif = self.app.get_service('UIF')
178
        self.cb = self.app.get_service('Callbacks')
179
 
180
 
181
    def double_click(self, treeview, row, col):
182
        model = treeview.get_model()
183
        self.pname.set_text(model[row][1])
184
 
185
 
186
    def populate(self, sapnotes):
187
        model = self.treeview.get_model()
188
        model.clear()
189
        #~ model.append([bool, str])
190
 
191
 
192
    def changed(self, *args):
193
        try:
194
            model, treeiters = self.selection.get_selected_rows()
195
            selected = set()
196
            if len(treeiters) > 0:
197
                for treeiter in treeiters:
198
                    if treeiter != None:
199
                        selected.add(model[treeiter][0])
200
            print (selected)
201
 
202
        except Exception as error:
203
            self.log.error (self.get_traceback())
204
 
205
 
206
    def on_cell_toggled(self, widget, path):
207
        model = self.treeview.get_model()
208
        model[path][0] = not model[path][0]
209
 
210
 
211
    def get_selected_notes(self):
212
        sapnoteview = self.gui.get_widget('sapnoteview')
213
        return sapnoteview.get_selected_notes()
214
 
215
 
216
    def link_to_task(self, *args):
217
        self.save_tasks()
218
        self.hide_window()
219
        sapnotes = list(self.get_selected_notes())
220
        model = self.treeview.get_model()
221
        tasks = []
222
        for row in model:
223
           link = row[0]
224
           if link == True:
225
                tasks.append(row[1])
226
 
227
        sapnotes.sort()
228
        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)
232
        self.cb.refresh_view(view='tasks')
233
 
234
 
235
    def get_all_tasks(self):
236
        tasks = []
237
        model = self.treeview.get_model()
238
        for row in model:
239
            tasks.append(row[1])
240
        tasks.sort()
241
 
242
        return tasks
243
 
244
 
245
    def load_tasks(self, sapnotes=[]):
246
        model = self.treeview.get_model()
247
        model.clear()
248
        try:
249
            alltasks = set(self.get_config_value('Tasks').split(','))
250
        except:
251
            alltasks = set()
252
 
10 t00mlabs 253
        self.log.debug("All Tasks: %s" % alltasks)
4 t00mlabs 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)
10 t00mlabs 260
        self.log.debug("Linked Tasks: %s" % alltasks)
4 t00mlabs 261
 
262
        for task in alltasks:
263
            if task in linkedtasks:
264
                model.append([True, task])
265
            else:
10 t00mlabs 266
                if len(task) > 0:
267
                    model.append([False, task])
4 t00mlabs 268
 
269
 
270
 
271
    def save_tasks(self):
272
        settings = {}
273
        tasks = self.get_all_tasks()
274
 
275
        settings['Tasks'] = ','.join(tasks)
276
        self.config[self.section] = settings
277
        self.save_config()
278
 
279
 
280
    def save_tasks_from_stats(self, alltasks):
281
        settings = {}
282
        settings['Tasks'] = ','.join(alltasks)
283
        self.config[self.section] = settings
284
        self.save_config()