Subversion Repositories basico

Rev

Rev 4 | 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: projects.py
4
# Author: Tomás Vírseda
5
# License: GPL v3
6
# Description: Projects 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 Projects(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('winProjects')
27
        self.parent = self.gui.get_widget('mainwindow')
28
        self.window.set_transient_for(self.parent)
29
        self.pname = self.gui.add_widget('etyProjectName')
30
        self.pname.connect('activate', self.add_project)
31
        self.boxproj = self.gui.add_widget('boxProjects')
32
        self.btnadd = self.gui.add_widget('btnAddProject')
33
        self.btnadd.connect('clicked', self.add_project)
34
        self.btndel = self.gui.add_widget('btnDelProject')
35
        self.btndel.connect('clicked', self.delete_project)
36
        self.btncancel = self.gui.add_widget('btnCancelProjects')
37
        self.btncancel.connect('clicked', self.hide_window)
38
        self.btnaccept = self.gui.add_widget('btnAcceptProjects')
39
        self.btnaccept.connect('clicked', self.link_to_project)
40
        self.treeview = self.gui.add_widget('trvprojwin', Gtk.TreeView())
41
        self.boxproj.add(self.treeview)
42
        self.hide_window()
43
 
44
        # setup model
45
        model = Gtk.ListStore(
46
            bool,           # CheckBox
47
            str             # Project name
48
        )
49
        self.treeview.set_model(model)
50
 
51
        # setup columns
52
        # Checkbox
53
        renderer = Gtk.CellRendererToggle()
54
        renderer.connect("toggled", self.on_cell_toggled)
55
        column = Gtk.TreeViewColumn('X', renderer, active=0)
56
        column.set_visible(True)
57
        column.set_expand(False)
58
        column.set_clickable(False)
59
        column.set_sort_indicator(False)
60
        self.treeview.append_column(column)
61
 
62
        # Project name
63
        renderer = Gtk.CellRendererText()
64
        column = Gtk.TreeViewColumn('Project name', renderer, text=1)
65
        column.set_visible(True)
66
        column.set_expand(True)
67
        column.set_clickable(True)
68
        column.set_sort_indicator(True)
69
        self.treeview.append_column(column)
70
 
71
        # Treeview features
72
        self.treeview.set_headers_visible(True)
73
        self.treeview.set_enable_search(True)
74
        self.treeview.set_grid_lines(Gtk.TreeViewGridLines.HORIZONTAL)
75
        self.treeview.set_search_column(1)
76
        self.treeview.connect('row-activated', self.double_click)
77
        selection = self.treeview.get_selection()
78
        selection.set_mode(Gtk.SelectionMode.SINGLE)
79
        self.treeview.set_search_entry(self.pname)
80
 
81
 
82
    def delete_project(self, *args):
74 t00mlabs 83
        db = self.get_service('DB')
4 t00mlabs 84
        found = 0
85
        selection = self.treeview.get_selection()
86
        result = selection.get_selected()
87
        if result: #result could be None
88
            model, iter = result
89
            project = model.get_value(iter, 1)
90
            self.log.debug(project)
74 t00mlabs 91
            sapnotes = db.get_notes()
4 t00mlabs 92
            for sapnote in sapnotes:
93
                projects = sapnotes[sapnote]['projects']
94
                if project in projects:
95
                    found += 1
96
            if found > 1:
97
                self.log.debug("Project %s is still assigned to other SAP Notes" % project)
98
                head = "Project could not be deleted"
99
                body = "Project %s is still assigned to other SAP Notes" % project
100
                self.uif.message_dialog(head , body)
101
            else:
102
                model.remove(iter)
103
 
104
 
105
 
106
 
107
    def add_project(self, *args):
108
        project = self.pname.get_text()
109
        model = self.treeview.get_model()
110
 
111
        found = False
112
        for row in model:
113
            if row[1].upper() == project.upper():
114
                found = True
115
 
116
        if not found and len(project) > 0:
117
            model.append([False, project])
118
 
119
        self.pname.set_text('')
120
 
