332 | | printeradded = 0 |
333 | | printers = self.storage.getMatchingPrinters(options["printer"]) |
334 | | if not printers : |
335 | | pname = options["printer"] |
336 | | if options["add"] and pname : |
337 | | if self.isValidName(pname) : |
338 | | printerid = self.storage.addPrinter(pname) |
339 | | printers = [ (printerid, pname) ] |
340 | | printeradded = 1 |
341 | | else : |
342 | | raise PyKotaToolError, _("Invalid printer name %s") % pname |
343 | | else : |
344 | | raise PyKotaToolError, _("There's no printer matching %s") % pname |
345 | | |
| 335 | |
| 336 | suffix = (options["groups"] and "Group") or "User" |
| 337 | |
347 | | if options["softlimit"] : |
348 | | try : |
349 | | softlimit = int(options["softlimit"].strip()) |
350 | | except ValueError : |
351 | | raise PyKotaToolError, _("Invalid softlimit value %s.") % options["softlimit"] |
352 | | if options["hardlimit"] : |
353 | | try : |
354 | | hardlimit = int(options["hardlimit"].strip()) |
355 | | except ValueError : |
356 | | raise PyKotaToolError, _("Invalid hardlimit value %s.") % options["hardlimit"] |
357 | | if (softlimit is not None) and (hardlimit is not None) and (hardlimit < softlimit) : |
358 | | # error, exchange them |
359 | | self.logger.log_message(_("Hard limit %i is less than soft limit %i, values will be exchanged.") % (hardlimit, softlimit)) |
360 | | (softlimit, hardlimit) = (hardlimit, softlimit) |
| 339 | if not options["noquota"] : |
| 340 | if options["softlimit"] : |
| 341 | try : |
| 342 | softlimit = int(options["softlimit"].strip()) |
| 343 | except ValueError : |
| 344 | raise PyKotaToolError, _("Invalid softlimit value %s.") % options["softlimit"] |
| 345 | if options["hardlimit"] : |
| 346 | try : |
| 347 | hardlimit = int(options["hardlimit"].strip()) |
| 348 | except ValueError : |
| 349 | raise PyKotaToolError, _("Invalid hardlimit value %s.") % options["hardlimit"] |
| 350 | if (softlimit is not None) and (hardlimit is not None) and (hardlimit < softlimit) : |
| 351 | # error, exchange them |
| 352 | self.logger.log_message(_("Hard limit %i is less than soft limit %i, values will be exchanged.") % (hardlimit, softlimit)) |
| 353 | (softlimit, hardlimit) = (hardlimit, softlimit) |
396 | | uwaschanged = {} # tracks changed made at the user level |
397 | | gwaschanged = {} # tracks changed made at the group level |
398 | | for (printerid, printer) in printers : |
| 383 | if options["prototype"] : |
| 384 | protoentry = getattr(self.storage, "get%s" % suffix)(options["prototype"]) |
| 385 | |
| 386 | printeradded = 0 |
| 387 | printers = self.storage.getMatchingPrinters(options["printer"]) |
| 388 | if not printers : |
| 389 | pname = options["printer"] |
| 390 | if options["add"] and pname : |
| 391 | if self.isValidName(pname) : |
| 392 | printers = [ self.storage.addPrinter(pname) ] |
| 393 | if printers[0].Exists : |
| 394 | printeradded = 1 |
| 395 | else : |
| 396 | raise PyKotaToolError, _("Impossible to add printer %s") % pname |
| 397 | else : |
| 398 | raise PyKotaToolError, _("Invalid printer name %s") % pname |
| 399 | else : |
| 400 | raise PyKotaToolError, _("There's no printer matching %s") % pname |
| 401 | if not names : |
| 402 | if options["add"] and not printeradded : |
| 403 | raise PyKotaToolError, _("You have to pass user or group names on the command line") |
| 404 | else : |
| 405 | names = [ "*" ] # all users |
| 406 | |
| 407 | changed = {} # tracks changes made at the user/group level |
| 408 | for printer in printers : |
406 | | if options["groups"] : |
407 | | prototype = self.storage.getGroupPQuota(self.storage.getGroupId(options["prototype"]), printerid) |
408 | | else : |
409 | | # default is user quota edition |
410 | | prototype = self.storage.getUserPQuota(self.storage.getUserId(options["prototype"]), printerid) |
411 | | if prototype is None : |
412 | | self.logger.log_message(_("Prototype %s not found in Quota Storage for printer %s.") % (options["prototype"], printer)) |
413 | | continue # skip this printer |
414 | | else : |
415 | | (softlimit, hardlimit) = (prototype["softlimit"], prototype["hardlimit"]) |
416 | | if hardlimit is None : |
417 | | hardlimit = softlimit |
418 | | if hardlimit is not None : |
419 | | self.logger.log_message(_("Undefined hard limit set to soft limit (%s) on printer %s.") % (str(hardlimit), printer)) |
420 | | if softlimit is None : |
421 | | softlimit = hardlimit |
422 | | if softlimit is not None : |
423 | | self.logger.log_message(_("Undefined soft limit set to hard limit (%s) on printer %s.") % (str(softlimit), printer)) |
424 | | if (not options["ingroups"] and not options["reset"] and not options["noquota"] and not options["prototype"] and not options["limitby"] and not options["balance"] and not options["delete"] and not options["charge"]) and ((hardlimit is None) or (softlimit is None)) : |
425 | | raise PyKotaToolError, _("Both hard and soft limits must be set ! Aborting.") |
| 414 | if protoentry.Exists : |
| 415 | protoquota = getattr(self.storage, "get%PQuota" % suffix)(protoentry, printer) |
| 416 | if not protoquota.Exists : |
| 417 | self.logger.log_message(_("Prototype %s not found in Quota Storage for printer %s.") % (protoentry.Name, printer.Name)) |
| 418 | continue # skip this printer |
| 419 | else : |
| 420 | (softlimit, hardlimit) = (protoquota.SoftLimit, protoquota.HardLimit) |
| 421 | else : |
| 422 | self.logger.log_message(_("Prototype object %s not found in Quota Storage.") % protoentry.Name) |
| 423 | if not options["noquota"] : |
| 424 | if hardlimit is None : |
| 425 | hardlimit = softlimit |
| 426 | if hardlimit is not None : |
| 427 | self.logger.log_message(_("Undefined hard limit set to soft limit (%s) on printer %s.") % (str(hardlimit), printer.Name)) |
| 428 | if softlimit is None : |
| 429 | softlimit = hardlimit |
| 430 | if softlimit is not None : |
| 431 | self.logger.log_message(_("Undefined soft limit set to hard limit (%s) on printer %s.") % (str(softlimit), printer.Name)) |
| 432 | |
432 | | if options["groups"] : |
433 | | allidnames = self.storage.getPrinterGroups(printerid) or [] |
434 | | else : |
435 | | allidnames = self.storage.getPrinterUsers(printerid) or [] |
436 | | for (ident, name) in [(i, n) for (i, n) in allidnames if self.matchString(n, names)]: |
437 | | if options["groups"] : |
438 | | if not gwaschanged.has_key(name) : |
439 | | gwaschanged[name] = {} |
440 | | quota = self.storage.getGroupPQuota(ident, printerid) |
441 | | else : |
442 | | if not uwaschanged.has_key(name) : |
443 | | uwaschanged[name] = {"ingroups": []} |
444 | | quota = self.storage.getUserPQuota(ident, printerid) |
445 | | if quota is None : |
| 440 | allentries = getattr(self.storage, "getPrinter%ssAndQuotas" % suffix)(printer) |
| 441 | |
| 442 | for (entry, entrypquota) in [(e, q) for (e, q) in allentries if self.matchString(e.Name, names)] : |
| 443 | if not changed.has_key(entry.Name) : |
| 444 | changed[entry.Name] = {} |
| 445 | if not entrypquota.Exists : |
452 | | if options["groups"] : |
453 | | if self.isValidName(name) : |
454 | | (ident, printerid) = self.storage.addGroupPQuota(name, printerid) |
455 | | quota = self.storage.getGroupPQuota(ident, printerid) |
| 452 | if self.isValidName(entry.Name) : |
| 453 | if not entry.Exists : |
| 454 | entry = getattr(self.storage, "add%s" % suffix)(entry) |
| 455 | entrypquota = getattr(self.storage, "add%sPQuota" % suffix)(entry, printer) |
| 456 | else : |
| 457 | if options["groups"] : |
| 458 | self.logger.log_message(_("Invalid group name %s") % entry.Name) |
457 | | self.logger.log_message(_("Invalid group name %s") % name) |
458 | | else : |
459 | | if self.isValidName(name) : |
460 | | (ident, printerid) = self.storage.addUserPQuota(name, printerid) |
461 | | quota = self.storage.getUserPQuota(ident, printerid) |
462 | | else : |
463 | | self.logger.log_message(_("Invalid user name %s") % name) |
464 | | if quota is None : |
465 | | self.logger.log_message(_("Quota not found for object %s on printer %s.") % (name, printer)) |
| 460 | self.logger.log_message(_("Invalid user name %s") % entry.Name) |
| 461 | if not entrypquota.Exists : |
| 462 | self.logger.log_message(_("Quota not found for object %s on printer %s.") % (entry.Name, printer.Name)) |
473 | | if options["groups"] : |
474 | | if options["noquota"] or options["prototype"] or ((softlimit is not None) and (hardlimit is not None)) : |
475 | | self.storage.setGroupPQuota(ident, printerid, softlimit, hardlimit) |
| 467 | if options["noquota"] or options["prototype"] or ((softlimit is not None) and (hardlimit is not None)) : |
| 468 | entrypquota.setLimits(softlimit, hardlimit) |
| 469 | if limitby : |
| 470 | if changed[entry.Name].get("limitby") is None : |
| 471 | entry.setLimitBy(limitby) |
| 472 | changed[entry.Name]["limitby"] = limitby |
| 473 | |
| 474 | if not options["groups"] : |
477 | | self.storage.resetGroupPQuota(ident, printerid) |
478 | | if limitby : |
479 | | if gwaschanged[name].get("limitby") is None : |
480 | | self.storage.limitGroupBy(ident, limitby) |
481 | | gwaschanged[name]["limitby"] = limitby |
482 | | self.warnGroupPQuota(name, printer) |
483 | | else : |
484 | | if options["noquota"] or options["prototype"] or ((softlimit is not None) and (hardlimit is not None)) : |
485 | | self.storage.setUserPQuota(ident, printerid, softlimit, hardlimit) |
486 | | if options["reset"] : |
487 | | self.storage.resetUserPQuota(ident, printerid) |
488 | | if limitby : |
489 | | if uwaschanged[name].get("limitby") is None : |
490 | | self.storage.limitUserBy(ident, limitby) |
491 | | uwaschanged[name]["limitby"] = limitby |
| 476 | entrypquota.reset() |
| 477 | |
500 | | if groupname not in uwaschanged[name]["ingroups"] : |
501 | | groupid = self.storage.getGroupId(groupname) |
502 | | if groupid is not None : |
503 | | self.storage.addUserToGroup(ident, groupid) |
504 | | uwaschanged[name]["ingroups"].append(groupname) |
| 486 | if groupname not in changed[name]["ingroups"] : |
| 487 | group = self.storage.getGroup(groupname) |
| 488 | if group.Exists : |
| 489 | self.storage.addUserToGroup(entry, group) |
| 490 | changed[entry.Name]["ingroups"].append(groupname) |