Mercurial > p > roundup > code
annotate roundup/actions.py @ 7752:b2dbab2b34bc
fix(refactor): multiple fixups using ruff linter; more testing.
Converting to using the ruff linter and its rulesets. Fixed a number
of issues.
admin.py:
sort imports
use immutable tuples as default value markers for parameters where a
None value is valid.
reduced some loops to list comprehensions for performance
used ternary to simplify some if statements
named some variables to make them less magic
(e.g. _default_savepoint_setting = 1000)
fixed some tests for argument counts < 2 becomes != 2 so 3 is an
error.
moved exception handlers outside of loops for performance where
exception handler will abort loop anyway.
renamed variables called 'id' or 'dir' as they shadow builtin
commands.
fix translations of form _("string %s" % value) -> _("string %s") %
value so translation will be looked up with the key before
substitution.
end dicts, tuples with a trailing comma to reduce missing comma
errors if modified
simplified sorted(list(self.setting.keys())) to
sorted(self.setting.keys()) as sorted consumes whole list.
in if conditions put compared variable on left and threshold condition
on right. (no yoda conditions)
multiple noqa: suppression
removed unneeded noqa as lint rulesets are a bit different
do_get - refactor output printing logic: Use fast return if not
special formatting is requested; use isinstance with a tuple
rather than two isinstance calls; cleaned up flow and removed
comments on algorithm as it can be easily read from the code.
do_filter, do_find - refactor output printing logic. Reduce
duplicate code.
do_find - renamed variable 'value' that was set inside a loop. The
loop index variable was also named 'value'.
do_pragma - added hint to use list subcommand if setting was not
found. Replaced condition 'type(x) is bool' with 'isinstance(x,
bool)' for various types.
test_admin.py
added testing for do_list
better test coverage for do_get includes: -S and -d for multilinks,
error case for -d with non-link.
better testing for do_find including all output modes
better testing for do_filter including all output modes
fixed expected output for do_pragma that now includes hint to use
pragma list if setting not found.
| author | John Rouillard <rouilj@ieee.org> |
|---|---|
| date | Fri, 01 Mar 2024 14:53:18 -0500 |
| parents | 4c9acc580769 |
| children | 1287b0c3d261 |
| rev | line source |
|---|---|
| 4083 | 1 # |
| 2 # Copyright (C) 2009 Stefan Seefeld | |
| 3 # All rights reserved. | |
| 4 # For license terms see the file COPYING.txt. | |
| 5604 | 5 # Actions used in REST and XMLRPC APIs |
| 4083 | 6 # |
| 7 | |
|
5071
a7541077cf12
Remove 'import *' statement from actions.py
John Kristensen <john@jerrykan.com>
parents:
4357
diff
changeset
|
8 from roundup.exceptions import Unauthorised |
| 4083 | 9 from roundup import hyperdb |
| 6003 | 10 |
| 4083 | 11 |
| 12 class Action: | |
| 13 def __init__(self, db, translator): | |
| 14 self.db = db | |
| 15 self.translator = translator | |
| 16 | |
| 17 def handle(self, *args): | |
| 18 """Action handler procedure""" | |
| 19 raise NotImplementedError | |
| 20 | |
| 21 def execute(self, *args): | |
| 22 """Execute the action specified by this object.""" | |
| 23 | |
| 24 self.permission(*args) | |
| 25 return self.handle(*args) | |
| 26 | |
| 27 def permission(self, *args): | |
| 28 """Check whether the user has permission to execute this action. | |
| 29 | |
| 30 If not, raise Unauthorised.""" | |
| 31 | |
| 32 pass | |
| 33 | |
| 34 def gettext(self, msgid): | |
| 35 """Return the localized translation of msgid""" | |
| 36 return self.translator.gettext(msgid) | |
| 37 | |
| 38 _ = gettext | |
| 39 | |
| 40 | |
| 5604 | 41 class PermCheck(Action): |
| 42 def permission(self, designator): | |
| 43 | |
| 44 classname, itemid = hyperdb.splitDesignator(designator) | |
| 45 perm = self.db.security.hasPermission | |
| 46 | |
| 6003 | 47 if not perm('Retire', self.db.getuid(), classname=classname, |
| 48 itemid=itemid): | |
| 5604 | 49 raise Unauthorised(self._('You do not have permission to retire ' |
| 50 'or restore the %(classname)s class.') | |
| 6003 | 51 % locals()) |
| 52 | |
| 5604 | 53 |
| 54 class Retire(PermCheck): | |
| 4083 | 55 |
| 56 def handle(self, designator): | |
| 57 | |
| 58 classname, itemid = hyperdb.splitDesignator(designator) | |
| 59 | |
| 60 # make sure we don't try to retire admin or anonymous | |
| 61 if (classname == 'user' and | |
|
7176
4c9acc580769
chore: flake8 whitespace fixes
John Rouillard <rouilj@ieee.org>
parents:
6003
diff
changeset
|
62 self.db.user.get(itemid, 'username') in ('admin', 'anonymous')): |
|
4c9acc580769
chore: flake8 whitespace fixes
John Rouillard <rouilj@ieee.org>
parents:
6003
diff
changeset
|
63 |
|
4357
13b3155869e0
Beginnings of a big code cleanup / modernisation to make 2to3 happy
Richard Jones <richard@users.sourceforge.net>
parents:
4125
diff
changeset
|
64 raise ValueError(self._( |
|
13b3155869e0
Beginnings of a big code cleanup / modernisation to make 2to3 happy
Richard Jones <richard@users.sourceforge.net>
parents:
4125
diff
changeset
|
65 'You may not retire the admin or anonymous user')) |
| 4083 | 66 |
| 67 # do the retire | |
| 68 self.db.getclass(classname).retire(itemid) | |
| 69 self.db.commit() | |
| 70 | |
| 71 | |
| 5604 | 72 class Restore(PermCheck): |
| 73 | |
| 74 def handle(self, designator): | |
| 4083 | 75 |
| 76 classname, itemid = hyperdb.splitDesignator(designator) | |
| 77 | |
| 5604 | 78 # do the restore |
| 79 self.db.getclass(classname).restore(itemid) | |
| 80 self.db.commit() |
