5 from PyQt4.QtCore import *
6 from PyQt4.QtGui import *
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
15 MEMBERSHIP_STATUS_COLUMN = 1
16 SERVER_MEMBERSHIP_STATUS_COLUMN = 2
18 user_status = { "in": "Already Selected",
19 "out": "Not Selected",
21 "remove": "To be Removed"}
23 class UserView(QTableView):
24 def __init__(self, parent=None):
25 QTableView.__init__(self, parent)
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.")
34 def mouseDoubleClickEvent(self, event):
35 index = self.currentIndex()
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()
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']))
52 model.setData(status_index, QString(user_status['out']))
54 model.emit(SIGNAL("dataChanged(QModelIndex, QModelIndex)"), node_index, node_index)
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)
62 def hideUnusableColumns(self):
63 self.hideColumn(SERVER_MEMBERSHIP_STATUS_COLUMN)
65 class UserModel(QStandardItemModel):
66 def __init__(self, rows=0, columns=4, parent=None):
67 QStandardItemModel.__init__(self, rows, columns, parent)
69 def updateModel(self, sliceRec):
76 #for pi in sliceRec.get_field("PI", default=[]):
78 # if not name in added_persons:
79 # slice_persons.append({"name": name, "role": "PI", "member": user_status["in"]})
80 # added_persons.append(name)
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)
89 while (os.path.exists(config.getAuthorityListFile(i))):
90 rec = self.readUserRecord(i)
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)
98 rootItem = self.invisibleRootItem()
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"]))])
106 headers = QStringList() << "User Name" << "Status" << "ServerStatus"
107 self.setHorizontalHeaderLabels(headers)
109 def updateRecord(self, slicerec):
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())
118 if (childStatus == user_status['add']):
119 researcher = slicerec.get_field("researcher", [])
120 researcher.append(childName)
121 slicerec["researcher"] = researcher
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)
132 def getResearchers(self):
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())
140 if (childStatus == user_status['add']) or (childStatus == user_status['in']):
141 researchers.append(childName)
145 def readUserRecord(self, i):
146 rec_file = config.getAuthorityListFile(i)
147 if os.path.exists(rec_file):
148 xml = open(rec_file).read()
150 rec.load_from_string(xml)
154 class UsersWidget(QWidget):
155 def __init__(self, parent):
156 QWidget.__init__(self, parent)
158 self.process = SfiProcess(self)
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)
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)
174 self.userView = UserView()
176 refresh = QPushButton("Refresh", self)
177 refresh.setSizePolicy(QSizePolicy.Maximum, QSizePolicy.Maximum)
178 submit = QPushButton("Submit", self)
179 submit.setSizePolicy(QSizePolicy.Maximum, QSizePolicy.Maximum)
181 bottomlayout = QHBoxLayout()
182 bottomlayout.addWidget(refresh, 0, Qt.AlignLeft)
183 bottomlayout.addStretch()
184 bottomlayout.addWidget(submit, 0, Qt.AlignRight)
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)
193 self.userModel = UserModel(parent=self)
195 self.connect(refresh, SIGNAL('clicked()'), self.refresh)
196 self.connect(submit, SIGNAL('clicked()'), self.submit)
200 def submitFinished(self):
201 self.setStatus("<font color='green'>Slice data submitted.</font>")
202 QTimer.singleShot(1000, self.refresh)
204 def getSliceRecordFinished(self):
205 self.setStatus("<font color='green'>Authority data refreshed.</font>", timeout=5000)
206 self.refreshAuthority()
208 def getAuthorityRecordFinished(self):
209 self.setStatus("<font color='green'>Slice data refreshed.</font>", timeout=5000)
211 #self.parent().signalAll("usersUpdated")
213 def readSliceRecord(self):
214 rec_file = config.getSliceRecordFile()
215 if os.path.exists(rec_file):
216 xml = open(rec_file).read()
218 rec.load_from_string(xml)
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)
231 def setStatus(self, msg, timeout=None):
232 self.parent().setStatus(msg, timeout)
234 def checkRunningProcess(self):
235 if self.process.isRunning():
236 self.setStatus("<font color='red'>There is already a process running. Please wait.</font>")
241 if self.checkRunningProcess():
244 rec = self.readSliceRecord()
245 change = self.userModel.updateRecord(rec)
248 self.setStatus("<font color=red>No change in slice data. Not submitting!</font>", timeout=3000)
251 rec_file = config.getSliceRecordFile()
252 file(rec_file, "w").write(rec.save_to_string())
254 self.disconnect(self.process, SIGNAL('finished()'), self.getAuthorityRecordFinished)
255 self.connect(self.process, SIGNAL('finished()'), self.submitFinished)
257 self.process.updateRecord(rec_file)
258 self.setStatus("Sending slice record. This will take some time...")
261 if not config.getSlice():
262 self.setStatus("<font color='red'>Slice not set yet!</font>")
265 if self.process.isRunning():
266 self.setStatus("<font color='red'>There is already a process running. Please wait.</font>")
269 self.disconnect(self.process, SIGNAL('finished()'), self.submitFinished)
270 self.connect(self.process, SIGNAL('finished()'), self.getSliceRecordFinished)
272 self.process.getSliceRecord()
273 self.setStatus("Refreshing slice record. This will take some time...")
275 def refreshAuthority(self):
276 self.disconnect(self.process, SIGNAL('finished()'), self.getSliceRecordFinished)
277 self.connect(self.process, SIGNAL('finished()'), self.getAuthorityRecordFinished)
279 self.process.listRecords(config.getAuthority(), "user", config.getAuthorityListFile())
280 self.setStatus("Refreshing user records. This will take some time...")
282 def updateView(self):
283 sliceRec = self.readSliceRecord()
286 # wait until we've resolved the slicerecord before displaying
287 # anything to the user.
288 self.userModel.clear()
290 self.userModel.updateModel(sliceRec)
292 self.userView.setModel(self.userModel)
293 self.userView.hideColumn(SERVER_MEMBERSHIP_STATUS_COLUMN)
294 self.userView.resizeColumnToContents(0)
296 def updateSliceName(self):
297 self.slicename.setText("Slice : %s" % (config.getSlice() or "None"))
302 class UserScreen(SfaScreen):
303 def __init__(self, parent):
304 SfaScreen.__init__(self, parent)
306 slice = UsersWidget(self)
307 self.init(slice, "Users", "OneLab SFA crawler")
309 def configurationChanged(self):
310 self.widget.updateSliceName()
311 self.widget.updateView()