all: cleanup logging to be structured

This commit is contained in:
Jens Langhammer 2020-02-18 21:35:58 +01:00
parent ff25c1c057
commit 4e8238603a
10 changed files with 20 additions and 37 deletions

View File

@ -18,7 +18,7 @@ def get_links(model_instance):
links = {} links = {}
if not isinstance(model_instance, Model): if not isinstance(model_instance, Model):
LOGGER.warning("Model %s is not instance of Model", model_instance) LOGGER.warning("Model is not instance of Model", model_instance=model_instance)
return links return links
try: try:
@ -43,7 +43,7 @@ def get_htmls(context, model_instance):
htmls = [] htmls = []
if not isinstance(model_instance, Model): if not isinstance(model_instance, Model):
LOGGER.warning("Model %s is not instance of Model", model_instance) LOGGER.warning("Model is not instance of Model", model_instance=model_instance)
return htmls return htmls
try: try:

View File

@ -70,7 +70,7 @@ class SignUpForm(forms.Form):
"""Check if username is used already""" """Check if username is used already"""
username = self.cleaned_data.get("username") username = self.cleaned_data.get("username")
if User.objects.filter(username=username).exists(): if User.objects.filter(username=username).exists():
LOGGER.warning("Username %s already exists", username) LOGGER.warning("username already exists", username=username)
raise ValidationError(_("Username already exists")) raise ValidationError(_("Username already exists"))
return username return username
@ -79,7 +79,7 @@ class SignUpForm(forms.Form):
email = self.cleaned_data.get("email") email = self.cleaned_data.get("email")
# Check if user exists already, error early # Check if user exists already, error early
if User.objects.filter(email=email).exists(): if User.objects.filter(email=email).exists():
LOGGER.debug("email %s exists in django", email) LOGGER.debug("email already exists", email=email)
raise ValidationError(_("Email already exists")) raise ValidationError(_("Email already exists"))
return email return email

View File

@ -156,26 +156,9 @@ class SignUpView(UserPassesTestMixin, FormView):
for error in exc.messages: for error in exc.messages:
errors.append(error) errors.append(error)
return self.form_invalid(form) return self.form_invalid(form)
# needs_confirmation = True
# if self._invitation and not self._invitation.needs_confirmation:
# needs_confirmation = False
# if needs_confirmation:
# nonce = Nonce.objects.create(user=self._user)
# LOGGER.debug(str(nonce.uuid))
# # Send email to user
# send_email.delay(self._user.email, _('Confirm your account.'),
# 'email/account_confirm.html', {
# 'url': self.request.build_absolute_uri(
# reverse('passbook_core:auth-sign-up-confirm', kwargs={
# 'nonce': nonce.uuid
# })
# )
# })
# self._user.is_active = False
# self._user.save()
self.consume_invitation() self.consume_invitation()
messages.success(self.request, _("Successfully signed up!")) messages.success(self.request, _("Successfully signed up!"))
LOGGER.debug("Successfully signed up %s", form.cleaned_data.get("email")) LOGGER.debug("Successfully signed up", email=form.cleaned_data.get("email"))
return redirect(reverse("passbook_core:auth-login")) return redirect(reverse("passbook_core:auth-login"))
def consume_invitation(self): def consume_invitation(self):

View File