121
 
122
 
123
    def show_window(self, sapnote=None):
124
        rootwin = self.gui.get_widget('mainwindow')
125
        self.window.set_transient_for(rootwin)
126
        self.load_projects(sapnote)
127
        self.window.set_no_show_all(False)
128
        self.window.show_all()
129
 
130
        #~ if sapnote is not None:
131
            #~ projects = self.sap.get_linked_projects(sapnote)
132
            #~ self.log.debug("Projects for SAP Note %s: %s" % (sapnote, projects))
133
            #~ dialog = Gtk.MessageDialog(self.parent, 0, Gtk.MessageType.INFO, Gtk.ButtonsType.OK, "Task completed")
134
            #~ dialog.format_secondary_text("All SAP Notes were downloaded sucessfully.")
135
            #~ dialog.run()
136
            #~ dialog.destroy()
137
 
138
    def hide_window(self, *args):
139
        self.window.set_no_show_all(True)
140
        self.window.hide()
141
 
142
 
143
    def get_services(self):
144
        self.gui = self.app.get_service("GUI")
145
        self.sap = self.app.get_service('SAP')
146
        self.uif = self.app.get_service('UIF')
147
 
148
 
149
    def double_click(self, treeview, row, col):
150
        model = treeview.get_model()
151
        self.pname.set_text(model[row][1])
152
 
153
 
154
    def populate(self, sapnotes):
155
        model = self.treeview.get_model()
156
        model.clear()
157
        #~ model.append([bool, str])
158
 
159
 
160
    def changed(self, *args):
161
        try:
162
            model, treeiters = self.selection.get_selected_rows()
163
            selected = set()
164
            if len(treeiters) > 0:
165
                for treeiter in treeiters:
166
                    if treeiter != None:
167
                        selected.add(model[treeiter][0])
168
            print (selected)
169
 
170
        except Exception as error:
171
            self.log.error (self.get_traceback())
172
 
173
 
174
    def on_cell_toggled(self, widget, path):
175
        model = self.treeview.get_model()
176
        model[path][0] = not model[path][0]
177
 
178
 
179
    def get_selected_notes(self):
180
        sapnoteview = self.gui.get_widget('sapnoteview')
181
        return sapnoteview.get_selected_notes()
182
 
183
 
184
    def link_to_project(self, *args):
185
        self.save_projects()
186
        self.hide_window()
187
        sapnotes = list(self.get_selected_notes())
188
        model = self.treeview.get_model()
189
        projects = []
190
        for row in model:
191
           link = row[0]
192
           if link == True:
193
                projects.append(row[1])
194
 
195
        sapnotes.sort()
196
        projects.sort()
197
        self.log.debug('SAP Notes: %s' % sapnotes)
198
        self.log.debug(' Projects: %s' % projects)
199
        self.sap.link_to_project(sapnotes, projects)
200
        sapnoteview = self.gui.get_widget('sapnoteview')
201
        sapnoteview.refresh()
202
 
203
 
204
    def get_all_projects(self):
205
        projects = []
206
        model = self.treeview.get_model()
207
        for row in model:
208
            projects.append(row[1])
209
        projects.sort()
210
 
211
        return projects
212
 
213
 
214
    def load_projects(self, sapnote=''):
215
        try:
216
            projects = self.get_config_value('Projects').split(',')
217
        except:
218
            projects = []
219
        model = self.treeview.get_model()
220
        model.clear()
221
 
222
        for project in projects:
223
            if len(project) > 0:
224
                if len(sapnote) > 0:
225
                    snprjs = self.sap.get_linked_projects(sapnote)
226
                    self.log.debug(snprjs)
227
                    if project in snprjs:
228
                        model.append([True, project])
229
                    else:
230
                        model.append([False, project])
231
                else:
232
                    model.append([False, project])
233
 
234
 
235
    def save_projects(self):
236
        settings = {}
237
        projects = self.get_all_projects()
238
 
239
        settings['Projects'] = ','.join(projects)
240
        self.config[self.section] = settings
241
        self.save_config()