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 class UserModel(QStandardItemModel):
63 def __init__(self, rows=0, columns=4, parent=None):
64 QStandardItemModel.__init__(self, rows, columns, parent)
66 def updateModel(self, sliceRec):
73 #for pi in sliceRec.get_field("PI", default=[]):
75 # if not name in added_persons:
76 # slice_persons.append({"name": name, "role": "PI", "member": user_status["in"]})
77 # added_persons.append(name)
79 for researcher in sliceRec.get_field("researcher", default=[]):
80 name = str(researcher)
81 if not name in added_persons:
82 slice_persons.append({"name": name, "role": "researcher", "member": user_status["in"]})
83 added_persons.append(name)
86 while (os.path.exists(config.getAuthorityListFile(i))):
87 rec = self.readUserRecord(i)
89 name = str(rec.get_name())
90 if not name in added_persons:
91 slice_persons.append({"name": name, "role": "", "member": user_status["out"]})
92 added_persons.append(name)
95 rootItem = self.invisibleRootItem()
97 for person in slice_persons:
98 rootItem.appendRow([QStandardItem(QString(person["name"])),
99 #QStandardItem(QString(person["role"])),
100 QStandardItem(QString(person["member"])),
101 QStandardItem(QString(person["member"]))])
103 headers = QStringList() << "User Name" << "Status" << "ServerStatus"
104 self.setHorizontalHeaderLabels(headers)
106 def updateRecord(self, slicerec):
109 item = self.invisibleRootItem()
110 children = item.rowCount()
111 for row in range(0, children):
112 childName = str(item.child(row, NAME_COLUMN).data(Qt.DisplayRole).toString())
113 childStatus = str(item.child(row, MEMBERSHIP_STATUS_COLUMN).data(Qt.DisplayRole).toString())
115 if (childStatus == user_status['add']):
116 researcher = slicerec.get_field("researcher", [])
117 researcher.append(childName)
118 slicerec["researcher"] = researcher
120 elif (childStatus == user_status['remove']):
121 if childName in slicerec.get_field("PI"):
122 slicerec.get_field("PI").remove(childName)
123 if childName in slicerec.get_field("researcher"):
124 slicerec.get_field("researcher").remove(childName)
129 def readUserRecord(self, i):
130 rec_file = config.getAuthorityListFile(i)
131 if os.path.exists(rec_file):
132 xml = open(rec_file).read()
134 rec.load_from_string(xml)
138 class UsersWidget(QWidget):
139 def __init__(self, parent):
140 QWidget.__init__(self, parent)
142 self.process = SfiProcess(self)
144 self.slicename = QLabel("", self)
145 self.updateSliceName()
146 self.slicename.setScaledContents(False)
147 searchlabel = QLabel ("Search: ", self)
148 searchlabel.setScaledContents(False)
149 searchbox = QLineEdit(self)
150 searchbox.setAttribute(Qt.WA_MacShowFocusRect, 0)
152 toplayout = QHBoxLayout()
153 toplayout.addWidget(self.slicename, 0, Qt.AlignLeft)
154 toplayout.addStretch()
155 toplayout.addWidget(searchlabel, 0, Qt.AlignRight)
156 toplayout.addWidget(searchbox, 0, Qt.AlignRight)
158 self.userView = UserView()
160 refresh = QPushButton("Refresh", self)
161 refresh.setSizePolicy(QSizePolicy.Maximum, QSizePolicy.Maximum)
162 submit = QPushButton("Submit", self)
163 submit.setSizePolicy(QSizePolicy.Maximum, QSizePolicy.Maximum)
165 bottomlayout = QHBoxLayout()
166 bottomlayout.addWidget(refresh, 0, Qt.AlignLeft)
167 bottomlayout.addStretch()
168 bottomlayout.addWidget(submit, 0, Qt.AlignRight)
170 layout = QVBoxLayout()
171 layout.addLayout(toplayout)
172 layout.addWidget(self.userView)
173 layout.addLayout(bottomlayout)
174 self.setLayout(layout)
175 self.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
177 self.userModel = UserModel(parent=self)
179 self.connect(refresh, SIGNAL('clicked()'), self.refresh)
180 self.connect(submit, SIGNAL('clicked()'), self.submit)
184 def submitFinished(self):
185 self.setStatus("<font color='green'>Slice data submitted.</font>")
186 QTimer.singleShot(1000, self.refresh)
188 def getSliceRecordFinished(self):
189 self.setStatus("<font color='green'>Authority data refreshed.</font>", timeout=5000)
190 self.refreshAuthority()
192 def getAuthorityRecordFinished(self):
193 self.setStatus("<font color='green'>Slice data refreshed.</font>", timeout=5000)
195 #self.parent().signalAll("usersUpdated")
197 def readSliceRecord(self):
198 rec_file = config.getSliceRecordFile()
199 if os.path.exists(rec_file):
200 xml = open(rec_file).read()
202 rec.load_from_string(xml)
206 def readAuthorityRecord(self):
207 rec_file = config.getAuthorityRecordFile()
208 if os.path.exists(rec_file):
209 xml = open(rec_file).read()
210 rec = AuthorityRecord()
211 rec.load_from_string(xml)
215 def setStatus(self, msg, timeout=None):
216 self.parent().setStatus(msg, timeout)
218 def checkRunningProcess(self):
219 if self.process.isRunning():
220 self.setStatus("<font color='red'>There is already a process running. Please wait.</font>")
225 if self.checkRunningProcess():
228 rec = self.readSliceRecord()
229 change = self.userModel.updateRecord(rec)
232 self.setStatus("<font color=red>No change in slice data. Not submitting!</font>", timeout=3000)
235 rec_file = config.getSliceRecordFile()
236 file(rec_file, "w").write(rec.save_to_string())
238 self.disconnect(self.process, SIGNAL('finished()'), self.getAuthorityRecordFinished)
239 self.connect(self.process, SIGNAL('finished()'), self.submitFinished)
241 self.process.updateRecord(rec_file)
242 self.setStatus("Sending slice record. This will take some time...")
245 if not config.getSlice():
246 self.setStatus("<font color='red'>Slice not set yet!</font>")
249 if self.process.isRunning():
250 self.setStatus("<font color='red'>There is already a process running. Please wait.</font>")
253 self.disconnect(self.process, SIGNAL('finished()'), self.submitFinished)
254 self.connect(self.process, SIGNAL('finished()'), self.getSliceRecordFinished)
256 self.process.getSliceRecord()
257 self.setStatus("Refreshing slice record. This will take some time...")
259 def refreshAuthority(self):
260 self.disconnect(self.process, SIGNAL('finished()'), self.getSliceRecordFinished)
261 self.connect(self.process, SIGNAL('finished()'), self.getAuthorityRecordFinished)
263 self.process.listRecords(config.getAuthority(), "user", config.getAuthorityListFile())
264 self.setStatus("Refreshing user records. This will take some time...")
266 def updateView(self):
267 sliceRec = self.readSliceRecord()
270 # wait until we've resolved the slicerecord before displaying
271 # anything to the user.
272 self.userModel.clear()
274 self.userModel.updateModel(sliceRec)
276 self.userView.setModel(self.userModel)
277 self.userView.hideColumn(SERVER_MEMBERSHIP_STATUS_COLUMN)
278 self.userView.resizeColumnToContents(0)
280 def updateSliceName(self):
281 self.slicename.setText("Slice : %s" % (config.getSlice() or "None"))
286 class UserScreen(SfaScreen):
287 def __init__(self, parent):
288 SfaScreen.__init__(self, parent)
290 slice = UsersWidget(self)
291 self.init(slice, "Users", "OneLab SFA crawler")
293 def configurationChanged(self):
294 self.widget.updateSliceName()
295 self.widget.updateView()