| 161 | def modifyEntry(self, entry, groups, limitby, description, overcharge=None, balance=None, balancevalue=None, comment=None, email=None) : |
| 162 | """Modifies an entry.""" |
| 163 | if description is not None : # NB : "" is allowed ! |
| 164 | entry.setDescription(description) |
| 165 | if limitby : |
| 166 | entry.setLimitBy(limitby) |
| 167 | if not groups : |
| 168 | if email : |
| 169 | entry.Email = email |
| 170 | if overcharge is not None : # NB : 0 is allowed ! |
| 171 | entry.setOverChargeFactor(overcharge) |
| 172 | if balance : |
| 173 | if balance.startswith("+") or balance.startswith("-") : |
| 174 | newbalance = float(entry.AccountBalance or 0.0) + balancevalue |
| 175 | newlifetimepaid = float(entry.LifeTimePaid or 0.0) + balancevalue |
| 176 | entry.setAccountBalance(newbalance, newlifetimepaid, comment) |
| 177 | else : |
| 178 | diff = balancevalue - float(entry.AccountBalance or 0.0) |
| 179 | newlifetimepaid = float(entry.LifeTimePaid or 0.0) + diff |
| 180 | entry.setAccountBalance(balancevalue, newlifetimepaid, comment) |
| 181 | |
| 182 | def manageUsersGroups(self, ugroups, user, remove) : |
| 183 | """Manage user group membership.""" |
| 184 | for ugroup in ugroups : |
| 185 | if remove : |
| 186 | ugroup.delUserFromGroup(user) |
| 187 | else : |
| 188 | ugroup.addUserToGroup(user) |
| 189 | |
165 | | if options["delete"] : |
166 | | self.display("%s...\n" % _("Deletion")) |
167 | | todelete = getattr(self.storage, "getMatching%ss" % suffix)(",".join(names)) |
168 | | nbtotal = len(todelete) |
169 | | for i in range(nbtotal) : |
170 | | entry = todelete[i] |
171 | | if entry.Exists : |
172 | | entry.delete() |
173 | | percent = 100.0 * float(i) / float(nbtotal) |
174 | | self.display("\r%.02f%%" % percent) |
| 195 | if not options["add"] : |
| 196 | if not options["list"] : |
| 197 | self.display(_("Extracting datas...")) |
| 198 | if not names : # NB : can't happen for --delete because it's catched earlier |
| 199 | names = ["*"] |
| 200 | entries = getattr(self.storage, "getMatching%ss" % suffix)(",".join(names)) |
| 201 | if not entries : |
| 202 | raise PyKotaCommandLineError, _("There's no %s matching %s") % (_(suffix.lower()), " ".join(names)) |
| 203 | |
| 204 | if options["list"] : |
| 205 | if suffix == "User" : |
| 206 | maildomain = self.config.getMailDomain() |
| 207 | smtpserver = self.config.getSMTPServer() |
| 208 | for entry in entries : |
| 209 | email = entry.Email |
| 210 | if not email : |
| 211 | if maildomain : |
| 212 | email = "%s@%s" % (entry.Name, maildomain) |
| 213 | elif smtpserver : |
| 214 | email = "%s@%s" % (entry.Name, smtpserver) |
| 215 | else : |
| 216 | email = "%s@%s" % (entry.Name, "localhost") |
| 217 | msg = "%s - <%s>" % (entry.Name, email) |
| 218 | if entry.Description : |
| 219 | msg += " - %s" % entry.Description |
| 220 | print msg |
| 221 | print " %s" % (_("Limited by : %s") % entry.LimitBy) |
| 222 | print " %s" % (_("Account balance : %.2f") % (entry.AccountBalance or 0.0)) |
| 223 | print " %s" % (_("Total paid so far : %.2f") % (entry.LifeTimePaid or 0.0)) |
| 224 | print " %s" % (_("Overcharging factor : %.2f") % entry.OverCharge) |
| 225 | print |
| 226 | else : |
| 227 | for entry in entries : |
| 228 | msg = "%s" % entry.Name |
| 229 | if entry.Description : |
| 230 | msg += " - %s" % entry.Description |
| 231 | print msg |
| 232 | print " %s" % (_("Limited by : %s") % entry.LimitBy) |
| 233 | print " %s" % (_("Group balance : %.2f") % (entry.AccountBalance or 0.0)) |
| 234 | print " %s" % (_("Total paid so far : %.2f") % (entry.LifeTimePaid or 0.0)) |
| 235 | print |
| 236 | elif options["delete"] : |
| 237 | self.display("\n%s..." % _("Deletion")) |
| 238 | getattr(self.storage, "deleteMany%ss" % suffix)(entries) |
| 239 | self.display("\n") |
176 | | if options["add"] : |
177 | | self.display("%s...\n" % _("Creation")) |
178 | | rejectunknown = self.config.getRejectUnknown() |
179 | | entries = [] |
180 | | nbtotal = len(names) |
181 | | for i in range(nbtotal) : |
182 | | ename = names[i] |
183 | | email = "" |
184 | | if not options["groups"] : |
185 | | splitname = ename.split('/', 1) # username/email |
186 | | if len(splitname) == 1 : |
187 | | splitname.append("") |
188 | | (ename, email) = splitname |
189 | | if email and (email.count('@') != 1) : |
190 | | self.printInfo(_("Invalid email address %s") % email) |
191 | | email = "" |
192 | | entry = getattr(self.storage, "get%s" % suffix)(ename) |
193 | | if entry.Exists : |
194 | | if options["skipexisting"] : |
195 | | self.printInfo(_("%s %s already exists, skipping.") % (suffix, entry.Name)) |
196 | | else : |
197 | | self.printInfo(_("%s %s already exists, will be modified.") % (suffix, entry.Name)) |
198 | | entries.append(entry) |
199 | | else : |
200 | | if self.isValidName(entry.Name) : |
| 241 | limitby = options["limitby"] |
| 242 | if limitby : |
| 243 | limitby = limitby.strip().lower() |
| 244 | if limitby : |
| 245 | if limitby not in ('quota', 'balance', 'noquota', \ |
| 246 | 'noprint', 'nochange') : |
| 247 | raise PyKotaCommandLineError, _("Invalid limitby value %s") % options["limitby"] |
| 248 | if (limitby in ('nochange', 'noprint')) and options["groups"] : |
| 249 | raise PyKotaCommandLineError, _("Invalid limitby value %s") % options["limitby"] |
| 250 | |
| 251 | overcharge = options["overcharge"] |
| 252 | if overcharge : |
| 253 | try : |
| 254 | overcharge = float(overcharge.strip()) |
| 255 | except (ValueError, AttributeError) : |
| 256 | raise PyKotaCommandLineError, _("Invalid overcharge value %s") % options["overcharge"] |
| 257 | |
| 258 | balance = options["balance"] |
| 259 | if balance : |
| 260 | balance = balance.strip() |
| 261 | try : |
| 262 | balancevalue = float(balance) |
| 263 | except ValueError : |
| 264 | raise PyKotaCommandLineError, _("Invalid balance value %s") % options["balance"] |
| 265 | |
| 266 | if options["ingroups"] : |
| 267 | usersgroups = self.storage.getMatchingGroups(options["ingroups"]) |
| 268 | if not usersgroups : |
| 269 | raise PyKotaCommandLineError, _("There's no users group matching %s") % " ".join(options["ingroups"].split(',')) |
| 270 | else : |
| 271 | usersgroups = [] |
| 272 | |
| 273 | description = options["description"] |
| 274 | if description : |
| 275 | description = options["description"].strip() |
| 276 | |
| 277 | comment = options["comment"] |
| 278 | if comment : |
| 279 | comment = options["comment"].strip() |
| 280 | skipexisting = options["skipexisting"] |
| 281 | groups = options["groups"] |
| 282 | remove = options["remove"] |
| 283 | self.storage.beginTransaction() |
| 284 | try : |
| 285 | if options["add"] : |
| 286 | self.display("%s...\n" % _("Creation")) |
| 287 | rejectunknown = self.config.getRejectUnknown() |
| 288 | nbtotal = len(names) |
| 289 | for i in range(nbtotal) : |
| 290 | ename = names[i] |
| 291 | email = None |
| 292 | if not groups : |
| 293 | splitname = ename.split('/', 1) # username/email |
| 294 | if len(splitname) == 1 : |
| 295 | splitname.append("") |
| 296 | (ename, email) = splitname |
| 297 | if email and (email.count('@') != 1) : |
| 298 | raise PyKotaCommandLineError, _("Invalid email address %s") % email |
| 299 | if self.isValidName(ename) : |
216 | | if email : |
217 | | entry.Email = email |
218 | | entry = getattr(self.storage, "add%s" % suffix)(entry) |
219 | | entries.append(entry) |
| 315 | entry = globals()["Storage%s" % suffix](self.storage, ename) |
| 316 | if groups : |
| 317 | self.modifyEntry(entry, groups, limitby, \ |
| 318 | description) |
| 319 | else : |
| 320 | self.modifyEntry(entry, groups, limitby, \ |
| 321 | description, overcharge,\ |
| 322 | balance, balancevalue, \ |
| 323 | comment, email) |
| 324 | oldentry = getattr(self.storage, "add%s" % suffix)(entry) |
| 325 | if oldentry is not None : |
| 326 | if skipexisting : |
| 327 | self.printInfo(_("%s %s already exists, skipping.") % (_(suffix), ename)) |
| 328 | else : |
| 329 | self.printInfo(_("%s %s already exists, will be modified.") % (_(suffix), ename)) |
| 330 | if groups : |
| 331 | self.modifyEntry(oldentry, groups, \ |
| 332 | limitby, description) |
| 333 | else : |
| 334 | self.modifyEntry(oldentry, groups, limitby, \ |
| 335 | description, overcharge,\ |
| 336 | balance, balancevalue, \ |
| 337 | comment, email) |
| 338 | oldentry.save() |
| 339 | if not groups : |
| 340 | self.manageUsersGroups(usersgroups, oldentry, remove) |
| 341 | elif usersgroups and not groups : |
| 342 | self.manageUsersGroups(usersgroups, \ |
| 343 | self.storage.getUser(ename), \ |
| 344 | remove) |
| 345 | else : |
| 346 | raise PyKotaCommandLineError, _("Invalid name %s") % ename |
| 347 | percent = 100.0 * float(i) / float(nbtotal) |
| 348 | self.display("\r%.02f%%" % percent) |
| 349 | else : |
| 350 | self.display("\n%s...\n" % _("Modification")) |
| 351 | nbtotal = len(entries) |
| 352 | for i in range(nbtotal) : |
| 353 | entry = entries[i] |
| 354 | if groups : |
| 355 | self.modifyEntry(entry, groups, limitby, description) |
221 | | if options["groups"] : |
222 | | self.printInfo(_("Invalid group name %s") % entry.Name) |
223 | | else : |
224 | | self.printInfo(_("Invalid user name %s") % entry.Name) |
225 | | |
226 | | percent = 100.0 * float(i) / float(nbtotal) |
227 | | self.display("\r%.02f%%" % percent) |
228 | | self.done() |
229 | | else : |
230 | | entries = getattr(self.storage, "getMatching%ss" % suffix)(",".join(names)) |
231 | | if not entries : |
232 | | raise PyKotaCommandLineError, _("There's no %s matching %s") % (suffix.lower(), " ".join(names)) |
233 | | |
234 | | if options["list"] : |
235 | | if suffix == "User" : |
236 | | maildomain = self.config.getMailDomain() |
237 | | smtpserver = self.config.getSMTPServer() |
238 | | for entry in entries : |
239 | | if entry.Exists : |
240 | | email = entry.Email |
241 | | if not email : |
242 | | if maildomain : |
243 | | email = "%s@%s" % (entry.Name, maildomain) |
244 | | elif smtpserver : |
245 | | email = "%s@%s" % (entry.Name, smtpserver) |
246 | | else : |
247 | | email = "%s@%s" % (entry.Name, "localhost") |
248 | | msg = "%s - <%s>" % (entry.Name, email) |
249 | | if entry.Description : |
250 | | msg += " - %s" % entry.Description |
251 | | print msg |
252 | | print " %s" % (_("Limited by : %s") % entry.LimitBy) |
253 | | print " %s" % (_("Account balance : %.2f") % (entry.AccountBalance or 0.0)) |
254 | | print " %s" % (_("Total paid so far : %.2f") % (entry.LifeTimePaid or 0.0)) |
255 | | print " %s" % (_("Overcharging factor : %.2f") % entry.OverCharge) |
256 | | print |
257 | | else : |
258 | | for entry in entries : |
259 | | if entry.Exists : |
260 | | msg = "%s" % entry.Name |
261 | | if entry.Description : |
262 | | msg += " - %s" % entry.Description |
263 | | print msg |
264 | | print " %s" % (_("Limited by : %s") % entry.LimitBy) |
265 | | print " %s" % (_("Group balance : %.2f") % (entry.AccountBalance or 0.0)) |
266 | | print " %s" % (_("Total paid so far : %.2f") % (entry.LifeTimePaid or 0.0)) |
267 | | print |
268 | | else : |
269 | | self.display("%s...\n" % _("Modification")) |
270 | | |
271 | | limitby = options["limitby"] |
272 | | if limitby : |
273 | | limitby = limitby.strip().lower() |
274 | | if limitby : |
275 | | if limitby not in ('quota', 'balance', 'noquota', \ |
276 | | 'noprint', 'nochange') : |
277 | | raise PyKotaCommandLineError, _("Invalid limitby value %s") % options["limitby"] |
278 | | if (limitby in ('nochange', 'noprint')) and options["groups"] : |
279 | | raise PyKotaCommandLineError, _("Invalid limitby value %s") % options["limitby"] |
280 | | |
281 | | overcharge = options["overcharge"] |
282 | | if overcharge : |
283 | | try : |
284 | | overcharge = float(overcharge.strip()) |
285 | | except (ValueError, AttributeError) : |
286 | | raise PyKotaCommandLineError, _("Invalid overcharge value %s") % options["overcharge"] |
287 | | |
288 | | balance = options["balance"] |
289 | | if balance : |
290 | | balance = balance.strip() |
291 | | try : |
292 | | balancevalue = float(balance) |
293 | | except ValueError : |
294 | | raise PyKotaCommandLineError, _("Invalid balance value %s") % options["balance"] |
295 | | |
296 | | if options["ingroups"] : |
297 | | usersgroups = self.storage.getMatchingGroups(options["ingroups"]) |
298 | | if not usersgroups : |
299 | | raise PyKotaCommandLineError, _("There's no users group matching %s") % " ".join(options["ingroups"].split(',')) |
300 | | else : |
301 | | usersgroups = [] |
302 | | |
303 | | description = options["description"] |
304 | | if description : |
305 | | description = options["description"].strip() |
306 | | |
307 | | comment = options["comment"] |
308 | | if comment : |
309 | | comment = options["comment"].strip() |
310 | | |
311 | | nbtotal = len(entries) |
312 | | for i in range(nbtotal) : |
313 | | entry = entries[i] |
314 | | |
315 | | # We need a transaction because we may have to create a record |
316 | | # in the payments' table at the same time we modify the user's balance. |
317 | | self.storage.beginTransaction() |
318 | | try : |
319 | | if description is not None : # NB : "" is allowed ! |
320 | | entry.setDescription(description) |
321 | | if limitby : |
322 | | entry.setLimitBy(limitby) |
323 | | |
324 | | if suffix == "User" : |
325 | | if overcharge is not None : # NB : 0 is allowed ! |
326 | | entry.setOverChargeFactor(overcharge) |
327 | | |
328 | | if suffix == "User" : |
329 | | if balance : |
330 | | if balance.startswith("+") or balance.startswith("-") : |
331 | | newbalance = float(entry.AccountBalance or 0.0) + balancevalue |
332 | | newlifetimepaid = float(entry.LifeTimePaid or 0.0) + balancevalue |
333 | | entry.setAccountBalance(newbalance, newlifetimepaid, comment) |
334 | | else : |
335 | | diff = balancevalue - float(entry.AccountBalance or 0.0) |
336 | | newlifetimepaid = float(entry.LifeTimePaid or 0.0) + diff |
337 | | entry.setAccountBalance(balancevalue, newlifetimepaid, comment) |
338 | | |
339 | | for ugroup in usersgroups : |
340 | | if options["remove"] : |
341 | | ugroup.delUserFromGroup(entry) |
342 | | else : |
343 | | ugroup.addUserToGroup(entry) |
| 357 | self.modifyEntry(entry, groups, limitby, description, \ |
| 358 | overcharge, balance, balancevalue, \ |
| 359 | comment) |
| 360 | self.manageUsersGroups(usersgroups, entry, remove) |