@ -35,7 +35,7 @@ class HaveIBeenPwendPolicy(Policy):
full_hash, count = line.split(":") full_hash, count = line.split(":")
if pw_hash[5:] == full_hash.lower(): if pw_hash[5:] == full_hash.lower():
final_count = int(count) final_count = int(count)
LOGGER.debug("Got count %d for hash %s", final_count, pw_hash[:5]) LOGGER.debug("got hibp result", count=final_count, hash=pw_hash[:5])
if final_count > self.allowed_count: if final_count > self.allowed_count:
message = _( message = _(
"Password exists on %(count)d online lists." % {"count": final_count} "Password exists on %(count)d online lists." % {"count": final_count}

View File

@ -18,13 +18,13 @@ LOGGER = get_logger()
class BaseOAuthClient: class BaseOAuthClient:
"""Base OAuth Client""" """Base OAuth Client"""
_session = None _session: Session = None
def __init__(self, source, token=""): # nosec def __init__(self, source, token=""): # nosec
self.source = source self.source = source
self.token = token self.token = token
self._session = Session() self._session = Session()
self._session.headers.update({"User-Agent": "web:passbook:%s" % __version__}) self._session.headers.update({"User-Agent": "passbook %s" % __version__})
def get_access_token(self, request, callback=None): def get_access_token(self, request, callback=None):
"Fetch access token from callback request." "Fetch access token from callback request."
@ -36,7 +36,7 @@ class BaseOAuthClient:
response = self.request("get", self.source.profile_url, token=raw_token) response = self.request("get", self.source.profile_url, token=raw_token)
response.raise_for_status() response.raise_for_status()
except RequestException as exc: except RequestException as exc:
LOGGER.warning("Unable to fetch user profile: %s", exc) LOGGER.warning("Unable to fetch user profile", exc=exc)
return None return None
else: else:
return response.json() or response.text return response.json() or response.text
@ -51,7 +51,7 @@ class BaseOAuthClient:
additional = parameters or {} additional = parameters or {}
args.update(additional) args.update(additional)
params = urlencode(args) params = urlencode(args)
LOGGER.info("Redirect args: %s", args) LOGGER.info("redirect args", **args)
return "{0}?{1}".format(self.source.authorization_url, params) return "{0}?{1}".format(self.source.authorization_url, params)
def parse_raw_token(self, raw_token): def parse_raw_token(self, raw_token):
@ -91,7 +91,7 @@ class OAuthClient(BaseOAuthClient):
) )
response.raise_for_status() response.raise_for_status()
except RequestException as exc: except RequestException as exc:
LOGGER.warning("Unable to fetch access token: %s", exc) LOGGER.warning("Unable to fetch access token", exc=exc)
return None return None
else: else:
return response.text return response.text
@ -106,7 +106,7 @@ class OAuthClient(BaseOAuthClient):
) )
response.raise_for_status() response.raise_for_status()
except RequestException as exc: except RequestException as exc:
LOGGER.warning("Unable to fetch request token: %s", exc) LOGGER.warning("Unable to fetch request token", exc=exc)
return None return None
else: else:
return response.text return response.text
@ -195,7 +195,7 @@ class OAuth2Client(BaseOAuthClient):
) )
response.raise_for_status() response.raise_for_status()
except RequestException as exc: except RequestException as exc:
LOGGER.warning("Unable to fetch access token: %s", exc) LOGGER.warning("Unable to fetch access token", exc=exc)
return None return None
else: else:
return response.text return response.text

View File

@ -24,7 +24,7 @@ class AzureADOAuth2Client(OAuth2Client):
response = self.request("get", self.source.profile_url, headers=headers) response = self.request("get", self.source.profile_url, headers=headers)
response.raise_for_status() response.raise_for_status()
except RequestException as exc: except RequestException as exc:
LOGGER.warning("Unable to fetch user profile: %s", exc) LOGGER.warning("Unable to fetch user profile", exc=exc)
return None return None
else: else:
return response.json() or response.text return response.json() or response.text

View File

@ -40,7 +40,7 @@ class DiscordOAuth2Client(OAuth2Client):
) )
response.raise_for_status() response.raise_for_status()
except RequestException as exc: except RequestException as exc:
LOGGER.warning("Unable to fetch user profile: %s", exc) LOGGER.warning("Unable to fetch user profile", exc=exc)
return None return None
else: else:
return response.json() or response.text return response.json() or response.text

View File

@ -49,7 +49,7 @@ class RedditOAuth2Client(OAuth2Client):
) )
response.raise_for_status() response.raise_for_status()
except RequestException as exc: except RequestException as exc:
LOGGER.warning("Unable to fetch user profile: %s", exc) LOGGER.warning("Unable to fetch user profile", exc=exc)
return None return None
else: else:
return response.json() or response.text return response.json() or response.text

View File

@ -22,7 +22,7 @@ class TwitterOAuthClient(OAuthClient):
) )
response.raise_for_status() response.raise_for_status()
except RequestException as exc: except RequestException as exc:
LOGGER.warning("Unable to fetch user profile: %s", exc) LOGGER.warning("Unable to fetch user profile", exc=exc)
return None return None
else: else:
return response.json() or response.text return response.json() or response.text

View File

@ -124,9 +124,9 @@ class OAuthCallback(OAuthClientMixin, View):
source=self.source, identifier=identifier, request=request source=self.source, identifier=identifier, request=request
) )
if user is None: if user is None:
LOGGER.debug("Handling new user") LOGGER.debug("Handling new user", source=self.source)
return self.handle_new_user(self.source, connection, info) return self.handle_new_user(self.source, connection, info)
LOGGER.debug("Handling existing user") LOGGER.debug("Handling existing user", source=self.source)
return self.handle_existing_user(self.source, user, connection, info) return self.handle_existing_user(self.source, user, connection, info)
# pylint: disable=unused-argument # pylint: disable=unused-argument
@ -179,7 +179,7 @@ class OAuthCallback(OAuthClientMixin, View):
def handle_login_failure(self, source, reason): def handle_login_failure(self, source, reason):
"Message user and redirect on error." "Message user and redirect on error."
LOGGER.warning("Authentication Failure: %s", reason) LOGGER.warning("Authentication Failure", reason=reason)
messages.error(self.request, _("Authentication Failed.")) messages.error(self.request, _("Authentication Failed."))
return redirect(self.get_error_redirect(source, reason)) return redirect(self.get_error_redirect(source, reason))