[python-bugzilla] [PATCH] Add is_logged_in utility method.

Cole Robinson crobinso at redhat.com
Sat Nov 29 19:46:19 UTC 2014


On 11/24/2014 06:42 PM, abn at redhat.com wrote:
> From: Arun Babu Neelicattu <abn at redhat.com>
>
> ---
>   bugzilla/base.py | 20 ++++++++++++++++++++
>   1 file changed, 20 insertions(+)
>
> diff --git a/bugzilla/base.py b/bugzilla/base.py
> index e06223f..7aba942 100644
> --- a/bugzilla/base.py
> +++ b/bugzilla/base.py
> @@ -697,6 +697,26 @@ class BugzillaBase(object):
>           self.password = ''
>           self.logged_in = False
>
> +    def is_logged_in(self):
> +        """
> +        Utility method to check if this instance has already been logged in.
> +
> +        If the instance has its logged_in attribute set to True, this method
> +        returns true. Otherwise, to test if this session is authenticated, the
> +        method calls the User.get() XMLRPC method with ids set. Logged-out users
> +        cannot pass the 'ids' parameter and will result in a 505 error.
> +        """
> +        try:
> +            if self.logged_in:
> +                return True
> +            self._proxy.User.get({'ids': self._listify([])})

listify is redundant here since we know we are passing a list. (listify is 
just a helper to turn None into a list, where it's required)

> +            return True
> +        except Fault:
> +            e = sys.exc_info()[1]
> +            if e.faultCode == 505:
> +                return False
> +            raise e
> +
>
>       #############################################
>       # Fetching info about the bugzilla instance #
>

Having this capability is definitely useful. But a few ideas:

- We should file a bug with upstream bugzilla to actually provide a real API 
for this. Bugzilla 5.0 may actually have an API for this... I recall some 
change WRT login state, but I'm not certain. I can check next week

- I think there's places in the test suite where we already try to approximate 
this, grep _check_rh_privs. Might be able to use this as well. It might not 
fit though, but regardless I'd like to have some sort of unit test for this

- After this we will have an API disparity: there's self.logged_in which still 
has confusing semantics as pingou pointed out. So maybe turn self.logged_in 
into a readonly property that will call this function, and cache the value. 
Dunno if it's worth it

- Cole


More information about the python-bugzilla mailing list