add user selection to create slice screen
[sface.git] / sface / screens / userscreen.py
1
2 import datetime
3 import os
4 import pickle
5 from PyQt4.QtCore import *
6 from PyQt4.QtGui import *
7
8 from sfa.util.record import SfaRecord, SliceRecord, AuthorityRecord, UserRecord
9 from sface.config import config
10 from sface.sfiprocess import SfiProcess
11 from sface.screens.sfascreen import SfaScreen
12
13 NAME_COLUMN = 0
14 #ROLE_COLUMN = 1
15 MEMBERSHIP_STATUS_COLUMN = 1
16 SERVER_MEMBERSHIP_STATUS_COLUMN = 2
17
18 user_status = { "in": "Already Selected",
19                 "out": "Not Selected",
20                 "add": "To be Added",
21                 "remove": "To be Removed"}
22
23 class UserView(QTableView):
24     def __init__(self, parent=None):
25         QTableView.__init__(self, parent)
26
27         self.setSelectionBehavior(QAbstractItemView.SelectRows)
28         self.setSelectionMode(QAbstractItemView.SingleSelection)
29         self.setAlternatingRowColors(True)
30         self.setAttribute(Qt.WA_MacShowFocusRect, 0)
31         self.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
32         self.setToolTip("Double click on a row to change its status.")
33
34     def mouseDoubleClickEvent(self, event):
35         index = self.currentIndex()
36         model = index.model()
37         status_index = model.index(index.row(), MEMBERSHIP_STATUS_COLUMN, index.parent())
38         status_data = status_index.data().toString()
39         server_status_data = model.index(index.row(), SERVER_MEMBERSHIP_STATUS_COLUMN, index.parent()).data().toString()
40         node_index = model.index(index.row(), NAME_COLUMN, index.parent())
41         node_data = node_index.data().toString()
42
43         # This is a hostname
44         if status_data == user_status['in']:
45             model.setData(status_index, QString(user_status['remove']))
46         elif status_data == user_status['out']:
47             model.setData(status_index, QString(user_status['add']))
48         elif status_data in (user_status['add'], user_status['remove']):
49             if server_status_data == user_status["in"]:
50                 model.setData(status_index, QString(user_status['in']))
51             else:
52                 model.setData(status_index, QString(user_status['out']))
53
54         model.emit(SIGNAL("dataChanged(QModelIndex, QModelIndex)"), node_index, node_index)
55
56     def currentChanged(self, current, previous):
57         model = current.model()
58         node_index = model.index(current.row(), 0, current.parent())
59         node_data = node_index.data().toString()
60         self.emit(SIGNAL('hostnameClicked(QString)'), node_data)
61
62     def hideUnusableColumns(self):
63         self.hideColumn(SERVER_MEMBERSHIP_STATUS_COLUMN)
64
65 class UserModel(QStandardItemModel):
66     def __init__(self, rows=0, columns=4, parent=None):
67          QStandardItemModel.__init__(self, rows, columns, parent)
68
69     def updateModel(self, sliceRec):
70         self.clear()
71
72         added_persons = []
73         slice_persons = []
74
75         if sliceRec:
76             #for pi in sliceRec.get_field("PI", default=[]):
77             #    name = str(pi)
78             #    if not name in added_persons:
79             #         slice_persons.append({"name": name, "role": "PI", "member": user_status["in"]})
80             #         added_persons.append(name)
81
82             for researcher in sliceRec.get_field("researcher", default=[]):
83                 name = str(researcher)
84                 if not name in added_persons:
85                      slice_persons.append({"name": name, "role": "researcher", "member": user_status["in"]})
86                      added_persons.append(name)
87
88         i=1
89         while (os.path.exists(config.getAuthorityListFile(i))):
90             rec = self.readUserRecord(i)
91             if rec:
92                 name = str(rec.get_name())
93                 if not name in added_persons:
94                     slice_persons.append({"name": name, "role": "", "member": user_status["out"]})
95                     added_persons.append(name)
96             i=i+1
97
98         rootItem = self.invisibleRootItem()
99
100         for person in slice_persons:
101             rootItem.appendRow([QStandardItem(QString(person["name"])),
102                                #QStandardItem(QString(person["role"])),
103                                QStandardItem(QString(person["member"])),
104                                QStandardItem(QString(person["member"]))])
105
106         headers = QStringList() << "User Name" << "Status" << "ServerStatus"
107         self.setHorizontalHeaderLabels(headers)
108
109     def updateRecord(self, slicerec):
110         change = False
111
112         item = self.invisibleRootItem()
113         children = item.rowCount()
114         for row in range(0, children):
115             childName = str(item.child(row, NAME_COLUMN).data(Qt.DisplayRole).toString())
116             childStatus = str(item.child(row, MEMBERSHIP_STATUS_COLUMN).data(Qt.DisplayRole).toString())
117
118             if (childStatus == user_status['add']):
119                 researcher = slicerec.get_field("researcher", [])
120                 researcher.append(childName)
121                 slicerec["researcher"] = researcher
122                 change = True
123             elif (childStatus == user_status['remove']):
124                 if childName in slicerec.get_field("PI"):
125                      slicerec.get_field("PI").remove(childName)
126                 if childName in slicerec.get_field("researcher"):
127                      slicerec.get_field("researcher").remove(childName)
128                 change = True
129
130         return change
131
132     def getResearchers(self):
133         researchers = []
134         item = self.invisibleRootItem()
135         children = item.rowCount()
136         for row in range(0, children):
137             childName = str(item.child(row, NAME_COLUMN).data(Qt.DisplayRole).toString())
138             childStatus = str(item.child(row, MEMBERSHIP_STATUS_COLUMN).data(Qt.DisplayRole).toString())
139
140             if (childStatus == user_status['add']) or (childStatus == user_status['in']):
141                 researchers.append(childName)
142
143         return researchers
144
145     def readUserRecord(self, i):
146         rec_file = config.getAuthorityListFile(i)
147         if os.path.exists(rec_file):
148             xml = open(rec_file).read()
149             rec = UserRecord()
150             rec.load_from_string(xml)
151             return rec
152         return None
153
154 class UsersWidget(QWidget):
155     def __init__(self, parent):
156         QWidget.__init__(self, parent)
157
158         self.process = SfiProcess(self)
159
160         self.slicename = QLabel("", self)
161         self.updateSliceName()
162         self.slicename.setScaledContents(False)
163         searchlabel = QLabel ("Search: ", self)
164         searchlabel.setScaledContents(False)
165         searchbox = QLineEdit(self)
166         searchbox.setAttribute(Qt.WA_MacShowFocusRect, 0)
167
168         toplayout = QHBoxLayout()
169         toplayout.addWidget(self.slicename, 0, Qt.AlignLeft)
170         toplayout.addStretch()
171         toplayout.addWidget(searchlabel, 0, Qt.AlignRight)
172         toplayout.addWidget(searchbox, 0, Qt.AlignRight)
173
174         self.userView = UserView()
175
176         refresh = QPushButton("Refresh", self)
177         refresh.setSizePolicy(QSizePolicy.Maximum, QSizePolicy.Maximum)
178         submit = QPushButton("Submit", self)
179         submit.setSizePolicy(QSizePolicy.Maximum, QSizePolicy.Maximum)
180
181         bottomlayout = QHBoxLayout()
182         bottomlayout.addWidget(refresh, 0, Qt.AlignLeft)
183         bottomlayout.addStretch()
184         bottomlayout.addWidget(submit, 0, Qt.AlignRight)
185
186         layout = QVBoxLayout()
187         layout.addLayout(toplayout)
188         layout.addWidget(self.userView)
189         layout.addLayout(bottomlayout)
190         self.setLayout(layout)
191         self.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
192
193         self.userModel = UserModel(parent=self)
194
195         self.connect(refresh, SIGNAL('clicked()'), self.refresh)
196         self.connect(submit, SIGNAL('clicked()'), self.submit)
197
198         self.updateView()
199
200     def submitFinished(self):
201         self.setStatus("<font color='green'>Slice data submitted.</font>")
202         QTimer.singleShot(1000, self.refresh)
203
204     def getSliceRecordFinished(self):
205         self.setStatus("<font color='green'>Authority data refreshed.</font>", timeout=5000)
206         self.refreshAuthority()
207
208     def getAuthorityRecordFinished(self):
209         self.setStatus("<font color='green'>Slice data refreshed.</font>", timeout=5000)
210         self.updateView()
211         #self.parent().signalAll("usersUpdated")
212
213     def readSliceRecord(self):
214         rec_file = config.getSliceRecordFile()
215         if os.path.exists(rec_file):
216             xml = open(rec_file).read()
217             rec = SliceRecord()
218             rec.load_from_string(xml)
219             return rec
220         return None
221
222     def readAuthorityRecord(self):
223         rec_file = config.getAuthorityRecordFile()
224         if os.path.exists(rec_file):
225             xml = open(rec_file).read()
226             rec = AuthorityRecord()
227             rec.load_from_string(xml)
228             return rec
229         return None
230
231     def setStatus(self, msg, timeout=None):
232         self.parent().setStatus(msg, timeout)
233
234     def checkRunningProcess(self):
235         if self.process.isRunning():
236             self.setStatus("<font color='red'>There is already a process running. Please wait.</font>")
237             return True
238         return False
239
240     def submit(self):
241         if self.checkRunningProcess():
242             return
243
244         rec = self.readSliceRecord()
245         change = self.userModel.updateRecord(rec)
246
247         if not change:
248             self.setStatus("<font color=red>No change in slice data. Not submitting!</font>", timeout=3000)
249             return
250
251         rec_file = config.getSliceRecordFile()
252         file(rec_file, "w").write(rec.save_to_string())
253
254         self.disconnect(self.process, SIGNAL('finished()'), self.getAuthorityRecordFinished)
255         self.connect(self.process, SIGNAL('finished()'), self.submitFinished)
256
257         self.process.updateRecord(rec_file)
258         self.setStatus("Sending slice record. This will take some time...")
259
260     def refresh(self):
261         if not config.getSlice():
262             self.setStatus("<font color='red'>Slice not set yet!</font>")
263             return
264
265         if self.process.isRunning():
266             self.setStatus("<font color='red'>There is already a process running. Please wait.</font>")
267             return
268
269         self.disconnect(self.process, SIGNAL('finished()'), self.submitFinished)
270         self.connect(self.process, SIGNAL('finished()'), self.getSliceRecordFinished)
271
272         self.process.getSliceRecord()
273         self.setStatus("Refreshing slice record. This will take some time...")
274
275     def refreshAuthority(self):
276         self.disconnect(self.process, SIGNAL('finished()'), self.getSliceRecordFinished)
277         self.connect(self.process, SIGNAL('finished()'), self.getAuthorityRecordFinished)
278
279         self.process.listRecords(config.getAuthority(), "user", config.getAuthorityListFile())
280         self.setStatus("Refreshing user records. This will take some time...")
281
282     def updateView(self):
283         sliceRec = self.readSliceRecord()
284
285         if not sliceRec:
286             # wait until we've resolved the slicerecord before displaying
287             # anything to the user.
288             self.userModel.clear()
289         else:
290             self.userModel.updateModel(sliceRec)
291
292         self.userView.setModel(self.userModel)
293         self.userView.hideColumn(SERVER_MEMBERSHIP_STATUS_COLUMN)
294         self.userView.resizeColumnToContents(0)
295
296     def updateSliceName(self):
297         self.slicename.setText("Slice : %s" % (config.getSlice() or "None"))
298
299
300
301
302 class UserScreen(SfaScreen):
303     def __init__(self, parent):
304         SfaScreen.__init__(self, parent)
305
306         slice = UsersWidget(self)
307         self.init(slice, "Users", "OneLab SFA crawler")
308
309     def configurationChanged(self):
310         self.widget.updateSliceName()
311         self.widget.updateView()
312
313